Hello everyone,
I would like the following code to take only the first cross over or cross under, as the more we progress in the trend the more likely to have fake out.
Can you please indicate how can I modify the code to have only the first Cross Long or Short? and how to keep the trade all the way up or down?
Thank you.
DEFPARAM CUMULATEORDERS = FALSE
ca=0
cv=0
ATR = AverageTrueRange[PATR](close)
MAF = WeightedAverage[PF](WeightedClose)
GRAPHONPRICE MAF coloured("cyan") as "MAF"
ca = (close crosses over MAF )
cv = (close crosses under MAF )
if time>=080000 and time<=213000 then
IF NOT LongOnMarket AND ca THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
// Conditions to enter short positions
IF NOT ShortOnMarket AND cv THEN
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
IF LONGONMARKET and cv THEN
SELL AT MARKET
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
IF SHORTONMARKET and ca THEN
EXITSHORT AT MARKET
BUY 1 CONTRACTS AT MARKET
ENDIF
ENDIF
// Stops and targets : Enter your protection stops and profit targets here
SET TARGET PPROFIT TP
SET STOP PLOSS SL
IF TIME>=214500 THEN
SELL AT MARKET
EXITSHORT AT MARKET
ENDIF
There you go (I couldn’t test it as some variables are missing):
DEFPARAM CUMULATEORDERS = FALSE
ONCE FirstCrossOver = 1
ONCE FirstCrossUnder = 1
IF IntraDayBarIndex = 0 THEN
FirstCrossOver = 1
FirstCrossUnder = 1
ENDIF
ca=0
cv=0
ATR = AverageTrueRange[PATR](close)
MAF = WeightedAverage[PF](WeightedClose)
GRAPHONPRICE MAF coloured("cyan") as "MAF"
ca = (close crosses over MAF ) AND FirstCrossOver
cv = (close crosses under MAF ) AND FirstCrossUnder
if time>=080000 and time<=213000 then
IF NOT LongOnMarket AND ca THEN
BUY 1 CONTRACTS AT MARKET
FirstCrossOver = 0
ENDIF
// Conditions to enter short positions
IF NOT ShortOnMarket AND cv THEN
SELLSHORT 1 CONTRACTS AT MARKET
FirstCrossUnder = 0
ENDIF
IF LONGONMARKET and cv THEN
SELL AT MARKET
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
IF SHORTONMARKET and ca THEN
EXITSHORT AT MARKET
BUY 1 CONTRACTS AT MARKET
ENDIF
ENDIF
// Stops and targets : Enter your protection stops and profit targets here
SET TARGET PPROFIT TP
SET STOP PLOSS SL
IF TIME>=214500 THEN
SELL AT MARKET
EXITSHORT AT MARKET
ENDIF
Thank you Roberto. The code works fine for the first Signal of the day, then doesn’t take any of the other Signals in the same direction. What I would like to achieve is to take the first Signal Long until the First Signal Short happens, then take a Short until the next Signal Long happens and take Long, etc.
What I would like to avoid is taking two consecutive Long or two consecutive Short. If I exited the first Signal Long with TP or SL and then a second Signal Long appears, I d’ont want the system to take this second Signal in the same direction of the previous one.
I’ve added the variables optimized with backtest on 2k units on 5 min chart.
I hope my request is clearer now.
DEFPARAM CUMULATEORDERS = FALSE
ca=0
cv=0
PATR = 5
PF = 12
TP = 9
SL = 20
GRAPHONPRICE MAF coloured("cyan") as "MAF"
ONCE FirstCrossOver = 1
ONCE FirstCrossUnder = 1
IF IntraDayBarIndex = 0 THEN
FirstCrossOver = 1
FirstCrossUnder = 1
ENDIF
ATR = AverageTrueRange[PATR](close)
MAF = WeightedAverage[PF](WeightedClose)
ca = (close crosses over MAF) AND FirstCrossOver
cv = (close crosses under MAF) AND FirstCrossUnder
if time>=080000 and time<=213000 then
IF NOT LongOnMarket AND ca THEN
BUY 1 CONTRACTS AT MARKET
FirstCrossOver = 0
ENDIF
// Conditions to enter short positions
IF NOT ShortOnMarket AND cv THEN
SELLSHORT 1 CONTRACTS AT MARKET
FirstCrossUnder = 0
ENDIF
IF LONGONMARKET and cv THEN
SELL AT MARKET
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
IF SHORTONMARKET and ca THEN
EXITSHORT AT MARKET
BUY 1 CONTRACTS AT MARKET
ENDIF
ENDIF
// Stops and targets : Enter your protection stops and profit targets here
SET TARGET PPROFIT TP
SET STOP PLOSS SL
IF TIME>=214500 THEN
SELL AT MARKET
EXITSHORT AT MARKET
ENDIF
There CANNOT be two consecutive CROSSOVERs in the same direction!