Multi time frame and trailing stop management
Forums › ProRealTime English forum › ProOrder support › Multi time frame and trailing stop management
- This topic has 62 replies, 5 voices, and was last updated 6 years ago by
robertogozzi.
-
-
05/29/2019 at 1:49 PM #99664
The conditions in line 53 will not be met again if price then drops below those conditions. You need a separate flag that you set to say they have been met and then only reset that flag when the position is closed. Without this the stop loss is moved but then price falls and the conditions for the stop loss are no longer true so it is not set again.
05/29/2019 at 2:02 PM #99666As I suggested in another topic on the italian forum, you’d better use another variable to keep track of TRADEPRICE, since this is a system constant, while you need a variable that, after PerCent2 is hit, changes its value to 50% of the gain, in order to start trailing normally fron that point.
I suggested using MyTradePrice throughout the strategy, but the very first time the trade has been entered to initialize it, in which case your could initialize it with something like this:
123IF OnMarket AND Not OnMarket[1] THEN //only when just entered a tradeMyTradePrice = TRADEPRICEENDIFWhen PerCent2 is hit then assign it the new value you want to start from, which will presumably be MyTradePrice + 50% profit.
Line 53 seems to be unnecessarily complicated!
05/29/2019 at 2:11 PM #99668I apologize but I did not understand your explanation. I do not have the sufficient capacity to understand what you have explained to me. My knowledge of the PRT code is very limited at the moment. What I can confirm is that I have analyzed dozens of operations and I noticed that the level of trailing stop, after being moved to 50% of the initial target, no longer follows the price trend. I tried to correctly apply the code created by Nicolas but otherwise there is still something I can’t understand. If you want to spend a few minutes helping me, I kindly ask you to write me the line of code to replace the one that doesn’t work now. I continue to try to find a solution. GRAZIE !!!05/29/2019 at 2:22 PM #99669Hi Roberto, I well remember your suggestion about the “Mytradeprice” but I was hoping that by applying Nicolas’s code there would no longer be any need to use this type of instruction. Instead it seems to me that it is the only way to tell the system to move the level of trailing. Where should I put the three lines you sent me? After line 16? Unfortunately I’m losing a little with all these instructions and flags. I put the flag to not repeat the order if I go below the minimum of the pattern, I put the flag to re-propose the order even the following days if it is not immediately executed the day after … There is a lot of information that now I struggle to manage. I will try again to follow your advice but I will surely still need your precious help. GRAZIE.05/29/2019 at 2:53 PM #99672Something like this perhaps (not tested):
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667//PARAMETRI//inserire qui sotto tutti i parametri BASE del codice// Definizione dei parametri del codiceDEFPARAM CumulateOrders = False // Posizioni cumulate disattivateMioRischio = 100 //importo massimo in euro che voglio rischiare per ogni singola operazionePercStop1 = 0.50 //attiva il pareggio al 50% del profittoPercStop2 = 0.80 //attiva lo SL a metà del profitto quanto questo ragguinge la seconda %PagoSpese = 0.0005 //numero di pips che aggiungo al breakeven per ripagarmi delle spese//DEFPARAM FlatAfter = 220000 //Range orario DOPO il quale non bisogna più essere in posizione (eventuale)//DEFPARAM FLATBEFORE = 090000 //Range orario PRIMA del quale non bisogna ancora essere in posizione (eventuale)//INSERIRE EVENTUALE PARAMETRO DAY OF THE WEEK PER ULTERIORE FILTRO DA TESTAREIF Not OnMarket THENBreakEven = 0ENDIFONCE Flag = 0IF OnMarket OR LOW < MinimoPat THENFlag=0 //Azzerare il prezzo serve a non inserire ancora l'ordine pendente...ENDIF //... una volta entrato oppure quando non ci sono più le condizioni//IF Not OnMarket THEN//BreakEven = 0//ENDIF//CONDIZIONI//inserire qui sotto tutte le condizioni che caratterizzano il pattern o la strategia da tradareAO=Average[5](MedianPrice)-Average[34](MedianPrice)Cond1 = AO[0]>0Cond2 = AO[1]<0CondTOT = Cond1 AND Cond2 //Condizione globale da tradareIF CondTOT THENFlag = 1ENDIF//COMPORTAMENTI//inserire qui sotto come si deve comportare la strategia quando è ONMARKETIF CondTOT AND Not LongOnMarket THENTargetProfit = (HIGH - LOW)/pipsize //VALORE in PIPS del mio TARGET INIZIALEPrezzo = HIGH //VALORE al quale si entra in posizione se vengono soddisfatte TUTTE le CONDIZIONIStopLoss = (HIGH - LOW)/pipsize //VALORE in PIPS dello STOP LOSS INIZIALEMinimoPat = LOWENDIF//Moltiplicatore = (MioRischio/(StopLoss/Prezzo)/pipsize) //QUANTITA' dello strumento da negoziare per avere in caso di perdita un valore//corrispondente a MIORISCHIO (nei parametri iniziali)IF not onmarket AND Flag THENBuy Moltiplicatore lots AT (Prezzo+0.0000) STOP //Ordine automatico di ENTRATA LONG in posizione con eventuale aggiunta di pipsENDIF//IF LongOnMarket AND ((close - Tradeprice) >= (TargetProfit * PercStop2)) THEN//IF LongOnMarket THEN//Breakeven = max(BreakEven,Tradeprice + (TargetProfit / 2)) //mettere al sicuro il 50% del profitto (è un BreakEven + metà profitto)//ENDIF//ENDIFIF LongOnMarket THENIF Breakeven = 0 AND ((High - Tradeprice) >= (TargetProfit * PercStop1)*pipsize) THENExitPrice = Tradeprice + PagoSpeseBreakeven = 1ENDIFIF Breakeven = 1 AND ((High - Tradeprice) >= (TargetProfit * PercStop2)*pipsize) AND High >= ExitPrice + ((TargetProfit * (PercStop2 - PercStop1))*pipsize) THENExitPrice = Tradeprice + (TargetProfit/2)Breakeven = 2ENDIFENDIFIF OnMarket AND Breakeven>0 THENSELL AT ExitPrice STOPENDIFSET STOP pLOSS StopLossgraphonprice exitpricegraph breakeven05/29/2019 at 3:04 PM #9967405/29/2019 at 3:22 PM #99675I wrote this:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667DEFPARAM CumulateOrders = FalseMioRischio = 100 //importo massimo euro che voglio rischiare per ogni singola operazionePercStop1 = 0.50 //attiva il pareggio al 50% del profittoPercStop2 = 0.80 //attiva lo SL a metà del profitto quanto questo ragguinge la seconda %PagoSpese = 5 * pipsize //numero pips che aggiungo a breakeven per ripagarmi delle speseMyStep = 5 * pipsize //5 pip step after PercSTop2 is hitIF Not OnMarket THENBreakEven = 0MyTradePrice = 0ENDIFIF OnMarket AND Not OnMarket[1] THENMyTradePrice = TRADEPRICEBreakEven = 0Flag = 0SET STOP pLOSS StopLossSET TARGET pPROFIT TargetProfitENDIFONCE Flag = 0IF OnMarket OR LOW < MinimoPat THEN //?????ENDIFAO=Average[5](MedianPrice)-Average[34](MedianPrice)Cond1 = AO[0]>0Cond2 = AO[1]<0CondTOT = Cond1 AND Cond2 //Condizione globale da tradareIF CondTOT THENFlag = 1ENDIFIF CondTOT AND Not LongOnMarket THENTargetProfit = (HIGH - LOW)/pipsize //VALORE in PIPS del mio TARGET INIZIALEPrezzo = HIGH //VALORE al quale si entra in posizione// se vengono soddisfatte TUTTE le CONDIZIONIStopLoss = (HIGH - LOW)/pipsize //VALORE in PIPS dello STOP LOSS INIZIALEMinimoPat = LOWProfitto1 = (TargetProfit * PercStop1)Profitto2 = (TargetProfit * PercStop2)ENDIFMoltiplicatore = (MioRischio/(StopLoss/Prezzo)/pipsize)IF not onmarket AND Flag THENMoltiplicatore = 1BUY Moltiplicatore lots AT (Prezzo+0*pipsize) STOPSET STOP pLOSS StopLossSET TARGET pPROFIT TargetProfitENDIF//------------------------------------------------------------------CurrentProfit = (close - MyTradePrice) / pipsizeIF CurrentProfit > Profitto2 AND MyTradePrice < (TRADEPRICE + Profitto2 * pipsize) THENMyTradePrice = TRADEPRICE + Profitto2 * pipsizeELSIF CurrentProfit > Profitto1 AND BreakEven = 0 THENBreakEven = MyTradePrice + PagoSpese * pipsizeENDIFIF BreakEven > 0 AND CurrentProfit > 0 THENIF close - MyTradePrice >= MyTradePrice + MyStep THENBreakEven = BreakEven + MyStepMyTradePrice = MyTradePrice + MyStepENDIFENDIFIF BreakEven AND OnMarket THENSELL AT BreakEven STOPENDIFgraphonprice tradeprice coloured(0,255,0,255) AS "Entry"graphonprice MyTradePrice coloured(255,0,0,255) AS "MyTradePrice"graphonprice BreakEven coloured(0,0,255,255) AS "Breakeven"graph CurrentProfit coloured(255,0,0,255) AS "CurProfit"graph Profitto1 coloured(0,255,0,255) AS "Profit1"graph Profitto2 coloured(0,0,255,255) AS "Profit2"graph StopLoss coloured(181,181,181,255) AS "SL"graph TargetProfit coloured(18,255,81,255) AS "TP"But your SL & TP are so tight that it is impossible to have a breakeven, also in LIVE trading your orders would be rejected because they do not abide to minimun distance requirements!
I also added a STEP value, but I never reached it.
You should change the place of your SL.
1 user thanked author for this post.
05/29/2019 at 3:24 PM #99676As I recommended before, use EXTENSIVELY both Graph and GraphOnPrice or your’ll end up making mistakes on top of previous mistakes. And try to find one solution at a time!
05/29/2019 at 4:43 PM #99678This works, but I had to add a minimun TP and SL (line 34) to be able to trail it and I set Moltiplicatore=1 just before BUY:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970DEFPARAM CumulateOrders = FalseMioRischio = 100 //importo massimo euro che voglio rischiare per ogni singola operazionePercStop1 = 0.50 //attiva il pareggio al 50% del profittoPercStop2 = 0.80 //attiva lo SL a metà del profitto quanto questo ragguinge la seconda %PagoSpese = 5 * pipsize //numero pips che aggiungo a breakeven per ripagarmi delle speseMyStep = 5 * pipsize //5 pip step after PercSTop2 is hitIF Not OnMarket THENBreakEven = 0MyTradePrice = 0ENDIFIF OnMarket AND Not OnMarket[1] THENMyTradePrice = TRADEPRICEBreakEven = 0Flag = 0SET STOP pLOSS StopLossSET TARGET pPROFIT TargetProfitENDIFONCE Flag = 0IF OnMarket OR LOW < MinimoPat THEN //?????ENDIFAO=Average[5](MedianPrice)-Average[34](MedianPrice)Cond1 = AO[0]>0Cond2 = AO[1]<0CondTOT = Cond1 AND Cond2 //Condizione globale da tradareIF CondTOT THENFlag = 1ENDIFIF CondTOT AND Not LongOnMarket THENTargetProfit = (HIGH - LOW)/pipsize //VALORE in PIPS del mio TARGET INIZIALEPrezzo = HIGH //VALORE al quale si entra in posizione// se vengono soddisfatte TUTTE le CONDIZIONIStopLoss = (HIGH - LOW)/pipsize //VALORE in PIPS dello STOP LOSS INIZIALEMinimoPat = LOWTargetProfit = max(80,TargetProfit)StopLoss = TargetProfit * 0.5Profitto1 = (TargetProfit * PercStop1)Profitto2 = (TargetProfit * PercStop2)ENDIFMoltiplicatore = (MioRischio/(StopLoss/Prezzo)/pipsize)IF not onmarket AND Flag THENMoltiplicatore = 1BUY Moltiplicatore lots AT (Prezzo+0*pipsize) STOPSET STOP pLOSS StopLossSET TARGET pPROFIT TargetProfitENDIF//------------------------------------------------------------------CurrentProfit = (close - MyTradePrice) / pipsizeIF CurrentProfit > Profitto2 AND MyTradePrice < (TRADEPRICE + Profitto2 * pipsize) THENMyTradePrice = TRADEPRICE + Profitto2 * pipsizeBreakEven = TRADEPRICE + Profitto1 * pipsizeELSIF CurrentProfit > Profitto1 AND BreakEven = 0 THENBreakEven = MyTradePrice + PagoSpeseENDIFIF BreakEven > 0 AND CurrentProfit > 0 AND MyTradePrice > (TRADEPRICE + PagoSpese) THENIF close - MyTradePrice >= MyStep THENBreakEven = BreakEven + MyStepMyTradePrice = MyTradePrice + MyStepENDIFENDIFIF BreakEven AND OnMarket THENSELL AT BreakEven STOPENDIFgraphonprice tradeprice coloured(0,255,0,255) AS "Entry"graphonprice MyTradePrice coloured(255,0,0,255) AS "MyTradePrice"graphonprice BreakEven coloured(0,0,255,255) AS "Breakeven"graph CurrentProfit coloured(255,0,0,255) AS "CurProfit"graph Profitto1 coloured(0,255,0,255) AS "Profit1"graph Profitto2 coloured(0,0,255,255) AS "Profit2"graph StopLoss coloured(181,181,181,255) AS "SL"graph TargetProfit coloured(18,255,81,255) AS "TP"After launching it the first time you’ll see a couple of flat lines, due to BREAKEVEN being 0, far from the current price, so you’ll have to enlarge the scale pretty much when your first trade is entered.
05/30/2019 at 1:59 PM #99715Hi everyone. I thank everyone for the kind answers you gave me. I am making some changes to my code to make it as similar as possible to what I will use in paper trading. Following Roberto’s suggestion that indicated that the value between breakeven and trailing was too small, I am changing some parameters. Initially the stop loss and take profit values were equal and corresponded to the range of the setup candle. If the range of this candle had been of a few pips, there was the risk of not being able to understand well if the code worked or if it didn’t work for a programming error or because there were too few pips of difference. Now I will take the ATR value as a take profit and stop loss value with a multiplier to be able to manage the amplitude of the ATR and optimize the results. I have also noticed that the suggestion proposed by Roberto is very similar to the MFE and the trailing management in this way I think is very convenient and interesting. I think the only problem left is the trailing stop and I wanted to ask you a little clarification. After bringing the initial stop loss to breakeven (currently in my system this works without any problem) when I start the trailing stop I find myself having to manage two values: the number of pips that triggers the trailing stop and the number of pips from the which the trailing stop must start. I am still confused about this problem. I give an example to ask you for clarification. Suppose we have an ATR of 100 pips (100 pips below the trade price there is a stop loss and 100 pips above the trade price is my initial target). When my profit reaches 50 pips (ie half of my initial target) I automatically move the stop loss from -100 to zero (ie to my entry price). When then I reach 80 pips of gain I would like to move the stop loss from zero to +50 pips and at this moment to start the trailing stop (checking with the candles at 1 minute the “high” value reached to move the trailing without big delay) . Now I kindly ask you: is the value you call “trailing start” +80 or +50? If the trailing start is +80 (ie the number of gain pips from which trailing starts) how should I call the value +50 to make it work in the code? Honestly, I still can’t understand it. Can you please give me a practical example? In the coming days I hope to be able to show and share with you the new version of my almost final code. GRAZIE !!05/30/2019 at 2:37 PM #99719Would you please format your text in a more suitable way? Thank you.
05/30/2019 at 2:55 PM #9972005/30/2019 at 3:20 PM #99721Sorry, I concentrated on explaining the problem and I didn’t give importance to readability. I try to format the text…
I thank everyone for the kind answers you gave me.
I am making some changes to my code to make it as similar as possible to what I will use in paper trading.
Following Roberto’s suggestion that indicated that the value between breakeven and trailing was too small, I am changing some parameters.
Initially the stop loss and take profit values were equal and corresponded to the range of the setup candle. If the range of this candle had been of a few pips, there was the risk of not being able to understand well if the code worked or if it didn’t work for a programming error or because there were too few pips of difference.
Now I will take the ATR value as a take profit and stop loss value with a multiplier to be able to manage the amplitude of the ATR and optimize the results.
I have also noticed that the suggestion proposed by Roberto is very similar to the MFE and the trailing management in this way I think is very convenient and interesting.
I think the only problem left is the trailing stop and I wanted to ask you a little clarification.
After bringing the initial stop loss to breakeven (currently in my system this works without any problem) when I start the trailing stop I find myself having to manage two values: the number of pips that triggers the trailing stop and the number of pips from the which the trailing stop must start.
I am still confused about this problem. I give an example to ask you for clarification.
Suppose we have an ATR of 100 pips (100 pips below the trade price there is a stop loss and 100 pips above the trade price is my initial target).
When my profit reaches 50 pips (ie half of my initial target) I automatically move the stop loss from -100 to zero (ie to my entry price). When then I reach 80 pips of gain I would like to move the stop loss from zero to +50 pips and at this moment to start the trailing stop (checking with the candles at 1 minute the “high” value reached to move the trailing without big delay) .
Now I kindly ask you: is the value you call “trailing start” +80 or +50?
If the trailing start is +80 (ie the number of gain pips from which trailing starts) how should I call the value +50 to make it work in the code? Honestly,
I still can’t understand it. Can you please give me a practical example? In the coming days I hope to be able to show and share with you the new version of my almost final code.
GRAZIE !!
05/30/2019 at 6:06 PM #99725In my code there is NO variable named TrailingStart, I simply use MyTradePrice to start from 80 as the basis to start trailing (at that point BreakEven = +50%). You can change tha value of MyStep to change the pace at which trailing stop works after hitting Percent2 (+80%).
I changed a bit your code to get rid of comments and extra lines that made it less readable.
If you add what you are missing or add new features, you’d better use my code, which is working fine (maybe removing the MAX() function and the line where MOLTIPLICATORE=1).
You may still want to use the MFE code or whatever else, in that case you just have to reinvent the (working) wheel.
I want to share a secret with you: “use EXTENSIVELY both Graph and GraphOnPrice!“. 🙂
1 user thanked author for this post.
05/31/2019 at 10:26 AM #99749Hi Roberto and thanks for the suggestions.
I’m using the Graph function more and more and actually it’s helping me a lot.
Your help is always very useful, but unfortunately at the moment I don’t have the necessary experience to understand it well and insert it correctly in my codes.
Sometimes I may appear to be insistent or boring, but believe me I just need only to get information that is not easy for me to understand now.
The last code you gave me is undoubtedly very useful and I’ll start right from that to make the last changes to my code.
As already mentioned above, with all the suggestions you gave me, everything is working well (entering the trade, repeating the order the day after, canceling the order if the conditions are no longer in place, etc.).
Only the management of the trailing stop is yet to be settled.
This weekend I will spend a lot of time correcting the code and adding your latest trailing tips.
Thank you again very much for all the support you are giving me.
I will surely keep you informed on the developments of my code1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on