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

Get List of Existing Unmanaged Orders upon Strategy Restart

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

    Get List of Existing Unmanaged Orders upon Strategy Restart

    Hello NT8 Support / All,

    Is there a way to obtain a list (collection) of all existing unmanaged orders upon startup of a strategy?

    This capability would aid in disaster recovery (i.e. NT8 "hangs" and must be restarted -- which has happened more than once). And it would also allow one strategy to "see" and potentially access a collection of orders submitted by a different strategy.

    A user case example:

    • My strategy NinjaScript submits an unmanaged entry order, and OCO bracket stop / target orders -- 3 unmanaged orders in all.
    • I disable the strategy (turn it off). The orders remain active, of course.
    • I enable the strategy again, or enable a different strategy.
    • The strategy is able to obtain a list of order objects for the unmanaged orders that already exist at the time of strategy startup.

    Based on my testing, I have observed the following after restarting the strategy:
    • Account.Orders does not return any of the existing orders that were submitted unmanaged.
    • Also, OnOrderUpdate() is not triggered for changes to any pre-existing orders after the strategy is restarted.
    • From the perspective of the restarted strategy, it is as if the previously issued unmanaged orders no longer exist. Yet there they are on the NT chart.

    Surely somewhere inside NT8 there is a collection of the unmanaged order objects that can be accessed by the strategy. However, I have not come across it in documentation.

    Thanks in advance for your assistance.

    #2
    This might help. I use unmanaged orders and this is how I find outstanding orders:

    foreach(Order order in Account.Orders)
    {
    Print(String.Format("Order in Account-> {0} - {1}", order.Name, order.OrderState));
    }
    You might want to make a unique signalName

    OrderCounter = OrderCounter + 1;
    if(OrderCounter == 100000) OrderCounter = 0;
    string signalName = "MyOrder." + Instrument + "." + OrderState + "." + OrderCounter .ToString("D5");
    I then have an array/database (initialized at start up) of these orders/positions to then do something. On each order, I save the data in a database. I also save the signalName to be able to pick up where I left off on the restart:

    post_data = "a" + account +
    "&i=" + instrument +
    "&s=" + signalName +
    "&m=" + position +
    "&q=" + quantity +
    "&p=" + price;
    "&c=" + action;

    response = do_webrequest("myphp.php", post_data); // load or retrieve to MySQL database based on desired action

    private string do_webrequest(string php_script, string parameters)
    {
    string URI = BASEURI + php_script;
    string result_response;
    //string myParameters = "param1=value1&param2=value2&param3=value3";

    using (WebClient wc = new WebClient())
    {
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString(URI, parameters);
    result_response = HtmlResult;
    }
    if(result_response == "") return "NOK";
    return result_response;
    }
    When you say:
    Account.Orders does not return any of the existing orders that were submitted unmanaged.
    Also, OnOrderUpdate() is not triggered for changes to any pre-existing orders after the strategy is restarted.

    I have found that this is not exactly true. This for only orders still sitting with IB. If the order is done, then this is not true. Not sure, perhaps I am doing something different than you. Even after a full NT8 restart. To see the "orders" that were executed and have positions open, you then need to do:

    foreach (Position pos in Account.Positions)
    {
    Print("Account: " + pos.Account.Name + " Instrument: " + instrument + " Position: " + position + " Quantity: " + pos.Quantity + " Avg Price: " + pos.AveragePrice);
    }
    As a footnote this is the State.Configure I use:

    Calculate = Calculate.OnBarClose;
    Slippage = 0;
    IsUnmanaged = true;
    RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
    DefaultQuantity = 1;
    EntryHandling = EntryHandling.AllEntries;
    ConnectionLossHandling = ConnectionLossHandling.KeepRunning;
    Last edited by Bluepoint; 01-01-2017, 02:37 AM.

    Comment


      #3
      Hello D Trader,

      To clarify, you are not looking for a list of working unmanaged orders and instead you are wanting the full trade history of filled orders and trade performance from an account, is this correct?
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Chelsea / Bluepoint,

        Thanks so much for your assistance... over a holiday too!

        I have discovered the solution to why enumeration of Account.Orders was not returning any objects for existing orders -- the code was inadvertently placed inside a Dispatcher.InvokeAsync code block. As soon as the Account.Orders code was placed outside the Dispatcher code block, it worked exactly as I would have expected (and as Bluepoint reiterated).

        If I encounter additional related issues, I will return to this thread. For now, it appears the problem is solved.

        Thanks again for your assistance.
        Last edited by D Trader; 01-01-2017, 05:45 PM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by trilliantrader, 04-18-2024, 08:16 AM
        4 responses
        18 views
        0 likes
        Last Post trilliantrader  
        Started by mgco4you, Today, 09:46 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by wzgy0920, Today, 09:53 PM
        0 responses
        9 views
        0 likes
        Last Post wzgy0920  
        Started by Rapine Heihei, Today, 08:19 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by Rapine Heihei, Today, 08:25 PM
        0 responses
        10 views
        0 likes
        Last Post Rapine Heihei  
        Working...
        X