Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

What's wrong with my multi-instrument code?

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

    What's wrong with my multi-instrument code?

    Hi,

    I am trying out multi-instument strategy and wrote the following code.
    The strategy use MSFT 1 minute bar volume to decide when to long or short another stock,
    but when I ran the Strategy analyzer, I noticed that all the entries are executed 1 bar later than expected. Can anyone help me out?

    Thanks.


    namespace NinjaTrader.Strategy
    {
    public class test1 : Strategy
    {

    protected override void Initialize()
    {
    CalculateOnBarClose = true;
    Add("MSFT", PeriodType.Minute, 1);

    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress == 0)
    {

    // Condition set 1
    if (Volumes[1][0] > 20000
    )
    {
    EnterLongLimit(DefaultQuantity, Closes[0][0], "");
    }

    // Condition set 2
    if (Volumes[1][0] < 4000
    )
    {
    EnterShortLimit(DefaultQuantity, Closes[0][0] , "");
    }

    SetStopLoss("",CalculationMode.Ticks, 10, false);
    SetProfitTarget("",CalculationMode.Ticks, 10);

    }
    }

    #region Properties
    [Description("")]
    [Category("Parameters")]
    public int V1
    {
    get { return v1; }
    set { v1 = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Hi rp088, thanks for the post - the behavior you noticed is expected in backtesting as you would have to wait for the developing bar to close to place an order on the next bar.

    To backtest with a finer resolution, please check this reference sample - http://www.ninjatrader-support2.com/...ead.php?t=6652

    Comment


      #3
      Hi Bertrand,

      Thanks for your reply. I am having a little difficulty understanding the sample in your link, since I am not much a programmer.

      Is there a simple way to fix the problem in my code, like change or add some argument?

      Comment


        #4
        This is not an issue with your code, but with the nature of backtesting and it's implications for running your strategy. You have to wait until the bar that produces your trade entry condition closes to be sure the signal is generated. Then you can place order for the next bar to execute your signal.

        The link I posted is an advanced workaround to add a finer chart for execution to your strategy, since you can execute orders to various bars objects in NinjaTrader.

        For more background info, you can review this link - http://www.ninjatrader-support.com/H...struments.html

        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