Reputation: 143
I wrote a strategy in Pine Script in order to backtest it on Trading View. It seems it compiles with no error but when backtesting it says "No Data". I can't figure out what is wrong with my code and I will show to you. The Strategy consists of two indicator: Support & Resistance and Awesome Oscillator. The first indicator has been copied from another script and it works (plotting), the second is a simple Awesome Oscillator and obviously it works (plotting).
I added some function in order to get entry signals thanks to Awesome Oscillator and Take Profit and Stop Loss levels thanks to Support & Resistance.
The problem is, even if the script has no recognizable error, it doesn't place any order.
I tried to play another strategy script to see if it's a Trading View issue, but indeed an other public strategy works.
The only thing I've modified is to move when
parameter in a if(condition)
. No change, still "No Data" error in the Strategy Tester section.
Searching for the Internet result in no answer.
Here my strategy code. The first (longest) part is copied and it works well (Support & Resistance). In the second part I added an Awesome Oscillator, functions and code to execute the strategy.
//@version=4
// Support and Resistance Levels + Zones for 3 time frames, based on volume at fractal levels
// Original script is thanks to synapticex I have just migrated to version 3, heavily modified it and added Support and Resistance Zones
strategy(title="Consiglio Strategy", shorttitle="Consiglio", overlay=true, format=format.inherit, precision=1, scale=scale.none, pyramiding=0, calc_on_order_fills=true, calc_on_every_tick=true)
risk = input(1, title="% Risk per Trade", minval = 0.01, step=0.1)
// Inputs
// 136 version
// Time Frame 1 = TF1 = Current Time Frame
TF1_Menu = input(title='Current', defval='S/R Zones', options=['S/R', 'S/R Zones', 'N/A'], type=input.string)
TF1_VolMA1Input = input(title="Current - Volume MA - Threshold", type=input.integer, defval=6)
TF1 = timeframe.period
// Time Frame 2 = TF2 = Custom 1 Time Frame
TF2_Menu = input(title='Custom 1', defval='S/R Zones', options=['S/R', 'S/R Zones', 'N/A'], type=input.string)
TF2 = input(title="Custom 1 - Timeframe", type=input.resolution, defval = "240")
TF2_VolMA1Input = input(title="Custom 1 - Volume MA - Threshold", type=input.integer, defval=6)
// Time Frame 3 = TF3 = Custom 2 Time Frame
TF3_Menu = input(title='Custom 2', defval='S/R Zones', options=['S/R', 'S/R Zones', 'N/A'], type=input.string)
TF3 = input(title="Custom 2 - Timeframe", type=input.resolution, defval = "1D")
TF3_VolMA1Input = input(title="Custom 2 - Volume MA - Threshold", type=input.integer, defval=6)
InvertColors = input(false, title = "Invert Colors - Support / Resistance")
// S/R - Current Time Frame = Time Frame 1 = TF1
TF1_Vol = security(syminfo.tickerid,TF1, volume)
TF1_VolMA = sma(TF1_Vol, TF1_VolMA1Input)
TF1_High = security(syminfo.tickerid,TF1, high)
TF1_Low = security(syminfo.tickerid,TF1, low)
TF1_Open = security(syminfo.tickerid,TF1, open)
TF1_Close = security(syminfo.tickerid,TF1, close)
TF1_Up = TF1_High[3] > TF1_High[4] and TF1_High[4] > TF1_High[5] and TF1_High[2] < TF1_High[3] and TF1_High[1] < TF1_High[2] and (TF1_Vol[3] > TF1_VolMA[3]) // or volume[3] > VolMA2Current[3])
TF1_Down = TF1_Low[3] < TF1_Low[4] and TF1_Low[4] < TF1_Low[5] and TF1_Low[2] > TF1_Low[3] and TF1_Low[1] > TF1_Low[2] and (TF1_Vol[3] > TF1_VolMA[3]) // or volume[3] > VolMA2Current[3])
TF1_CalcFractalUp()=>
TF1_FractalUp = 0.0
TF1_FractalUp := TF1_Up ? TF1_High[3] : TF1_FractalUp[1]
TF1_CalcFractalDown()=>
TF1_FractalDown = 0.0
TF1_FractalDown := TF1_Down ? TF1_Low[3] : TF1_FractalDown[1]
TF1_FractalUp = security(syminfo.tickerid,TF1, TF1_CalcFractalUp())
TF1_FractalDown = security(syminfo.tickerid,TF1, TF1_CalcFractalDown())
// Zones - Current Time Frame = Time Frame 1 = TF1
// Fractal Up Zones
TF1_CalcFractalUpLowerZone()=>
TF1_FractalUpLowerZone = 0.0
TF1_FractalUpLowerZone := TF1_Up and TF1_Close[3] > TF1_Open[3] ? TF1_Close[3] : TF1_Up and TF1_Close[3] < TF1_Open[3] ? TF1_Open[3] : TF1_FractalUpLowerZone[1]
TF1_CalcFractalUpUpperZone()=>
TF1_FractalUpUpperZone = 0.0
TF1_FractalUpUpperZone := TF1_Up and TF1_Close[3] > TF1_Open[3] ? (TF1_High[3] - TF1_Close[3]) + TF1_High[3] : TF1_Up and TF1_Close[3] < TF1_Open[3] ? (TF1_High[3] - TF1_Open[3]) + TF1_High[3] : TF1_FractalUpUpperZone[1]
TF1_FractalUpLowerZone = security(syminfo.tickerid, TF1, TF1_CalcFractalUpLowerZone())
TF1_FractalUpUpperZone = security(syminfo.tickerid, TF1, TF1_CalcFractalUpUpperZone())
TF1_ResistanceUpperZone = TF1_FractalUpUpperZone
TF1_ResistanceLowerZone = TF1_FractalUpLowerZone
// Fractal Down Zones
TF1_CalcFractalDownUpperZone()=>
TF1_FractalDownUpperZone = 0.0
TF1_FractalDownUpperZone := TF1_Down and TF1_Close[3] > TF1_Open[3] ? TF1_Open[3] : TF1_Down and TF1_Close[3] < TF1_Open[3] ? TF1_Close[3] : TF1_FractalDownUpperZone[1]
TF1_CalcFractalDownLowerZone()=>
TF1_FractalDownLowerZone = 0.0
TF1_FractalDownLowerZone := TF1_Down and TF1_Close[3] > TF1_Open[3] ? TF1_Low[3] + (TF1_Low[3] - TF1_Open[3]) : TF1_Down and TF1_Close[3] < TF1_Open[3] ? TF1_Low[3] + (TF1_Low[3] - TF1_Close[3]) : TF1_FractalDownLowerZone[1]
TF1_FractalDownLowerZone = security(syminfo.tickerid, TF1, TF1_CalcFractalDownLowerZone())
TF1_FractalDownUpperZone = security(syminfo.tickerid, TF1, TF1_CalcFractalDownUpperZone())
TF1_SupportUpperZone = TF1_FractalDownUpperZone
TF1_SupportLowerZone = TF1_FractalDownLowerZone
// Colors - Current Time Frame = Time Frame 1 = TF1
TF1_ResistanceColor = not InvertColors ? color.red : color.green
TF1_SupportColor = not InvertColors ? color.green : color.red
TF1_ResZoneColor = (TF1_FractalUp != TF1_FractalUp[1])? na:color.red
TF1_ResZoneColorInverted = (TF1_FractalUp != TF1_FractalUp[1])? na:color.green
TF1_SupZoneColor = (TF1_FractalDown != TF1_FractalDown[1])? na:color.green
TF1_SupZoneColorInverted = (TF1_FractalDown != TF1_FractalDown[1])? na:color.red
TF1_ResistanceZonesColor = not InvertColors and TF1_Menu=='S/R Zones' ? TF1_ResZoneColor : InvertColors and TF1_Menu=='S/R Zones' ? TF1_ResZoneColorInverted : na // red : lime
TF1_SupportZonesColor = not InvertColors and TF1_Menu=='S/R Zones' ? TF1_SupZoneColor : InvertColors and TF1_Menu=='S/R Zones' ? TF1_SupZoneColorInverted : na // lime : red
// S/R & S/R Zone Plots - Current Time Frame = Time Frame 1 = TF1
TF1_ResistanceUpZone = plot(TF1_Menu=='S/R Zones'? TF1_ResistanceUpperZone : na, "Current Timeframe - Resistance - Upper Zone", color=TF1_ResistanceZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false) // offset=-2
TF1_ResistanceDownZone = plot(TF1_Menu=='S/R Zones'? TF1_ResistanceLowerZone : na, "Current Timeframe - Resistance - Lower Zone", color=TF1_ResistanceZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false) // offset=-2
fill(TF1_ResistanceUpZone, TF1_ResistanceDownZone, color = TF1_ResistanceZonesColor, transp=93, title = "Current Timeframe - Resistance Zone Shading")
plot((TF1_Menu=='S/R' or TF1_Menu=='S/R Zones')? TF1_FractalUp : na, "Current Timeframe - Resistance", color=TF1_ResistanceColor, linewidth=1, style=plot.style_circles, transp=5, offset=-3, join=false)
plot((TF1_Menu=='S/R' or TF1_Menu=='S/R Zones')? TF1_FractalDown : na, "Current Timeframe - Support", color=TF1_SupportColor, linewidth=1, style=plot.style_circles, transp=5, offset=-3, join=false)
TF1_SupportUpZone = plot(TF1_Menu=='S/R Zones'? TF1_SupportUpperZone : na, "Current Timeframe - Support - Uper Zone", color=TF1_SupportZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false)
TF1_SupportDownZone = plot(TF1_Menu=='S/R Zones'? TF1_SupportLowerZone : na, "Current Timeframe - Support - Lower Zone", color=TF1_SupportZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false)
fill(TF1_SupportUpZone, TF1_SupportDownZone, color =TF1_SupportZonesColor, transp=93, title = "Current Timeframe - Support Zone Shading")
// S/R - Custom 1 Time Frame = Time Frame 2 = TF2
TF2_Vol = security(syminfo.tickerid,TF2, volume)
TF2_VolMA = sma(TF2_Vol, TF2_VolMA1Input)
TF2_High = security(syminfo.tickerid,TF2, high)
TF2_Low = security(syminfo.tickerid,TF2, low)
TF2_Open = security(syminfo.tickerid,TF2, open)
TF2_Close = security(syminfo.tickerid,TF2, close)
TF2_Up = TF2_High[3] > TF2_High[4] and TF2_High[4] > TF2_High[5] and TF2_High[2] < TF2_High[3] and TF2_High[1] < TF2_High[2] and (TF2_Vol[3] > TF2_VolMA[3]) // or volume[3] > VolMA2Custom 1[3])
TF2_Down = TF2_Low[3] < TF2_Low[4] and TF2_Low[4] < TF2_Low[5] and TF2_Low[2] > TF2_Low[3] and TF2_Low[1] > TF2_Low[2] and (TF2_Vol[3] > TF2_VolMA[3]) // or volume[3] > VolMA2Custom 1[3])
TF2_CalcFractalUp()=>
TF2_FractalUp = 0.0
TF2_FractalUp := TF2_Up ? TF2_High[3] : TF2_FractalUp[1]
TF2_CalcFractalDown()=>
TF2_FractalDown = 0.0
TF2_FractalDown := TF2_Down ? TF2_Low[3] : TF2_FractalDown[1]
TF2_FractalUp = security(syminfo.tickerid,TF2, TF2_CalcFractalUp())
TF2_FractalDown = security(syminfo.tickerid,TF2, TF2_CalcFractalDown())
// Zones - Custom 1 Time Frame = Time Frame 2 = TF2
// Fractal Up Zones
TF2_CalcFractalUpLowerZone()=>
TF2_FractalUpLowerZone = 0.0
TF2_FractalUpLowerZone := TF2_Up and TF2_Close[3] > TF2_Open[3] ? TF2_Close[3] : TF2_Up and TF2_Close[3] < TF2_Open[3] ? TF2_Open[3] : TF2_FractalUpLowerZone[1]
TF2_CalcFractalUpUpperZone()=>
TF2_FractalUpUpperZone = 0.0
TF2_FractalUpUpperZone := TF2_Up and TF2_Close[3] > TF2_Open[3] ? (TF2_High[3] - TF2_Close[3]) + TF2_High[3] : TF2_Up and TF2_Close[3] < TF2_Open[3] ? (TF2_High[3] - TF2_Open[3]) + TF2_High[3] : TF2_FractalUpUpperZone[1]
TF2_FractalUpLowerZone = security(syminfo.tickerid, TF2, TF2_CalcFractalUpLowerZone())
TF2_FractalUpUpperZone = security(syminfo.tickerid, TF2, TF2_CalcFractalUpUpperZone())
TF2_ResistanceUpperZone = TF2_FractalUpUpperZone
TF2_ResistanceLowerZone = TF2_FractalUpLowerZone
// Fractal Down Zones
TF2_CalcFractalDownUpperZone()=>
TF2_FractalDownUpperZone = 0.0
TF2_FractalDownUpperZone := TF2_Down and TF2_Close[3] > TF2_Open[3] ? TF2_Open[3] : TF2_Down and TF2_Close[3] < TF2_Open[3] ? TF2_Close[3] : TF2_FractalDownUpperZone[1]
TF2_CalcFractalDownLowerZone()=>
TF2_FractalDownLowerZone = 0.0
TF2_FractalDownLowerZone := TF2_Down and TF2_Close[3] > TF2_Open[3] ? TF2_Low[3] + (TF2_Low[3] - TF2_Open[3]) : TF2_Down and TF2_Close[3] < TF2_Open[3] ? TF2_Low[3] + (TF2_Low[3] - TF2_Close[3]) : TF2_FractalDownLowerZone[1]
TF2_FractalDownLowerZone = security(syminfo.tickerid, TF2, TF2_CalcFractalDownLowerZone())
TF2_FractalDownUpperZone = security(syminfo.tickerid, TF2, TF2_CalcFractalDownUpperZone())
TF2_SupportUpperZone = TF2_FractalDownUpperZone
TF2_SupportLowerZone = TF2_FractalDownLowerZone
// Colors - Custom 1 Time Frame = Time Frame 2 = TF2
TF2_ResistanceColor = not InvertColors ? color.red : color.green
TF2_SupportColor = not InvertColors ? color.green : color.red
TF2_ResZoneColor = (TF2_FractalUp != TF2_FractalUp[1])? na:color.red
TF2_ResZoneColorInverted = (TF2_FractalUp != TF2_FractalUp[1])? na:color.green
TF2_SupZoneColor = (TF2_FractalDown != TF2_FractalDown[1])? na:color.green
TF2_SupZoneColorInverted = (TF2_FractalDown != TF2_FractalDown[1])? na:color.red
TF2_ResistanceZonesColor = not InvertColors and TF2_Menu=='S/R Zones' ? TF2_ResZoneColor : InvertColors and TF2_Menu=='S/R Zones' ? TF2_ResZoneColorInverted : na // fuchsia : green
TF2_SupportZonesColor = not InvertColors and TF2_Menu=='S/R Zones' ? TF2_SupZoneColor : InvertColors and TF2_Menu=='S/R Zones' ? TF2_SupZoneColorInverted : na // green : fuchsia
// S/R & S/R Zone Plots - Custom 1 Time Frame = Time Frame 2 = TF2
TF2_ResistanceUpZone = plot(TF2_Menu=='S/R Zones'? TF2_ResistanceUpperZone : na, "Custom 1 Timeframe - Resistance - Upper Zone", color=TF2_ResistanceZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false) // offset=-2
TF2_ResistanceDownZone = plot(TF2_Menu=='S/R Zones'? TF2_ResistanceLowerZone : na, "Custom 1 Timeframe - Resistance - Lower Zone", color=TF2_ResistanceZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false) // offset=-2
fill(TF2_ResistanceUpZone, TF2_ResistanceDownZone, color = TF2_ResistanceZonesColor, transp=93, title = "Custom 1 Timeframe - Resistance Zone Shading")
plot((TF2_Menu=='S/R' or TF2_Menu=='S/R Zones')? TF2_FractalUp : na, "Custom 1 Timeframe - Resistance", color=TF2_ResistanceColor, linewidth=1, style=plot.style_circles, transp=5, offset=-3, join=false)
plot((TF2_Menu=='S/R' or TF2_Menu=='S/R Zones')? TF2_FractalDown : na, "Custom 1 Timeframe - Support", color=TF2_SupportColor, linewidth=1, style=plot.style_circles, transp=5, offset=-3, join=false)
TF2_SupportUpZone = plot(TF2_Menu=='S/R Zones'? TF2_SupportUpperZone : na, "Custom 1 Timeframe - Support - Uper Zone", color=TF2_SupportZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false)
TF2_SupportDownZone = plot(TF2_Menu=='S/R Zones'? TF2_SupportLowerZone : na, "Custom 1 Timeframe - Support - Lower Zone", color=TF2_SupportZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false)
fill(TF2_SupportUpZone, TF2_SupportDownZone, color =TF2_SupportZonesColor, transp=93, title = "Custom 1 Timeframe - Support Zone Shading")
// S/R - Custom 2 Time Frame = Time Frame 3 = TF3
TF3_Vol = security(syminfo.tickerid,TF3, volume)
TF3_VolMA = sma(TF3_Vol, TF3_VolMA1Input)
TF3_High = security(syminfo.tickerid,TF3, high)
TF3_Low = security(syminfo.tickerid,TF3, low)
TF3_Open = security(syminfo.tickerid,TF3, open)
TF3_Close = security(syminfo.tickerid,TF3, close)
TF3_Up = TF3_High[3] > TF3_High[4] and TF3_High[4] > TF3_High[5] and TF3_High[2] < TF3_High[3] and TF3_High[1] < TF3_High[2] and (TF3_Vol[3] > TF3_VolMA[3]) // or volume[3] > VolMA2Custom 2[3])
TF3_Down = TF3_Low[3] < TF3_Low[4] and TF3_Low[4] < TF3_Low[5] and TF3_Low[2] > TF3_Low[3] and TF3_Low[1] > TF3_Low[2] and (TF3_Vol[3] > TF3_VolMA[3]) // or volume[3] > VolMA2Custom 2[3])
TF3_CalcFractalUp()=>
TF3_FractalUp = 0.0
TF3_FractalUp := TF3_Up ? TF3_High[3] : TF3_FractalUp[1]
TF3_CalcFractalDown()=>
TF3_FractalDown = 0.0
TF3_FractalDown := TF3_Down ? TF3_Low[3] : TF3_FractalDown[1]
TF3_FractalUp = security(syminfo.tickerid,TF3, TF3_CalcFractalUp())
TF3_FractalDown = security(syminfo.tickerid,TF3, TF3_CalcFractalDown())
// Zones - Custom 2 Time Frame = Time Frame 3 = TF3
// Fractal Up Zones
TF3_CalcFractalUpLowerZone()=>
TF3_FractalUpLowerZone = 0.0
TF3_FractalUpLowerZone := TF3_Up and TF3_Close[3] > TF3_Open[3] ? TF3_Close[3] : TF3_Up and TF3_Close[3] < TF3_Open[3] ? TF3_Open[3] : TF3_FractalUpLowerZone[1]
TF3_CalcFractalUpUpperZone()=>
TF3_FractalUpUpperZone = 0.0
TF3_FractalUpUpperZone := TF3_Up and TF3_Close[3] > TF3_Open[3] ? (TF3_High[3] - TF3_Close[3]) + TF3_High[3] : TF3_Up and TF3_Close[3] < TF3_Open[3] ? (TF3_High[3] - TF3_Open[3]) + TF3_High[3] : TF3_FractalUpUpperZone[1]
TF3_FractalUpLowerZone = security(syminfo.tickerid, TF3, TF3_CalcFractalUpLowerZone())
TF3_FractalUpUpperZone = security(syminfo.tickerid, TF3, TF3_CalcFractalUpUpperZone())
TF3_ResistanceUpperZone = TF3_FractalUpUpperZone
TF3_ResistanceLowerZone = TF3_FractalUpLowerZone
// Fractal Down Zones
TF3_CalcFractalDownUpperZone()=>
TF3_FractalDownUpperZone = 0.0
TF3_FractalDownUpperZone := TF3_Down and TF3_Close[3] > TF3_Open[3] ? TF3_Open[3] : TF3_Down and TF3_Close[3] < TF3_Open[3] ? TF3_Close[3] : TF3_FractalDownUpperZone[1]
TF3_CalcFractalDownLowerZone()=>
TF3_FractalDownLowerZone = 0.0
TF3_FractalDownLowerZone := TF3_Down and TF3_Close[3] > TF3_Open[3] ? TF3_Low[3] + (TF3_Low[3] - TF3_Open[3]) : TF3_Down and TF3_Close[3] < TF3_Open[3] ? TF3_Low[3] + (TF3_Low[3] - TF3_Close[3]) : TF3_FractalDownLowerZone[1]
TF3_FractalDownLowerZone = security(syminfo.tickerid, TF3, TF3_CalcFractalDownLowerZone())
TF3_FractalDownUpperZone = security(syminfo.tickerid, TF3, TF3_CalcFractalDownUpperZone())
TF3_SupportUpperZone = TF3_FractalDownUpperZone
TF3_SupportLowerZone = TF3_FractalDownLowerZone
// Colors - Custom 2 Time Frame = Time Frame 3 = TF3
TF3_ResistanceColor = not InvertColors ? color.red : color.green
TF3_SupportColor = not InvertColors ? color.red : color.green
TF3_ResZoneColor = (TF3_FractalUp != TF3_FractalUp[1])? na:color.red
TF3_ResZoneColorInverted = (TF3_FractalUp != TF3_FractalUp[1])? na:color.green
TF3_SupZoneColor = (TF3_FractalDown != TF3_FractalDown[1])? na:color.red
TF3_SupZoneColorInverted = (TF3_FractalDown != TF3_FractalDown[1])? na:color.green
TF3_ResistanceZonesColor = not InvertColors and TF3_Menu=='S/R Zones' ? TF3_ResZoneColor : InvertColors and TF3_Menu=='S/R Zones' ? TF3_ResZoneColorInverted : na // orange : blue
TF3_SupportZonesColor = not InvertColors and TF3_Menu=='S/R Zones' ? TF3_SupZoneColor : InvertColors and TF3_Menu=='S/R Zones' ? TF3_SupZoneColorInverted : na // blue : orange
// S/R & S/R Zone Plots - Custom 2 Time Frame = Time Frame 3 = TF3
TF3_ResistanceUpZone = plot(TF3_Menu=='S/R Zones'? TF3_ResistanceUpperZone : na, "Custom 2 Timeframe - Resistance - Upper Zone", color=TF3_ResistanceZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false) // offset=-2
TF3_ResistanceDownZone = plot(TF3_Menu=='S/R Zones'? TF3_ResistanceLowerZone : na, "Custom 2 Timeframe - Resistance - Lower Zone", color=TF3_ResistanceZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false) // offset=-2
fill(TF3_ResistanceUpZone, TF3_ResistanceDownZone, color = TF3_ResistanceZonesColor, transp=93, title = "Custom 2 Timeframe - Resistance Zone Shading") // ResistanceColorCustom 2)
plot((TF3_Menu=='S/R' or TF3_Menu=='S/R Zones')? TF3_FractalUp : na, "Custom 2 Timeframe - Resistance", color=TF3_ResistanceColor, linewidth=1, style=plot.style_circles, transp=5, offset=-3, join=false)
plot((TF3_Menu=='S/R' or TF3_Menu=='S/R Zones')? TF3_FractalDown : na, "Custom 2 Timeframe - Support", color=TF3_SupportColor, linewidth=1, style=plot.style_circles, transp=5, offset=-3, join=false)
TF3_SupportUpZone = plot(TF3_Menu=='S/R Zones'? TF3_SupportUpperZone : na, "Custom 2 Timeframe - Support - Uper Zone", color=TF3_SupportZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false)
TF3_SupportDownZone = plot(TF3_Menu=='S/R Zones'? TF3_SupportLowerZone : na, "Custom 2 Timeframe - Support - Lower Zone", color=TF3_SupportZonesColor, linewidth=1, style=plot.style_line, transp=45, offset=-3, join=false)
fill(TF3_SupportUpZone, TF3_SupportDownZone, color =TF3_SupportZonesColor, transp=93, title = "Custom 2 Timeframe - Support Zone Shading")
// AWESOME OSCILLATOR
src = close
AOthreshold = input(defval = 80, title="AO Threshold", type=input.float, minval=0, step=0.1)
AO = (sma(hl2,5) - sma(hl2,34)) / 2
// FUNCTIONS
RRR() =>
rrr = (1 + (strategy.grossprofit / strategy.wintrades) / (strategy.grossloss / strategy.losstrades)) * (strategy.wintrades / (strategy.wintrades + strategy.losstrades)) - 1
na(rrr) or rrr < 0 ? 1 : rrr
upperLimit() => min(TF1_ResistanceLowerZone, min(TF2_ResistanceLowerZone, TF3_ResistanceLowerZone))
lowerLimit() => max(TF1_SupportUpperZone, max(TF2_SupportUpperZone, TF3_SupportUpperZone))
entryPrice(takeProfit, stopLoss) => ((RRR() * stopLoss) + takeProfit) / (RRR() + 1)
safeVolumeLong(entry, stopLoss) => ((strategy.initial_capital + strategy.netprofit) * risk / 100) / (entry - stopLoss) * entry
safeVolumeShort(stopLoss, entry) => ((strategy.initial_capital + strategy.netprofit) * risk / 100) / (stopLoss - entry) * stopLoss
// SIGNALS
longCondition = false
shortCondition = false
if(AO > AOthreshold)
longCondition = (AO[0] - AO[1]) > 0
if(AO < AOthreshold)
shortCondition = (AO[0] - AO[1]) < 0
// EXECUTE STRATEGY
longEntryPrice = entryPrice(upperLimit(), lowerLimit())
shortEntryPrice = entryPrice(lowerLimit(), upperLimit())
volumeLong = safeVolumeLong(longEntryPrice, lowerLimit()) / longEntryPrice
volumeShort = safeVolumeShort(lowerLimit(), shortEntryPrice) / shortEntryPrice
if(longCondition)
strategy.entry('Long entry', strategy.long, volumeLong, longEntryPrice, stop=na)
strategy.entry('Short entry', strategy.short, volumeShort, shortEntryPrice, stop=na)
if(shortCondition)
strategy.exit('Long exit', 'Long entry', qty=na, qty_percent=100.0, limit=upperLimit(), stop=lowerLimit())
strategy.exit('Short exit', 'Short entry', qty=na, qty_percent=100.0, limit=lowerLimit(), stop=upperLimit())
I expect at least some orders being placed but no one shows up when backtesting. What's wrong with my code?
Upvotes: 2
Views: 7027
Reputation: 8789
Replace these lines at the end of your code. You're initializing a new local variable in the if
block with the =
operator. Need to use :=
to assign value to existing global scope variables rather than create a new local one which will be lost outside the if
block:
if(AO > AOthreshold)
longCondition := (AO[0] - AO[1]) > 0
if(AO < AOthreshold)
shortCondition := (AO[0] - AO[1]) < 0
Upvotes: 2