Add a stop loss and buy order simultaneously ?

I would like to know whether is possible to add a stop loss and buy order simultaneously? If yes which function and how it is applied?

Thanks in advance,

Leon F



  • ptunneyptunney Posts: 246


    No, that would not be safe. The buy may not fill and the stop may fill.

    The best process is to wait until you receive order fill confirmation in ON_FILL and either set your stop there or set a flag to set the stop up on your next pass through your main code. I normally set a flag and check next pass round in ON_TRADE.


  • Thanks Paul, I have not used on_trade or on_fill either. Do you have a test sample for them.

  • ptunneyptunney Posts: 246


    Psuedo code description for now..

    You are going to set up what is called a state system...

    State System
    0 = ready to trade
    1 = entry order submitted
    2 = in position
    3 = exit order submitted
    99 = exited and done trading for the day.

    There are normally three development stages to your state system..
    Stage 1 : We don't use on_fill, we just set the state when we send the order and assume we are in that state. (Useful for CQ Lite)
    Stage 2 : We use on_fill and change the state accordingly (needs CQ Elite)
    Stage 3 : We check how many shares got filled and write code to manage when we didn't enter enough or didn't exit the full amount). (requires CQ Elite)

    Most of the time for backtesting you can keep it at stage 1, super simple..

    STAGE 1

    ON_START : Set to self.state zero in on_start
    ON_TRADE # I'm assuming you are using on_trade for your decisions
    Only process entry logic when self.state is 0. Then when you send your order, immediately set to 2.
    Only process your exit logic when self.state is 2, when you send an exit, immediately set to 99 (or zero if you want to be able to re-enter)
    Make sure you have something in there so you don't enter and immediately exit... either put your exit first or capture timestamp and restrict your exit to at least x time after entry.

    STAGE 2

    In On_Start set self.state to zero

    On_trade or On_minute # depending which you are using to decide when to trade
    One of the first things you should always check is... what is my state?
    if self.state = 0 :
    rest of my code to decide to get in
    if self.state = 3 :
    rest of my code to decide when to get out

    On_fill # is only called when an order is filled.. so you set the state depending on where you are in the process..
    if state = 1: # we are waiting for our entry to be filled...
    up to you if you check the event. to make sure you fully filled.....
    self.stat = 2 # set the state to "in position"
    if self.state = 3 : # we are waiting for our exit to be filled.
    again, check that you are fully exited.
    self.state = 99 # we are not going to trade it again today.. or set it back to 0 to allow it to re-qualify.

    STAGE 3
    Simple, when you are ready to go LIVE you will want to add extra code in case you get partial fills on your entry or exit..
    You just add extra states and add extra code in your main area to deal with that...
    Don't worry about that for now...

    Hope this helps!


Sign In or Register to comment.