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

ChangeOrder Unmanaged Orders

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

    ChangeOrder Unmanaged Orders

    Hi,

    I am enclosing some sample code to show my problem.

    if(targetLong!=null)
    {

    CancelOrder(targetLong);
    targetLong=null;
    targetLong = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, sellpoint, 0, "TgtLong", "profit target long");

    }
    else
    targetLong = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, sellpoint, 0, "TgtLong", "profit target long");

    Within the context of my entire code snippet shown above, when I backtest using the code above, I get the results I am expecting but have problems in real time as it will cancel the order on one bar and submit the new order on the next bar so I have targetLong working on every other bar when it should be working on the current bar.
    In backtesting, this is not the case, the targetLong order is changed on every bar while the position is long. So I changed to code above and used the ChangeOrder method but now my backtest results are different but it acts right in realtime. See below for changed code snippet:

    if(targetLong!=null)
    {
    ChangeOrder(targetLong, 1, sellpoint, 0);


    }
    else
    targetLong = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, sellpoint, 0, "TgtLong", "profit target long");

    Shouldn't both code snippets do the same thing in backtesting since they are doing the same thing? I can see there is an issue in real time as perhaps the program in the first code snippet is waiting for a cancel confirmation before it submits the order therefore it gets skipped for one bar whereas in the second code snippet it just changes the order to the new level rather than cancelling and resubmitting. If I am wrong, please correct me!

    I understand if this is a work in progress and there might be some things to resolve in the backtesting as unmanaged orders are a newer feature. What I am most concerned is that my code works right in real time. What I want to know is if there is an issue with the backtesting engine handling the ChangeOrder method or am I just plain wrong in my thinking with respect to the ChangeOrder method?


    Thanks,

    Cowpux

    #2
    Hello Cowpux,

    You may be experiencing the difference between realtime orders and backtest.

    When NinjaTrader is running with Calculate on bar close (COBC) as true, orders are placed after the bar closes using logic calculated from that bar. In backtest, COBC is always true.

    Below is a link to the help guide on Calculate on bar close.


    This means that any orders that are triggered from that bar are submitted after the bar closes as the new bar opens. As the order is placed as the new bar opens and the timestamp of the order is within the time of the next bar, the order will show on the next bar.

    If the strategy were running with COBC as false, or if the script has intra-bar granularity and submitted orders on a smaller timeframe, this would cause the timestamp of the order to be within the bar it is triggered on as the order may be submitted intra-bar. This order would show on the same bar as the bar that triggered the order submission.

    This is outlined in the help guide on Discrepancies: Real-Time vs Backtest.
    http://www.ninjatrader.com/support/h...ime_vs_bac.htm

    What this means is, it comes down to the time of the order submission/modification. If the time of the action is on the start of the new bar it will appear on the new bar.

    Adding tick granularity is the single easiest way to ensure that a strategy performs the same in real time as it does in backtest.

    (Just a heads up, adding tick granularity will cause a lot more data to be processed and is more CPU intensive.)

    Below is a link to an example on intra-bar granularity.
    http://www.ninjatrader.com/support/f...ead.php?t=6652
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the response. I understand intrabar granularity. I guess what I am trying to say is that regardless of intrabar granularity, the behavior of the order placement should be the same between the 2 code snippets and should be the same in backtesting correct? Cancelling and then resubmitting an order at a new level should give the same results as changing an existing order to a new level in a backtest. However, when trading in real time, cancelling and submitting a new order does not not work but using ChangeOrder does work. Yet the backtest results change a significant amount when using ChangeOrder on the same data yet the realtime behavior is correct when using ChangeOrder.

      Comment


        #4
        Hi Cowpux,

        It depends on when the cancel/order change happens. I can't say if it would be the same without knowing exactly when and how the cancellation and re-submission would happen. If you are not using intra-bar granularity, I would not expect the behavior to be the same as there can be differences between real time and backtest.

        I can say in backtest there wouldn't be any time that the script would have to wait until the cancellation successful message returns from the broker.

        Also, if you are modifying the price, I would recommend ChangeOrder() as this will happen faster in real time than cancelling and resubmitting. In backtest its instant.


        Would you like to create a new "toy" sample that demonstrates the issue so that I may let you know what will happen with the code? This would only be the code necessary to reproduce the issue.

        To export your script do the following:
        1. Click File -> Utilities -> Export NinjaScript
        2. Enter a unique name for the file in the value for 'File name:'
        3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
        4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


        By default your exported file will be in the following location:
        • (My) Documents/NinjaTrader 7/bin/Custom/ExportNinjaScript/<export_file_name.zip>


        Below is a link to the help guide on Exporting NinjaScripts.
        http://www.ninjatrader.com/support/h...nt7/export.htm
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by kaywai, 09-01-2023, 08:44 PM
        5 responses
        601 views
        0 likes
        Last Post NinjaTrader_Jason  
        Started by xiinteractive, 04-09-2024, 08:08 AM
        6 responses
        22 views
        0 likes
        Last Post xiinteractive  
        Started by Pattontje, Yesterday, 02:10 PM
        2 responses
        18 views
        0 likes
        Last Post Pattontje  
        Started by flybuzz, 04-21-2024, 04:07 PM
        17 responses
        230 views
        0 likes
        Last Post TradingLoss  
        Started by agclub, 04-21-2024, 08:57 PM
        3 responses
        17 views
        0 likes
        Last Post TradingLoss  
        Working...
        X