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

OCO Problem

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

    OCO Problem

    Hi -

    I am developing a strategy that places two OCO orders at the same price and time. One of the OCO orders has associated stop and profit target. The other OCO has an associated stop only. The goal is to enter with two contracts at the same price, divest one of the contracts at a small profit target and let the other contract run, with a trailing stop. I have the strategy largely working.

    Each OCO order has a unique ID. I use managed code to update each OCO leg in C# code. The problem is that the the OCO orders are both being executed at the first OCO's profit target. This puzzling because the 2nd OCO does not have a profit target. I have used the TraceOrders feature to capture the sequence of events (full log included as an attachment.) I see somethings in this log that I don't understand.

    Find below a snippet of the TraceOrders generated by this strategy:

    OCO order with a Stop and profit target (two legged OCO) : Signal='YM-20S-1-STD-L-A' oco='1fc9340ab894463bb45449e5ef335b50'
    OCO order with Stop only (one legged OCO): FromEntrySignal='YM-20S-1-STD-L-B' oco='147637e606f34ed9accff3291c10ab76'

    Each OCO has 1 contract.

    All of the orders of each OCO reference quantity=1. All is as expected until I move both OCO stop legs to BreakEven plus one tick. For some unexplained reason, both OCO Stop orders now have a quantity=2. While there is no log evidence of this, the 'Profit Target' must be also changed to a quantity of 2, because when the 'Profit Target' is hit, BOTH OCOs are closed at the 'Profit Target' price. I expected only the OCO Signal='YM-20S-1-STD-L-A' to be executed and the 'YM-20S-1-STD-L-B' leg to still be active. I am under the impression that using unique signal names keeps the each OCO's orders independent.

    Also, when I tested this using Strategy analyzer. The trade executed as expected in the backtest. Only the 'YM-20S-1-STD-L-A' leg completes at the profit target and 'YM-20S-1-STD-L-B' leg continues.

    The appears as if this is only a problem when trading against the real-time simulator. I did test this on other days, and the behavior is still not working as I expect.

    What is the problem be?

    Thanks for your help.


    Output from the TraceOrders event:

    75. 1/4/2021 1:29:03 PM, 1/4/2021 1:33:00 PM, SimAccount4, 202, YM, 20S-, Strategy 'TradeSafeAutomated04/189138844': Amended stop order orderId='ad1f33e6527e47eb8011107d2dd1fbc2' account='SimAccount4' name='Stop loss' orderState=ChangePending instrument='YM 03-21' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=29982 quantity=1 tif=Gtc oco='147637e606f34ed9accff3291c10ab76' filled=0 averageFillPrice=0 onBehalfOf='' id=15263 time='2021-01-04 13:29:03' gtd='2099-12-01' statementDate='2021-01-04' stopPriceChanged=29990

    76. 1/4/2021 1:29:03 PM, 1/4/2021 2:00:00 PM, SimAccount4, 201, YM, 20S-, Strategy 'TradeSafeAutomated04/189138844': Amended target order orderId='8edb21d051cb4e5ea7d1964b977bf85f' account='SimAccount4' name='Profit target' orderState=ChangePending instrument='YM 03-21' orderAction=Sell orderType='Limit' limitPrice=30005 stopPrice=0 quantity=1 tif=Gtc oco='1fc9340ab894463bb45449e5ef335b50' filled=0 averageFillPrice=0 onBehalfOf='' id=15262 time='2021-01-04 13:29:03' gtd='2099-12-01' statementDate='2021-01-04' limitPriceChanged=30005

    77. 1/4/2021 1:29:03 PM, 1/4/2021 2:00:00 PM, SimAccount4, 201, YM, 20S-, Strategy 'TradeSafeAutomated04/189138844': Amended stop order orderId='487c90c341ac45bc9b98805177f3613d' account='SimAccount4' name='Stop loss' orderState=ChangePending instrument='YM 03-21' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=29982 quantity=1 tif=Gtc oco='1fc9340ab894463bb45449e5ef335b50' filled=0 averageFillPrice=0 onBehalfOf='' id=15261 time='2021-01-04 13:29:03' gtd='2099-12-01' statementDate='2021-01-04' stopPriceChanged=29990

    78. 1/4/2021 1:29:03 PM, 1/4/2021 2:00:00 PM, SimAccount4, 201, YM, 20S-, Strategy 'TradeSafeAutomated04/189138844': Amended stop order orderId='ad1f33e6527e47eb8011107d2dd1fbc2' account='SimAccount4' name='Stop loss' orderState=ChangePending instrument='YM 03-21' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=29982 quantity=1 tif=Gtc oco='147637e606f34ed9accff3291c10ab76' filled=0 averageFillPrice=0 onBehalfOf='' id=15263 time='2021-01-04 13:29:03' gtd='2099-12-01' statementDate='2021-01-04' stopPriceChanged=29990

    79. >>Move Profit Stop Stop Target: 30001 (my diagnostic print)

    80. 1/4/2021 1:29:06 PM, 1/4/2021 1:29:20 PM, SimAccount4, 3422, YM, 20S-, Strategy 'TradeSafeAutomated04/189138844': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='YM-20S-1-STD-L-B' Mode=Price Value=30001 IsSimulatedStop=False IsMarketIfTouched=False
    81. 1/4/2021 1:29:06 PM, 1/4/2021 1:29:20 PM, SimAccount4, 3422, YM, 20S-, Strategy 'TradeSafeAutomated04/189138844': Amended stop order orderId='ad1f33e6527e47eb8011107d2dd1fbc2' account='SimAccount4' name='Stop loss' orderState=Accepted instrument='YM 03-21' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=29990 quantity=2 tif=Gtc oco='147637e606f34ed9accff3291c10ab76' filled=0 averageFillPrice=0 onBehalfOf='' id=15263 time='2021-01-04 13:29:03' gtd='2099-12-01' statementDate='2021-01-04' stopPriceChanged=30001

    82. 1/4/2021 1:29:06 PM, 1/4/2021 1:29:20 PM, SimAccount4, 3422, YM, 20S-, Strategy 'TradeSafeAutomated04/189138844': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='YM-20S-1-STD-L-A' Mode=Price Value=30001 IsSimulatedStop=False IsMarketIfTouched=False
    83. 1/4/2021 1:29:06 PM, 1/4/2021 1:29:20 PM, SimAccount4, 3422, YM, 20S-, Strategy 'TradeSafeAutomated04/189138844': Amended stop order orderId='487c90c341ac45bc9b98805177f3613d' account='SimAccount4' name='Stop loss' orderState=Accepted instrument='YM 03-21' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=29990 quantity=2 tif=Gtc oco='1fc9340ab894463bb45449e5ef335b50' filled=0 averageFillPrice=0 onBehalfOf='' id=15261 time='2021-01-04 13:29:03' gtd='2099-12-01' statementDate='2021-01-04' stopPriceChanged=30001


    Segment of the orderOrder grid from the Control Center
    YM 03-21 Sell Stop Market 2 0 30001 Cancelled 0 0 2 Stop loss TradeSafeAutomated04 147637e606f34ed9accff3291c10ab76 GTC SimAccount4 ad1f33e6527e47eb8011107d2dd1fbc2 1/4/2021 13:29:06
    YM 03-21 Sell Stop Market 2 0 30001 Cancelled 0 0 2 Stop loss TradeSafeAutomated04 1fc9340ab894463bb45449e5ef335b50 GTC SimAccount4 487c90c341ac45bc9b98805177f3613d 1/4/2021 13:29:06
    YM 03-21 Sell Limit 2 30005 0 Filled 2 30005 0 Profit target TradeSafeAutomated04 1fc9340ab894463bb45449e5ef335b50 GTC SimAccount4 8edb21d051cb4e5ea7d1964b977bf85f 1/4/2021 13:29:06
    YM 03-21 Buy Stop Limit 1 30000 30000 Filled 1 30000 0 YM-20S-1-STD-L-B TradeSafeAutomated04 GTC SimAccount4 089bedd545bd46889d60b43cfb5a5cc2 1/4/2021 13:29:04
    YM 03-21 Buy Stop Limit 1 30000 30000 Filled 1 30000 0 YM-20S-1-STD-L-A TradeSafeAutomated04 GTC SimAccount4 f41626be0e3845e4a6f6ad56649d1f26 1/4/2021 13:29:03
    Last edited by NinjaTrader_Jim; 01-06-2021, 07:40 AM. Reason: We have your log file, but we have removed it from the forums since the log files usually contain some personal account information

    #2
    Hello kweiss,

    Can you provide some detail on how the targets are submitted and managed? Are these from the Set methods or did you submit the type of order for each target yourself?

    If you are already managing the orders yourself you may want to try using unmanaged orders to see if that has the same effect.

    Another way to approach this would be to try and replicate what you are doing for order submission in a copy of the SampleMACrossOver so that we can have more context of what you are trying in code that is not working. From the log information I can't really tell what you are doing in the logic specifically to understand the outcome. Very likely making a simple test that only submits the orders in error to demonstrate the problem would be the best starting point at finding a solution.



    Please let me know if I may be of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      HI -

      I am using the NT intrinsic commands SetStopLoss and SetProfitTarget. Each as a unique SignalName.

      Looking at the logs shows that I am not setting the ProfitTarget to the place that it was executed. Also, the SetProfitTarget order is changing quantity from 1 contract to 2 contracts. I can not be doing this because there is no parameter to set quantity in the calling sequence of SetProfitTarger.

      The strategy works in the Strategy analyzer.

      Would these facts point to a problem in your real-time simulator confusing orders?


      Are there any other logs we can look at to verify that I am setting the targets incorrectly? They appear to be correct from the TraceOrders reported.



      Thank you.
      Last edited by kweiss; 01-06-2021, 09:42 AM.

      Comment


        #4
        Hello kweiss,

        Thanks for the additional details.

        No that wouldn't specifically point to a problem as I don't have all the details surrounding what your trying in code. If you see that it works in one mode and not another that would generally be a situation where we would need to see the code to better understand what the expectation would be for what you tried to do.

        To assist here I would need to know how the SetStopLoss and SetProfitTarget were being used in contrast to the entry. The easiest way to explore this problem would be to not use the existing script which may be complex and instead make a simple test script that just submits an entry and targets in the same way in realtime. A test like that would allow us to further explore the logic used in contrast to the result you are seeing to understand why that happened. You could use the SampleMACrossOver as a starting point for that test or also just make a new empty strategy.


        Please let me know if I may be of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Barry Milan, Yesterday, 10:35 PM
        5 responses
        16 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by DanielSanMartin, Yesterday, 02:37 PM
        2 responses
        13 views
        0 likes
        Last Post DanielSanMartin  
        Started by DJ888, 04-16-2024, 06:09 PM
        4 responses
        13 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by terofs, Today, 04:18 PM
        0 responses
        11 views
        0 likes
        Last Post terofs
        by terofs
         
        Started by nandhumca, Today, 03:41 PM
        0 responses
        8 views
        0 likes
        Last Post nandhumca  
        Working...
        X