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

Cancel all orders and start fresh on each enable?

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

    Cancel all orders and start fresh on each enable?

    Is there a way to check if there are historically pending orders that are somehow still in the pipe and have an action to cancel all orders and essentially reset everything before you enable the strategy each day so you start trading/sending orders fresh? As I research about order coding, I see methods like GetRealTimeOrder() and the possibility of historical orders.

    Like

    if (GetRealTimeOrder() != null)
    {
    Cancel all orders and reset all states
    }

    #2
    Hello Boonfly8,

    Thanks for opening the thread.

    When a strategy is enabled, it processes historical data to determine its virtual position. This historical position should not be confused with a live account position that exists from a previous run of a strategy. Orders generated from Historical data can be matched to live working orders from a previous instance of that strategy with the Immediately Submit start behavior so the position can be resumed by a strategy.

    GetRealtimeOrder is used to transition Order objects that you manage in your strategy from a historical state to a realtime state.

    If you would like to have your strategy start fresh each time you enable so it does not process historical data and generate a virtual position, you could place a return statement for State.Historical at the start of your script's OnBarUpdate method.

    I.E. if (State == State.Historical) return;

    Publicly available documentation on the items discussed are linked below.

    Strategy Vs. Account Position - https://ninjatrader.com/support/help..._account_p.htm

    Syncing Account Positions - https://ninjatrader.com/support/help..._positions.htm

    GetRealtimeOrder() - https://ninjatrader.com/support/help...ltimeorder.htm

    Please let us know if you have any additional questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,

      Thank you for the reply. It is clear now that Strategy/Virtual position vs real account position. On a curious note, why does NT have a strategy position? if the trade gets a buy condition, but the operator turns the trade on an hour late, the strategy will send a market order to sync with account, but the price it gets might be different than when the trade received the condition?

      It looks like the easiest way to do this is to set the default to "wait until flat". This brings up two additional questions:

      1. is there a way to choose error handling that if the strategy get a fatal error and NT shuts down, send a market order to dump the position at shutdown.

      2. if NT shutsdown without being flat, and the operator turns NT back on, what is the state of that position? Should the operator just exit via super DOM, then turn the strategy back on via "wait until flat"?

      Comment


        #4
        Hello Boonfly8,

        NinjaTrader maintains separate strategy positions for organization if you were to enable multiple strategies on the same account/instrument.

        if the trade gets a buy condition, but the operator turns the trade on an hour late, the strategy will send a market order to sync with account, but the price it gets might be different than when the trade received the condition?
        If the operator enables the strategy and the strategy generates a historical position (from historical data), the strategy will either cancel any live working orders from a previous run and will wait until the strategy's position becomes flat before submitting live orders (Wait Until Flat), or the strategy will attempt to match the historical trades with any live working orders from a previous run so the position can be resumed (Immediately Submit.) Synchronize Account is an additional and optional behavior that will tell the account to submit a market order to sync the account position to the strategy position.

        Wait until flat is designed to be "the least disruptive," however each start behavior is best understood as "when we are not sure the strategy/account is in the position we want," (Wait Until Flat) or if "we are sure the strategy and account are where we want it upon enable" (Immediately Submit.)

        1. is there a way to choose error handling that if the strategy get a fatal error and NT shuts down, send a market order to dump the position at shutdown.
        The default behavior for error handling is to cancel orders, close open positions, and abort the strategy. Your own error handling can be done by setting RealtimeErrorHandling to be IgnoreAllErrors, and to handle order rejections in OnOrderUpdate(). You could also override the CloseStrategy method and define your own close/abort behavior.

        RealtimeErrorHandling - https://ninjatrader.com/support/help...orhandling.htm
        OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm
        CloseStrategy() - https://ninjatrader.com/support/help...sestrategy.htm

        2. if NT shutsdown without being flat, and the operator turns NT back on, what is the state of that position? Should the operator just exit via super DOM, then turn the strategy back on via "wait until flat"?
        Depending on the NinjaScript Strategy Options (Control Center > Tools > Options > Strategies) the strategy may cancel the orders upon disable or keep them active.

        When you re-enable, those existing orders could be matched with the trades generated from historical data (Immediately Submit) or the orders will be cancelled and the strategy will wait until the historical position is closed (Wait Until Flat.)

        I have included some videos that can explain these behaviors more visually. The NinjaTrader 7 video is still relevant for the general start behavior function.

        NT8 immediately submit demo - https://www.screencast.com/t/FUCm2zymF8Cd

        NT7 Syncing Account Positions - https://www.youtube.com/watch?v=US9cKUwLMOA

        Please let us know if you have any additional questions.
        Last edited by NinjaTrader_Jim; 07-30-2018, 08:17 AM.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Aviram Y, Today, 05:29 AM
        0 responses
        2 views
        0 likes
        Last Post Aviram Y  
        Started by quantismo, 04-17-2024, 05:13 PM
        3 responses
        25 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by ScottWalsh, 04-16-2024, 04:29 PM
        7 responses
        34 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by cls71, Today, 04:45 AM
        0 responses
        6 views
        0 likes
        Last Post cls71
        by cls71
         
        Started by mjairg, 07-20-2023, 11:57 PM
        3 responses
        217 views
        1 like
        Last Post PaulMohn  
        Working...
        X