Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsSinceEntryExecution() returns -1 even when market position is not flat

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

    BarsSinceEntryExecution() returns -1 even when market position is not flat

    I have the following code inside the OnBarUpdate() method

    if (PositionAccount.MarketPosition != MarketPosition.Flat)
    {
    Print("Bars since execution = " + BarsSinceEntryExecution());
    }

    Which gives the following in the output window.
    Bars since execution = -1

    Apparently -1 means there is no order, and yet -- there must be otherwise PositionAccount.Market would be equal to MarketPosition.Flat.

    What am I missing?

    Another clue is the trade window thinks I am flat too, though I see the entry on the chart.

    Complete code attached. See line 167.

    Thanks for the help!
    Renaud
    ​​
    Attached Files

    #2
    Hello Renaud,

    Thanks for your post.

    I have created a copy of the SampleMACrossover strategy that comes default with NinjaTrader and modified it to check if (IsFirstTickOfBar) and if (Position.Market != MarketPosition.Flat) and prints out BarsSinceEntryExecution() within that condition.

    When running the modified sample script, I do see BarsSinceEntryExecution() printing out the correct values in the NinjaScript Output window. See the attached image.

    I also attached the modified sample for you to test on your end.

    Note that the entry order must have been submitted by that specific instance of the strategy for BarsSinceEntryExecution() to print out the number of bars since the last entry execution made by the strategy. Strategies cannot see orders that are placed by other strategy instances or orders that were manually placed.

    "Another clue is the trade window thinks I am flat too, though I see the entry on the chart."

    Is the order on the chart placed by that specific strategy instance? Was the order submitted by a different strategy instance? Or, was the order placed manually from and order entry window such as Chart Trader?

    Does the Position column for that strategy in the Strategies tab of the Control Center show that you are in a position, such as reporting 1 Long or 1 Short?

    Please send me a screenshot demonstrating what you are referring to so that I may look into this further.
    • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
    • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.
    Attached Files
    <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


      #3
      Well I'm not sure when "a specific instance of the strategy" is created and destroyed. I assume this happens when I check or uncheck "Enabled" in the Strategies/Properties window and hit "Apply." Is that correct? If so there is only one instance running at a time.

      Your code in SampleMAMod seems very similar to mine; I'm not sure what the difference is.

      Regarding the position column showing flat with an order appearing on the chart; that was a few iterations ago and the problem went away. My apologies. My workaround for the time being was to create a bool called firstBar, initializing it to true and setting it false in the first "IsFirstTickOfBar" event that has an open market position. I only needed BarsSinceEntryExecution() to tell me if it I was on the bar where the long or short entry was filled. Basically, I just want to adjust my stops and profit targets as soon as the bar closes where I entered the trade. There is probably a more elegant way to do this.

      Anyway thanks for the help Brandon. You can delete this post if you like.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      71 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      143 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      76 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      47 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      51 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X