How to manage consecutive stop orders in a program ?

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #70725 quote
    Toto le Heros
    Participant
    Veteran

    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,

    // Définition des paramètres du code
    DEFPARAM 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ée
    noEntryBeforeTime = 080000
    timeEnterBefore = 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ée
    noEntryAfterTime = 210000
    timeEnterAfter = time < noEntryAfterTime
    
    // Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiés
    daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
    
    // Conditions pour ouvrir une position acheteuse
    c1 = (close CROSSES OVER 13000)
    
    IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
    BUY 4 CONTRACT AT MARKET
    BUY 2 CONTRACTS AT CLOSE+30 STOP
    ENDIF
    
    // Conditions pour ouvrir une position en vente à découvert
    c2 = (close CROSSES UNDER 12900)
    
    IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
    SELLSHORT 4 CONTRACT AT MARKET
    SELLSHORT 2 CONTRACT AT CLOSE-30 STOP
    ENDIF
    
    // Stops et objectifs
    SET STOP LOSS 50
    SET TARGET PROFIT 75
    
    #70736 quote
    robertogozzi
    Moderator
    Master

    It’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/

    #70738 quote
    Toto le Heros
    Participant
    Veteran

    Thanks 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

    // Définition des paramètres du code
    DEFPARAM 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ée
    noEntryBeforeTime = 080000
    timeEnterBefore = 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ée
    noEntryAfterTime = 210000
    timeEnterAfter = time < noEntryAfterTime
    
    // Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiés
    daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
    
    // Conditions pour ouvrir une position acheteuse
    c1 = (close CROSSES OVER 13000)
    
    IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
    BUY 4 CONTRACT AT MARKET
    BUY 2 CONTRACTS AT CLOSE+30 STOP
    ENDIF
    
    // Conditions pour ouvrir une position en vente à découvert
    c2 = (close CROSSES UNDER 12900)
    
    IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
    SELLSHORT 4 CONTRACT AT MARKET
    SELLSHORT 2 CONTRACT AT CLOSE-30 STOP
    ENDIF
    

     

    If you could help, that would be great and you will enjoy in return a nice piece of winning trading methodology I think.

    BR,

    #70740 quote
    Eric
    Participant
    Master

    If you want different take profits for 4 contracts

    then use 2 systems (same code) instead of 1 (or 4 instead of 1)

    and also trailing stop

    #70741 quote
    Toto le Heros
    Participant
    Veteran

    Thanks 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.

    #70743 quote
    robertogozzi
    Moderator
    Master

    I’m working on it, will be back within 20-30 minutes.

    #70747 quote
    robertogozzi
    Moderator
    Master

    This what I managed to arrange

    // Définition des paramètres du code
    DEFPARAM CumulateOrders = False // Cumul des positions désactivé
    // Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
    DEFPARAM FLATAFTER = 210000
    
    ONCE MyStop      = 50 * pipsize
    ONCE MyProfit    = MyStop * 1.5
    ONCE MyLimit1    = 30 * pipsize      //limit to set SL to breakeven (will only be checked at the CLOSING of the bar)
    ONCE MyLimit2    = 50 * pipsize
    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ée
    noEntryBeforeTime = 080000
    timeEnterBefore = 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ée
    noEntryAfterTime = 210000
    timeEnterAfter = time < noEntryAfterTime
     
    // Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiés
    daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
    
    // set stop Loss & Target to default values when not onmarket
    IF Not OnMarket THEN
       MyStop   = 50 * pipsize
       MyProfit = MyStop * 1.5
    ENDIF
    
    ///////////////////////////////////////////////////////////////////////////
    // 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 THEN
       IF close >= (TRADEPRICE + MyLimit1) THEN
          MyStop = TRADEPRICE + Points2Keep                  //set SL at breakeven + points to keep
       ENDIF
       IF CountOfPosition = 4 THEN
          BUY 2 CONTRACTS AT TRADEPRICE + MyLimit1 STOP      //2nd order
       ENDIF
       IF CountOfPosition = 6 THEN
          BUY 1 CONTRACTS AT TRADEPRICE + MyLimit2 STOP      //3rd order
       ENDIF
       SELL AT TRADEPRICE - MyStop STOP                      //Stop Loss
       SELL AT TRADEPRICE + MyProfit LIMIT                   //Target Profit
    ENDIF
    
    IF ShortOnMarket THEN
       IF close <= (TRADEPRICE - MyLimit1) THEN
          MyStop = TRADEPRICE - Points2Keep                  //set SL at breakeven + points to keep
       ENDIF
       IF CountOfPosition = 4 THEN
          SELLSHORT 2 CONTRACTS AT TRADEPRICE - MyLimit1 STOP//2nd order
       ENDIF
       IF CountOfPosition = 6 THEN
          SELLSHORT 1 CONTRACTS AT TRADEPRICE - MyLimit2 STOP//3rd order
       ENDIF
       EXITSHORT AT TRADEPRICE + MyStop STOP                 //Stop Loss
       EXITSHORT AT TRADEPRICE - MyProfit LIMIT              //Target Profit
    ENDIF
    ///////////////////////////////////////////////////////////////////////////
    
    // Conditions pour ouvrir une position acheteuse
    c1 = (close CROSSES OVER 13000)
     
    IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THEN
       BUY 4 CONTRACT AT MARKET                              //1st order
       BUY 2 CONTRACTS AT CLOSE + MyLimit1 STOP              //2nd order
       BUY 1 CONTRACTS AT CLOSE + MyLimit2 STOP              //3rd order
       SELL AT CLOSE - MyStop STOP                           //Stop Loss
       SELL AT CLOSE + MyProfit LIMIT                        //Target Profit
    ENDIF
     
    // Conditions pour ouvrir une position en vente à découvert
    c2 = (close CROSSES UNDER 12900)
     
    IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THEN
       SELLSHORT 4 CONTRACT AT MARKET                        //1st order
       SELLSHORT 2 CONTRACT AT CLOSE - MyLimit1 STOP         //2nd order
       SELLSHORT 1 CONTRACT AT CLOSE - MyLimit2 STOP         //3rd order
       EXITSHORT AT close + MyStop STOP                      //Stop Loss
       EXITSHORT AT close - MyProfit LIMIT                   //Target Profit
    ENDIF
    Nicolas thanked this post
    #70748 quote
    robertogozzi
    Moderator
    Master

    I only tested it for syntax, let me know about logic issues.

    #70750 quote
    robertogozzi
    Moderator
    Master

    I 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.

    #70751 quote
    Toto le Heros
    Participant
    Veteran

    Tx a lot Robertogozzi !

    I will try it “against” my manual practice and will let you know in case I would see something not matching.

    #70759 quote
    Leo
    Participant
    Veteran

    Comulate Order = true

    #70895 quote
    Ludde500
    Participant
    Average
    Is it possible to change the code so you can manually enter PRT and change STOP LOSS / TARGET PROFIT as you do when trading manually or is there any other system setting that controls it?
    #70918 quote
    Ludde500
    Participant
    Average

    #70937 quote
    robertogozzi
    Moderator
    Master

    Whenever you manually modify a trade opened by ProOrder, the strategy is immediately quit.

    #70980 quote
    robertogozzi
    Moderator
    Master

    I 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.

    // Définition des paramètres du code
    DEFPARAM CumulateOrders = True // Cumul des positions désactivé
    // Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
    DEFPARAM FLATAFTER = 210000
    
    ONCE MyStop      = 50 * pipsize
    ONCE MyProfit    = MyStop * 1.5
    ONCE 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ée
    noEntryBeforeTime = 080000
    timeEnterBefore = 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ée
    noEntryAfterTime = 210000
    timeEnterAfter = time < noEntryAfterTime
     
    // Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiés
    daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
    
    // set stop Loss & Target to default values when not onmarket
    IF Not OnMarket THEN
       MyStop   = 50 * pipsize
       MyProfit = MyStop * 1.5
    ENDIF
    
    ///////////////////////////////////////////////////////////////////////////
    // 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 THEN
       IF close >= (TRADEPRICE + MyLimit1) THEN
          MyStop = TRADEPRICE + Points2Keep                  //set SL at breakeven + points to keep
       ENDIF
       IF CountOfPosition = 4 THEN
          BUY 2 CONTRACTS AT TRADEPRICE + MyLimit1 STOP      //2nd order
          BUY 1 CONTRACTS AT TRADEPRICE + MyLimit2 STOP      //3rd order
       ENDIF
       IF CountOfPosition = 6 THEN
          BUY 1 CONTRACTS AT TRADEPRICE + MyLimit2 STOP      //3rd order
       ENDIF
       SELL AT TRADEPRICE - MyStop STOP                      //Stop Loss
       SELL AT TRADEPRICE + MyProfit LIMIT                   //Target Profit
    ENDIF
    
    IF ShortOnMarket THEN
       IF close <= (TRADEPRICE - MyLimit1) THEN
          MyStop = TRADEPRICE - Points2Keep                  //set SL at breakeven + points to keep
       ENDIF
       IF CountOfPosition = 4 THEN
          SELLSHORT 2 CONTRACTS AT TRADEPRICE - MyLimit1 STOP//2nd order
          SELLSHORT 1 CONTRACTS AT TRADEPRICE - MyLimit2 STOP//3rd order
       ENDIF
       IF CountOfPosition = 6 THEN
          SELLSHORT 1 CONTRACTS AT TRADEPRICE - MyLimit2 STOP//3rd order
       ENDIF
       EXITSHORT AT TRADEPRICE + MyStop STOP                 //Stop Loss
       EXITSHORT AT TRADEPRICE - MyProfit LIMIT              //Target Profit
    ENDIF
    ///////////////////////////////////////////////////////////////////////////
    
    // Conditions pour ouvrir une position acheteuse
    c1 = (close CROSSES OVER 13000)
     
    IF c1 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THEN
       BUY 4 CONTRACT AT MARKET                              //1st order
       BUY 2 CONTRACTS AT CLOSE + MyLimit1 STOP              //2nd order
       BUY 1 CONTRACTS AT CLOSE + MyLimit2 STOP              //3rd order
       SELL AT CLOSE - MyStop STOP                           //Stop Loss
       SELL AT CLOSE + MyProfit LIMIT                        //Target Profit
    ENDIF
     
    // Conditions pour ouvrir une position en vente à découvert
    c2 = (close CROSSES UNDER 12900)
     
    IF c2 AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND Not OnMarket THEN
       SELLSHORT 4 CONTRACT AT MARKET                        //1st order
       SELLSHORT 2 CONTRACT AT CLOSE - MyLimit1 STOP         //2nd order
       SELLSHORT 1 CONTRACT AT CLOSE - MyLimit2 STOP         //3rd order
       EXITSHORT AT close + MyStop STOP                      //Stop Loss
       EXITSHORT AT close - MyProfit LIMIT                   //Target Profit
    ENDIF
Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.

How to manage consecutive stop orders in a program ?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 14 replies,
has 5 voices, and was last updated by robertogozzi
7 years, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 05/17/2018
Status: Active
Attachments: No files
Logo Logo
Loading...