Advertisement
Maurizio-Ciullo

38 Lezione Personale 1 How To Debug Pine Script

Apr 6th, 2023 (edited)
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © Maurizio-Ciullo
  3.  
  4. // https://www.youtube.com/watch?v=b3PaVZkDbDI&t=808s
  5.  
  6. //@version=5
  7. indicator("38 Lezione Personale 1 How To Debug Pine Script")
  8. plot(close)
  9.  
  10. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<    // Create debugging values //    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  11. float averageVolume = ta.sma(volume, 50)      // Media volume 50 candele
  12. bool raisedVolume = volume > averageVolume    // Volume ultima barra > averageVolume
  13. // plot(raisedVolume)                         // Qui avrò un errore perchè non posso plottare un buleano
  14.  
  15. // Nell'esempio sotto "che va bene", però se avessimo un plot di valore molto basso
  16. // pine andrebbe a fare lo scaling a partire dal quel valore fino al valore attuale
  17. // plot(raisedVolume ? 1 : 0, title="Raised Volume")
  18.  
  19.                                     // in questo modo, con plotchar evitiamo lo scaling
  20. plotchar(raisedVolume ? 1 : 0, title="Raised Volume", text=" ", color=color.new(color.red, 100))
  21. bgcolor(raisedVolume ? color.new(color.red, 50) : na)  // Imposto il background. Per far funionare il color devo eliminare il color iniziale
  22.  
  23.  
  24. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<    // Altro esempio per vedere le differenze dello scaling e no scaling //    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  25. // Displaying Data In The Data Window
  26. // Plot works but preferred option is plotchar (as it will not affect chart scale)
  27. plotchar(averageVolume, title="Raised Volume", color=color.new(color.red, 100))
  28. plotchar(volume, title="Current Volume", color=color.new(color.red, 100))
  29.  
  30. // Plotchar Example With A very Low Value As Example (1)
  31. plot(1, title="Average Volume", color=color.new(color.red, 100))
  32. plotchar(1, title="Average Volume", text=" ", color=color.new(color.red, 100))
  33.  
  34.  
  35. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<    // Per Il For Loop Le Cose Diventano Più Complicate //    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  36. // Con il foor loop non posso usare qualsiasi plot in local scope
  37. // for i = 0 to 10   // Il conteggio in questo modo sarà di 11 barre compreso lo 0
  38. //     plotchar(volume[i], text=" ", color=color.new(color.red, 100))
  39.  
  40. // Ottengo il bar size delle ultime 10 barre high - low
  41. float totalBarSize = 0.0
  42. for i = 0 to 9
  43.     totalBarSize := totalBarSize + (high[i] - low[i])
  44. plotchar(totalBarSize, title="Total Bar Size", text=" ", color=color.new(color.red, 100))
  45.  
  46. // Ottengo la media bar size delle ultime 10 barre high - low
  47. float averageBarSize = totalBarSize / 10
  48. plotchar(averageBarSize, title="Average 10 Bar Size", text=" ", color=color.new(color.red, 100))
  49.  
  50. // Ora uniamo il tutto con un'input
  51. int loopLenght = input.int(title="loopLenght", defval=10, minval=1)
  52. float totalBarSize2 = 0.0
  53. for i = 0 to loopLenght -1  // Meno 1 perchè include lo 0
  54.     totalBarSize2 := totalBarSize2 + (high[i] - low[i])
  55.  
  56. float averageBarSize2 = totalBarSize2 / loopLenght
  57. plotchar(averageBarSize2, title="Average 10 Bar Size 2", text=" ", color=color.new(color.red, 100))
  58. plotchar(loopLenght, title="Loop Back Lenght", text=" ", color=color.new(color.red, 100))
  59.  
  60.  
  61. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<    // Fare Il Debug Con Le String Text Creando Delle Labels //    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  62. // Si potrebbe anche creare un'array, inserire ogni singolo valore nell'array e usare plotchar per plottare i risultati ma con le labels si fa prima
  63.  
  64. string debugString = ""   // Creo una string vuota
  65. float totalBarSize3 = 0.0
  66. for i = 0 to loopLenght -1  // Meno 1 perchè include lo 0
  67.     totalBarSize3 := totalBarSize3 + (high[i] - low[i]) // Restituisce la size delle barre che si sommano man mano fino a loopLenght
  68.     //debugString := debugString + "[" + str.tostring(i) + "]" + "\n"  // Assegno alla stringa il loop convertito in stringa e vado a capo con "\n"
  69.     // Aggiungo i valori che mi servono ai valori del loop della riga sopra
  70.     debugString := debugString + "[" + str.tostring(i) + "] TotalBarSize " + str.tostring(totalBarSize3) + //"\n"
  71.          " Bar Size= " + str.tostring(high[i] - low[i]) + "\n" // This line can be added on the same line or below creating a new line with 1ne more spase after indentation
  72. // Per verificare se è tutto ok misurale la differenza tra massimo e minimo dell'ultima candela e sommarla alla differenza massimo e minimo della candela/e precedenti
  73. if barstate.islast
  74.     label.new(bar_index, high, debugString, text="debugString", color=color.black, textcolor = color.white, textalign = text.align_left)
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement