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 charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        65 views
        0 likes
        Last Post charlesugo_1  
        Started by DannyP96, 05-18-2026, 02:38 PM
        1 response
        149 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        162 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        99 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        286 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Working...
        X