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.
-
-
03/26/2022 at 9:37 PM #190532
NAS-5m-MACross-v4.1-L1.itf contains 216 lines of code.
NAS 5m MACross v4.1 L contains 288 lines of code.
Maybe there is good reason?
1 user thanked author for this post.
03/26/2022 at 10:35 PM #190534Oh Yes There is a very good reason :’D , i did a mistake by replacing a lot of lines by what @nonetheless have gave me. Thanks for the notice.
Also i did remove the MM code.
If i correct that, the code will be : ( no spread, MM off, 2000 capital)NAS 5m MACross v4.1 L(2)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271// opt: 01/03/2022//=======================================================================DEFPARAM CUMULATEORDERS = FALSEDEFPARAM preloadbars = 10000//Money Managementpositionsize=1//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 >=h1-10000 and time <h2-10000elsif not DLS thenTradetime = time >=h1 and time <h2endifendifif not ADLS thenTradetime = time >=h1 and time <h2endif//Long Entry FilterTimeframe(4 hours)FMA1 = average[p,t](typicalprice)//FMA2 = average[p1,t](typicalprice)cb1 = FMA1 > FMA1[1]//cs1 = FMA1 < FMA2Timeframe(15 minutes)M15 = average[p2,t2](typicalprice)cb2 = (close>m15)//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//Stochastic RSI | indicatorlengthRSI = lr //RSI periodlengthStoch = ls //Stochastic periodsmoothK = sk //Smooth signal of stochastic RSIsmoothD = sd //Smooth signal of smoothed stochastic RSImyRSI = RSI[lengthRSI](close)MinRSI = lowest[lengthStoch](myrsi)MaxRSI = highest[lengthStoch](myrsi)StochRSI = (myRSI-MinRSI) / (MaxRSI-MinRSI)K = average[smoothK](stochrsi)*100D = average[smoothD](K)cb4 = K>D// Conditions to enter long positionsIf Tradetime and cb1 and cb2 and cb3 and cb4 ThenBuy PositionSize CONTRACTS AT MARKETENDIFSET STOP %LOSS slSET TARGET %PROFIT tp// Break even and trailing stop RTSIF 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 //-0.5 rounds always to Lower integer, +0.4 rounds always to Higher integer, 0 defaults PRT behaviourPriceDistance = 4 * 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//----------------------------------------------------------------------------RSIexit = 1 // in profitif RSIexit thenmyrsi2=rsi[r](close)if myrsi2<rl and barindex-tradeindex>1 and longonmarket and positionperf>0 thensell at marketendifif myrsi2>70 and barindex-tradeindex>1 and shortonmarket and positionperf>0 thenexitshort at marketendifendif//----------------------------------------------------------------------------DSD = 1if DSD thenonce openStrongLong = 0once openStrongShort = 0if (time <= 120000 or time >= 170000) then // 070000, 100000openStrongLong = 0openStrongShort = 0endif//detect strong direction for market openonce rangeOK = rok // 30once tradeMin = tm // 1000IF (time >= 120500) AND (time <= 120500 + tradeMin) AND ABS(close - open) > rangeOK THENIF close > open and close > open[1] THENopenStrongLong = 1openStrongShort = 0ENDIFIF close < open and close < open[1] THENopenStrongLong = 0openStrongShort = 1ENDIFENDIFonce bollperiod = bp // 20once bollMAType = 1once s = 2bollMA = average[bollperiod, bollMAType](close)STDDEV = STD[bollperiod]bollUP = bollMA + s * STDDEVbollDOWN = bollMA - s * STDDEVIF bollUP = bollDOWN THENbollPercent = 50ELSEbollPercent = 100 * (close - bollDOWN) / (bollUP - bollDOWN)ENDIFonce trendPeriod = trp //80once trendPeriodResume = tpr // 10once trendGap = 4once trendResumeGap = 4if not onmarket thenfullySupported = 0fullyResisted = 0endif//Market supported in the wrong directionIF shortonmarket AND fullySupported = 0 AND summation[trendPeriod](bollPercent > 50) >= trendPeriod - trendGap THENfullySupported = 1ENDIF//Market pull back but continue to be supportedIF shortonmarket AND fullySupported = 1 AND bollPercent[trendPeriodResume + 1] < 0 AND summation[trendPeriodResume](bollPercent > 50) >= trendPeriodResume - trendResumeGap THENexitshort at marketENDIF//Market resisted in wrong directionIF longonmarket AND fullyResisted = 0 AND summation[trendPeriod](bollPercent < 50) >= trendPeriod - trendGap THENfullyResisted = 1ENDIF//Market pull back but continue to be resistedIF longonmarket AND fullyResisted = 1 AND bollPercent[trendPeriodResume + 1] > 100 AND summation[trendPeriodResume](bollPercent < 50) >= trendPeriodResume - trendResumeGap THENsell at marketENDIF//Started real wrong directiononce strongTrend = st // 60once strongPeriod = sp // 4once strongTrendGap = stg // 2IF shortonmarket and openStrongLong and barindex - tradeindex < 12 and summation[strongPeriod](bollPercent > strongTrend) = strongPeriod - strongTrendGap thenexitshort at marketENDIFIF longonmarket and openStrongShort and barindex - tradeindex < 12 and summation[strongPeriod](bollPercent < 100 - strongTrend) = strongPeriod - strongTrendGap thensell at marketENDIFendif03/27/2022 at 2:30 PM #190581In my opinion, it´s sensless to do a backtest without spread, the results are falsified.
1 user thanked author for this post.
03/27/2022 at 2:37 PM #190582I agree with you, it’s just to help to find what’s wrong with my backtest wich is 55% less good than Nonetheless Backtest :’D
1 user thanked author for this post.
03/27/2022 at 2:38 PM #190583I though that, but surely the spread is in the code somehow / somewhere??
I just glanced over the code, but spread didn’t jump out at me.
1 user thanked author for this post.
03/27/2022 at 2:51 PM #190584Attached are the results with a constant exposure value of €10k, gives a better visual on the histogram and the curve.
I am comparing my backtest with the backtest of this “constant exposure” that i don’t know what it is. Thanks
03/27/2022 at 4:41 PM #190592Line 223 STDDEV = STD[bollperiod]
STDDEV = STD[bollperiod] (price)
Missing the (price) ?
1 user thanked author for this post.
03/27/2022 at 6:32 PM #190596comparing my backtest with the backtest of this “constant exposure”
this means positionsize = 10000/close
shows performance if you had risked a constant value (€10k) rather than a constant size per point
this would definitely explain the difference you are seeing!
1 user thanked author for this post.
03/27/2022 at 6:56 PM #190597Thank you very much @nonetheless, I understand now the difference !
03/28/2022 at 2:24 PM #190664What are your experiences with this type of optimization? I see a lot of small wins and a big SL. In the report, the average gains are also much smaller than the average losses. In my experience, with such values, the strategy usually collapses in sideways phases, so it is not robust. I optimize as much as possible in such a way that the average wins are always greater than the average losses. So you still have the chance to come out plus/minus 0 in sideways phases. Hit rate is then of course lower, between 40-60%.
how do you see it?
03/28/2022 at 3:38 PM #190670I have some systems with only 55% win rate, but very high risk/reward, others with over 90% wins but risk/reward around .3 or so. Then I have one with the best of both worlds … but very few trades, maybe one per month. There’s always going to be a trade-off between these factors and I think the difference is largely psychological. Either can turn a profit in the long term, it depends on what you’re comfortable with.
A broker I had years ago liked to say, “No one ever went bankrupt by booking small gains” … and I guess that message stuck as I tend towards high win rate, even if the wins are small. This often means a low breakeven level and high SL that others may find makes them anxious. Personally it doesn’t bother me to carry a trade the wrong way for 1.6 or 1.8% knowing that 90% of the time it will turn around and close for a profit.
Attached is the first few weeks OOS for v4.1L, positionsize = 1
(nicely demonstrates one good thing about long-only systems: what you lose on the way down you will probably get back on the way up)
1 user thanked author for this post.
03/28/2022 at 3:54 PM #190672Thanks for the information. I’ll just try it on one of my systems to achieve a high hit rate. Then I’ll try running it next to the original and compare.
03/30/2022 at 11:33 AM #190777Here is my humble contribution to an MA Cross system. I use SP500 here because most of my systems are based on it (many systems, little margin, little spread, portfolio building).
I solved it completely conventionally, with simple SMAs. The all-important trend seems to be the weekly trend. M15/SMA480 = D1/SMA5Here is the system, long/short combined. Maybe there is still room for improvement?
You always have a golden touch. Maybe you can take a look?
SP500 M5123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186//30.03.2022 200000k//US500 M5 Spread 0.6//German Timedefparam preloadbars = 10000defparam CUMULATEORDERS = falsepossize = 5timeframe(15minute, updateonclose)RangeMAD5 = average[480,0](close) //Wochentrend Daily MA5RangelongD5 = close > RangeMAD5RangeshortD5 = close < RangeMAD5RangeMAD20 = average[1920,0](close) //"Monatstrend" Daily MA20RangelongD20 = RangeMAD5 > RangeMAD20RangeshortD20 = RangeMAD5 < RangeMAD20MA1 = average[x1,0](close)MA2 = average[x2,0](close)//MA3 = average[x3,0](close)MA4 = average[x5,1](close)MA5 = average[x6,1](close)longA = MA1 > MA2 //and MA2 > MA3 //and MA2 > MA2[1]shortA = MA4 < MA5 //and MA5 < MA5[1]MAL1 = average[5,0](close)MAL2 = average[15,0](close) //15longB = MAL1 crosses over MAL2shortB = MAL1 crosses under MAL2timeframe(default)//M5long = RangelongD5 and longB and longA //and RangelongD20short = RangeshortD5 and shortB and shortA //and RangeshortD20Exit1 = RangeshortD5Exit2 = RangelongD5// trading windowONCE BuyTime = 110000ONCE SellTime = 213000ONCE BuyTime2 = 150000ONCE SellTime2 = 213000// position managementIF Time >= buyTime AND Time <= SellTime THENIf long then //not onmarket andBUY possize CONTRACT AT marketSET STOP %LOSS hlSET TARGET %PROFIT glEndIfendifIF Time >= buyTime2 AND Time <= SellTime2 THENIf short thensellshort possize CONTRACT AT marketSET STOP %LOSS hsSET TARGET %PROFIT gsEndIfendifIf longonmarket and Exit1 thensell at marketendifIf shortonmarket and Exit2 thenexitshort at marketendifif time = 223000 then //223000//sell at marketexitshort at marketendifif time = 225500 and dayofweek=5 then //225500sell at marketexitshort at marketendif////////////////////////////////////////// %trailing stop function incl. cumulative positionsonce trailingstoptype = 1if trailingstoptype then//====================trailingpercentlong = startl // %trailingpercentshort = start // %once acceleratorlong = stepl // typically tst*0.1once acceleratorshort= step // typically tss*0.1ts2sensitivity = 2 // [1] close [2] high/low [3] low/high [4] typicalprice//====================once steppercentlong = (trailingpercentlong/10)*acceleratorlongonce steppercentshort = (trailingpercentshort/10)*acceleratorshortif onmarket thentrailingstartlong = positionprice*(trailingpercentlong/100)trailingstartshort = positionprice*(trailingpercentshort/100)trailingsteplong = positionprice*(steppercentlong/100)trailingstepshort = positionprice*(steppercentshort/100)endifif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thennewsl = 0mypositionprice = 0endifpositioncount = abs(countofposition)if newsl > 0 thenif positioncount > positioncount[1] thenif longonmarket thennewsl = max(newsl,positionprice * newsl / mypositionprice)elsenewsl = min(newsl,positionprice * newsl / mypositionprice)endifendifendifif ts2sensitivity=1 thents2sensitivitylong=closets2sensitivityshort=closeelsif ts2sensitivity=2 thents2sensitivitylong=hights2sensitivityshort=lowelsif ts2sensitivity=3 thents2sensitivitylong=lowts2sensitivityshort=highelsif ts2sensitivity=4 thents2sensitivitylong=(typicalprice)ts2sensitivityshort=(typicalprice)endifif longonmarket thenif newsl=0 and ts2sensitivitylong-positionprice>=trailingstartlong thennewsl = positionprice+trailingsteplong + 0.2endifif newsl>0 and ts2sensitivitylong-newsl>=trailingsteplong thennewsl = newsl+trailingsteplongendifendifif shortonmarket thenif newsl=0 and positionprice-ts2sensitivityshort>=trailingstartshort thennewsl = positionprice-trailingstepshortendifif newsl>0 and newsl-ts2sensitivityshort>=trailingstepshort thennewsl = newsl-trailingstepshortendifendifif barindex-tradeindex>1 thenif longonmarket thenif newsl>0 thensell at newsl stopendifif newsl>0 thenif low crosses under newsl thensell at marketendifendifendifif shortonmarket thenif newsl>0 thenexitshort at newsl stopendifif newsl>0 thenif high crosses over newsl thenexitshort at marketendifendifendifendifmypositionprice = positionpriceendifif (shortonmarket and newsl > 0) or (longonmarket and newsl>0) thenif positioncount > positioncount[1] thenif longonmarket thennewsl = max(newsl,positionprice * newsl / mypositionprice)endifif shortonmarket thennewsl = min(newsl,positionprice * newsl / mypositionprice)endifendifendif//////////////////////////////////////////////////////////////03/30/2022 at 6:17 PM #190795This is the same version (rewritten)of Phoentzs [ T4 -Multi -SP500 -M5-V3] , with Roberto’trailing. The performance up to 200k is slighthly better, up to 100k is better (see image).
SP500 v4123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145//TS MULTI SP500 v4 - CFD 1 euro - Spread 0.6defparam CUMULATEORDERS = falsepositionSize = 5//---------------------------------------------------------------------------------------timeFrame(15 minute, updateOnClose) //15 minutesavgWeekly = average[480,0](close) //weekTrend Dailyc1L = close > avgWeeklyc1S = close < avgWeeklyavg1 = average[15,0](close)avg2 = average[55,0](close)avg3 = average[14,1](close)avg4 = average[20,1](close)c2L = avg1 > avg2 //trendc2S = avg3 < avg4avg5 = average[5,0](close)avg6 = average[15,0](close)c3L = avg5 crosses over avg6 //triggerc3S = avg5 crosses under avg6//---------------------------------------------------------------------------------------timeFrame(default) //5 minutes = DEFAULTcLong = 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 //LONGif cLong thenbuy positionSize contract at marketendifendifif longOnMarket and cLongExit thensell at marketendif//----------------------------if time >= buyTimeShort and time <= sellTimeShort then //SHORTif cShort thensellshort positionSize contract at marketendifendifif shortOnMarket and cShortExit thenexitShort at marketendif//----------------------------------------------------------------------------------------if longOnMarket then //SL & TPset 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) //TrPIF 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.6 //-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.
03/30/2022 at 7:28 PM #190798 -
AuthorPosts
Find exclusive trading pro-tools on