Special trailing stop – hope it is possible!
Forums › ProRealTime English forum › ProOrder support › Special trailing stop – hope it is possible!
- This topic has 13 replies, 2 voices, and was last updated 5 years ago by
robertogozzi.
-
-
02/06/2020 at 6:52 PM #118868
Hello,
I am trying to create some sort of a trailing stop in which once I set the parameters for the initial stop loss (ATR*3) and everytime the price of the asset increases the equivalent of that stop loss I would risk a percentage of the profits (trailing the stop). Percentage that would be changing everytime the price increases ‘N’ times the initial value of the initial stop loss;
i.e. when the price of the asset bought has increased 1 time the ATR*3 (initial amount of the stop loss) I would risk the 100% of the profit, so the stop should be trailed to a break even position. Next if the price increases 2 times the ATR*3 (initial amount of stop loss), I would risk the 80% of the profit, meaning that I should trail the stop, but just enough to lock in a 20% profit and let the market take the other 80%, and so on…
So far I have the following and I am out of ideas to code it and wondering if is even possible to do it:
Initial stop1234IF not longonmarket thenBUY 1 CONTRACTS AT MARKETSET STOP LOSS averagetruerange[10](close[0])*3EndifWould appreciate any help!
02/07/2020 at 9:51 PM #118977Well, so far I have been trying out this code (I think I am close to do it), but still not sure why it is not working yet:
Update123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194Defparam cumulateorders = falseTimeframe (1 week)c1 = close[1]c2 = close[2]c3 = close[3]a1= open[1]a2= open[2]a3= open[3]MyADX18 = ADX[24]ADXperiods = 2MySMA24=average[24](c1)MySMA200= average[200](c1)Efficiency = call "Efficiency"Condition1 = c2>c3 and c1>c2Condition2= a2>a3 and a1>a2Condition3 = LOWEST[ADXperiods + 1](MyADX18) > 20Condition4 = c1 > MySMA24Condition5 = c1 > MySMA200Condition6 = Efficiency>0.7Timeframe (default)Mystop = (averagetruerange[10](close[0]))*3IF not longonmarket and Condition1 and Condition2 and Condition3 and Condition4 and Condition5 and Condition6 THENBUY 1 CONTRACTS AT MARKETSET STOP LOSS MystopEndifIF LONGONMARKET AND (Not Condition4 and not Condition5) or (Not Condition3 and Not Condition4) or (Not Condition3 and Not Condition5) THENSELL 1 CONTRACTS AT MARKETENDIF//StopsIF NOT ONMARKET THENnewSL=0ENDIFClose0 = Tradeprice(1)Trailingstart = Close[0] + MystopR3=((close0+(mystop*3)-Close0))*0.8R4=((close0+(mystop*4)-Close0))*0.75R5=((close0+(mystop*5)-Close0))*0.45R6=((close0+(mystop*6)-Close0))*0.45R7=((close0+(mystop*7)-Close0))*0.4R8=((close0+(mystop*8)-Close0))*0.3R9=((close0+(mystop*9)-Close0))*0.25R10=((close0+(mystop*10)-Close0))*0.25R11=((close0+(mystop*11)-Close0))*0.25R12=((close0+(mystop*12)-Close0))*0.12R13=((close0+(mystop*13)-Close0))*0.10R14=((close0+(mystop*14)-Close0))*0.10R15=((close0+(mystop*15)-Close0))*0.09R16=((close0+(mystop*16)-Close0))*0.09R17=((close0+(mystop*17)-Close0))*0.09R18=((close0+(mystop*18)-Close0))*0.08R19=((close0+(mystop*19)-Close0))*0.08R20=((close0+(mystop*20)-Close0))*0.08R21=((close0+(mystop*21)-Close0))*0.08R22=((close0+(mystop*22)-Close0))*0.05IF newSL>0 THENSELL AT newSL STOPENDIFIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-Close0>=trailingstart and Close<(close0+(Mystop*2)) THENnewSL = (close0+mystop)ENDIF//next moves (trailing stop)IF newSL>0 AND close-newSL>=R3 AND close-newSL<R4 THENnewSL = newSL+R3ENDIFIF newSL>newSL+R3 AND close-newSL>=R4 AND close-newSL<R5 THENnewSL = newSL+R4ENDIFIF newSL>newSL+R4 AND close-newSL>=R5 AND close-newSL<R6 THENnewSL = newSL+R5ENDIFIF newSL>newSL+R5 AND close-newSL>=R6 AND close-newSL<R7 THENnewSL = newSL+R6ENDIFIF newSL>newSL+R6 AND close-newSL>=R7 AND close-newSL<R8 THENnewSL = newSL+R7ENDIFIF newSL>newSL+R7 AND close-newSL>=R8 AND close-newSL<R9 THENnewSL = newSL+R8ENDIFIF newSL>newSL+R8 AND close-newSL>=R9 AND close-newSL<R10 THENnewSL = newSL+R9ENDIFIF newSL>newSL+R9 AND close-newSL>=R10 AND close-newSL<R11 THENnewSL = newSL+R10ENDIFIF newSL>newSL+R10 AND close-newSL>=R11 AND close-newSL<R12 THENnewSL = newSL+R11ENDIFIF newSL>newSL+R11 AND close-newSL>=R12 AND close-newSL<R13 THENnewSL = newSL+R12ENDIFIF newSL>newSL+R12 AND close-newSL>=R13 AND close-newSL<R14 THENnewSL = newSL+R13ENDIFIF newSL>newSL+R13 AND close-newSL>=R14 AND close-newSL<R15 THENnewSL = newSL+R14ENDIFIF newSL>newSL+R14 AND close-newSL>=R15 AND close-newSL<R16 THENnewSL = newSL+R15ENDIFIF newSL>newSL+R15 AND close-newSL>=R16 AND close-newSL<R17 THENnewSL = newSL+R16ENDIFIF newSL>newSL+R16 AND close-newSL>=R17 AND close-newSL<R18 THENnewSL = newSL+R17ENDIFIF newSL>newSL+R17 AND close-newSL>=R18 AND close-newSL<R19 THENnewSL = newSL+R18ENDIFIF newSL>newSL+R18 AND close-newSL>=R19 AND close-newSL<R20 THENnewSL = newSL+R19ENDIFIF newSL>newSL+R19 AND close-newSL>=R20 AND close-newSL<R21 THENnewSL = newSL+R20ENDIFIF newSL>newSL+R20 AND close-newSL>=R21 AND close-newSL<R22 THENnewSL = newSL+R21ENDIFIF newSL>newSL+R21 AND close-newSL>=R22 THENnewSL = newSL+R22ENDIFENDIFWould appreciate some help!
02/08/2020 at 1:03 AM #118986Try this:
1234567891011121314151617181920212223242526272829303132333435IF Not OnMarket THENMyProfit = 0PrevProfit = 0ENDIFMyLongConditions = close CROSSES OVER average[200,0](close)MyShortConditions = close CROSSES UNDER average[200,0](close)IF MyLongConditions AND Not LongOnMarket thenBUY 1 CONTRACTS AT MARKETMySL = averagetruerange[10](close[0])*3SET STOP LOSS MySLEndifIF MyShortConditions AND Not ShortOnMarket thenSELLSHORT 1 CONTRACTS AT MARKETMySL = averagetruerange[10](close[0])*3SET STOP LOSS MySLEndifIF LongOnMarket THENxProfit = close - TradePriceIF xProfit >= (MyProfit + MySL) THENPrevProfit = MyProfitMyProfit = MyProfit + MySLENDIFIF MyProfit > 0 THENSELL AT TradePrice + PrevProfit STOPENDIFELSIF ShortOnMarket THENxProfit = TradePrice - closeIF xProfit >= (MyProfit + MySL) THENPrevProfit = MyProfitMyProfit = MyProfit + MySLENDIFIF MyProfit > 0 THENSELL AT TradePrice - PrevProfit STOPENDIFENDIF02/08/2020 at 1:21 AM #118991Thank you Roberto! I will try it and let you know.
02/08/2020 at 4:17 PM #119032Hi Roberto, I tried the code you suggested, with some changes for adapting it to what I need, however it stills not doing exactly wat I need. It does trail the first stop (thank you for that! I couldn’t even that work before), however the next trailings are not done.
I need not only the stop to trail the initial ATR*3 when the profits increase, but to trail in different proportions.
Basically, ATR*3 = 1R, so if profit>= 1R then the stop should trail to Tradeprice; if profits >= 2R then the stop should trail to Tradeprice+2R*80%; if profits >= 3R then the stop should trail to Tradeprice+2R*75%, and so on…
I tried for that pourpose these codes and neither of them work:
Code 1123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282// Condiciones para entrada de posiciones largasDefparam cumulateorders = falseTimeframe (1 week)c1 = close[1]c2 = close[2]c3 = close[3]a1= open[1]a2= open[2]a3= open[3]MyADX18 = ADX[18]ADXperiods = 2MySMA24=average[24](c1)MySMA200= average[200](c1)Efficiency = call "Efficiency"Condition1 = c2>c3 and c1>c2Condition2= a2>a3 and a1>a2Condition3 = LOWEST[ADXperiods + 1](MyADX18) > 20Condition4 = c1 > MySMA24Condition5 = c1 > MySMA200Condition6 = Efficiency>0.7Timeframe (default)IF Not OnMarket THENMyProfit = 0PrevProfit = 0ENDIFMyLongConditions = Condition1 and Condition2 and Condition3 and Condition4 and Condition5 and Condition6IF MyLongConditions and not longonmarket THENBUY 1 CONTRACTS AT MARKETMystop = (averagetruerange[10](close[0]))*3SET STOP LOSS MystopEndifIF LONGONMARKET AND (Not Condition4 and not Condition5) or (Not Condition3 and Not Condition4) or (Not Condition3 and Not Condition5) THENSELL 1 CONTRACTS AT MARKETENDIF//StopsClose0 = TradepriceR3=0.8R4=0.75R5=0.45R6=0.45R7=0.4R8=0.3R9=0.25R10=0.25R11=0.25R12=0.12R13=0.10R14=0.10R15=0.09R16=0.09R17=0.09R18=0.08R19=0.08R20=0.08R21=0.08R22=0.05IF LongOnMarket THENxProfit = close - Close0IF xProfit >= (MyProfit + Mystop) AND xProfit<(MyProfit + (Mystop*2)) THENPrevProfit = MyProfitMyProfit = MyProfit + MystopENDIFIF MyProfit > 0 and Myprofit < Mystop*2 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*2 AND xProfit<Mystop*3 THENPrevProfit = MyProfitMyProfit = xProfit*R3ENDIFIF MyProfit => Mystop and Myprofit < Mystop*3 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*3 AND xProfit<Mystop*4 THENPrevProfit = MyProfitMyProfit = xProfit*R4ENDIFIF MyProfit => Mystop*2 and Myprofit < Mystop*4 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*4 AND xProfit<Mystop*5 THENPrevProfit = MyProfitMyProfit = xProfit*R5ENDIFIF MyProfit => Mystop*3 and Myprofit < Mystop*5 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*5 AND xProfit<Mystop*6 THENPrevProfit = MyProfitMyProfit = xProfit*R6ENDIFIF MyProfit => Mystop*4 and Myprofit < Mystop*6 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*6 AND xProfit<Mystop*7 THENPrevProfit = MyProfitMyProfit = xProfit*R7ENDIFIF MyProfit => Mystop*5 and Myprofit < Mystop*7 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*7 AND xProfit<Mystop*8 THENPrevProfit = MyProfitMyProfit = xProfit*R8ENDIFIF MyProfit => Mystop*6 and Myprofit < Mystop*8 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*8 AND xProfit<Mystop*9 THENPrevProfit = MyProfitMyProfit = xProfit*R9ENDIFIF MyProfit => Mystop*7 and Myprofit < Mystop*9 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*9 AND xProfit<Mystop*10 THENPrevProfit = MyProfitMyProfit = xProfit*R10ENDIFIF MyProfit => Mystop*8 and Myprofit < Mystop*10 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*10 AND xProfit<Mystop*11 THENPrevProfit = MyProfitMyProfit = xProfit*R11ENDIFIF MyProfit => Mystop*9 and Myprofit < Mystop*11 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*11 AND xProfit<Mystop*12 THENPrevProfit = MyProfitMyProfit = xProfit*R12ENDIFIF MyProfit => Mystop*10 and Myprofit < Mystop*12 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*12 AND xProfit<Mystop*13 THENPrevProfit = MyProfitMyProfit = xProfit*R13ENDIFIF MyProfit => Mystop*11 and Myprofit < Mystop*13 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*13 AND xProfit<Mystop*14 THENPrevProfit = MyProfitMyProfit = xProfit*R14ENDIFIF MyProfit => Mystop*12 and Myprofit < Mystop*14 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*14 AND xProfit<Mystop*15 THENPrevProfit = MyProfitMyProfit = xProfit*R15ENDIFIF MyProfit => Mystop*13 and Myprofit < Mystop*15 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*15 AND xProfit<Mystop*16 THENPrevProfit = MyProfitMyProfit = xProfit*R16ENDIFIF MyProfit => Mystop*14 and Myprofit < Mystop*16 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*16 AND xProfit<Mystop*17 THENPrevProfit = MyProfitMyProfit = xProfit*R17ENDIFIF MyProfit => Mystop*15 and Myprofit < Mystop*17 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*17 AND xProfit<Mystop*18 THENPrevProfit = MyProfitMyProfit = xProfit*R18ENDIFIF MyProfit => Mystop*16 and Myprofit < Mystop*18 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*18 AND xProfit<Mystop*19 THENPrevProfit = MyProfitMyProfit = xProfit*R19ENDIFIF MyProfit => Mystop*17 and Myprofit < Mystop*19 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*19 AND xProfit<Mystop*20 THENPrevProfit = MyProfitMyProfit = xProfit*R20ENDIFIF MyProfit => Mystop*18 and Myprofit < Mystop*20 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*20 AND xProfit<Mystop*21 THENPrevProfit = MyProfitMyProfit = xProfit*R21ENDIFIF MyProfit => Mystop*19 and Myprofit < Mystop*21 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*21 AND xProfit<Mystop*22 THENPrevProfit = MyProfitMyProfit = xProfit*R22ENDIFIF MyProfit => Mystop*20 and Myprofit < Mystop*22 THENSELL AT Close0 + PrevProfit STOPENDIFIF xProfit >= Mystop*22 THENPrevProfit = MyProfitMyProfit = xProfit*R22ENDIFIF MyProfit => Mystop*21 THENSELL AT Close0 + PrevProfit STOPENDIFEndifAnd this one:
Code 2123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172Defparam cumulateorders = falseTimeframe (1 week)c1 = close[1]c2 = close[2]c3 = close[3]a1= open[1]a2= open[2]a3= open[3]MyADX18 = ADX[18]ADXperiods = 2MySMA24=average[24](c1)MySMA200= average[200](c1)Efficiency = call "Efficiency"Condition1 = c2>c3 and c1>c2Condition2= a2>a3 and a1>a2Condition3 = LOWEST[ADXperiods + 1](MyADX18) > 20Condition4 = c1 > MySMA24Condition5 = c1 > MySMA200Condition6 = Efficiency>0.7Timeframe (default)IF Not OnMarket THENMyProfit = 0PrevProfit = 0ENDIFMyLongConditions = Condition1 and Condition2 and Condition3 and Condition4 and Condition5 and Condition6IF MyLongConditions and not longonmarket THENBUY 1 CONTRACTS AT MARKETMystop = (averagetruerange[10](close[0]))*3SET STOP LOSS MystopEndifIF LONGONMARKET AND (Not Condition4 and not Condition5) or (Not Condition3 and Not Condition4) or (Not Condition3 and Not Condition5) THENSELL 1 CONTRACTS AT MARKETENDIF//StopsR3=0.8R4=0.75R5=0.45R6=0.45R7=0.4R8=0.3R9=0.25R10=0.25R11=0.25R12=0.12R13=0.10R14=0.10R15=0.09R16=0.09R17=0.09R18=0.08R19=0.08R20=0.08R21=0.08R22 = 0.05IF LongOnMarket THENxProfit = close - TradePriceIF LongOnMarket THENxProfit = close - TradePriceIF xProfit >= (MyProfit + Mystop) THENPrevProfit = MyProfitMyProfit = MyProfit + MystopENDIFIF MyProfit > 0 AND MyProfit<Mystop*2 THENSELL AT TradePrice + PrevProfit STOPELSIF MyProfit >= Mystop*2 AND MyProfit<Mystop*3 THENSELL AT TradePrice + PrevProfit*R3 STOPELSIF MyProfit >= Mystop*3 AND MyProfit<Mystop*4 THENSELL AT TradePrice + PrevProfit*R4 STOPELSIF MyProfit >= Mystop*4 AND MyProfit<Mystop*5 THENSELL AT TradePrice + PrevProfit*R5 STOPELSIF MyProfit >= Mystop*5 AND MyProfit<Mystop*6 THENSELL AT TradePrice + PrevProfit*R6 STOPELSIF MyProfit >= Mystop*6 AND MyProfit<Mystop*7 THENSELL AT TradePrice + PrevProfit*R7 STOPELSIF MyProfit >= Mystop*7 AND MyProfit<Mystop*8 THENSELL AT TradePrice + PrevProfit*R8 STOPELSIF MyProfit >= Mystop*8 AND MyProfit<Mystop*9 THENSELL AT TradePrice + PrevProfit*R9 STOPELSIF MyProfit >= Mystop*9 AND MyProfit<Mystop*10 THENSELL AT TradePrice + PrevProfit*R10 STOPELSIF MyProfit >= Mystop*10 AND MyProfit<Mystop*11 THENSELL AT TradePrice + PrevProfit*R11 STOPELSIF MyProfit >= Mystop*11 AND MyProfit<Mystop*12 THENSELL AT TradePrice + PrevProfit*R12 STOPELSIF MyProfit >= Mystop*12 AND MyProfit<Mystop*13 THENSELL AT TradePrice + PrevProfit*R13 STOPELSIF MyProfit >= Mystop*13 AND MyProfit<Mystop*14 THENSELL AT TradePrice + PrevProfit*R14 STOPELSIF MyProfit >= Mystop*14 AND MyProfit<Mystop*15 THENSELL AT TradePrice + PrevProfit*R15 STOPELSIF MyProfit >= Mystop*15 AND MyProfit<Mystop*16 THENSELL AT TradePrice + PrevProfit*R16 STOPELSIF MyProfit >= Mystop*16 AND MyProfit<Mystop*17 THENSELL AT TradePrice + PrevProfit*R17 STOPELSIF MyProfit >= Mystop*17 AND MyProfit<Mystop*18 THENSELL AT TradePrice + PrevProfit*R18 STOPELSIF MyProfit >= Mystop*18 AND MyProfit<Mystop*19 THENSELL AT TradePrice + PrevProfit*R19 STOPELSIF MyProfit >= Mystop*19 AND MyProfit<Mystop*20 THENSELL AT TradePrice + PrevProfit*R20 STOPELSIF MyProfit >= Mystop*20 AND MyProfit<Mystop*21 THENSELL AT TradePrice + PrevProfit*R21 STOPELSIF MyProfit >= Mystop*21 THENSELL AT TradePrice + PrevProfit*R22 STOPENDIFENDIFENDIFDo you have any inputs on how could I make the code work for what I atempt to do?
Thank you!
02/08/2020 at 5:01 PM #119034You don’t need all those lines!
My example after moving to breakeven trails SL so that it increases each time leaving your profit at risk equal to your initial SL.
So when your profit reaches your SL, it is set to breakeven, then when it increases of another SL (your profit is SL*2) your stop loss is set to SL*1 snd so on… so that your stop loss is always set to initial SL*(n-1) where n is the number of multiple times of SL that your profit grew.
Use GRAPH to monitor your profits and your stop loss.
02/08/2020 at 5:55 PM #119036Right. But the SL is not suposed to trail equally. It should vary according to the increase of profit. Is there a posibility to do that?
02/08/2020 at 8:45 PM #119048You can do (almost) anything, just tell us what you want to achieve.
02/08/2020 at 9:14 PM #119054Thank you Roberto.
I will try to explain what I am aming to achieve:
I need to trail a stop which starts (as yo know) at ATR*3. You actually posted a code that helps me trail that stop every time the price goes up the equivalent of the ATR*3. However, that is nos exactly what I need.
What I need is to trail the stop based on a pre-fixed percentage of the profits, everytime the price goes up a ATR*3 of the entry (I would call the ATR*3 of the entry “R”).
Exactly would be like this:
- Price increases 1R, then I would risk a 100% of the profits (1R). Therefore, the stop should trail to a breakeven position (“Tradeprice”).
- Price increases 2R, then I would risk an 80% of the profits, meaning that the stop should trail to a price = Tradeprice + 20% of the profits (20%*2R). Locking in 20% of the profits and letting the market take the other 80%.
- Price increases 3R, then I would risk a 75% of the profits, meaning that the stop should trail to a price = Tradeprice + 25% of the profits (25%*3R). Locking in 25% of the profits and letting the market take the other 75%.
- Price increases 4R, then I would risk a 45% of the profits, meaning that the stop should trail to a price = Tradeprice + 55% of the profits (55%*4R). Locking in 55% of the profits and letting the market take the other 45%.
- Price increases 5R, then I would risk a 45% of the profits, meaning that the stop should trail to a price = Tradeprice + 55% of the profits (55%*5R). Locking in 55% of the profits and letting the market take the other 45%.
- Price increases 6R, then I would risk a 40% of the profits, meaning that the stop should trail to a price = Tradeprice + 60% of the profits (60%*6R). Locking in 60% of the profits and letting the market take the other 40%.
- Price increases 7R, then I would risk a 30% of the profits, meaning that the stop should trail to a price = Tradeprice + 70% of the profits (70%*7R). Locking in 70% of the profits and letting the market take the other 30%.
That is namely what I need.
I have been trying to code it for several days, but I have been strugling to do it. Would be amazing if you could help me!
Thank you.
02/09/2020 at 12:09 AM #119070Ok, now I understand why you need many IF’s.
I won’t be able to take a look at it until Monday.
02/09/2020 at 12:14 AM #119071Got it. I’ll be attentive to your thoughts.
02/10/2020 at 7:04 PM #119227Hi Roberto! Were you able to have a look at it? I am still trying to figure out how… I have been working in the code all weekend and today and still nothing 🙁
02/10/2020 at 8:42 PM #119239Well. Think I did it!
Just for anyone that may be looking for something like it:
Final123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171// Condiciones para entrada de posiciones largasDefparam cumulateorders = falseTimeframe (1 week)c1 = close[1]c2 = close[2]c3 = close[3]a1= open[1]a2= open[2]a3= open[3]MyADX18 = ADX[18]ADXperiods = 2MySMA24=average[24](c1)MySMA200= average[200](c1)Efficiency = call "Efficiency"Condition1 = c2>c3 and c1>c2Condition2= a2>a3 and a1>a2Condition3 = LOWEST[ADXperiods + 1](MyADX18) > 20Condition4 = c1 > MySMA24Condition5 = c1 > MySMA200Condition6 = Efficiency>0.7Timeframe (default)MyLongConditions = Condition1 and Condition2 and Condition3 and Condition4 and Condition5 and Condition6IF MyLongConditions and not longonmarket THENBUY 1 CONTRACTS AT MARKETMystop = averagetruerange[10](Close)*3SET STOP LOSS MystopEndifIF LONGONMARKET AND (Not Condition4 and not Condition5) or (Not Condition3 and Not Condition4) or (Not Condition3 and Not Condition5) THENSELL 1 CONTRACTS AT MARKETENDIF//StopsClose0 = TradepriceR3=0.2R4=0.25R5=0.55R6=0.55R7=0.6R8=0.7R9=0.75R10=0.75R11=0.75R12=0.88R13=0.9R14=0.9R15=0.91R16=0.91R17=0.91R18=0.92R19=0.92R20=0.92R21=0.92R22=0.95IF LongOnMarket THENXprofit = HIGHEST[barindex-tradeindex](Close) - close0If xProfit >= Mystop AND xProfit < Mystop*2 thenSell at Close0 StopELSIf xProfit >= Mystop*2 AND xProfit < Mystop*3 thenSell at Close0+(xProfit*R3) StopELSIf xProfit >= Mystop*3 AND xProfit < Mystop*4 thenSell at Close0+(xProfit*R4) StopELSIf xProfit >= Mystop*4 AND xProfit < Mystop*5 thenSell at Close0+(xProfit*R5) StopELSIf xProfit >= Mystop*5 AND xProfit < Mystop*6 thenSell at Close0+(xProfit*R6) StopELSIf xProfit >= Mystop*6 AND xProfit < Mystop*7 thenSell at Close0+(xProfit*R7) StopELSIf xProfit >= Mystop*7 AND xProfit < Mystop*8 thenSell at Close0+(xProfit*R8) StopELSIf xProfit >= Mystop*8 AND xProfit < Mystop*9 thenSell at Close0+(xProfit*R9) StopELSIf xProfit >= Mystop*9 AND xProfit < Mystop*10 thenSell at Close0+(xProfit*R10) StopELSIf xProfit >= Mystop*10 AND xProfit < Mystop*11 thenSell at Close0+(xProfit*R11) StopELSIf xProfit >= Mystop*11 AND xProfit < Mystop*12 thenSell at Close0+(xProfit*R12) StopELSIf xProfit >= Mystop*12 AND xProfit < Mystop*13 thenSell at Close0+(xProfit*R13) StopELSIf xProfit >= Mystop*13 AND xProfit < Mystop*14 thenSell at Close0+(xProfit*R14) StopELSIf xProfit >= Mystop*14 AND xProfit < Mystop*15 thenSell at Close0+(xProfit*R15) StopELSIf xProfit >= Mystop*15 AND xProfit < Mystop*16 thenSell at Close0+(xProfit*R16) StopELSIf xProfit >= Mystop*16 AND xProfit < Mystop*17 thenSell at Close0+(xProfit*R17) StopELSIf xProfit >= Mystop*17 AND xProfit < Mystop*18 thenSell at Close0+(xProfit*R18) StopELSIf xProfit >= Mystop*18 AND xProfit < Mystop*19 thenSell at Close0+(xProfit*R19) StopELSIf xProfit >= Mystop*19 AND xProfit < Mystop*20 thenSell at Close0+(xProfit*R20) StopELSIf xProfit >= Mystop*20 AND xProfit < Mystop*21 thenSell at Close0+(xProfit*R21) StopELSIf xProfit >= Mystop*21 thenSell at Close0+(xProfit*R22) StopENDIFEndif02/10/2020 at 9:29 PM #119243Great achievement!
I had no time today to take a look at it.
-
AuthorPosts
Find exclusive trading pro-tools on