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

Wait until flat before executing live

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

    Wait until flat before executing live

    Hi there,

    Is it possible to configure "Wait until flat before executing live" on a strategy by strategy basis ?

    An API call during Initialize() or OnStartUp() would be perfect.

    Thanks in advance
    Dominique

    #2
    Hi Dominique, unfortunately this would not be possible at this time. We have this item on our feedback list in product management though and I've made sure to add a vote for your request here in.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand,

      For now I am using the global setting through Options / Strategies / NinjaScript.

      I am on MarketReplay, and my strategy restores during OnStartUp() all open orders as of the prior OnTermination().

      Problem is, some of these orders get "executed" as Ninja progresses through the Historical part of bars.

      I am sure there are pros & cons for that behavior, which I won't challenge (for a change - ha!), so I am rather trying to submit these orders as soon as Ninja is done with going through Historical bars, but before the 1st live bar (think about starting the strategy in advance on Sunday afternoon, I want it to resubmit stop/target orders immediately, not at 6:01pm EST).

      So my question here is, how do I know when Ninja is calling OnBarUpdate() for the last Historical bar ?

      Thanks in advance
      Dominique

      Comment


        #4
        Hi Dominique, you can check this condition for example via CurrentBar and Count...

        if (CurrentBar == Count -2) // we're on last historical provided COBC == true.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Hi Bertrand,

          Thanks, this works good to identify the last Historical bar.

          When I restore my open orders on that last Historical bar, I can see them going PendingSubmit / Accepted / Working by tracing them in OnOrderUpdate(), however they don't show-up in the ControlCenter Orders list, so I suppose they are not treated by Ninja as real orders ?

          What should I do to get these "restored" orders treated as real orders?

          Thanks in advance
          Dominique

          Comment


            #6
            Hi Dominique, this is still all in WaitUntilFlat mode now, correct?
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Hi Bertrand,

              No, it is with "Immediately submit live historical orders".

              Cheers
              Dominique

              Comment


                #8
                Thanks Dominique, all scenarios you could face with the settings to start a script for live trading would be covered here - http://www.ninjatrader.com/support/h..._positions.htm

                Would there be then a historical strategy position those orders needed to bracket?

                So you main issue is not the restoring of those orders, but the point at which they are restored? In other words you want to force another timing for this process?
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  I want to restore the orders before the market starts trading, as some brokers reject stop orders which stop price is exceeded (ZenFire for example).

                  I was testing on MarketReplay, using "Immediately submit live historical orders", and the orders were processed according to OnOrderUpdate, but did not show-up in the Control Center list of orders.

                  Comment


                    #10
                    I just re-did the exact same testing, with exact same results.

                    This is either a bug specific to MarketReplay, or there is something that I am missing ...

                    Comment


                      #11
                      Just tested on Sim101, same thing.

                      Comment


                        #12
                        Hi Bertrand,

                        Here is what I just tested directly on my IB sim account, using "Immediately submit live historical orders". The orders are processed according to OnOrderUpdate(), however they are not sent for execution.

                        Can you test it and find what's wrong ?

                        Thanks in advance
                        Dominique




                        using System;
                        using System.Globalization;
                        using System.ComponentModel;
                        using System.Diagnostics;
                        using System.Drawing;
                        using System.Drawing.Drawing2D;
                        using System.Xml.Serialization;
                        using System.Collections.Generic;
                        using System.IO;
                        using NinjaTrader.Cbi;
                        using NinjaTrader.Data;
                        using NinjaTrader.Indicator;
                        using NinjaTrader.Gui.Chart;
                        using NinjaTrader.Strategy;
                        using NinjaTrader.DND;


                        namespace NinjaTrader.Strategy
                        {
                        [Description("TEST")]
                        public class TestRestoreOrders : Strategy
                        {

                        private int _TradeManagerTF=0;

                        protected override void Initialize()
                        {

                        ((Strategy) this).Print(this.ToString() + " => Initialize()");

                        // 1-sec TimeFrame for Trade Management
                        // ====================================
                        if (_TradeManagerTF != 0)
                        {
                        Add( PeriodType.Second, 1);
                        _TradeManagerTF = 1;
                        ((Strategy) this).Print("_TradeManagerTF " + _TradeManagerTF );
                        }


                        // Misc
                        // ====
                        BarsRequired = 0; // process strategy from very 1st bar (CurrentBar == 0)
                        ExitOnClose = false;
                        Slippage = 1;

                        RealtimeErrorHandling = RealtimeErrorHandling.TakeNoAction;
                        Unmanaged = true;

                        TraceOrders = true;

                        ((Strategy) this).Print(this.ToString() + " => Initialize() complete");
                        }


                        protected override void OnBarUpdate()
                        {

                        if (Historical && (BarsInProgress == 0) && (CurrentBar == Count-2))
                        {
                        Print(" ================================================== ================================================== ======= ");

                        IOrder order = SubmitOrder( _TradeManagerTF, OrderAction.BuyToCover, OrderType.Stop, 1, 0, Close[0]+100*TickSize, "", "RestoreOrder" );
                        Print("Order submitted : " + order.ToString() );

                        Print(" ================================================== ================================================== ======= ");
                        }

                        } // OnBarUpdate();


                        protected override void OnOrderUpdate(IOrder order)
                        {
                        Print("OnOrderUpdate() : " + order.ToString());

                        }

                        }
                        }

                        Comment


                          #13
                          Hello Dominique,

                          I think I have found the cause of the issue.

                          The order you are submitting is a BuyToCover. This is an exit order, and there is no position to exit.

                          I recommend you change OrderAction.BuyToCover to OrderAction.Buy.


                          Let me know if that doesn't correct things for you.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Bertrand,

                            This is simulating a scenario where the strategy was short, then did a reload historical data which also restarts it.

                            I currently manage the strategy position completely separate from Ninja (I totally ignore Position), the strategy knows (from what it reads from its position-status file) that it is short, hence the buy to cover. Are you saying the strategy must send a sell signal prior to the buy to cover, so that this one is accepted? It works just fine like it is, when the buy to cover is sent on a live bar....

                            Cheers
                            Dominique

                            Comment


                              #15
                              Hi Dominique,

                              If you are in a short position the buy to cover order will exit your position.

                              However, I am not seeing anything in the code you have provided that puts the strategy in a short position.

                              I recommend you print the strategy position just before the order.

                              For example:

                              Print(Position.MarketPosition.ToString());


                              Just as a tip, I changed OrderAction.BuyToCover to OrderAction.Buy.
                              After doing this I ran the strategy using the Market Replay on the CL 06-13.
                              The order does show in the orders and the executions tab of the Control Center.
                              This means there are no issues with the Market Replay. Orders made in the Market Replay do show up in the Control Center. The issue is with the logic of the code.


                              Please let me know if I can be of any further assistance.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jxs_xrj, 01-12-2020, 09:49 AM
                              6 responses
                              3,290 views
                              1 like
                              Last Post jgualdronc  
                              Started by Touch-Ups, Today, 10:36 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post Touch-Ups  
                              Started by geddyisodin, 04-25-2024, 05:20 AM
                              11 responses
                              62 views
                              0 likes
                              Last Post halgo_boulder  
                              Started by Option Whisperer, Today, 09:55 AM
                              0 responses
                              8 views
                              0 likes
                              Last Post Option Whisperer  
                              Started by halgo_boulder, 04-20-2024, 08:44 AM
                              2 responses
                              25 views
                              0 likes
                              Last Post halgo_boulder  
                              Working...
                              X