Advertisement
Maurizio-Ciullo

Indicatore Z Score Trade Dependency

Oct 30th, 2023
1,074
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //@version=5
  2.  
  3. /////////////////////////////////////////////////////////////// Indicatore Z Score Trade Dependency ///////////////////////////////////////////////////////////////
  4.  
  5. // Funzionamento: Da tradestastion verificare se lo z-score è a +2 o -2 per avere una probabilità di continuità o alternanza, se non ci sono questi dati non usare lo z-score.
  6.  
  7. // Individuare il numero di strisce positive ricorrenti dall'array e impostare l'input max positive strips su quel numero ES: 5,
  8. // poi diminuire la size con: EntryCapitalPercentZScorePositiveStrips dato che dopo qual numero di vittorie la 6sta operazione potrebbe perdere.
  9.  
  10. // Individuare il numero di strisce negative ricorrenti dall'array e impostare l'input max negative strips un numero inferiore ES: se c'è ricorrenza sul 5 imposto l'input a 4
  11. // dato che dopo il 5 trade protrò vincere allora scalo di uno l'input così al 4rto trade visto che perdo in modo ricorrente entro con una size minore: EntryCapitalPercentZScoreNegativeStrips
  12. //  ed al 5nto invece che potrei vincere entro normalmente.
  13.  
  14. // Se l'input max positive strips o max negative strips è 0 allora entro con la size desiderata dall'input sempre uguale per tutta la strategia.
  15.  
  16.  
  17. strategy(title='Indicatore Z Score Trade Dependency', overlay=true,
  18.          //max_bars_back=5000, // Serve Per Caricare Più Storico Per Il Trailing Stop
  19.          pyramiding=0,
  20.          initial_capital=1000,
  21.          commission_type=strategy.commission.percent,
  22.          commission_value=0.1,
  23.          slippage=3,
  24.          default_qty_type=strategy.percent_of_equity,
  25.          precision=4,
  26.          default_qty_value=24)
  27.  
  28.  
  29. MM_Method = input(13, "MM_Method", group="Money Management Method")
  30. InitialCapital = input.float(defval = 1000, title="Initial Capital", group="Capital Management", step=0.1)
  31. EntryCapitalPercent = input.float(defval = 24, title="Entry Capital Percent", group="Capital Management", step=0.1)
  32. EntryCapitalPercentZScore = input.float(defval = 24, title="Entry Capital Percent Z Score", group="Z Score Trade Dependency", step=0.1)
  33. EntryCapitalPercentZScorePositiveStrips = input.float(defval = 10, title="Entry Capital Percent Z Score Positive Strips", group="Z Score Trade Dependency", step=0.1)
  34. EntryCapitalPercentZScoreNegativeStrips = input.float(defval = 5, title="Entry Capital Percent Z Score Negative Strips", group="Z Score Trade Dependency", step=0.1)
  35. MaxPositiveStrips = input.int(defval = 2, minval=0, title="Max Positive Strips", group="Z Score Trade Dependency", step=1, tooltip = "If Count Won Trades == Input, Position Size = Entry Capital Percent Z Score Positive Strips, [If 0 Entry Capita lPercent Z Score]")
  36. MaxNegativeStrips = input.int(defval = 2, minval=0, title="Max Negative Strips", group="Z Score Trade Dependency", step=1, tooltip = "If Count Lost Trades == Input, Position Size = Entry Capital Percent Z Score Negative Strips, [If 0 Entry Capital Percent Z Score]")
  37.  
  38. // // Start checking if last trade was lost
  39. var lastTradeWasLoss = false
  40. if (strategy.losstrades[0] > strategy.losstrades[1])
  41.     lastTradeWasLoss := true  
  42. if (strategy.wintrades[0] > strategy.wintrades[1])
  43.     // // successful trade, reset
  44.     lastTradeWasLoss := false
  45. plotshape(MM_Method == 13 ? lastTradeWasLoss : na, title="Last Trade Was Loss", color=color.red)                              
  46. bgcolor(MM_Method == 13 and lastTradeWasLoss == 1 ? color.red : lastTradeWasLoss == 0 ? color.green : na, transp=90)        
  47.  
  48. // // Debug last Trade Was Loss "It Swithes from 0 to 1 everytime the entire strip changes from win to loss"
  49. // if strategy.closedtrades != strategy.closedtrades[1]
  50. //     log.info(str.tostring(lastTradeWasLoss), "lastTradeWasLoss")
  51. // // End checking if last trade was lost
  52.  
  53.  
  54. // // Counter
  55. var countLostTrades = 0
  56. var countWonTrades = 0
  57. if (strategy.losstrades[0] > strategy.losstrades[1])
  58.     countLostTrades := countLostTrades - 1
  59. else if (strategy.wintrades[0] > strategy.wintrades[1])
  60.     countLostTrades := 0
  61. if (strategy.wintrades[0] > strategy.wintrades[1])
  62.     countWonTrades := countWonTrades + 1
  63. else if (strategy.losstrades[0] > strategy.losstrades[1])
  64.     countWonTrades := 0
  65. plotshape(MM_Method == 13 ? countLostTrades : na, title="Count Lost Trades", color=color.red)              
  66. plotshape(MM_Method == 13 ? countWonTrades : na, title="Count Won Trades", color=color.green)            
  67.  
  68. // // Debug count Lost Trades E count Won Trades One By One
  69. // if strategy.closedtrades != strategy.closedtrades[1] and countLostTrades != 0
  70. //     log.info(str.tostring(countLostTrades), "countLostTrades")
  71. // if strategy.closedtrades != strategy.closedtrades[1] and countWonTrades != 0
  72. //     log.info(str.tostring(countWonTrades), "countWonTrades")
  73.  
  74.  
  75. /////////////////////////////////////////////////////////////// Start Strips Win/Loss Max Trades Plotter. It returns the maximum of the strip. ///////////////////////////////////////////////////////////////
  76.  
  77. //////////////////////// Start Start End Bar Index Returns the bar index at the beginning/end of the strip. Not Needed "keep commented". ////////////////////////
  78. // plot(bar_index, title="Bar Index")
  79.  
  80. // var startBarIndex = 0.0
  81. // if countLostTrades[0] < 0 and countLostTrades[1] == 0
  82. //     startBarIndex := bar_index
  83. // plot(startBarIndex, title="Start Bar Index")
  84. // plotshape(countLostTrades[0] < 0 and countLostTrades[1] == 0, title="Start Bar Index Plotshape", color=color.green, size=size.normal)
  85.  
  86. // var endBarIndex = 0.0
  87. // if countWonTrades[0] > 0 and countWonTrades[1] == 0
  88. //     endBarIndex := bar_index
  89. // plot(endBarIndex, title="End Bar Index")
  90. // plotshape(countWonTrades[0] > 0 and countWonTrades[1] == 0, title="End Bar Index Plotshape", color=color.red, size=size.normal)
  91. //////////////////////// End Start End Bar Index Returns the bar index at the beginning/end of the strip. Not Needed "keep commented". ////////////////////////
  92.  
  93.  
  94. maxStripWonTrades = 0
  95. if countLostTrades[0] < 0 and countLostTrades[1] == 0
  96.     maxStripWonTrades := countWonTrades[1]
  97. plot(MM_Method == 13 ? maxStripWonTrades : na, title="Max Strip Won Trades")
  98. plotshape(MM_Method == 13 and countLostTrades[0] < 0 and countLostTrades[1] == 0 ? countLostTrades[0] < 0 and countLostTrades[1] == 0 : na, title="Start Bar Index Plotshape", color=color.green, size=size.normal)
  99.  
  100. maxStripLostTrades = 0
  101. if countWonTrades[0] > 0 and countWonTrades[1] == 0
  102.     maxStripLostTrades := countLostTrades[1]
  103. plot(MM_Method == 13 ? maxStripLostTrades : na, title="Max Strip Lost Trades")
  104. plotshape(MM_Method == 13 and countWonTrades[0] > 0 and countWonTrades[1] == 0 ? countWonTrades[0] > 0 and countWonTrades[1] == 0 : na, title="End Bar Index Plotshape", color=color.red, size=size.normal)
  105.  
  106. // // // Debug Start Strips Win/Loss Max Trades Plotter.
  107. if MM_Method == 13 and maxStripWonTrades != 0
  108.     log.info(str.tostring(maxStripWonTrades), "Max Strip Won Trades")
  109. //    log.info("\nMax Strip Won Trades : {0}" , str.tostring(maxStripWonTrades))      // More detailed logs !!!
  110. if MM_Method == 13 and maxStripLostTrades != 0
  111.     log.info(str.tostring(maxStripLostTrades), "Max Strip Lost Trades")
  112. //    log.info("\nMax Strip Lost Trades : {0}" , str.tostring(maxStripLostTrades))    // More detailed logs !!!
  113.  
  114. /////////////////////////////////////////////////////////////// End Strips Win/Loss Max Trades Plotter. It returns the maximum of the strip. ///////////////////////////////////////////////////////////////
  115.  
  116.  
  117. // Start Position Size Calculation
  118. openProfit = strategy.opentrades.profit(0)
  119. plot(MM_Method == 13 ? openProfit : na, title = "Open Profit")
  120.  
  121.  
  122. EntryBalancePercentIncDec = countWonTrades == MaxPositiveStrips and MaxPositiveStrips != 0 and strategy.opentrades == 0 ? 10 :
  123.      countLostTrades == - MaxNegativeStrips and MaxNegativeStrips != 0 and strategy.opentrades == 0 ? 5 :
  124.          countWonTrades != MaxPositiveStrips and strategy.opentrades == 0 ? 24 :
  125.              countLostTrades != - MaxNegativeStrips and strategy.opentrades == 0 ? 24 :
  126.                  countLostTrades == (- MaxNegativeStrips + 1) and openProfit < 0 ? 5 :
  127.                      countWonTrades == (MaxPositiveStrips - 1) and openProfit > 0 ? 10 :
  128.                  24
  129. // End Position Size Calculation
  130.  
  131.  
  132. posSize = 0.0
  133. if (MM_Method == 13)                                     // Monetario In Percentuale Al Balance Win/Loss Incremented/Decremented (Imposta Input EntryCapitalPercent)
  134.     posSize := EntryBalancePercentIncDec
  135.  
  136. plot(posSize, title="Position Size")
  137. plot(InitialCapital + strategy.netprofit, title="Actual Equity")
  138.  
  139.  
  140. /////////////////////////////////////////////////////////////// Inizio Condizioni Di Entrata / Uscita Long/Short/Swing ///////////////////////////////////
  141. // Condizione Di Ingresso Long
  142. longCondition = ta.crossover(ta.sma(close,31), ta.sma(close, 28))
  143. if (longCondition)
  144.     strategy.entry("Entry Long", strategy.long, qty=posSize)
  145.  
  146. // Condizione Chiusura Long
  147. closeConditionLong = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
  148. if (closeConditionLong)
  149.     strategy.close("Entry Long")
  150.  
  151.  
  152. // // Prova swing posizone per vedere se chiude tutte le posizioni.
  153. // // Condizioni Di Swing Short (Commentare il: Condizione Chiusura Long E decommentare Condizioni Di Swing Short Per fare questo test!!! )
  154.  
  155. // shortCondition = ta.crossunder(ta.sma(close, 7), ta.sma(close, 12))
  156. // if (shortCondition)
  157. //     strategy.entry("My Short Entry Id", strategy.short, qty=posSize)
  158.  
  159.  
  160. // Condizione Di Ingresso Short
  161. shortCondition = ta.crossover(ta.sma(close, 7), ta.sma(close, 12))
  162. if (shortCondition)
  163.     strategy.entry("My Short Entry Id", strategy.short, qty=posSize)
  164.  
  165. // Condizione Chiusura Short
  166. closeConditionShort = ta.crossunder(ta.sma(close, 5), ta.sma(close, 10))
  167. if (closeConditionShort)
  168.     strategy.close("My Short Entry Id")
  169. /////////////////////////////////////////////////////////////// Fine Condizioni Di Entrata / Uscita Long/Short/Swing ///////////////////////////////////
  170.  
  171.  
  172. // Monitoraggio posizione
  173. bought = strategy.position_size[0]> strategy.position_size[1]
  174. Close_TP = false    
  175. Close_TP := strategy.position_size[1] - strategy.position_size[0] and strategy.position_size[1] != 0 and strategy.position_size[0] != 0
  176.  
  177. plotshape(Close_TP,title="Close_TP", style=shape.xcross, color=color.blue, size =size.small, editable = true)
  178. plot(strategy.position_size[1],"Position Old")
  179. plot(strategy.position_size,"Position")
  180.  
  181.  
  182. // Table Inputs
  183. fattoreArrotondamento = input.int(1, "Fattore di arrotondamento",options = [1,10,100,1000,10000,100000, 1000000, 10000000])
  184. posizione = input.string("bottom_right", "posizione tabella", ["bottom_left", "bottom_center", "bottom_right", "middle_left", "middle_center", "middle_right", "top_left", "top_center" ,"top_right"])
  185. coloreBarreBullish = input.color(color.rgb(47, 159, 68), "Colore celle bullish")
  186. coloreBarreBearish = input.color(color.rgb(227, 65, 65), "Colore celle bearish")
  187. coloreSfondoTabella = input.color(color.rgb(53, 61, 68), "Colore sfondo tabella")
  188. coloreEtichetteSfondo = input.color(color.rgb(19, 50, 70), "Colore sfondo etichetta")
  189. coloreEtichetteTesto = input.color(color.rgb(255, 255, 255), "Colore testo etichetta")
  190. dimensioneTesto = input.string("small", "Dimensione Carattere", options=["tiny", "small", "normal", "large", "huge"])
  191.  
  192.  
  193. // Creating a table and cells
  194. var tabellaInfo = table.new(position = posizione, columns = 100, rows = 100, bgcolor = coloreSfondoTabella)
  195. table.cell(tabellaInfo, 0, 0, "MaxStripWonTrades", text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign= text.align_center)
  196. table.cell(tabellaInfo, 0, 1, "MaxStripLostTrades", text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign= text.align_center)
  197. table.cell(tabellaInfo, 0, 2, "MaxStripWonLostTrades", text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign= text.align_center)
  198.  
  199.  
  200. // Count Total Won/Lost Strips
  201. var contTotalStrips = 0
  202. if maxStripWonTrades > 0 and MM_Method == 13
  203.     contTotalStrips := contTotalStrips +1
  204. if maxStripLostTrades < 0 and MM_Method == 13
  205.     contTotalStrips := contTotalStrips +1
  206. plot(contTotalStrips, title="Cont Total Strips")
  207.  
  208. // Count Total Won Strips
  209. var contWonStrips = 0
  210. if maxStripWonTrades > 0 and MM_Method == 13
  211.     contWonStrips := contWonStrips +1
  212. plot(contWonStrips, title="Cont Won Strips")
  213.  
  214. // Count Total Lost Strips
  215. var contLostStrips = 0
  216. if maxStripLostTrades < 0 and MM_Method == 13
  217.     contLostStrips := contLostStrips +1
  218. plot(contLostStrips, title="Cont Lost Strips")
  219.  
  220.  
  221. // Creating three arrays to detect their size
  222. var arrayTotalStrips = array.new_float(contTotalStrips, 0)
  223. var arrayMaxStripWonTrades = array.new_float(contWonStrips, 0)
  224. var arrayMaxStripLostTrades = array.new_float(contLostStrips, 0)
  225.  
  226.  
  227. // Put a variable results into an array
  228. if maxStripWonTrades != 0
  229.     array.push(arrayMaxStripWonTrades, maxStripWonTrades)
  230.  
  231. if maxStripLostTrades != 0
  232.     array.push(arrayMaxStripLostTrades, maxStripLostTrades)
  233.  
  234. if maxStripLostTrades != 0
  235.     array.push(arrayTotalStrips, maxStripLostTrades)
  236.  
  237.  
  238. // // Loop all the array size and display all the lenght on the screen with table.cell "arrayMaxStripWonTrades"
  239. // for i=0 to array.size(arrayMaxStripWonTrades)-1
  240. //     table.cell(tabellaInfo, i+1, 0, str.tostring(i), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
  241. table.cell(tabellaInfo, 2, 0, str.tostring(array.size(arrayMaxStripWonTrades)), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
  242. for i=0 to 0
  243.     table.cell(tabellaInfo, i+1, 0, str.tostring(arrayMaxStripWonTrades), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
  244.  
  245. // // Loop all the array size and display all the lenght on the screen with table.cell "arrayMaxStripLostTrades"
  246. // for i=0 to array.size(arrayMaxStripLostTrades)-1
  247. //    table.cell(tabellaInfo, i+1, 1, str.tostring(i), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
  248. table.cell(tabellaInfo, 2, 1, str.tostring(array.size(arrayMaxStripLostTrades)), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
  249. for i=0 to 0
  250.     table.cell(tabellaInfo, i+1, 1, str.tostring(arrayMaxStripLostTrades), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
  251.  
  252. // // Loop all the array size and display all the lenght on the screen with table.cell "arrayTotalStrips"
  253. // for i=0 to array.size(arrayTotalStrips)-1
  254.     // table.cell(tabellaInfo, i+1, 2, str.tostring(i), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center
  255. // for i=0 to 0
  256. //     table.cell(tabellaInfo, i+1, 2, str.tostring(arrayMaxStripWonTrades) + str.tostring(arrayMaxStripLostTrades) , text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
  257.  
  258.  
  259. // Create a label when size changes
  260. inPosition =  countWonTrades == MaxPositiveStrips and MaxPositiveStrips != 0 and strategy.opentrades[1] == 0 and strategy.opentrades == 1 ? true :                                                               // No Swing Position
  261.      countLostTrades == - MaxNegativeStrips and MaxNegativeStrips != 0 and strategy.opentrades[1] == 0 and strategy.opentrades == 1 ? true :                                                                     // No Swing Position
  262.          countWonTrades == MaxPositiveStrips and MaxPositiveStrips != 0 and strategy.opentrades[1] == 1 and strategy.opentrades == 1 and strategy.position_size[1] != strategy.position_size ? true :            // Swing Position
  263.              countLostTrades == - MaxNegativeStrips and MaxNegativeStrips != 0 and strategy.opentrades[1] == 1 and strategy.opentrades == 1 and strategy.position_size[1] != strategy.position_size ? true : na  // Swing Position
  264.  
  265. plotshape(MM_Method == 13 ? inPosition : na, title= " In Position", color=color.fuchsia, size = size.large)
  266.  
  267. labelChange = ""
  268. if inPosition == true
  269.     labelChange := countWonTrades == MaxPositiveStrips ? "Change_Size: " + str.tostring(EntryCapitalPercentZScorePositiveStrips) :    // No Swing Position
  270.          countLostTrades == - MaxNegativeStrips ? "Change_Size: " + str.tostring(EntryCapitalPercentZScoreNegativeStrips) : na        // No Swing Position
  271.      
  272.     label.new(bar_index, na, text=labelChange, yloc=yloc.abovebar, color=color.red)
  273.  
  274.  
  275.  
Advertisement
Comments
  • tradingviewcodes
    235 days
    # text 0.12 KB | 0 0
    1. download all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment
Advertisement