Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy - Managing interruptions

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

    Strategy - Managing interruptions

    I have 2 scenarios that recently happened that I would like to solve for:

    1) Computer Windows started updating while Strategy was running
    2) My Broker system never "accepted" a stop order after opening the position - it was accepted in the demo at the same time but not in Live account

    In both scenarios my strategy could not manage any manual intervention after the fact or just update properly on the next bar update.

    - Scenario 1 - I wanted to add a manual position and then have the Strategy take over but it cannot take over on manual order positions.
    - Scenario 2 - On the next Bar Update it should have recognized the Short Stop conditions were present and added the Short stop. I added a Short Stop myself but since strategy was still running when a new long order conditions came in I ended up with double long position - one to close the order I already closed with manual Short stop and one to for the new position - a long overfill.

    The main problem I believe is this code:

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    StartBehavior = StartBehavior.WaitUntilFlat;
    }
    }

    It should be this I believe (please confirm):

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    IsAdoptAccountPositionAware = true;
    StartBehavior =StartBehavior.AdoptAccountPosition
    }
    }

    I do believe I need more than just this. I have 10 Unique Order entries per side and is not just one type.

    - Scenario 1 - If I enter a manual order since the initial entry was missed....I believe I need code to manually match it to one of my 10 Unique orders (or create a new "Long11ManualOrder" to match it to)
    - Scenario 2 - Since the system added the initial entry and identified it, I would think it the Strategy would know which of the Unique Orders was used. BUT I am not sure if the change above in green alone will be enough for this Scenario for the Strategy to match it to the order type and take over.

    Please advise how best to handle each scenario?

    -------------------------------------------------------------------------------
    A Sample snippet of my unique Order names - this is just for context so it makes it easier to understand:

    protected override void OnBarUpdate()
    {
    if (High[0] > Position.AveragePrice * 1.0014)
    {
    ExitLongStopMarket(0, true, QuantitySize, Position.AveragePrice - MaxLongStopPoints * 4 * TickSize, "LongStops1", "Long1");
    ExitLongStopMarket(0, true, QuantitySize, Position.AveragePrice - MaxLongStopPoints * 4 * TickSize, "LongStops2", "Long2Reverse");
    }
    //8 more Stops not being shown (Long3, Long4, Long 5, etc)
    }

    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 nativeError)
    {
    //Assign Stop Orders
    if (order.Name == "LongStops1" && order != StopLongOrder1)
    {
    StopLongOrder1 = order;
    }
    if (order.Name == "LongStops2" && order != StopLongOrder2)
    {
    StopLongOrder2 = order;
    }
    if (order.Name == "LongStops3" && order != StopLongOrder3)
    {
    StopLongOrder3 = order;
    }
    if (order.Name == "LongStops4" && order != StopLongOrder4)
    {
    StopLongOrder4 = order;
    }
    //6 more not being shown
    }

    protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, Cbi.MarketPosition marketPosition, string orderId, DateTime time)
    {
    //Long Targets and Initial Stops
    if (execution.Order.Name == "Long1" && execution.Order.OrderState == OrderState.Filled)
    {
    ExitLongStopMarket(0, true, QuantitySize, BigT_Auto_Equilibrium_V21.PriorS1[0], "LongStops1", "Long1");
    ExitLongMIT(0, true, QuantitySize, BigT_Auto_Equilibrium_V21.PriorR3[0] + TargetBoost * 4 * TickSize, "LongTarget", "Long1");
    }
    if (execution.Order.Name == "Long2Reverse" && execution.Order.OrderState == OrderState.Filled)
    {
    ExitLongStopMarket(0, true, QuantitySize, BigT_Auto_Equilibrium_V21.PriorS1[0], "LongStops2", "Long2Reverse");
    ExitLongMIT(0, true, QuantitySize, BigT_Auto_Equilibrium_V21.PriorR3[0] + TargetBoost * 4 * TickSize, "LongTarget", "Long2Reverse");
    }
    //8 more not being shown
    }
    Last edited by BigT4X; 09-16-2022, 09:53 AM.

    #2
    Hi, thanks for posting. The Position is simply a number representing how many contracts long or short an account is in. When you adopt the account position, you are inheriting the value from the account e.g. you can adopt 4 long or 5 short, what your strategy does to manage each individual contract after the start up is up to your code/ your strategy. The strategy will not sort each entry into individual entry orders not made by the strategy. There is the GetRealtimeOrder() method, which will transfer historical order references made by the strategy to real time order references, but this is only for entries made by the strategy. If you want to enter x mount of contracts manually, then use AdoptAccountPosition to have the strategy adopt that positions this is fine, your strategy can then set as many exit orders as it wants to manage each individual contract.

    Kind regards,
    -ChrisL

    Comment


      #3
      For Scenario 1.....if I have to add a manual order (ideally I don't want to)....can you provide a reference sample or suggestion how I would match the manual order to an order name to apply the stop logic? so that the strategy can manage it.

      Example: Say I want to always match any rare manual orders placed to either Long1 or Short1 order names (to keep it simple)?

      For Scenario 2....do you believe that adding "AdoptAccountPosition" will recognize the position that the Strategy itself made (it was NOT a manual order) and synch it back up with the Stop logic....even though it is missed the stop entry initially? Or do I need to do something with "GetRealtimeOrder()" to re-assign it again to get it managing the order again? And if so, do you have a reference sample or suggestion how that would look like?

      I am trying to avoid constant monitoring by myself to make sure its still working. This issue happened in the middle of the night.

      NinjaTrader_ChelseaB

      Comment


        #4
        Hi BigT4X, thanks for the follow up.

        GetRealTimeOrder() is only used for resting orders, not orders that have been filled already and it would also not be possible to set a resting order manually and associate that with the strategy. The resting order needs to originate from the strategy. You do not need to associate the open position with particular entry orders, all the strategy needs to do is set up exit orders that will accommodate for the adopted position size i.e. set up 3 exit orders if there is a 3 long position update.

        Code:
        protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
        {
          int positionCounter = 0;
          if(position.MarketPosition == MarketPosition.Long)
          {
            while(positionCounter < quantity)
            {
              ExitLongStopMarket(0, true, 1, averagePrice - TickSize*(5*positionCounter+1), "Stop"+positionCounter, "");
              positionCounter++;
            }
          }
        }


        Kind regards,
        -ChrisL
        ​​​​​​​

        Comment


          #5
          Sorry but you lost me.

          So Scenario 1 is NOT possible. If I enter a manual order that fills....then turn on the Strategy.....there is no way for the Strategy to take over and handle the rest of the trade (stops and targets)? AdoptAccountPosition does not do this? Any Manual order that fills I am on my own with manual stops and targets until it ends/closes? And GetRealTimeOrder() is for open orders not filled ones. Please confirm this is what you meant above.

          For Scenario 2 it IS possible using AdoptAccountPosition for a missed Stop/Target order (did not give status "accepted" by the broker) to get apply the next bar. This is because the original order originated in the Strategy and the Stop and Target will get added on the next Bar Update (assuming logic still applies).....because I am using AdoptAccountPosition. I just need to manage Position size? (Note: I do already use a "QuantitySize" Variable on the orders)

          Do I need any other logic for Scenario # 2? or is there an alternative to your PositionSize code and what if I am just using 1 contract at all times?
          NinjaTrader_ChelseaB

          Comment


            #6
            This Scenario 2 issue happened again with AdoptAccountPosition on. I can live with Scenario 1....but I need to resolve Scenario 2.

            For some silly reason on the fill of the new/reverse order.....its not triggering the Stop and Target orders all the time. It works perfectly fine in backtest and in live Demo. AND sometimes with a live account (Advantage Brokers). Now twice it has NOT worked. All the updating of orders after that depends on the initial OCO orders being placed.

            Could this be because of Multi-threading? I have turned that off now and hoping that will allow me to get the "Filled" message back in sequence to trigger the OCO orders. There are no errors showing in the logs.....the new OCO orders just don't happen....SOMETIMES. Which leads me to believe that I am not always getting a "filled" message in proper sequence.

            Code that is not triggering:
            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)

            //TURNED THIS OFF --protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, Cbi.MarketPosition marketPosition, string orderId, DateTime time)
            {
            //Long Targets and Initial Stops

            if (execution.Order.Name == "Long2Reverse" && execution.Order.OrderState == OrderState.Filled)
            {

            ExitLongStopMarket(0, true, QuantitySize, BigT_Auto_Equilibrium_V21.PriorS1[0], "LongStops2", "Long2Reverse"); //use to be S1M
            ExitLongMIT(0, true, QuantitySize, BigT_Auto_Equilibrium_V21.PriorR3[0] + TargetBoost * 4 * TickSize, "LongTarget", "Long2Reverse");

            }
            }

            UPDATE: Even with multi-threading turned off.....it happened again - a third time. It works in live demo without issue. Its not sending the above Target and Stop on a Fill all the time. Looking for suggestions.
            Last edited by BigT4X; 09-19-2022, 09:49 AM.

            Comment


              #7
              Attached are logs.

              There are 2 examples highlighted in these logs (CSV attached). You will see it does not always happen in the Live account.....and never happens in the Live Demo account. NinjaTrader Grid 2022-09-19 01-22 PM.csv

              - MNQ is Live account (Advantage Broker through Ninja)
              - NQ is Live Demo account (just Ninja)

              1) 22:30 last night....was currently Long MNQ and NQ.
              - Strategy triggers to go Short (Short2Reverse).
              - On the Live Demo (NQ)....it fills and then adds a Stop and Target order as expected (see code in post above)
              - On the Live account (MNQ) there are no errors. BUT somehow the Filled Order State does not come OR comes in the wrong order (sorry cannot tell) and thus no Stop and Target order is added.

              2) 11:45 AM this morning.....
              - Strategy triggers to go Short (Short2Reverse).
              - On the Live Demo (NQ)....it fills and then adds a Stop and Target order as expected (see code in post above)
              - On the Live account (MNQ) there are no errors. BUT somehow the Filled Order State does not come OR comes in the wrong order (sorry cannot tell) and thus no Stop and Target order is added.

              This is not reproduceable in the Demo. I am trying to figure out what is happening but I am sorry I cannot tell with 100% certainty. AND most importantly I would like to figure out how to fix this. Do I need to add an order state to check for? Do I need to put Initial Stop and Target in PositionUpdate?

              Update: Logs and email Sent to PlatformSupport. Case # 03277056
              Last edited by BigT4X; 09-19-2022, 12:22 PM.

              Comment


                #8
                Hi BigT4X, thanks for the follow up.

                1. Q: There is no way for the Strategy to take over and handle the rest of the trade (stops and targets)? AdoptAccountPosition does not do this"

                A. Yes, you can do this. My example in the last post shows how to set up stops and target based on the currently open position quantity. Since the strategy did not make the order, you can not use the Signal Name to identify the order, so you have to use the open quantity.

                2. Q: On Scenario 2

                A: Working orders can not exist on the account before starting a strategy that uses AdoptAccountPosition, If your strategy took the existing position, use the Immediately Submit startup behavior and the strategy will match the open orders, meaning that if the strategy took the position and submitted orders in the past before it was disabled, it will recognize these orders and re-submit the stop and target to the Strategy Position to match the Account Position. The behavior of this stop order does not seem normal. Is the order never reaching and going past the "Accepted " State? You can check any order's state in OnOrderUpdate The strategy can react to and handle each state an order goes through. I will have a look at the log files that you sent in and check for the filled status on your live account and will follow up here when finished.

                Kind regards,
                -ChrisL




                Last edited by NinjaTrader_ChrisL; 09-20-2022, 06:27 AM. Reason: Correction on working orders

                Comment


                  #9
                  Thanks for the reply.

                  Regarding # 1 - I need to use Signal name since I have 10 various signal types per side. I am not sure how I can have both signal name when it is known and use quantity on a manual order when signal name is not known....should I want to do that. It's Issue # 2 for the most part is driving this need.

                  Regards # 2 (my biggest concern). I have sent the Trace and Log files for review. I can certainly turn on "Immediately Submit" and code it that way if that will solve my issue. This issue of missing "Fill Order state" which drives the Code to submit the Stop and Target orders has already cost me money and sleep and is limiting it from being a true auto trader Strategy. Happy to send my code over email if needed as well. I will wait for a response to my trace files and logs first before turning this on.

                  Comment


                    #10
                    Hi BigT4X, I saw your email and I responded. I need to know the exact time stamp of when this happened as well as the associated order ID. I had a look at the log files you sent in and I could not find an order that missed the fill state.

                    Comment


                      #11
                      I have replied with a Detailed Spreadsheet and Email. Take a look there. There are a number of things that could be causing this issue but I am no expert in reading the Trace and Log files.

                      Please let me know if there is anything I can do to handle this behaviour?
                      Last edited by BigT4X; 09-21-2022, 02:40 PM.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Yesterday, 05:17 AM
                      0 responses
                      59 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      134 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      75 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      45 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      50 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X