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 cmoran13, Yesterday, 01:02 PM
        0 responses
        29 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        21 views
        0 likes
        Last Post PaulMohn  
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        160 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        95 views
        1 like
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        148 views
        2 likes
        Last Post CaptainJack  
        Working...
        X