Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to check if current position is not set instrument

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

    How to check if current position is not set instrument

    Hi
    I currently have following statement for checking there is no other position is opened before i enter a trade.
    Position.MarketPosition == MarketPosition.Flat

    However if i place same strategy on different instrument lets say i trade MNQ and 6B. I want both instruments act independently and be able to trade at the same time if setup occurs.
    So i cant be using Position.MarketPosition == MarketPosition.Flat because it will cancel out another instrument if there is a valid setup.

    How can i allow to trade on both instruments?

    #2
    When a strategy executes, it is wholly independent and doesn't know
    a darn thing about any other strategy, past, present or future, running
    or not. The Position values for each strategy will always refer only to
    themselves, it will never include status from other running strategies.

    Even if it is the same strategy, started twice so you have two instances,
    doesn't matter -- these instances won't know or overlap with each other,
    everything is kept separate, including the Position.

    Comment


      #3
      Hello tkaboris,

      Thanks for your notes.

      If you have one instance of the strategy enabled on say the MNQ instrument and another instance of the strategy enabled on the 6B instrument, the strategies would not see each other's strategy position. A Strategy Position is a position that is created by the entry and exit executions generated by a strategy and is independent of any other running strategy’s position.

      This means that Position.MarketPosition == MarketPosition.Flat could be used in the strategy to check if the strategy position of that specific strategy instance is flat.

      Note that strategies cannot see orders placed by other strategy instances. This means that the strategy enable on MNQ would have its own separate strategy position.

      See the help guide documentation below for more information.

      Strategy Position vs Account Position: https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?strategy_position_vs _account_p.htm
      Position.MarketPosition: https://ninjatrader.com/support/help...osition​
      <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

      Comment


        #4
        Thank you for your answer

        Comment


          #5
          Ok, what about if I have two strategies within the same strategy
          strategy 1 - for long
          strategy 2 - for long

          If strategy 1 is already long, how can i make sure if strategy 2 gets a valid signal will enter long too? (only long not short)

          EntriesPerDirection = 2;
          && (Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Long)

          Strangly with above logic i get multiple output for longs ..

          Code:
          if(TrendTrade && TrendTradeLong) {
          .........
          }
          if (ScalpTrade && ScalpTradeLong
                          && (Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Long)
                          && (BarsSinceExitExecution(0,"SLL",0) > 1 || BarsSinceExitExecution(0,"SLL",0) == -1)
                          && (BarsSinceExitExecution(0,"PTL",0) > 1 || BarsSinceExitExecution(0,"PTL",0) == -1)
          
                          && ((UseTimeFilter) ? ((Time[1].TimeOfDay >= Start.TimeOfDay) && (Time[1].TimeOfDay <= End.TimeOfDay)) : Close[0] > 0)
                          && !((ToTime(Time[0]) >= 92500 && ToTime(Time[0]) < 94500) || (ToTime(Time[0]) >= 82500 && ToTime(Time[0]) < 83500))
                          && (currentCount < X) //Has less than the amount of max trades specified
                          && (dailyPnL > -DailyLossLimit) //Loss remains 'above' limit
                          && (dailyPnL < DailyProfitLimit))        
                          {
                              EnterLong(1,PositionSize, "STL");
                              Print("===============================");
                              Print("Entering Scalp Trade Long: "  + " " + Time[1] + "- " + CurrentBar);
                              ScalpLong = true;
                          }​
          Entering Scalp Trade Long: 6/13/2023 11:53:19 AM- 3553
          ===============================
          Entering Scalp Trade Long: 6/13/2023 11:53:19 AM- 3553
          ===============================
          Entering Scalp Trade Long: 6/13/2023 11:53:19 AM- 3553
          ===============================
          Entering Scalp Trade Long: 6/13/2023 11:53:19 AM- 3553
          ===============================
          Entering Scalp Trade Long: 6/13/2023 11:53:19 AM- 3553
          ===============================
          Entering Scalp Trade Long: 6/13/2023 11:53:19 AM- 3553
          ===============================
          Entering Scalp Trade Long: 6/13/2023 11:53:19 AM- 3553
          ===============================
          Entering Scalp Trade Long: 6/13/2023 11:53:19 AM- 3553
          ===============================
          Entering Scalp Trade Long: 6/13/2023 11:53:19 AM- 3553
          ===============================
          Entering Scalp Trade Long: 6/13/2023 11:53:19 AM- 3553
          ===============================
          Entering Scalp Trade Long: 6/13/2023 11:53:19 AM- 3553
          ===============================
          Entering Scalp Trade Long: 6/13/2023 11:53:54 AM- 3554
          Current PNL Realized >> 62.08 << 6/13/2023 11:54:59 AM
          Cumulative PNL >> 0 << 6/13/2023 11:54:59 AM
          Daily PNL >> 62.08 << 6/13/2023 11:54:59 AM​
          Last edited by tkaboris; 08-08-2023, 05:42 AM.

          Comment


            #6
            Hello tkaboris,

            Thanks for your notes.

            If only a single strategy is being programmed and enabled then you would need to make sure that you are only checking if Position.MarketPosition == MarketPosition.Flat. This checks to see if the strategy position of the enabled strategy is in a flat position.

            Currently, you are checking if you are in a flat position or if you are in a long position.

            From the code you shared: (Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Long)

            This means that if you are in a flat position or in a long position, orders would be placed by the strategy

            See this help guide for more information about Position.MarketPosition: https://ninjatrader.com/support/help...etposition.htm
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              ok but why do i have multiple longs prints?

              Comment


                #8
                Hello tkaboris,

                Thanks for your notes.

                Since the condition is true, your Print statements in the condition are called and are output in the NinjaScript Output window.

                If you are using Calculate.OnEachTick for your strategy and the condition to call the Print statement is true, the prints would occur for each incoming tick that processes as long as the condition remains true.

                If you use Calculate.OnPriceChange for your strategy and the condition to call the Print statement is true, the prints would occur for each change in price that occurs as long as the condition remains true.

                Calculate: https://ninjatrader.com/support/help.../calculate.htm

                Or, if you have multiple data series added to the script and you are not calling this code within a BarsInProgress == 0 condition, the print could be appearing when each data series is processing.

                BarsInProgress: https://ninjatrader.com/support/help...inprogress.htm
                Working with Multi-timeframe/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm

                You could confirm your conditions are becoming true by adding prints one line above the position that prints out all the values being used in the condition in monitoring the NinjaScript Output window.

                Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                https://ninjatrader.com/support/foru...121#post791121
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #9
                  well its weird. I have two strategies TrendTrade and ScalpTrade,
                  TrendTrade never prints multiples but scalptrade does. Strategy on on price change. The print is only after entering long. So I have mulitple prints for long but entry only once..

                  Comment


                    #10
                    Hello tkaboris,

                    Thanks for your notes.

                    I suggest adding debugging prints to the script to understand exactly how your custom strategy logic is behaving.

                    In the strategy, add prints to the script one line above the condition that print out the condition values being used to call the Print statement in question to see if those conditions are becoming true. And, print out the BarsInProgress in the current print you shared to see what BarsInProgress is processing when the print occurs.

                    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                    https://ninjatrader.com/support/foru...121#post791121
                    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Today, 05:17 AM
                    0 responses
                    50 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    126 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    69 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    42 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    46 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X