Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trying to set up a crossover strategy with a single execution per crossover

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

    Trying to set up a crossover strategy with a single execution per crossover

    When creating a cross-over, the system processes the first trade when the condition is met, but then continues to process further trades as long as the condition is met. What i'd like to do is
    allow ONLY one trade, and that trade being the trade immediately following the crossover. Per the instruction of NinjaTrader, I added code as follows:

    This however results in an error code "The name savedBar does not exist in the current context" Can anyone help with this .

    // Set 1
    if (CrossAbove(HMA1, HMA2, 1)&& CurrentBar!=savedBar)
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");

    savedBar = CurrentBar; //save the entry bar number
    }

    // Set 2
    if (CrossBelow(HMA1, HMA2, 1)&& CurrentBar!=savedBar)
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), "");

    savedBar=CurrentBar; //save the entry bar number

    #2
    Hello sjbeaman,

    Thanks for your post and welcome to the NinjaTrader forums!

    You would need to declare savedBar as an int variable type. You can do this above OnStateChange() like: private int savedBar;
    If you are using the strategy builder, you would need to create the variable in the inputs and variables screen, please see "Understanding the Inputs and Variables screen" https://ninjatrader.com/support/help...er_screens.htm

    CurrentBar is a system variable that provided the bar number that the system is strategy is currently processing its code on. Reference: https://ninjatrader.com/support/help...currentbar.htm

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    558 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X