Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- /////////////////////////////////////////////////////////////// Indicatore Z Score Trade Dependency ///////////////////////////////////////////////////////////////
- // 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.
- // Individuare il numero di strisce positive ricorrenti dall'array e impostare l'input max positive strips su quel numero ES: 5,
- // poi diminuire la size con: EntryCapitalPercentZScorePositiveStrips dato che dopo qual numero di vittorie la 6sta operazione potrebbe perdere.
- // 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
- // 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
- // ed al 5nto invece che potrei vincere entro normalmente.
- // 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.
- strategy(title='Indicatore Z Score Trade Dependency', overlay=true,
- //max_bars_back=5000, // Serve Per Caricare Più Storico Per Il Trailing Stop
- pyramiding=0,
- initial_capital=1000,
- commission_type=strategy.commission.percent,
- commission_value=0.1,
- slippage=3,
- default_qty_type=strategy.percent_of_equity,
- precision=4,
- default_qty_value=24)
- MM_Method = input(13, "MM_Method", group="Money Management Method")
- InitialCapital = input.float(defval = 1000, title="Initial Capital", group="Capital Management", step=0.1)
- EntryCapitalPercent = input.float(defval = 24, title="Entry Capital Percent", group="Capital Management", step=0.1)
- EntryCapitalPercentZScore = input.float(defval = 24, title="Entry Capital Percent Z Score", group="Z Score Trade Dependency", step=0.1)
- EntryCapitalPercentZScorePositiveStrips = input.float(defval = 10, title="Entry Capital Percent Z Score Positive Strips", group="Z Score Trade Dependency", step=0.1)
- EntryCapitalPercentZScoreNegativeStrips = input.float(defval = 5, title="Entry Capital Percent Z Score Negative Strips", group="Z Score Trade Dependency", step=0.1)
- 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]")
- 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]")
- // // Start checking if last trade was lost
- var lastTradeWasLoss = false
- if (strategy.losstrades[0] > strategy.losstrades[1])
- lastTradeWasLoss := true
- if (strategy.wintrades[0] > strategy.wintrades[1])
- // // successful trade, reset
- lastTradeWasLoss := false
- plotshape(MM_Method == 13 ? lastTradeWasLoss : na, title="Last Trade Was Loss", color=color.red)
- bgcolor(MM_Method == 13 and lastTradeWasLoss == 1 ? color.red : lastTradeWasLoss == 0 ? color.green : na, transp=90)
- // // Debug last Trade Was Loss "It Swithes from 0 to 1 everytime the entire strip changes from win to loss"
- // if strategy.closedtrades != strategy.closedtrades[1]
- // log.info(str.tostring(lastTradeWasLoss), "lastTradeWasLoss")
- // // End checking if last trade was lost
- // // Counter
- var countLostTrades = 0
- var countWonTrades = 0
- if (strategy.losstrades[0] > strategy.losstrades[1])
- countLostTrades := countLostTrades - 1
- else if (strategy.wintrades[0] > strategy.wintrades[1])
- countLostTrades := 0
- if (strategy.wintrades[0] > strategy.wintrades[1])
- countWonTrades := countWonTrades + 1
- else if (strategy.losstrades[0] > strategy.losstrades[1])
- countWonTrades := 0
- plotshape(MM_Method == 13 ? countLostTrades : na, title="Count Lost Trades", color=color.red)
- plotshape(MM_Method == 13 ? countWonTrades : na, title="Count Won Trades", color=color.green)
- // // Debug count Lost Trades E count Won Trades One By One
- // if strategy.closedtrades != strategy.closedtrades[1] and countLostTrades != 0
- // log.info(str.tostring(countLostTrades), "countLostTrades")
- // if strategy.closedtrades != strategy.closedtrades[1] and countWonTrades != 0
- // log.info(str.tostring(countWonTrades), "countWonTrades")
- /////////////////////////////////////////////////////////////// Start Strips Win/Loss Max Trades Plotter. It returns the maximum of the strip. ///////////////////////////////////////////////////////////////
- //////////////////////// Start Start End Bar Index Returns the bar index at the beginning/end of the strip. Not Needed "keep commented". ////////////////////////
- // plot(bar_index, title="Bar Index")
- // var startBarIndex = 0.0
- // if countLostTrades[0] < 0 and countLostTrades[1] == 0
- // startBarIndex := bar_index
- // plot(startBarIndex, title="Start Bar Index")
- // plotshape(countLostTrades[0] < 0 and countLostTrades[1] == 0, title="Start Bar Index Plotshape", color=color.green, size=size.normal)
- // var endBarIndex = 0.0
- // if countWonTrades[0] > 0 and countWonTrades[1] == 0
- // endBarIndex := bar_index
- // plot(endBarIndex, title="End Bar Index")
- // plotshape(countWonTrades[0] > 0 and countWonTrades[1] == 0, title="End Bar Index Plotshape", color=color.red, size=size.normal)
- //////////////////////// End Start End Bar Index Returns the bar index at the beginning/end of the strip. Not Needed "keep commented". ////////////////////////
- maxStripWonTrades = 0
- if countLostTrades[0] < 0 and countLostTrades[1] == 0
- maxStripWonTrades := countWonTrades[1]
- plot(MM_Method == 13 ? maxStripWonTrades : na, title="Max Strip Won Trades")
- 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)
- maxStripLostTrades = 0
- if countWonTrades[0] > 0 and countWonTrades[1] == 0
- maxStripLostTrades := countLostTrades[1]
- plot(MM_Method == 13 ? maxStripLostTrades : na, title="Max Strip Lost Trades")
- 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)
- // // // Debug Start Strips Win/Loss Max Trades Plotter.
- if MM_Method == 13 and maxStripWonTrades != 0
- log.info(str.tostring(maxStripWonTrades), "Max Strip Won Trades")
- // log.info("\nMax Strip Won Trades : {0}" , str.tostring(maxStripWonTrades)) // More detailed logs !!!
- if MM_Method == 13 and maxStripLostTrades != 0
- log.info(str.tostring(maxStripLostTrades), "Max Strip Lost Trades")
- // log.info("\nMax Strip Lost Trades : {0}" , str.tostring(maxStripLostTrades)) // More detailed logs !!!
- /////////////////////////////////////////////////////////////// End Strips Win/Loss Max Trades Plotter. It returns the maximum of the strip. ///////////////////////////////////////////////////////////////
- // Start Position Size Calculation
- openProfit = strategy.opentrades.profit(0)
- plot(MM_Method == 13 ? openProfit : na, title = "Open Profit")
- EntryBalancePercentIncDec = countWonTrades == MaxPositiveStrips and MaxPositiveStrips != 0 and strategy.opentrades == 0 ? 10 :
- countLostTrades == - MaxNegativeStrips and MaxNegativeStrips != 0 and strategy.opentrades == 0 ? 5 :
- countWonTrades != MaxPositiveStrips and strategy.opentrades == 0 ? 24 :
- countLostTrades != - MaxNegativeStrips and strategy.opentrades == 0 ? 24 :
- countLostTrades == (- MaxNegativeStrips + 1) and openProfit < 0 ? 5 :
- countWonTrades == (MaxPositiveStrips - 1) and openProfit > 0 ? 10 :
- 24
- // End Position Size Calculation
- posSize = 0.0
- if (MM_Method == 13) // Monetario In Percentuale Al Balance Win/Loss Incremented/Decremented (Imposta Input EntryCapitalPercent)
- posSize := EntryBalancePercentIncDec
- plot(posSize, title="Position Size")
- plot(InitialCapital + strategy.netprofit, title="Actual Equity")
- /////////////////////////////////////////////////////////////// Inizio Condizioni Di Entrata / Uscita Long/Short/Swing ///////////////////////////////////
- // Condizione Di Ingresso Long
- longCondition = ta.crossover(ta.sma(close,31), ta.sma(close, 28))
- if (longCondition)
- strategy.entry("Entry Long", strategy.long, qty=posSize)
- // Condizione Chiusura Long
- closeConditionLong = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
- if (closeConditionLong)
- strategy.close("Entry Long")
- // // Prova swing posizone per vedere se chiude tutte le posizioni.
- // // Condizioni Di Swing Short (Commentare il: Condizione Chiusura Long E decommentare Condizioni Di Swing Short Per fare questo test!!! )
- // shortCondition = ta.crossunder(ta.sma(close, 7), ta.sma(close, 12))
- // if (shortCondition)
- // strategy.entry("My Short Entry Id", strategy.short, qty=posSize)
- // Condizione Di Ingresso Short
- shortCondition = ta.crossover(ta.sma(close, 7), ta.sma(close, 12))
- if (shortCondition)
- strategy.entry("My Short Entry Id", strategy.short, qty=posSize)
- // Condizione Chiusura Short
- closeConditionShort = ta.crossunder(ta.sma(close, 5), ta.sma(close, 10))
- if (closeConditionShort)
- strategy.close("My Short Entry Id")
- /////////////////////////////////////////////////////////////// Fine Condizioni Di Entrata / Uscita Long/Short/Swing ///////////////////////////////////
- // Monitoraggio posizione
- bought = strategy.position_size[0]> strategy.position_size[1]
- Close_TP = false
- Close_TP := strategy.position_size[1] - strategy.position_size[0] and strategy.position_size[1] != 0 and strategy.position_size[0] != 0
- plotshape(Close_TP,title="Close_TP", style=shape.xcross, color=color.blue, size =size.small, editable = true)
- plot(strategy.position_size[1],"Position Old")
- plot(strategy.position_size,"Position")
- // Table Inputs
- fattoreArrotondamento = input.int(1, "Fattore di arrotondamento",options = [1,10,100,1000,10000,100000, 1000000, 10000000])
- 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"])
- coloreBarreBullish = input.color(color.rgb(47, 159, 68), "Colore celle bullish")
- coloreBarreBearish = input.color(color.rgb(227, 65, 65), "Colore celle bearish")
- coloreSfondoTabella = input.color(color.rgb(53, 61, 68), "Colore sfondo tabella")
- coloreEtichetteSfondo = input.color(color.rgb(19, 50, 70), "Colore sfondo etichetta")
- coloreEtichetteTesto = input.color(color.rgb(255, 255, 255), "Colore testo etichetta")
- dimensioneTesto = input.string("small", "Dimensione Carattere", options=["tiny", "small", "normal", "large", "huge"])
- // Creating a table and cells
- var tabellaInfo = table.new(position = posizione, columns = 100, rows = 100, bgcolor = coloreSfondoTabella)
- table.cell(tabellaInfo, 0, 0, "MaxStripWonTrades", text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign= text.align_center)
- table.cell(tabellaInfo, 0, 1, "MaxStripLostTrades", text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign= text.align_center)
- table.cell(tabellaInfo, 0, 2, "MaxStripWonLostTrades", text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign= text.align_center)
- // Count Total Won/Lost Strips
- var contTotalStrips = 0
- if maxStripWonTrades > 0 and MM_Method == 13
- contTotalStrips := contTotalStrips +1
- if maxStripLostTrades < 0 and MM_Method == 13
- contTotalStrips := contTotalStrips +1
- plot(contTotalStrips, title="Cont Total Strips")
- // Count Total Won Strips
- var contWonStrips = 0
- if maxStripWonTrades > 0 and MM_Method == 13
- contWonStrips := contWonStrips +1
- plot(contWonStrips, title="Cont Won Strips")
- // Count Total Lost Strips
- var contLostStrips = 0
- if maxStripLostTrades < 0 and MM_Method == 13
- contLostStrips := contLostStrips +1
- plot(contLostStrips, title="Cont Lost Strips")
- // Creating three arrays to detect their size
- var arrayTotalStrips = array.new_float(contTotalStrips, 0)
- var arrayMaxStripWonTrades = array.new_float(contWonStrips, 0)
- var arrayMaxStripLostTrades = array.new_float(contLostStrips, 0)
- // Put a variable results into an array
- if maxStripWonTrades != 0
- array.push(arrayMaxStripWonTrades, maxStripWonTrades)
- if maxStripLostTrades != 0
- array.push(arrayMaxStripLostTrades, maxStripLostTrades)
- if maxStripLostTrades != 0
- array.push(arrayTotalStrips, maxStripLostTrades)
- // // Loop all the array size and display all the lenght on the screen with table.cell "arrayMaxStripWonTrades"
- // for i=0 to array.size(arrayMaxStripWonTrades)-1
- // table.cell(tabellaInfo, i+1, 0, str.tostring(i), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
- table.cell(tabellaInfo, 2, 0, str.tostring(array.size(arrayMaxStripWonTrades)), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
- for i=0 to 0
- table.cell(tabellaInfo, i+1, 0, str.tostring(arrayMaxStripWonTrades), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
- // // Loop all the array size and display all the lenght on the screen with table.cell "arrayMaxStripLostTrades"
- // for i=0 to array.size(arrayMaxStripLostTrades)-1
- // table.cell(tabellaInfo, i+1, 1, str.tostring(i), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
- table.cell(tabellaInfo, 2, 1, str.tostring(array.size(arrayMaxStripLostTrades)), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
- for i=0 to 0
- table.cell(tabellaInfo, i+1, 1, str.tostring(arrayMaxStripLostTrades), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
- // // Loop all the array size and display all the lenght on the screen with table.cell "arrayTotalStrips"
- // for i=0 to array.size(arrayTotalStrips)-1
- // table.cell(tabellaInfo, i+1, 2, str.tostring(i), text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center
- // for i=0 to 0
- // table.cell(tabellaInfo, i+1, 2, str.tostring(arrayMaxStripWonTrades) + str.tostring(arrayMaxStripLostTrades) , text_color = coloreEtichetteTesto, text_size = dimensioneTesto, text_halign=text.align_center)
- // Create a label when size changes
- inPosition = countWonTrades == MaxPositiveStrips and MaxPositiveStrips != 0 and strategy.opentrades[1] == 0 and strategy.opentrades == 1 ? true : // No Swing Position
- countLostTrades == - MaxNegativeStrips and MaxNegativeStrips != 0 and strategy.opentrades[1] == 0 and strategy.opentrades == 1 ? true : // No Swing Position
- 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
- 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
- plotshape(MM_Method == 13 ? inPosition : na, title= " In Position", color=color.fuchsia, size = size.large)
- labelChange = ""
- if inPosition == true
- labelChange := countWonTrades == MaxPositiveStrips ? "Change_Size: " + str.tostring(EntryCapitalPercentZScorePositiveStrips) : // No Swing Position
- countLostTrades == - MaxNegativeStrips ? "Change_Size: " + str.tostring(EntryCapitalPercentZScoreNegativeStrips) : na // No Swing Position
- label.new(bar_index, na, text=labelChange, yloc=yloc.abovebar, color=color.red)
Advertisement
Comments
-
- 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