Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Using ExitLongLimit and Handling of Partial Fills for Profit Target

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Using ExitLongLimit and Handling of Partial Fills for Profit Target

    Let's assume these sequence of events,

    OnBarUpdate:
    EnterLong(10, "L1");
    -- the order is partial filled for 5 contracts
    -- the OnExecution code sets the profit target using ExitLongLimit
    -- the TargetPrice is assumed calculated using ex.Price, but it's not important

    OnExecution #1:

    -- we'll find that ex.Order.OrderState == OrderState.PartFilled
    -- we'll set the profit target like this,
    TargetOrder = ExitLongLimit(1, true, ex.Order.Filled, TargetPrice, "L1tgt", "L1");
    -- which sets a profit target for 5 contracts

    -- a few bars later, another partial fill for 4 contracts --
    -- we'll modify TargetOrder to include these 4 additional contracts --

    OnExecution #2:

    -- we'll find that ex.Order.OrderState == OrderState.PartFilled
    -- we'll check profit target like this,
    if (TargetOrder != null)
    TargetOrder = ExitLongLimit(1, true, TargetOrder.Quantity + ex.Order.Filled, TargetOrder.LimitPrice, "L1tgt", "L1");
    -- which updates the existing profit target for 9 contracts
    [Edit: no it doesn't, this code has an error, see my post #3 below]

    -- a few bars later, the order is filled with the 1 remaining contract --
    -- we'll modify TargetOrder to include this 1 remaining contract --

    OnExecution #3:

    -- we'll find that ex.Order.OrderState == OrderState.Filled
    -- we'll check profit target like this,
    if (TargetOrder != null)
    TargetOrder = ExitLongLimit(1, true, TargetOrder.Quantity + ex.Order.Filled, TargetOrder.LimitPrice, "L1tgt", "L1");
    -- which updates the existing profit target to 10 contracts
    [Edit: no it doesn't, this code has an error, see my post #3 below]

    My Questions:

    1. For fills of 5, 4, and 1 contracts, do the OnExecution sequence of events make sense?
    [Edit: Only #1 is correct, code in #2 & #3 has an error]

    2. Is ex.Order.OrderState == OrderState.PartFilled correct for OnExecution #1 and #2?
    [Edit: yes, state will be OrderState.PartFilled for #1 and #2]

    3. Is ex.Order.OrderState == OrderState.Filled correct for OnExecution #3?
    [Edit: yes, final fill of the order will be OrderState.Filled]

    4a. In OnExecution #1, what is the value of TargetOrder.Quantity after ExitLongLimit() returns?
    [Edit: same as ex.Order.Filled, which should be value of '5']

    4b. In OnExecution #1, what is the value of TargetOrder.LimitPrice after ExitLongLimit() returns?
    [Edit: same as TargetPrice]

    5a. Is it safe to use TargetOrder.Quantity in ExitLongLimit() in OnExecution #2 and #3?
    [Edit: safe? sure, but my code is using it incorrectly.]

    5b. Is it safe to use TargetOrder.LimitPrice in ExitLongLimit() in OnExecution #2 and #3?
    [Edit: yes, TargetOrder.LimitPrice contains the limit price set when TargetOrder is first returned in #1 -- reusing it to set to same price in #2 & #3 is benign, and safe]

    6. I'm trying to get to a definitive answer for 5a and 5b, because I want to update the existing target order, and I assume I do this by using same last 2 arg strings in ExitLongLimit() in OnExecution #2 and #3 as was used in OnExecution #1.
    Is this Correct? [Edit: no, code in #2 & #3 is wrong, code in #1 is correct]
    Will this work? [Edit: no, code in #2 & #3 is wrong, code in #1 is correct]

    7. I see the advanced overload of ExitLongLimit() is documented like this,
    ExitLongLimit(int BIP, bool liveUntilCancelled, int quantity, double limitPrice, string signalName)
    but my examples above are using this overload,
    ExitLongLimit(int BIP, bool liveUntilCancelled, int quantity, double limitPrice, string signalName, string fromEntrySignal)
    note the addition of the last argument as fromEntrySignal.
    Is this version of ExitLongLimit() being used correctly? [Edit: yes]
    Is this overload documented? [Edit: no, not that I can find]
    Well, smarty pants, if it's not documented, how did you find it?
    Good question, I think I must have found it in example code,
    on the forums, or at BMT somewhere ...


    Thanks

    [Edit: added answers to my own questions in red]
    Last edited by bltdavid; 03-06-2016, 11:04 AM. Reason: added answers to my own questions in red

    #2
    Is ex.Order.Filled a cumulative total of all contracts filled to date, or is it just the number of contracts filled for this execution?
    [Edit: ex.Order.Filled is the cumulative total of contracts filled to date]

    In other words, in my above example of fills of 5, 4, and 1 contracts, which is correct?

    Partial Fill #2 in OnExecution #2:
    When 4 more contracts are partially filled (for a total of 9 filled contracts) does ex.Order.Filled contain "4" or does it contain "9"?
    [Edit: ex.Order.Filled will contain '9']

    Order is Completely Filled in OnExecution #3:
    When 1 more contract is filled (for a total of 10 contracts filled) does ex.Order.Filled contain "10" or does it contain "1"?
    [Edit: ex.Order.Filled will contain '10']
    Last edited by bltdavid; 03-06-2016, 12:36 PM. Reason: added answers to my own questions in red

    Comment


      #3
      I've figured this out.

      Inside OnExecution, ex.Order.Filled is a cumulative effect, so it always contains the total number of contracts filled to date for that order.

      In OnExecution #2 & #3 above, the updating of the target order is incorrect.

      I need to use,

      TargetOrder = ExitLongLimit(1, true, ex.Order.Filled, TargetPrice, "L1tgt", "L1");

      because ex.Order.Filled already contains exactly what I need. That is, it will contain values 5, 9, 10 on the three OnExecution's in my scenario.

      Behold, this is the same code as already exists to handle OnExecution #1.

      Comment


        #4
        Originally posted by leocrespo
        Thank you for posting the solution, I was wondering the same thing.
        You're welcome!

        I reviewed my previous posts and decided to answer my own questions in red.

        Hopefully, others who find this thread will benefit further.

        PS: This post was back when I went through a huge learning phase to understand order management. In my experience, learning NinjaScript programming well (or any language, really) requires lots of experiments and lots of attention to details.

        PPS: Studying your log files sure helps. (Forget the "Log" tab, use the files. The "Log" tab shows the most recent messages at the top, instead of appended to the bottom.) I also rely on a "tail" watching program (I use one called BareTail, but there are many) to track the progress of the log files as new messages are appended. Highly recommended.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by andrewtrades, Today, 04:57 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by chbruno, Today, 04:10 PM
        0 responses
        6 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Started by josh18955, 03-25-2023, 11:16 AM
        6 responses
        436 views
        0 likes
        Last Post Delerium  
        Started by FAQtrader, Today, 03:35 PM
        0 responses
        7 views
        0 likes
        Last Post FAQtrader  
        Started by rocketman7, Today, 09:41 AM
        5 responses
        19 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X