You only have to run the code when the strategy is ON (I set a simple condition to set the strategy ON and OFF based on a 10-bar consecutive negative score, then resuming trading after a 10-bar pause):
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Z-Score (code) 2
//
//The results can be INCORRECT when, on the SAME bar, the following 3 conditions are met:
//
// 1) a trade is closed
// 2) a new trade is opened (usually due to a Stop & Reverse)
// 3) the new trade hits TP
//
//Furthermore, optimization should be avoided when accumulating positions, unless it's
//possible to consider all of them as one.
//
//(streaks)-https://www.forextraders.com/forex-education/forex-money-management/using-the-z-score-to-determine-trade-size/
//(streaks)-https://www.earnforex.com/guides/z-score-optimization-in-forex-trading/
//
//(runs) -https://www.mypivots.com/dictionary/definition/233/z-score
//(runs) -https://www.referenceforbusiness.com/encyclopedia/Val-Z/Z-Score.html
//
// Counting STREAKS (+ are Winning trades - are Losing trades)
//
//trades: +-++--++-----+---+++- (21 trades)
//tally: 1 2 3 4 5 6 (6 streaks) a STREAK is a TWIN pair followed
// by a different sign
//
//
// Counting RUNS (+ are Winning trades - are Losing trades)
//
//trades: +-++--++-----+---+++- (21 trades)
//tally: 12 3 4 5 67 8 9 (9 runs) a RUN is any sign followed
// by a different sign
//
// --- start of Z-Score code
ONCE StrategyON= 1
ONCE Periods = 0
ONCE Streaks = 0
ONCE CurTrade = 0
ONCE TotalWin = 0
ONCE TotalLose = 0
IF StrategyON = 1 THEN
Periods = Periods + 1
MyProfit = StrategyProfit
// tally Total winning + losing streaks (streak = consecutive winning or losing trades)
IF MyProfit <> MyProfit[1] AND (BarIndex > 0) THEN
IF MyProfit > MyProfit[1] THEN
CurTrade = 1
ELSIF MyProfit < MyProfit[1] THEN
CurTrade = -1
ENDIF
//---------------------------------------------------------------------------------
// code using STREAKS (it seems to be returning only negative values)
// (a sign change preceded by a twin, i.e. a couple of the same sign)
//
//Twins = (CurTrade = CurTrade[1]) //TWO identical trades to make a STREAK
//Streaks = Streaks + ((CurTrade <> CurTrade[1]) AND Twins[1])//now Different trade +
// prior candle's TWINS
//
//---------------------------------------------------------------------------------
// code using RUNS (like PRT seems to be doing)
// (a run is each sign change, no matter twins)
Streaks = Streaks + (CurTrade <> CurTrade[1]) //now Different trade
//---------------------------------------------------------------------------------
//
// tally Winning & Losing trades
TotalWin = TotalWin + (CurTrade = 1)
TotalLose = TotalLose + (CurTrade = -1)
N = TotalWin + TotalLose
ENDIF
IF Streaks > 0 THEN
P = max(1, 2 * TotalWin * TotalLose)
R = Streaks
Zscore = ((N*(R-0.5))-P) / sqrt((P*(P-N)) / (N-1))
Zscore = round(Zscore * 100) / 100
ELSE
Zscore = 0
ENDIF
Zpos = Zscore > 0 //or whatever else > 0
Zneg = Zscore < 0 //or whatever else < 0
// --- end of Z-Score code
//
//*********************************************************************************
// Z-Score management
ONCE SkipOneTrade = 0
ONCE SkipFlag = 0
ONCE MinTrades = 51 //30 or 51
DirectionSwitch = (ShortOnMarket AND LongOnMarket[1]) OR (ShortOnMarket[1] AND LongOnMarket)
IF Zpos THEN
IF (CurTrade = 1) AND SkipFlag = 0 AND ((Not Onmarket) OR DirectionSwitch) THEN
IF N >= MinTrades THEN
SkipOneTrade = 1
SkipFlag = 1
ENDIF
ENDIF
ELSIF Zneg THEN
IF (CurTrade = -1) AND SkipFlag = 0 AND ((Not Onmarket) OR DirectionSwitch) THEN
IF N >= MinTrades THEN
SkipOneTrade = 1
SkipFlag = 1
ENDIF
ENDIF
ENDIF
//*********************************************************************************
ONCE MA = 100 //100
ONCE T = 1 //1=ema
IF close CROSSES OVER average[MA,T] AND Not OnMarket THEN
IF SkipOneTrade THEN
SkipOneTrade = 0
ELSE
BUY AT Market
SkipFlag = 0
ENDIF
ELSIF close CROSSES UNDER average[MA,T] AND Not OnMarket THEN
IF SkipOneTrade THEN
SkipOneTrade = 0
ELSE
SELLSHORT AT Market
SkipFlag = 0
ENDIF
ENDIF
set stop ploss 500 //500
set target pprofit 5000 //5000
//
//*********************************************************************************
//trailing stop function
trailingstart = 50 //50 trailing will start @trailinstart points profit
trailingstep = 5 //5 trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//
BarsOFF = 0
IF summation[10](Zneg) = 10 THEN
StrategyON = 0 //set strategy to OFF after 10 negative scores in a row
ENDIF
//*********************************************************************************
ELSE
IF Not OnMarket THEN
BarsOFF = BarsOFF + 1
IF BarsOFF > 10 THEN
StrategyON = 1 //resume trading after a 10-bar pause
ENDIF
ENDIF
ENDIF
//
//graph Zscore coloured(255,0,0,255)
//graph TotalWin
//graph TotalLose
BradParticipant
Junior
Hi Traders
Thanks for these codes, Roberto
I was looking to create a code that misses a trade after a losing trade. Using/coping the above codes, I was able to come up with this:
//NumberOfTradesSkipped = 1. (Default value, Optimized)
ONCE Periods = 0
ONCE CurrentTrade = 0
ONCE TotalWin = 0
ONCE TotalLose = 0
Periods = Periods + 1
MyProfit = StrategyProfit
IF MyProfit <> MyProfit[1] AND (BarIndex > 0) THEN
IF MyProfit > MyProfit[1] THEN
CurrentTrade = 1
ELSIF MyProfit < MyProfit[1] THEN
CurrentTrade = -1
ENDIF
TotalWin = TotalWin + (CurrentTrade = 1)
TotalLose = TotalLose + (CurrentTrade = -1)
ENDIF
ONCE SkipTrades = 0
ONCE SkipFlag = 0
IF (CurrentTrade = -1) AND (SkipFlag = 0) AND (Not Onmarket) THEN
SkipTrades = NumberOfTradesSkipped
SkipFlag = 1
ENDIF
// Condition to enter a long position:
IF NOT LongOnMarket AND BuySignal THEN
IF SkipTrades > 0 THEN
SkipTrades = SkipTrades - 1
ELSE
BUY 1 CONTRACTS AT MARKET
SkipFlag = 0
ENDIF
ENDIF
Can someone please check this code for anything I have missed? I might also have lines that are not needed that I am not aware of.
Regards
Brad
There you go:
ONCE NumberOfTradesSkipped = 1 // (Default value, Optimized)
MyProfit = StrategyProfit
IF MyProfit < MyProfit[1] AND (BarIndex > 0) THEN
SkipTrades = NumberOfTradesSkipped
ENDIF
ONCE SkipTrades = 0
// Condition to enter a long position:
IF NOT LongOnMarket AND close crosses over highest[20](close[1]) THEN
IF SkipTrades > 0 THEN
SkipTrades = SkipTrades - 1
ELSE
BUY 1 CONTRACTS AT MARKET
ENDIF
ENDIF
//
set stop ploss 200
set target pprofit 200
BradParticipant
Junior
Thanks, Roberto
That’s a significant change! Back to the drawing board for me.
Regards
Brad