How to manage consecutive stop orders in a program ?
Forums › ProRealTime English forum › ProOrder support › How to manage consecutive stop orders in a program ?
- This topic has 14 replies, 5 voices, and was last updated 5 years ago by robertogozzi.
-
-
05/17/2018 at 5:38 PM #70725
Hi,
Many thanks in advance for the time you will take to read, understand and hopefully answer this post.
Well I am trying to program something I am often doing while I am manually trading.
The idea is simple :
- I enter a trade with quite a lot of confidence on the chance that the trade has a big pontential :
- I put 4 lots on DAX for example TimeFrame=30 minutes
- As I am reasonably optimistic I place a LIMIT and a STOP, at +75 points (target) and -50 points (loss).
- Let’s imagine that my trade is a LONG one (for the purpose of the example) : I immediately place a BUY STOP of 2 lots at +30 points above my entry value (let’s say I entered at 13 000, so here at 13 030)
- Then I wait, and there is 2 alternatives :
- 1- I am stop at my STOP LOSS (sad, but easy…)
- 2- My BUY stop order is executed
- If case 2- happens, I am IMMEDIATELY doing the following :
- I move up my original stop loss at the breakeven point : from 12950 to 13010 in my example
- I place a LIMIT at the same LIMIT than the 1st order : at 13 075 in my example
- so that I have now 6 lots with a STOP LOSS at 13 010 and a TARGET PROFIT at 13 075
- and I also immediately place last a BUY STOP of 1 lot at +50 points above my entry value (so here at 13 050)
- Then I wait, and there is again 2 alternatives :
- 1- I am stop at my STOP LOSS (with no loss and no gain : breakeven)
- 2- My last BUY stop order is executed
- If case 2- happens, I am IMMEDIATELY doing the following :
- I move up my original stop loss at the breakeven point of order#2 and order#3 : from 13010 to 13036.7 in my example
- I place a LIMIT at the same LIMIT than the 1st order : at 13 075 in my example
- so that I have now 7 lots with a STOP LOSS at 13 036.7 and a TARGET PROFIT at 13 075
- Then I wait, and there a 2 final alternatives which will make me out of the program :
- 1- I am stop at my STOP LOSS (with gain of my order#1 and no pain no gain on my orders #2 and #3 “which protect each other”)
- 2- I am stop at my LIMIT with full gain on my 7 positions
- NB – On DAX I will close all positions whatever happens at 9.30pm and will not open any position before 8am.
I join hereafter the beginning of the corresponding piece of code. But I do not know how to manage the consecutive STOP ORDERS and STOP LOSS modifications and TARGET PROFITS as well…
If you could help, that would be great and you will enjoy in return a nice piece of winning trading methodology I think.
BR,
Consecutive stop ordesr1234567891011121314151617181920212223242526272829303132333435// Définition des paramètres du codeDEFPARAM CumulateOrders = False // Cumul des positions désactivé// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"DEFPARAM FLATAFTER = 210000// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiéenoEntryBeforeTime = 080000timeEnterBefore = time >= noEntryBeforeTime// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiéenoEntryAfterTime = 210000timeEnterAfter = time < noEntryAfterTime// Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiésdaysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// Conditions pour ouvrir une position acheteusec1 = (close CROSSES OVER 13000)IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THENBUY 4 CONTRACT AT MARKETBUY 2 CONTRACTS AT CLOSE+30 STOPENDIF// Conditions pour ouvrir une position en vente à découvertc2 = (close CROSSES UNDER 12900)IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THENSELLSHORT 4 CONTRACT AT MARKETSELLSHORT 2 CONTRACT AT CLOSE-30 STOPENDIF// Stops et objectifsSET STOP LOSS 50SET TARGET PROFIT 7505/18/2018 at 7:29 AM #70736It’s not possible to set a STOP LOSS/TARGET PROFIT at different points, it will always be the same for ALL the contracts you have bought, no matter when and at what price you traded them.
For breakeven and trailing stops you may find useful codes and suggestions here:
https://www.prorealcode.com/blog/learning/breakeven-code-automated-trading-strategy/
https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
05/18/2018 at 8:08 AM #70738Thanks for your answer Robertogozzi.
Maybe the fact that I wrote about “TARGET PROFIT” was a bit misleading in my explanation. Because you should understand from it that all my consecutive orders have the same exits points (but that points are changing according to new orders coming in) ; so I should rather speak about LIMIT/STOP ORDERS (in profit or loss) rather than about TARGET PROFIT/STOP LOSS.
You will find hereafter again my explanation with less misslideading words I hope :
The idea is simple :
- I enter a trade with quite a lot of confidence on the chance that the trade has a big pontential :
- I put 4 lots on DAX for example TimeFrame=30 minutes (Order #1)
- As I am reasonably optimistic I add a LIMIT order and a STOP order for these 4 lots : at +75 points (this is my objective) and -50 points (this is my protection).
- Let’s imagine that my trade is a LONG one (for the purpose of the example) : I immediately place a BUY STOP order of 2 lots at +30 points above my initial entry value (let’s say I entered at 13 000, so here at 13 030). Note that my BUY STOP is above my initial entry value (obvious), but below my objective.
- Then I wait, and there are 2 alternatives :
- 1- I am stopped at my protection level -50 points below my entry point (sad, but easy…)
- 2- My BUY stop order is executed (Order #2)
- If case 2- happens, I am IMMEDIATELY doing the following :
- I move up my protection at the breakeven point : from 12950 to 13010 in my example (this becomes my protection level for both Orders #1 and #2)
- I place a LIMIT at the same LIMIT than the 1st order : at 13 075 in my example
- so that I have now 6 lots with a protection at 13 010 and an objective at 13 075
- and I also immediately place last a BUY STOP order of 1 lot at +50 points above my very initial entry value (so here at 13 050)
- Then I wait, and there are again 2 alternatives :
- 1- I am stopped at my protection level (with no loss and no gain : breakeven)
- 2- My last BUY stop order is executed (Order #3)
- If case 2- happens, I am IMMEDIATELY doing the following :
- I move up my protection at the breakeven point of order#2 and order#3 : from 13010 to 13036.7 in my example ((this becomes my protection level for both Orders #1, #2 and #3)
- I place a LIMIT at the same LIMIT than the 1st and 2nd order : at 13 075 in my example
- so that I have now 7 lots with a protection at 13 036.7 and an objective at 13 075
- Then I wait, and there a 2 final alternatives which will make me out of the program :
- 1- I am stopped at my protection level (with gain of my order#1 and no pain no gain on my orders #2 and #3 “which protect each other”)
- 2- I am stopped at my objective with full gain on my 7 positions
- NB – On DAX I will close all positions whatever happens at 9.30pm and will not open any position before 8am.
I join hereafter the beginning of the corresponding piece of code. But I do not know how to manage the consecutive STOP and LIMIT orders
Hope it is more clear this way ;)12345678910111213141516171819202122232425262728293031// Définition des paramètres du codeDEFPARAM CumulateOrders = False // Cumul des positions désactivé// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"DEFPARAM FLATAFTER = 210000// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiéenoEntryBeforeTime = 080000timeEnterBefore = time >= noEntryBeforeTime// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiéenoEntryAfterTime = 210000timeEnterAfter = time < noEntryAfterTime// Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiésdaysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// Conditions pour ouvrir une position acheteusec1 = (close CROSSES OVER 13000)IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THENBUY 4 CONTRACT AT MARKETBUY 2 CONTRACTS AT CLOSE+30 STOPENDIF// Conditions pour ouvrir une position en vente à découvertc2 = (close CROSSES UNDER 12900)IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THENSELLSHORT 4 CONTRACT AT MARKETSELLSHORT 2 CONTRACT AT CLOSE-30 STOPENDIF…
If you could help, that would be great and you will enjoy in return a nice piece of winning trading methodology I think.
BR,
05/18/2018 at 8:52 AM #7074005/18/2018 at 8:55 AM #70741Thanks Eric.
And/but again to be clear, I DON’T “want” different take profits.
I would like my orders to have the same limit (the initial one) and a protection which evolves while number of orders is increasing.
05/18/2018 at 9:22 AM #70743I’m working on it, will be back within 20-30 minutes.
05/18/2018 at 9:53 AM #70747This what I managed to arrange
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485// Définition des paramètres du codeDEFPARAM CumulateOrders = False // Cumul des positions désactivé// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"DEFPARAM FLATAFTER = 210000ONCE MyStop = 50 * pipsizeONCE MyProfit = MyStop * 1.5ONCE MyLimit1 = 30 * pipsize //limit to set SL to breakeven (will only be checked at the CLOSING of the bar)ONCE MyLimit2 = 50 * pipsizeONCE Points2Keep = 10 * pipsize //can be ZERO (or even nagative if you want!)// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiéenoEntryBeforeTime = 080000timeEnterBefore = time >= noEntryBeforeTime// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiéenoEntryAfterTime = 210000timeEnterAfter = time < noEntryAfterTime// Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiésdaysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// set stop Loss & Target to default values when not onmarketIF Not OnMarket THENMyStop = 50 * pipsizeMyProfit = MyStop * 1.5ENDIF///////////////////////////////////////////////////////////////////////////// this section is to make sure each pending order is placed at each new bar, since// all pending orders expire after one bar!// Here I use TRADEPRICE because:// - Close would be different from the one when the trade was entered// - Tradeprice is more accurate//IF LongOnMarket THENIF close >= (TRADEPRICE + MyLimit1) THENMyStop = TRADEPRICE + Points2Keep //set SL at breakeven + points to keepENDIFIF CountOfPosition = 4 THENBUY 2 CONTRACTS AT TRADEPRICE + MyLimit1 STOP //2nd orderENDIFIF CountOfPosition = 6 THENBUY 1 CONTRACTS AT TRADEPRICE + MyLimit2 STOP //3rd orderENDIFSELL AT TRADEPRICE - MyStop STOP //Stop LossSELL AT TRADEPRICE + MyProfit LIMIT //Target ProfitENDIFIF ShortOnMarket THENIF close <= (TRADEPRICE - MyLimit1) THENMyStop = TRADEPRICE - Points2Keep //set SL at breakeven + points to keepENDIFIF CountOfPosition = 4 THENSELLSHORT 2 CONTRACTS AT TRADEPRICE - MyLimit1 STOP//2nd orderENDIFIF CountOfPosition = 6 THENSELLSHORT 1 CONTRACTS AT TRADEPRICE - MyLimit2 STOP//3rd orderENDIFEXITSHORT AT TRADEPRICE + MyStop STOP //Stop LossEXITSHORT AT TRADEPRICE - MyProfit LIMIT //Target ProfitENDIF///////////////////////////////////////////////////////////////////////////// Conditions pour ouvrir une position acheteusec1 = (close CROSSES OVER 13000)IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THENBUY 4 CONTRACT AT MARKET //1st orderBUY 2 CONTRACTS AT CLOSE + MyLimit1 STOP //2nd orderBUY 1 CONTRACTS AT CLOSE + MyLimit2 STOP //3rd orderSELL AT CLOSE - MyStop STOP //Stop LossSELL AT CLOSE + MyProfit LIMIT //Target ProfitENDIF// Conditions pour ouvrir une position en vente à découvertc2 = (close CROSSES UNDER 12900)IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THENSELLSHORT 4 CONTRACT AT MARKET //1st orderSELLSHORT 2 CONTRACT AT CLOSE - MyLimit1 STOP //2nd orderSELLSHORT 1 CONTRACT AT CLOSE - MyLimit2 STOP //3rd orderEXITSHORT AT close + MyStop STOP //Stop LossEXITSHORT AT close - MyProfit LIMIT //Target ProfitENDIF1 user thanked author for this post.
05/18/2018 at 9:54 AM #70748I only tested it for syntax, let me know about logic issues.
05/18/2018 at 10:03 AM #70750I use pipsize, despite not necessary when trading DAX, to make code portable so that you can use it for other instruments letting ProOrder to take care of conversions.
05/18/2018 at 10:20 AM #70751Tx a lot Robertogozzi !
I will try it “against” my manual practice and will let you know in case I would see something not matching.
05/18/2018 at 12:33 PM #7075905/19/2018 at 5:57 PM #7089505/20/2018 at 9:32 AM #7091805/20/2018 at 12:28 PM #70937Whenever you manually modify a trade opened by ProOrder, the strategy is immediately quit.
05/21/2018 at 12:34 AM #70980I changed CUMULATEORDERS to TRUE as suggested by Leo and also added a new line after lines 41 and 55 to accomodate for the 7 orders, in case on the following (just opening) bar ALL the three need to be entered.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687// Définition des paramètres du codeDEFPARAM CumulateOrders = True // Cumul des positions désactivé// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"DEFPARAM FLATAFTER = 210000ONCE MyStop = 50 * pipsizeONCE MyProfit = MyStop * 1.5ONCE MyLimit1 = 30 * pipsize //limit to set SL to breakeven (will only be checked at the CLOSING of the bar)ONCE MyLimit2 = 50 * pipsize //limit to set SL to breakeven (will only be checked at the CLOSING of the bar)ONCE Points2Keep = 10 * pipsize //can be ZERO (or even nagative if you want!)// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiéenoEntryBeforeTime = 080000timeEnterBefore = time >= noEntryBeforeTime// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiéenoEntryAfterTime = 210000timeEnterAfter = time < noEntryAfterTime// Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiésdaysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// set stop Loss & Target to default values when not onmarketIF Not OnMarket THENMyStop = 50 * pipsizeMyProfit = MyStop * 1.5ENDIF///////////////////////////////////////////////////////////////////////////// this section is to make sure each pending order is placed at each new bar, since// all pending orders expire after one bar!// Here I use TRADEPRICE because:// - Close would be different from the one when the trade was entered// - Tradeprice is more accurate//IF LongOnMarket THENIF close >= (TRADEPRICE + MyLimit1) THENMyStop = TRADEPRICE + Points2Keep //set SL at breakeven + points to keepENDIFIF CountOfPosition = 4 THENBUY 2 CONTRACTS AT TRADEPRICE + MyLimit1 STOP //2nd orderBUY 1 CONTRACTS AT TRADEPRICE + MyLimit2 STOP //3rd orderENDIFIF CountOfPosition = 6 THENBUY 1 CONTRACTS AT TRADEPRICE + MyLimit2 STOP //3rd orderENDIFSELL AT TRADEPRICE - MyStop STOP //Stop LossSELL AT TRADEPRICE + MyProfit LIMIT //Target ProfitENDIFIF ShortOnMarket THENIF close <= (TRADEPRICE - MyLimit1) THENMyStop = TRADEPRICE - Points2Keep //set SL at breakeven + points to keepENDIFIF CountOfPosition = 4 THENSELLSHORT 2 CONTRACTS AT TRADEPRICE - MyLimit1 STOP//2nd orderSELLSHORT 1 CONTRACTS AT TRADEPRICE - MyLimit2 STOP//3rd orderENDIFIF CountOfPosition = 6 THENSELLSHORT 1 CONTRACTS AT TRADEPRICE - MyLimit2 STOP//3rd orderENDIFEXITSHORT AT TRADEPRICE + MyStop STOP //Stop LossEXITSHORT AT TRADEPRICE - MyProfit LIMIT //Target ProfitENDIF///////////////////////////////////////////////////////////////////////////// Conditions pour ouvrir une position acheteusec1 = (close CROSSES OVER 13000)IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THENBUY 4 CONTRACT AT MARKET //1st orderBUY 2 CONTRACTS AT CLOSE + MyLimit1 STOP //2nd orderBUY 1 CONTRACTS AT CLOSE + MyLimit2 STOP //3rd orderSELL AT CLOSE - MyStop STOP //Stop LossSELL AT CLOSE + MyProfit LIMIT //Target ProfitENDIF// Conditions pour ouvrir une position en vente à découvertc2 = (close CROSSES UNDER 12900)IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THENSELLSHORT 4 CONTRACT AT MARKET //1st orderSELLSHORT 2 CONTRACT AT CLOSE - MyLimit1 STOP //2nd orderSELLSHORT 1 CONTRACT AT CLOSE - MyLimit2 STOP //3rd orderEXITSHORT AT close + MyStop STOP //Stop LossEXITSHORT AT close - MyProfit LIMIT //Target ProfitENDIF - I enter a trade with quite a lot of confidence on the chance that the trade has a big pontential :
-
AuthorPosts
Find exclusive trading pro-tools on