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

No Entry If Account Position Is >0

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

    No Entry If Account Position Is >0

    I want to have such a scheme:
    1) Use one account.
    2) Load several swing trading strategies on both MES/MNQ using that same one account.
    3) When one gets into a position, others are not entering into their positions, despite of the signal.

    My idea of achieving it was to check whether unrealized profit/loss is not equal to 0, before EnterLong/EnterShort:
    Code:
    if (State == State.Realtime && Account.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar) != 0)
    So questions are 2:
    1) Is it correct formula, meaning that current strategy may not have a position, but I run multiple strategies on the ACCOUNT, and ACCOUNT may have a position from different strategy? (I suppose so, but testing real time is a bit more complicated than on historical data)
    2) What happens, if Ninja crashes/disconnects/updates and I need to restart it and re-enable strategy. Would it synchronize correctly, since there would be UnrealizedProfitLoss, which, theoretically, could prevent from submitting position?

    #2
    Hello UltraNIX,

    Thanks for your post.

    Something you could do is consider checking PositionAccount with the Unmanaged approach to check the account position instead of the strategy position. Note that you cannot Exit positions larger than the strategy position, so the Managed approach may not be ideal.

    See the help guide documentation below for more information.
    PositionAccount: https://ninjatrader.com/support/help...ionaccount.htm
    Unmanaged Approach: https://ninjatrader.com/support/help...d_approach.htm

    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.)

    A Strategy Position is a virtual position that is created by the entry and exit executions generated by a strategy and is independent from any other running strategy’s position or an Account Position.

    An Account Position is a position you actually hold in a real-time trading account, whether it is a NinjaTrader internal simulation account (Sim101) or your live real-money brokerage account.

    See this help guide for more information about strategy position vs account position: https://ninjatrader.com/support/help..._account_p.htm

    When trading the same instrument on the same account, the performance of each trade on the Account Performance tab of the Control Center would match in the order the orders are received.

    The position for the account and the position for the strategy are separate. It is possible for the position of a strategy to be out of sync with the position of an account.

    Using two NinjaScript Strategies that are placing orders in opposite directions would also be affected by this. The Strategy Position is separate from the Account Position. This means that if one strategy has taken a long position and is showing long 1, and the other has taken a short position and is showing short 1, the account position would be flat and neither strategy would be in sync with the account.

    See this forum thread for more detailed information about running multiple strategies: https://ninjatrader.com/support/foru...tures-contract

    Re-enabling with ImmediatelySubmit would process historical data to determine strategy position. Checking account level items is only relevant when the strategy is running in real-time. Therefore, it would not be possible to re-enable strategies that use PositionAccount for positioning with ImmediatelySubmit and get the same strategy positions. You could consider using AdoptAccountPosition to capture an existing account position and attempt to protect it when OnStateChange > State.Realtime.

    The example attached could be referenced for using the Unmanaged approach and AdoptAccountPosition. This could be modified to use PositionAccount if tracking the real account position instead of adopting the account position on startup is desired.

    Also see the help guide documentation for more information on start behavior and OnStateChange().
    OnStateChange(): https://ninjatrader.com/support/help...tatechange.htm
    Start Behavior: https://ninjatrader.com/support/help..._positions.htm

    Let us know if we may assist further.
    Attached Files
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Is it necessary to have Unmanaged approach? I think I am used to managed.

      And I don't want to Exit anything. I want to prevent from entering a position, when current account has a position.

      Comment


        #4
        Hello UltraNIX,

        Thanks for your note.

        You could try checking PositionAccount with the Managed approach to check the account position instead of the strategy position and do something like flip a bool to control when trades are placed. For example, you could consider creating a bool (initially set to false), create your order entry conditions and flip the bool to true, check if the bool is true and call your entry order method, then check if the PositionAccount.MarketPosition != MarketPosition.Flat and flip the bool to false. This way trades are only made when the bool is true, and the bool would be false if PositionAccount is not in a flat position.

        PositionAccount: https://ninjatrader.com/support/help...ionaccount.htm
        PositionAccount.MarketPositon: https://ninjatrader.com/support/help...etposition.htm

        Let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          I am having hard time with this.

          Here's the code I'm using for prints (I run it on the 2nd bar of the day (Time[0].Date != Time[1].Date):
          Code:
          Print (Time[0] + " | AveragePrice: " + PositionAccount.AveragePrice);
          And even though it ALWAYS prints AveragePrice: 0, it enters and exits trades, but Account position is Sim101 doesn't reflect that.

          Maybe it's different with Realtime account, but I don't want to make experiments with real money for obvious reasons.

          EDIT: My issue occurs in backtesting. So maybe it doesn't work when backtesting, but maybe it does on Sim101 account, when it's on Realtime mode?
          Last edited by UltraNIX; 11-28-2021, 12:15 PM.

          Comment


            #6
            Hello UltraNIX,

            Thanks for your note.

            The Strategy Analyzer is used for backtesting a NinjaScript strategy based on user-defined parameters on a specific instrument and date range. Backtests do not place trades to specific accounts and the Strategy Analyzer does not implement an account. This is why you are seeing 0 populate for PositionAccount.AveragePrice.

            Instead of testing your strategy in the Strategy Analyzer, you could consider testing your strategy using the Playback connection with Market Replay data. The Playback connection could be used to mimic your strategy running on real-time data and you could see prints populate for PositionAccount.AveragePrice.

            See this help guide for more information about using the Playback connection: https://ninjatrader.com/support/help...connection.htm

            Let us know if we may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by mjbatts91, Yesterday, 04:48 PM
            2 responses
            23 views
            0 likes
            Last Post mjbatts91  
            Started by pibrew, Today, 06:10 PM
            1 response
            16 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by actualfacts.2021, 07-25-2021, 03:11 PM
            8 responses
            1,184 views
            0 likes
            Last Post linkcou
            by linkcou
             
            Started by reynoldsn, Today, 07:11 PM
            0 responses
            4 views
            0 likes
            Last Post reynoldsn  
            Started by needsomehelp147, 04-29-2024, 06:43 AM
            2 responses
            20 views
            0 likes
            Last Post needsomehelp147  
            Working...
            X