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

Need an advise on which Syncing Account Positions method to use

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

    Need an advise on which Syncing Account Positions method to use

    Hi team,

    I am trying to figure out which strategy sync method to use, but it seems that I need your advice here.
    I've been reading the following manual (https://ninjatrader.com/support/help..._positions.htm) a couple of times and thought I got it, but then on of my recent trades got me into a complete opposite direction. So, now I have to rethink it.

    What do I have today:
    1. I have two separate non related Strategies that I run each day at almost the same time (let's say 10:00 AM EST for strategy #1 and 10:15 AM for strategy #2).

    2. These strategies have a different logic, but they trade on similar instruments, which means that technically I could be in the same stock, which is traded as two unrelated "Strategy Positions". I could even have two identical orders entry orders placed by both strategies (rare , but happens).

    3. I do not leave positions overnight, meaning that each day I start trading when my Account Positions are flat.


    What do (not) I want to achieve:

    1. I don't want any interference between the two strategies. i.e. I don't want that a sync process of strategy #2 would somehow affect existing orders/positions that were already generated by strategy #1.

    2. I want it to submit live orders that were generated in a real time only. I want any entry orders generated, before a strategy was turned on, to be completely disregarded and not submitted retroactively.

    3. Bonus - in case a strategy turns off for some reason (e.g. tech reason) during a trading session and I turn it back, I do not want it to make any changes/affect any existing orders/traders.

    Looking for your advice in regard to which Syncing Account Positions practice would be the best fit here.

    Thanks!
    Last edited by DimaFridrich; 06-05-2020, 02:05 AM.

    #2
    Hello DimaFridrich,

    Thanks for your post.

    We would not recommend running two strategies on the same instrument/account. You can enable one strategy that is synchronized with the account, but when the other strategy takes a position, the first strategy will then be de-synchronized.

    We would suggest combining the strategy logic so it is inside one strategy so the two strategies do not compete. Another option would be to apply the strategy against separate accounts. You could talk to you broker about the possibility of adding an additional account.

    If you want to have the strategy start flat without calculating a position from historical data or submitting orders calculated from historical data, you can skip historical processing.

    3. Bonus - in case a strategy turns off for some reason (e.g. tech reason) during a trading session and I turn it back, I do not want it to make any changes/affect any existing orders/traders.
    Having the strategy resume a position would involve using ImmediatelySubmit. The strategy will need to process historical data to calculate orders and compare them to the live/working orders.

    If you want to skip historical processing, but have the strategy adopt and protect a position, you may want to look into using AdoptAccountPosition with the Unmanaged Approach. The original orders would be cancelled, but you could consider opening the same protective orders to have that position protected. I have attached an example strategy that can give some further direction.

    For another take on Syncing Account Positions, I have included some information on Start Behaviors below.

    General Start Behavior Information

    When a strategy is enabled, it processes historical data to determine trades that the strategy would have made on the data that is already on the PC/chart and to determine what position the strategy is in. (Strategy positions are separate from actual Account positions.)

    Wait Until Flat will wait until this virtual/historical position is closed before live orders can be submitted. It ensures that the strategy starts trading live from a flat position. If you enable the strategy when the account is flat, the strategy will waiting until this position calculated from historical data is closed so it is logically making trades starting from an entry signal.

    Immediately Submit automatically submits working orders from when the strategy processed historical data, and assumes the strategy position and account position are where you want it when you enable the strategy. This is typically used to have a strategy resume a position after disabling/enabling. If the strategy already had live orders running, the orders will resume with the new enablement of the strategy if they match the historically calculated orders. If the orders calculated from historical data do not match the live working orders, the live working orders will be cancelled and replaced by those calculated from historical data.

    Sync Account Positions is an additional option that has NinjaTrader submit an order to sync the account position to the position calculated by the strategy. (Not the other way around.)

    Adopt Account Position would be used if you want the strategy to inherit the Account Position on enablement.

    If you do not want the strategy to calculate a position from processing historical data. Simply add if (State == State.Historical) return; to the top of your strategy logic so historical processing is skipped. The strategy will then always start from a flat position because it has not calculated any orders.

    Strategy vs. Account Position — https://ninjatrader.com/support/help..._account_p.htm

    Start Behaviors — https://ninjatrader.com/support/help..._positions.htm


    We look forward to assisting.
    Attached Files
    Last edited by NinjaTrader_Jim; 06-05-2020, 09:45 AM.
    JimNinjaTrader Customer Service

    Comment


      #3
      Jim, thanks a lot for such a comprehensive reply
      It seems like you covered it all.

      I really like the suggestion to use (State == State.Historical) return; This in combination with "Wait Until Flat" seems to be the closest to what I'm looking for:

      1. Even if I started/restarted one of the strategies, it would have not touched any orders/trades created previously, as they happened in the past (goals #1 and 3# ---> check)
      2. Now, since "Wait Until Flat" starts to trade ones all the historical trades are closed, it would actually start submitting only new live orders generated after the start (goal # 2 --> check)

      I will also consider combining the two strategies, but this is something that would require some development and a pretty extensive restructuring of the code's logic. which I prefer not to touch now.

      Please correct me, if I am missing something here.
      Thanks
      Last edited by DimaFridrich; 06-07-2020, 01:56 AM.

      Comment


        #4
        Hi Jim,

        I'm a noob (trading, NinjaTrader) and wonder if you could be of assistance, please. As per your post above I don't want my strategy to calculate form historical data.

        "If you do not want the strategy to calculate a position from processing historical data. Simply add if (State == State.Historical) return; to the top of your strategy logic so historical processing is skipped. The strategy will then always start from a flat position because it has not calculated any orders."

        Problem I have, if I ad if (State == State.Historical) return; to the top of my strategy logic, the strategy stops submitting trades. I'm sure it is something simple. Strategy HG107Long attached for your convenience.

        I also seem to be unable to set parameters for time to trade successfully. Attached strategy HG11Long works fine, until I set a time of day for it to trade within. Strange.

        Many thanks,
        Attached Files
        Last edited by fr33b; 06-07-2020, 11:41 PM.

        Comment


          #5
          Hello DimaFridrich,

          WaitUntilFlat will still cancel live orders that were submitted by a previous run of the strategy if you disable/re-enable. To have those orders resumed, ImmediatelySubmit must be used and the strategy must calculate those same orders from historical data so it: 1. Processes historical data 2. Calculates order submissions from historical data 3. The historical orders match the live working orders.

          Since you close out positions and always start the strategy when you expect it to be flat, skipping historical processing sounds like what you are looking for.

          fr33b, welcome to the forums!

          When you call return from checking if the strategy is processing historical data, all strategy operations will be skipped when the script is processing historical data and only realtime processing will be used. We can expect that the Strategy Analyzer will not allow trades to take place because the Strategy Analyzer uses historical processing.

          You can enable the strategy on a chart when connected to a live feed or on the Playback Connection and it will process logic when the strategy has transitioned to processing realtime data. If you are not seeing trades in this context, you can take debugging steps to see why the your logic is/is not becoming true which would control how the Enter/Exit methods are called.

          When we enable the strategy when historical processing is skipped, we can see that the strategy is green when we enable it, meaning that it is waiting for the logic to submit a new order.

          I have included some debugging tips below.

          Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

          TraceOrders - https://ninjatrader.com/support/help...aceorders2.htm

          Debugging in the Strategy Builder - https://drive.google.com/file/d/1mTq...w?usp=drivesdk

          Playback Connection - https://ninjatrader.com/support/help...connection.htm

          Debugging Demo - https://drive.google.com/file/d/1rOz...w?usp=drivesdk

          We look forward to assisting.
          JimNinjaTrader Customer Service

          Comment


            #6
            Jim,
            I am wondering now what happens to my strategy logic if I decide to skip Historical data. See, I use historical data (let's say I calculate the opening price by checking the first candle when the strategy gets to the first bar of a day in historical mode and then I store it to later use for my live calculations. Wouldn't using (State == State.Historical) have an affect on the above?

            Comment


              #7
              Hello DimaFridrich,

              Correct. In that case, you could consider having the State.Historical check set a bool if the strategy should submit trades, and then protect your order submission logic with this bool. We can expect that other logic gets processed, but the specific order submission methods will be skipped, resulting in the strategy starting flat.

              You can also check for State.Realtime to flip the bool back to allow trading.

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

              Comment


                #8
                Originally posted by NinjaTrader_Jim View Post
                Hello DimaFridrich,

                Correct. In that case, you could consider having the State.Historical check set a bool if the strategy should submit trades, and then protect your order submission logic with this bool. We can expect that other logic gets processed, but the specific order submission methods will be skipped, resulting in the strategy starting flat.

                You can also check for State.Realtime to flip the bool back to allow trading.

                Let us know if you have any additional questions.
                Can you please elaborate on this more by providing a quick code/logic example?

                Thanks!

                Comment


                  #9
                  Hello DimaFridrich,

                  Please see below.

                  Code:
                  private bool AllowTrading;
                  protected override void OnStateChange()
                  {
                      if (State == State.SetDefaults)
                      {
                          ...
                      }
                      else if (State == State.Historical)
                      {
                          AllowTrading = false;
                      }
                      else if (State == State.Realtime)
                      {
                          AllowTrading = true;
                      }
                  }
                  
                  protected override void OnBarUpdate()
                  {
                      if (AllowTrading && Close[0] > Open[0])
                          EnterLong();
                  }
                  JimNinjaTrader Customer Service

                  Comment


                    #10
                    Perfectt, Thanks a lot

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by funk10101, Today, 09:43 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post funk10101  
                    Started by pkefal, 04-11-2024, 07:39 AM
                    11 responses
                    36 views
                    0 likes
                    Last Post jeronymite  
                    Started by bill2023, Yesterday, 08:51 AM
                    8 responses
                    44 views
                    0 likes
                    Last Post bill2023  
                    Started by yertle, Today, 08:38 AM
                    6 responses
                    25 views
                    0 likes
                    Last Post ryjoga
                    by ryjoga
                     
                    Started by algospoke, Yesterday, 06:40 PM
                    2 responses
                    24 views
                    0 likes
                    Last Post algospoke  
                    Working...
                    X