ProRealCode - Trading & Coding with ProRealTime™
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.
As 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:
IF OnMarket AND Not OnMarket[1] THEN //only when just entered a trade
MyTradePrice = TRADEPRICE
ENDIF
When 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!
Something like this perhaps (not tested):
//PARAMETRI
//inserire qui sotto tutti i parametri BASE del codice
// Definizione dei parametri del codice
DEFPARAM CumulateOrders = False // Posizioni cumulate disattivate
MioRischio = 100 //importo massimo in euro che voglio rischiare per ogni singola operazione
PercStop1 = 0.50 //attiva il pareggio al 50% del profitto
PercStop2 = 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 TESTARE
IF Not OnMarket THEN
BreakEven = 0
ENDIF
ONCE Flag = 0
IF OnMarket OR LOW < MinimoPat THEN
Flag=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 tradare
AO=Average[5](MedianPrice)-Average[34](MedianPrice)
Cond1 = AO[0]>0
Cond2 = AO[1]<0
CondTOT = Cond1 AND Cond2 //Condizione globale da tradare
IF CondTOT THEN
Flag = 1
ENDIF
//COMPORTAMENTI
//inserire qui sotto come si deve comportare la strategia quando è ONMARKET
IF CondTOT AND Not LongOnMarket THEN
TargetProfit = (HIGH - LOW)/pipsize //VALORE in PIPS del mio TARGET INIZIALE
Prezzo = HIGH //VALORE al quale si entra in posizione se vengono soddisfatte TUTTE le CONDIZIONI
StopLoss = (HIGH - LOW)/pipsize //VALORE in PIPS dello STOP LOSS INIZIALE
MinimoPat = LOW
ENDIF
//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 THEN
Buy Moltiplicatore lots AT (Prezzo+0.0000) STOP //Ordine automatico di ENTRATA LONG in posizione con eventuale aggiunta di pips
ENDIF
//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
//ENDIF
IF LongOnMarket THEN
IF Breakeven = 0 AND ((High - Tradeprice) >= (TargetProfit * PercStop1)*pipsize) THEN
ExitPrice = Tradeprice + PagoSpese
Breakeven = 1
ENDIF
IF Breakeven = 1 AND ((High - Tradeprice) >= (TargetProfit * PercStop2)*pipsize) AND High >= ExitPrice + ((TargetProfit * (PercStop2 - PercStop1))*pipsize) THEN
ExitPrice = Tradeprice + (TargetProfit/2)
Breakeven = 2
ENDIF
ENDIF
IF OnMarket AND Breakeven>0 THEN
SELL AT ExitPrice STOP
ENDIF
SET STOP pLOSS StopLoss
graphonprice exitprice
graph breakeven
I thank you very much for your kind help. I immediately try to change my code and check the results. Thank you again and I will certainly keep you informed on how this important change works. GRAZIE !!
I wrote this:
DEFPARAM CumulateOrders = False
MioRischio = 100 //importo massimo euro che voglio rischiare per ogni singola operazione
PercStop1 = 0.50 //attiva il pareggio al 50% del profitto
PercStop2 = 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 spese
MyStep = 5 * pipsize //5 pip step after PercSTop2 is hit
IF Not OnMarket THEN
BreakEven = 0
MyTradePrice = 0
ENDIF
IF OnMarket AND Not OnMarket[1] THEN
MyTradePrice = TRADEPRICE
BreakEven = 0
Flag = 0
SET STOP pLOSS StopLoss
SET TARGET pPROFIT TargetProfit
ENDIF
ONCE Flag = 0
IF OnMarket OR LOW < MinimoPat THEN //?????
ENDIF
AO=Average[5](MedianPrice)-Average[34](MedianPrice)
Cond1 = AO[0]>0
Cond2 = AO[1]<0
CondTOT = Cond1 AND Cond2 //Condizione globale da tradare
IF CondTOT THEN
Flag = 1
ENDIF
IF CondTOT AND Not LongOnMarket THEN
TargetProfit = (HIGH - LOW)/pipsize //VALORE in PIPS del mio TARGET INIZIALE
Prezzo = HIGH //VALORE al quale si entra in posizione
// se vengono soddisfatte TUTTE le CONDIZIONI
StopLoss = (HIGH - LOW)/pipsize //VALORE in PIPS dello STOP LOSS INIZIALE
MinimoPat = LOW
Profitto1 = (TargetProfit * PercStop1)
Profitto2 = (TargetProfit * PercStop2)
ENDIF
Moltiplicatore = (MioRischio/(StopLoss/Prezzo)/pipsize)
IF not onmarket AND Flag THEN
Moltiplicatore = 1
BUY Moltiplicatore lots AT (Prezzo+0*pipsize) STOP
SET STOP pLOSS StopLoss
SET TARGET pPROFIT TargetProfit
ENDIF
//------------------------------------------------------------------
CurrentProfit = (close - MyTradePrice) / pipsize
IF CurrentProfit > Profitto2 AND MyTradePrice < (TRADEPRICE + Profitto2 * pipsize) THEN
MyTradePrice = TRADEPRICE + Profitto2 * pipsize
ELSIF CurrentProfit > Profitto1 AND BreakEven = 0 THEN
BreakEven = MyTradePrice + PagoSpese * pipsize
ENDIF
IF BreakEven > 0 AND CurrentProfit > 0 THEN
IF close - MyTradePrice >= MyTradePrice + MyStep THEN
BreakEven = BreakEven + MyStep
MyTradePrice = MyTradePrice + MyStep
ENDIF
ENDIF
IF BreakEven AND OnMarket THEN
SELL AT BreakEven STOP
ENDIF
graphonprice 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.
As 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!
This 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:
DEFPARAM CumulateOrders = False
MioRischio = 100 //importo massimo euro che voglio rischiare per ogni singola operazione
PercStop1 = 0.50 //attiva il pareggio al 50% del profitto
PercStop2 = 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 spese
MyStep = 5 * pipsize //5 pip step after PercSTop2 is hit
IF Not OnMarket THEN
BreakEven = 0
MyTradePrice = 0
ENDIF
IF OnMarket AND Not OnMarket[1] THEN
MyTradePrice = TRADEPRICE
BreakEven = 0
Flag = 0
SET STOP pLOSS StopLoss
SET TARGET pPROFIT TargetProfit
ENDIF
ONCE Flag = 0
IF OnMarket OR LOW < MinimoPat THEN //?????
ENDIF
AO=Average[5](MedianPrice)-Average[34](MedianPrice)
Cond1 = AO[0]>0
Cond2 = AO[1]<0
CondTOT = Cond1 AND Cond2 //Condizione globale da tradare
IF CondTOT THEN
Flag = 1
ENDIF
IF CondTOT AND Not LongOnMarket THEN
TargetProfit = (HIGH - LOW)/pipsize //VALORE in PIPS del mio TARGET INIZIALE
Prezzo = HIGH //VALORE al quale si entra in posizione
// se vengono soddisfatte TUTTE le CONDIZIONI
StopLoss = (HIGH - LOW)/pipsize //VALORE in PIPS dello STOP LOSS INIZIALE
MinimoPat = LOW
TargetProfit = max(80,TargetProfit)
StopLoss = TargetProfit * 0.5
Profitto1 = (TargetProfit * PercStop1)
Profitto2 = (TargetProfit * PercStop2)
ENDIF
Moltiplicatore = (MioRischio/(StopLoss/Prezzo)/pipsize)
IF not onmarket AND Flag THEN
Moltiplicatore = 1
BUY Moltiplicatore lots AT (Prezzo+0*pipsize) STOP
SET STOP pLOSS StopLoss
SET TARGET pPROFIT TargetProfit
ENDIF
//------------------------------------------------------------------
CurrentProfit = (close - MyTradePrice) / pipsize
IF CurrentProfit > Profitto2 AND MyTradePrice < (TRADEPRICE + Profitto2 * pipsize) THEN
MyTradePrice = TRADEPRICE + Profitto2 * pipsize
BreakEven = TRADEPRICE + Profitto1 * pipsize
ELSIF CurrentProfit > Profitto1 AND BreakEven = 0 THEN
BreakEven = MyTradePrice + PagoSpese
ENDIF
IF BreakEven > 0 AND CurrentProfit > 0 AND MyTradePrice > (TRADEPRICE + PagoSpese) THEN
IF close - MyTradePrice >= MyStep THEN
BreakEven = BreakEven + MyStep
MyTradePrice = MyTradePrice + MyStep
ENDIF
ENDIF
IF BreakEven AND OnMarket THEN
SELL AT BreakEven STOP
ENDIF
graphonprice 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.
Would you please format your text in a more suitable way? Thank you.
It’s difficult to read and easily understand when text is all in one paragraph 🙂
Sorry, 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 !!
In 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!“. 🙂
Hi 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 code
Multi time frame and trailing stop management
This topic contains 62 replies,
has 5 voices, and was last updated by
robertogozzi
6 years, 5 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 05/21/2019 |
| Status: | Active |
| Attachments: | 21 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.