Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time to have order executed

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

    Time to have order executed

    Hi,

    I'm checking how long it takes to an order from the entry to execution in simulation mode
    With my Pc Win 10 i5 8gb ram I've have an average time of about 200ms
    Is it a good time?
    It seems a bit low for me
    This is my code

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    using System.Diagnostics;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class TestTempiEsecuzioneordine : Strategy
    {
    bool Go= true;
    Stopwatch st = new Stopwatch();


    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"TestExecutionTime";
    Name = "Test";
    Calculate = Calculate.OnPriceChange;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = false;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.ByStrategyPosition;
    BarsRequiredToTrade = 1;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    if (IsFirstTickOfBar)
    {
    Go= true;
    }

    if (bEsegui)
    {
    if (Close[0] >= High[1])
    {
    if (Position.MarketPosition != MarketPosition.Long)
    {
    st.Reset();
    st.Start();
    EnterLong(Convert.ToInt32(DefaultQuantity), @"E");
    Go= false;
    }
    }
    else if (Close[0] <= Low[1])
    {
    if (Position.MarketPosition != MarketPosition.Short)
    {
    st.Reset();
    st.Start();
    ExitLong(Convert.ToInt32(DefaultQuantity));
    Go= false;
    }
    }
    }
    }


    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    if (order.OrderState == OrderState.Filled)
    {
    st.Stop();

    double microseconds = (1000000.0 / Stopwatch.Frequency) * st.ElapsedTicks;
    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\nj\Test.txt", true))
    {
    sw.WriteLineAsync(microseconds.ToString());
    sw.Close();
    }
    Go= true;
    }
    }
    }
    }


    #2
    Hello dieci,

    There would be no predictable or expected amount of time we can provide as the amount of time it would take to process an order depends on each system's performance, and when the order was submitted in relation to data being received on the instrument thread driving the order processing. This would depend on the CPU single threaded performance, internal motherboard speed, and latency time to the data and brokerage servers.

    This thread will remain open for any community members that would like to give their opinion.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Ok Chelsea

      If someone would make a similar test, just to have a comparison...

      Comment


        #4
        Hi dieci,

        Yes it is slow ... by design ... as it executes a long list of 'training wheels" safeguards for you. You are gaining value from that delay.

        If instead your priority instead is a "need for speed" it seems like all of these threads end with a recommendation to use "Unmanaged Orders" https://ninjatrader.com/support/help...d_approach.htm

        After tiring of NT8's built in by design data lag constantly blowing up strategies and Charttrader ATMs I switched to unmanaged and simulating OCAs on my own machine. Has been a faster and better ride.

        Chelsea has posted some great thin starter examples on how use Unmanaged Orders in strategies.

        Hedge



        Comment


          #5
          Hi dieci,

          The unmanaged examples Hedge was referring to.

          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hi,
            thanks for your answer, using your first example I made another script and tested it, the reasults are the same, about 200ms from entry to execution
            Here is my code
            What's wrong?

            #region Using declarations
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.ComponentModel.DataAnnotations;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows;
            using System.Windows.Input;
            using System.Windows.Media;
            using System.Xml.Serialization;
            using NinjaTrader.Cbi;
            using NinjaTrader.Gui;
            using NinjaTrader.Gui.Chart;
            using NinjaTrader.Gui.SuperDom;
            using NinjaTrader.Gui.Tools;
            using NinjaTrader.Data;
            using NinjaTrader.NinjaScript;
            using NinjaTrader.Core.FloatingPoint;
            using NinjaTrader.NinjaScript.Indicators;
            using NinjaTrader.NinjaScript.DrawingTools;
            using System.Diagnostics;
            #endregion

            //This namespace holds Strategies in this folder and is required. Do not change it.
            namespace NinjaTrader.NinjaScript.Strategies
            {
            public class UnmanagedOCOExecutionTime : Strategy
            {
            private Order longStopEntry, shortStopEntry;
            private string ocoString;
            Stopwatch st = new Stopwatch();
            bool Go = true;

            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"unmanaged execution time";
            Name = "UnmanagedOCOExecutionTime";
            Calculate = Calculate.OnPriceChange;
            IsExitOnSessionCloseStrategy = true;
            ExitOnSessionCloseSeconds = 30;
            IsUnmanaged = true;
            }
            }

            protected override void OnBarUpdate() { }

            private void AssignOrderToVariable(ref Order order)
            {
            if (order.Name == "longStopEntry" && longStopEntry != order)
            longStopEntry = order;

            if (order.Name == "shortStopEntry" && shortStopEntry != order)
            shortStopEntry = order;
            }

            protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, Cbi.MarketPosition marketPosition, string orderId, DateTime time)
            {

            st.Stop();

            double microseconds = (1000000.0 / Stopwatch.Frequency) * st.ElapsedTicks;
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"test.txt", true))
            {
            sw.WriteLineAsync(microseconds.ToString());
            sw.Close();
            }

            if (execution.Name == "shortStopEntry")
            {
            longStopEntry = null;
            shortStopEntry = null;
            }

            Esegui = true;
            }

            protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
            {

            if (CurrentBars[0] < 1)
            return;

            if (Go)
            {
            if (Close[0] >= High[1])
            {
            if (longStopEntry == null)
            {
            st.Reset();
            st.Start();
            ocoString = string.Format("unmanagedentryoco{0}", DateTime.Now.ToString("hhmmssffff"));
            longStopEntry = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, ocoString, "longStopEntry");
            Go = false;
            }
            }
            else if (Close[0] <= Low[1])
            {
            if (longStopEntry != null && shortStopEntry == null)
            {
            st.Reset();
            st.Start();
            ocoString = string.Format("unmanagedentryoco{0}", DateTime.Now.ToString("hhmmssffff"));
            shortStopEntry = SubmitOrderUnmanaged(0, OrderAction.SellShort, OrderType.Market, 1, 0, 0, ocoString, "shortStopEntry");
            Go = false;
            }
            }
            }
            }

            protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
            {
            AssignOrderToVariable(ref order);
            }
            }
            }

            Comment


              #7
              Executed just now.

              Comment


                #8
                Hello dieci,

                This would indicate this is how fast your specific computer and CPU is able to process orders.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by dieci View Post
                  Hi,
                  thanks for your answer, using your first example I made another script and tested it, the results are the same, about 200ms from entry to execution
                  Here is my code
                  What's wrong?
                  As expected for an OCO order.


                  Why live so slowly and so constrained?

                  Be free!

                  Run Wild and Naked!



                  ... mod the code to submit without OCO and run the test again.

                  Comment


                    #10
                    Hi everybody,
                    made the changes hedgeplay suggested so my entry is

                    longStopEntry = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "", "longStopEntry");

                    nothing happens.

                    So with my infrastructure 200ms is the best I can achive I suppose, or I can do something else?

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by DJ888, 04-16-2024, 06:09 PM
                    6 responses
                    18 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by Jon17, Today, 04:33 PM
                    0 responses
                    1 view
                    0 likes
                    Last Post Jon17
                    by Jon17
                     
                    Started by Javierw.ok, Today, 04:12 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post Javierw.ok  
                    Started by timmbbo, Today, 08:59 AM
                    2 responses
                    10 views
                    0 likes
                    Last Post bltdavid  
                    Started by alifarahani, Today, 09:40 AM
                    6 responses
                    41 views
                    0 likes
                    Last Post alifarahani  
                    Working...
                    X