Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stopping a Strategy

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

    Stopping a Strategy

    I used the Wizard to create a strategy that would enter long when a short moving average is above a long moving average and price pulls back to the short moving average. I want to manually enable the strategy and have it take only one trade. I would like to, when I'm ready, re-enable the strategy when I find the conditions appropriate and again have it take only one trade.
    In the variable section, I added to the Wizard code:
    private int tradeOnceOnly = 1;

    This is the code for my OnBarUpdate with the tradeOnceOnly flag added:


    if (EMA(10)[0] > EMA(20)[0]
    && GetCurrentAsk() <= EMA(
    10)[0]
    && tradeOnceOnly ==
    1)
    {
    EnterLong(DefaultQuantity,
    "");
    tradeOnceOnly =
    2;
    }

    I thought that after the trade, it would stop trading because tradeOnceOnly went to 2. I could then turn the strategy off and when I was ready to trade again turn it on, but what's happening is that now that I've added the flag no trade is being taken. I ran a print statement on the flag and it starts for a few ticks at 1 and then goes to 2 although no trade has been taken.
    Please bear in mind that I'm not looking to have only 1 active trade but to stop trading after my first trade until I turn the strategy off and then on again.
    If anyone could help me, I would be most appreciaite.

    #2
    Hello,

    Thanks for your note.

    Whats occuring here is that all NinjaScript strategies run on historical data first. So the first trade in the chart it would have taken it is incrementing the value and not taking any other trades.

    So what you need to add is a check so that this strategy runs live only.

    For example

    if(Historical)
    return;


    Add this to the top of OnBarUpdate().

    Let me know if I can be of further assistance.
    BrettNinjaTrader Product Management

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    633 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    364 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    105 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    567 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    568 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X