//@version=5
strategy(title='Indicatore Z-Score', 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(maxStripWonTrades, 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(maxStripLostTrades, 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))
// // End Strips Win/Loss Max Trades Plotter. It returns the maximum of the strip.
// More detailed logs !!!
EntryBalancePercentIncDec = countWonTrades == MaxPositiveStrips and
MaxPositiveStrips != 0 ? (((InitialCapital) *
EntryCapitalPercentZScorePositiveStrips) / 100) / close :
countLostTrades == - MaxNegativeStrips and MaxNegativeStrips != 0 ?
(((InitialCapital) * EntryCapitalPercentZScoreNegativeStrips) / 100) / close :
countWonTrades != MaxPositiveStrips ? (((InitialCapital) *
EntryCapitalPercentZScore) / 100) / close :
countLostTrades != - MaxNegativeStrips ? (((InitialCapital) *
EntryCapitalPercentZScore) / 100) / close :
(((InitialCapital) * EntryCapitalPercentZScore) / 100) / close
//and MaxPositiveStrips != 0
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")
// 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")
// // Qui se voglio provare lo swing della posizone per vedere se chiude tutte le
posizioni. Funziona !!!, con questo codice chiude tutto specialmente al cambio
della size "va bene" !!!
// // 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 (closeConditionLong)
// strategy.close("My Short Entry Id")
// 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) // fare il merge di won and
lost che non esiste. sopra nel codice li ottengo semplicemente perchè li ottengo il
totle in modo separato !!!
// // 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 = strategy.position_size != strategy.position_size[1] and countWonTrades
== MaxPositiveStrips and not MaxPositiveStrips == 0 and not strategy.position_size
== 0 or strategy.position_size != strategy.position_size[1] and countLostTrades ==
- MaxNegativeStrips and not MaxNegativeStrips == 0 and not strategy.position_size
== 0 ? true : false
plotshape(inPosition, color=color.fuchsia, size = size.large)
labelChange = ""
if inPosition == true
labelChange := countWonTrades == MaxPositiveStrips ? "Change_Size: " +
str.tostring(EntryCapitalPercentZScorePositiveStrips) :
countLostTrades == - MaxNegativeStrips ? "Change_Size: " +
str.tostring(EntryCapitalPercentZScoreNegativeStrips) :
"Change_Size: " + str.tostring(EntryCapitalPercentZScore)
label.new(bar_index, na, text=labelChange, yloc=yloc.abovebar, color=color.red)
// Sistemazione label //
// Se c'è lo swing di posizione il codice non entra con la size giusta.
// Controllare tutto il codice che funziona bene con le size e le label, ATTENZIONE
a volte le label non dicono la verità, al momento se metto strips a 0 entra con:
EntryCapitalPercentZScore