Simple Moving Average Crossover Strategy
Forums › ProRealTime English forum › ProOrder support › Simple Moving Average Crossover Strategy
- This topic has 95 replies, 12 voices, and was last updated 1 year ago by CRISRJ.
-
-
04/01/2022 at 3:40 PM #19099004/01/2022 at 4:14 PM #190991
Good idea – think I’ll wait to pay for the Nordic version of this code posted here too :)!
04/03/2022 at 11:46 AM #191099I have modified my previous version (v4) in order to reduce the DD. In this version (v5) the longterm average (480 weekly) has been replaced with two weighted averages and has been added a filter to avoid operations in a market with low volatility (image below : 100k)
v5123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149//TS MULTIAVERAGE SP500 v5 - CFD 1 euro - Spread 0.6defparam CUMULATEORDERS = falsepositionSize = 5//---------------------------------------------------------------------------------------timeFrame(15 minute, updateOnClose) //TF 15 minutesc1L = close > average[500,2](close)c1S = close < average[450,2](close)avg1 = average[15,0](close)avg2 = average[55,0](close)avg3 = average[14,1](close)avg4 = average[20,1](close)c2L = avg1 > avg2c2S = avg3 < avg4avgTrigger = average[5,0](close)c3L = avgTrigger crosses over avg1c3S = avgTrigger crosses under avg1//---------------------------------------------------------------------------------------timeFrame(default) //TF 5 minutes = DEFAULT//------------------------------------------------period = 110 //low volatility filter (MauroPro)minLevel = 10 *pointsizeamplitudeRange = highest[period](high)-lowest[period](low)cMinRange = amplitudeRange > minLevel//-------------------------------------------------cLong = c1L and c2L and c3LcShort = c1S and c2S and c3ScLongExit = c1ScShortExit = c1L//--------------------------------------------------------------------------------------ONCE buyTime = 110000ONCE sellTime = 213000ONCE buyTimeShort = 150000ONCE sellTimeShort = 213000//--------------------------------------------------------------------------------------if time >= buyTime and time <= sellTime then //LONG POSITION: TECHNICAL ENTRYif cLong and cMinRange thenbuy positionSize contract at marketendifendifif longOnMarket and cLongExit then //LONG POSITION: TECHNICAL EXITsell positionSize contract at marketendif//----------------------------if time >= buyTimeShort and time <= sellTimeShort then //SHORT POSITION: TECHNICAL ENTRYif cShort and cMinRange thensellshort positionSize contract at marketendifendifif shortOnMarket and cShortExit then //SHORT POSITION: TECHNICAL EXITexitShort positionSize contract at marketendif//----------------------------------------------------------------------------------------if longOnMarket then //SL & TP Exitset stop %loss 1.7elsif shortOnMarket thenset stop %loss 0.5endifif longOnMarket thenset target %profit 1elsif shortOnMarket thenset target %profit 1endif//------------------------------------------------------------------------------------if time = 223000 then //time Exit//sell at marketexitShort at marketendifif time = 225500 and dayOfWeek=5 thensell at marketexitShort at marketendif//--------------------------------------------------------------------------------------------DirectionSwitch = (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) //TrP Exit (GozziIF Not OnMarket OR DirectionSwitch THENTrailStart = 40 // Start trailing profitsPointToKeep = 0.2 // 20% Profit percentage to keep when setting BreakEvenStepSize = 5 // Point to increase PercentagePerCentInc = 0.2 // 20% PerCent increment after each StepSize ChunkRoundTO = -0.5 //-0.5 rounds always to Lower integer, 0 defaults PRT behaviourPriceDistance = 6* pipsize //minimun distance from current pricemaxProfitL = 0maxProfitS = 0ProfitPerCent = PointToKeep //reset to desired default valueSellPriceX = 0SellPrice = 0ExitPriceX = 9999999ExitPrice = 9999999ELSEIF PositionPrice <> PositionPrice[1] AND (ExitPrice + SellPrice) <> 9999999 THEN //go on only if Trailing Stop had already started trailingIF LongOnMarket THENnewSlL = PositionPrice + ((close - PositionPrice) * ProfitPerCent) //calculate new SLSellPriceX = max(max(SellPriceX,SellPrice),newSlL)SellPrice = max(max(SellPriceX,SellPrice),PositionPrice + (maxProfitL * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous oneELSIF ShortOnMarket THENnewSlS = PositionPrice - ((PositionPrice - close) * ProfitPerCent)ExitPriceX = min(min(ExitPriceX,ExitPrice),newSlS)ExitPrice = min(min(ExitPriceX,ExitPrice),PositionPrice - (maxProfitS * pipsize))ENDIFENDIFENDIF//---------------------------------------------------------------------------------------------------------------------------------------------------IF LongOnMarket AND close > (PositionPrice + (maxProfitL * pipsize)) THEN //LONG positions// compute the value of the Percentage of profits, if any, to lock in for LONG tradesprofitL = (close - PositionPrice) / pipsize //convert price to pipsIF profitL >= TrailStart THEN // go ahead only if N+ pipsDiff1 = abs(TrailStart - profitL) //difference from current profit and TrailStartChunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) //number of STEPSIZE chunksProfitPerCent = PointToKeep + (PointToKeep * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCentProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%maxProfitL = max(profitL * ProfitPerCent, maxProfitL)ENDIFELSIF ShortOnMarket AND close < (PositionPrice - (maxProfitS * pipsize)) THEN //SHORT positionsprofitS = (PositionPrice - close) / pipsizeIF profitS >= TrailStart THENDiff2 = abs(TrailStart - profitS)Chunks2 = max(0,round((Diff2 / StepSize) + RoundTO))ProfitPerCent = PointToKeep + (PointToKeep * (Chunks2 * PerCentInc))ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))maxProfitS = max(profitS * ProfitPerCent, maxProfitL)ENDIFENDIF//--------------------------------------------------------------------------------------------------------------------------------------------------------------IF maxProfitL THEN //LONG positions - Place pending STOP order when maxProftiL > 0 (LONG positions)SellPrice = max(SellPrice,PositionPrice + (maxProfitL * pipsize)) //convert pips to priceIF abs(close - SellPrice) > PriceDistance THENIF close >= SellPrice THENSELL AT SellPrice STOPELSESELL AT SellPrice LIMITENDIFELSESELL AT MarketENDIFENDIFIF maxProfitS THENExitPrice = min(ExitPrice,PositionPrice - (maxProfitS * pipsize)) //SHORT positionsIF abs(close - ExitPrice) > PriceDistance THENIF close <= ExitPrice THENEXITSHORT AT ExitPrice STOPELSEEXITSHORT AT ExitPrice LIMITENDIFELSEEXITSHORT AT MarketENDIFENDIF//------------------------------------------------------------------------------------------------------------------------2 users thanked author for this post.
04/03/2022 at 11:50 AM #191100100k backtest
3 users thanked author for this post.
04/03/2022 at 5:38 PM #191110Thank you. This is also a good option. I can see that this strategy structure seems to offer a lot of possibilities. The main filter is a bit slower, is that better? I’m not at the computer right now… could you please post the long/short split?
04/04/2022 at 9:16 AM #19113604/04/2022 at 9:56 AM #191140Hi Phoentzs, the test was done with 4 contracts and not 5 as the original (I had put four to better split the operations and I forgot to change the positionSize).
I also have noticed that the system remains short for a very little time.
04/04/2022 at 10:27 AM #191143I had the feeling with my tests too. I think it’s because a fast trailing is responsible in the short. And not to forget, every evening shorts will be closed. I found that the index recovered overnight very often and then an open short position is not good.
01/17/2023 at 9:01 AM #207535Hello,
Sorry I ‘m new, and I try to back test this bot NAS -5- MA Cross-V3, and after the 15th of February 2022 none position, the Bot stop until today. Maybe somebody have a answer or maybe somebody can help me. In advance thank you.
Regards
Chris
01/17/2023 at 8:36 PM #20758401/17/2023 at 9:57 PM #20758601/17/2023 at 10:14 PM #20758701/18/2023 at 2:25 PM #207622Dear GraHal,
In first thanks a lot for your quick reply.
To be honest I try all the systems but this bot is very good and he never took position (now more of one year) and I don’t understand why, I try to check the code and with my level I see nothing is the reason why I ask for the experts.
Regards
Cris
01/18/2023 at 4:44 PM #207635Have you tried another bot? If yes, which one? The bot’s filters are designed for two things. Set the trend once and then keep the bot out of trouble and sideways phases. Maybe it’s because? There haven’t been many longer trends this year. But I can’t imagine that there was no trade at all. Maybe trading time? The bots are set to German time. Could you please post the relevant code?
01/18/2023 at 5:03 PM #207640Here’s the code …
CRISRJ is correct … no trades at all after 16 Feb 22!
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212//================================================// Code: NAS 5m MACross v3// Version 3// Index: NASDAQ// TF: 5 min// Spread: 1// Date: 27/01/2022// Notes: added Tradetime// changed SL, TP and Trailstart to percentage// 3 MTF levels, 4h, 15m, 5m// added max no. bars for any trade (EZT)////================================================DEFPARAM CUMULATEORDERS = FALSEDEFPARAM preloadbars = 10000//MONEY MANAGEMENT IIMM = 0 // = 0 for optimizationif MM = 0 thenpositionsize = 0.5ENDIFif MM thenMinSize = 0.5 // IG minimum position size allowedMaxSize = 2000 // IG tier 2 margin limitProfitAccrued = 0 // when restarting strategy, enter profit or loss to date in instrument currencyDD = 486 //MinSize drawdown in instrument currencyMultiplier = 3 //drawdown multiplierCapital = DD * MultiplierEquity = Capital + ProfitAccrued + StrategyProfitPositionSize = Max(MinSize, Equity * (MinSize/Capital))if positionsize > MaxSize thenpositionsize = MaxSizeendifPositionSize = Round(PositionSize*100)PositionSize = PositionSize/100ENDIF//Tradetime//adjustment for American Daylight Savings timeADLS =1if ADLS thenDLS =(Date >= 20100314 and date <=20100328) or (Date >= 20101031 and date <=20101107) or (Date >= 20110313 and date <=20110327) or (Date >= 20111030 and date <=20111106) or (Date >= 20120311 and date <=20120325) or (Date >= 20121028 and date <=20121104) or (Date >= 20130310 and date <=20130331) or (Date >= 20131027 and date <=20131103) or (Date >= 20140309 and date <=20140330) or (Date >= 20141026 and date <=20141102) or (Date >= 20150308 and date <=20150329) or (Date >= 20151025 and date <=20151101) or (Date >= 20160313 and date <=20160327) or (Date >= 20161030 and date <=20161106) or (Date >= 20170312 and date <=20170326) or (Date >= 20171030 and date <=20171105) or (Date >= 20180311 and date <=20180325) or (Date >= 20181028 and date <=20181104) or (Date >= 20190310 and date <=20190331) or (Date >= 20191027 and date <=20191103) or (Date >= 20200308 and date <=20200329) or (Date >= 20201025 and date <=20201101) or (Date >= 20210314 and date <=20210328) or (Date >= 20211031 and date <=20211107) or (Date >= 20220313 and date <=20220327) or (Date >= 20221030 and date <=20221106) or (Date >= 20230312 and date <=20230326) or (Date >= 20231029 and date <=20231105) or (Date >= 20240310 and date <=20240331) or (Date >= 20241027 and date <=20241103)If DLS thenTradetime = time >=133000 and time <200000elsif not DLS thenTradetime = time >=143000 and time <210000endifendifif not ADLS thenTradetime = time >=143000 and time <210000endif//Long Entry FilterTimeframe(4 hours)FMA1 = average[p,t](typicalprice)FMA2 = average[p1,t](typicalprice)cb1 = FMA1 > FMA2//cs1 = FMA1 < FMA2Timeframe(15 minutes)cb2 = close>average[p2,t2](typicalprice)//cs2 = close<average[p2,t2](typicalprice)Timeframe(Default)//Long Entry CriteriaMA1=average[p3,t3](typicalprice)MA2=average[p4,t3](typicalprice)cb3 = MA1 crosses over MA2//cs3 = MA1 crosses under MA2// Conditions to enter long positionsIf Tradetime and cb1 and cb2 and cb3 ThenBuy PositionSize CONTRACTS AT MARKETENDIF// Conditions to exit long positions//if longonmarket and positionperf>0 and cs3 then//sell at market//endif// Conditions to enter short positions//If close <= Lowest[Sbars](OPEN) then//Buy PositionSize CONTRACTS AT MARKET//ENDIF// Stops and targets//slvalue=close[1]/153*1.6//graph slvalue////tpvalue=close[1]/88*1.6//graph tpvalueSET STOP %LOSS slSET TARGET %PROFIT tp// Break even and trailing stop// https://www.prorealcode.com/topic/breakeeven-trailing-profit/IF Not OnMarket THEN//// when NOT OnMarket reset values to default valuests = (tradeprice*pc)/100 // % trailing startTrailStart = ts //30 Start trailing profits from this pointBasePerCent = base // 0.200 20.0% Profit percentage to keep when setting BerakEvenStepSize = ss //10 Pip chunks to increase PercentagePerCentInc = pci // 0.100 10.0% PerCent increment after each StepSize chunkBarNumber = bn //10 Add further % so that trades don't keep running too longBarPerCent = bpc // 0.100 10% Add this additional percentage every BarNumber barsRoundTO = -0.5 //-0.5 rounds always to Lower integer, +0.4 rounds always to Higher integer, 0 defaults PRT behaviourPriceDistance = 5 * pipsize //IG minimun distance from current pricey1 = 0 //reset to 0y2 = 0 //reset to 0ProfitPerCent = BasePerCent //reset to desired default valueTradeBar = BarIndexELSIF LongOnMarket AND close > (TradePrice + (y1 * pipsize)) THEN//LONG positions//// compute the value of the Percentage of profits, if any, to lock in for LONG trades//x1 = (close - tradeprice) / pipsize //convert price to pipsIF x1 >= TrailStart THEN // go ahead only if N+ pipsDiff1 = abs(TrailStart - x1) //difference from current profit and TrailStartChunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) //number of STEPSIZE chunksProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCent// compute number of bars elapsed and add an additionl percentage// (this percentage is different from PerCentInc, since it's a direct percentage, not a Percentage of BasePerCent)// (if BasePerCent is 20% and this is 10%, the whole percentage will be 30%, not 22%)BarCount = BarIndex - TradeBarIF BarCount MOD BarNumber = 0 THENProfitPerCent = ProfitPerCent + BarPerCentENDIF//ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%y1 = max(x1 * ProfitPerCent, y1) //y1 = % of max profitENDIFELSIF ShortOnMarket AND close < (TradePrice - (y2 * pipsize)) THEN//SHORT positions//// compute the value of the Percentage of profits, if any, to lock in for SHORT trades//x2 = (tradeprice - close) / pipsize //convert price to pipsIF x2 >= TrailStart THEN // go ahead only if N+ pipsDiff2 = abs(TrailStart - x2) //difference from current profit and TrailStartChunks2 = max(0,round((Diff2 / StepSize) + RoundTO)) //number of STEPSIZE chunksProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc)) //compute new size of ProfitPerCent// compute number of bars elapsed and add an additionl percentage// (this percentage is different from PerCentInc, since it's a direct percentage, not a Percentage of BasePerCent)// (if BasePerCent is 20% and this is 10%, the whole percentage will be 30%, not 22%)BarCount = BarIndex - TradeBarIF BarCount MOD BarNumber = 0 THENProfitPerCent = ProfitPerCent + BarPerCentENDIF//ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%y2 = max(x2 * ProfitPerCent, y2) //y2 = % of max profitENDIFENDIFIF y1 THEN //Place pending STOP order when y1 > 0 (LONG positions)SellPrice = Tradeprice + (y1 * pipsize) //convert pips to price//// check the minimun distance between ExitPrice and current price//IF abs(close - SellPrice) > PriceDistance THEN//// place either a LIMIT or STOP pending order according to current price positioning//IF close >= SellPrice THENSELL AT SellPrice STOPELSESELL AT SellPrice LIMITENDIFELSE////sell AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price//SELL AT MarketENDIFENDIFIF y2 THEN //Place pending STOP order when y2 > 0 (SHORT positions)ExitPrice = Tradeprice - (y2 * pipsize) //convert pips to price//// check the minimun distance between ExitPrice and current price//IF abs(close - ExitPrice) > PriceDistance THEN//// place either a LIMIT or STOP pending order according to current price positioning//IF close <= ExitPrice THENEXITSHORT AT ExitPrice STOPELSEEXITSHORT AT ExitPrice LIMITENDIFELSE////ExitShort AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price//EXITSHORT AT MarketENDIFENDIF//EXIT ZOMBIE TRADEEZT = 1if EZT thenIF (longonmarket and barindex-tradeindex(1)>= b1 and positionperf>0) or (longonmarket and barindex-tradeindex(1)>= b2 and positionperf<0) thensell at marketendifIF (shortonmarket and barindex-tradeindex(1)>= 4000 and positionperf>0) or (shortonmarket and barindex-tradeindex(1)>= 4000 and positionperf<0) thenexitshort at marketendifendif -
AuthorPosts
Find exclusive trading pro-tools on