Z-Score to improve strategies
Forums › ProRealTime English forum › ProOrder support › Z-Score to improve strategies
- This topic has 18 replies, 6 voices, and was last updated 7 months ago by Brad.
-
-
01/23/2024 at 6:35 PM #226788
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):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////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 codeONCE StrategyON= 1ONCE Periods = 0ONCE Streaks = 0ONCE CurTrade = 0ONCE TotalWin = 0ONCE TotalLose = 0IF StrategyON = 1 THENPeriods = Periods + 1MyProfit = StrategyProfit// tally Total winning + losing streaks (streak = consecutive winning or losing trades)IF MyProfit <> MyProfit[1] AND (BarIndex > 0) THENIF MyProfit > MyProfit[1] THENCurTrade = 1ELSIF MyProfit < MyProfit[1] THENCurTrade = -1ENDIF//---------------------------------------------------------------------------------// 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 tradesTotalWin = TotalWin + (CurTrade = 1)TotalLose = TotalLose + (CurTrade = -1)N = TotalWin + TotalLoseENDIFIF Streaks > 0 THENP = max(1, 2 * TotalWin * TotalLose)R = StreaksZscore = ((N*(R-0.5))-P) / sqrt((P*(P-N)) / (N-1))Zscore = round(Zscore * 100) / 100ELSEZscore = 0ENDIFZpos = Zscore > 0 //or whatever else > 0Zneg = Zscore < 0 //or whatever else < 0// --- end of Z-Score code////*********************************************************************************// Z-Score managementONCE SkipOneTrade = 0ONCE SkipFlag = 0ONCE MinTrades = 51 //30 or 51DirectionSwitch = (ShortOnMarket AND LongOnMarket[1]) OR (ShortOnMarket[1] AND LongOnMarket)IF Zpos THENIF (CurTrade = 1) AND SkipFlag = 0 AND ((Not Onmarket) OR DirectionSwitch) THENIF N >= MinTrades THENSkipOneTrade = 1SkipFlag = 1ENDIFENDIFELSIF Zneg THENIF (CurTrade = -1) AND SkipFlag = 0 AND ((Not Onmarket) OR DirectionSwitch) THENIF N >= MinTrades THENSkipOneTrade = 1SkipFlag = 1ENDIFENDIFENDIF//*********************************************************************************ONCE MA = 100 //100ONCE T = 1 //1=emaIF close CROSSES OVER average[MA,T] AND Not OnMarket THENIF SkipOneTrade THENSkipOneTrade = 0ELSEBUY AT MarketSkipFlag = 0ENDIFELSIF close CROSSES UNDER average[MA,T] AND Not OnMarket THENIF SkipOneTrade THENSkipOneTrade = 0ELSESELLSHORT AT MarketSkipFlag = 0ENDIFENDIFset stop ploss 500 //500set target pprofit 5000 //5000////*********************************************************************************//trailing stop functiontrailingstart = 50 //50 trailing will start @trailinstart points profittrailingstep = 5 //5 trailing step to move the "stoploss"//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF//BarsOFF = 0IF summation[10](Zneg) = 10 THENStrategyON = 0 //set strategy to OFF after 10 negative scores in a rowENDIF//*********************************************************************************ELSEIF Not OnMarket THENBarsOFF = BarsOFF + 1IF BarsOFF > 10 THENStrategyON = 1 //resume trading after a 10-bar pauseENDIFENDIFENDIF////graph Zscore coloured(255,0,0,255)//graph TotalWin//graph TotalLose03/19/2024 at 4:13 PM #230045Hi 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:
Skip N trades after a loss:1234567891011121314151617181920212223242526272829303132333435363738//NumberOfTradesSkipped = 1. (Default value, Optimized)ONCE Periods = 0ONCE CurrentTrade = 0ONCE TotalWin = 0ONCE TotalLose = 0Periods = Periods + 1MyProfit = StrategyProfitIF MyProfit <> MyProfit[1] AND (BarIndex > 0) THENIF MyProfit > MyProfit[1] THENCurrentTrade = 1ELSIF MyProfit < MyProfit[1] THENCurrentTrade = -1ENDIFTotalWin = TotalWin + (CurrentTrade = 1)TotalLose = TotalLose + (CurrentTrade = -1)ENDIFONCE SkipTrades = 0ONCE SkipFlag = 0IF (CurrentTrade = -1) AND (SkipFlag = 0) AND (Not Onmarket) THENSkipTrades = NumberOfTradesSkippedSkipFlag = 1ENDIF// Condition to enter a long position:IF NOT LongOnMarket AND BuySignal THENIF SkipTrades > 0 THENSkipTrades = SkipTrades - 1ELSEBUY 1 CONTRACTS AT MARKETSkipFlag = 0ENDIFENDIFCan 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
03/22/2024 at 4:16 PM #230291There you go:
1234567891011121314151617ONCE NumberOfTradesSkipped = 1 // (Default value, Optimized)MyProfit = StrategyProfitIF MyProfit < MyProfit[1] AND (BarIndex > 0) THENSkipTrades = NumberOfTradesSkippedENDIFONCE SkipTrades = 0// Condition to enter a long position:IF NOT LongOnMarket AND close crosses over highest[20](close[1]) THENIF SkipTrades > 0 THENSkipTrades = SkipTrades - 1ELSEBUY 1 CONTRACTS AT MARKETENDIFENDIF//set stop ploss 200set target pprofit 2001 user thanked author for this post.
03/23/2024 at 12:39 PM #230311Thanks, Roberto
That’s a significant change! Back to the drawing board for me.
Regards
Brad
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on