Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT Coding Help

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

    NT Coding Help

    Hello,

    Longtime Tradestation user looking at coding up some of my stuff on NT. I'm trying to get some of the basic strategy syntax down - can someone please help with the statement below?

    Data1 = 1 min chart NQ
    Data2 = 240 min chart NQ

    If Close of Data2 > Open of Data2 (last 240 min bar was green)
    and Close of Data1> Close of Data2[1] (current 1 min close of NQ is over the close of the last 240 min bar)
    then buy this bar;

    If Close of Data2 > Open of Data2 (last 240 min bar was green)
    and Close of Data1<Close of Data2[1] (current 1 min close of NQ is UNDER the close of the last 240 min bar)
    then SELL this bar;

    Thanks so much, Brian​

    #2
    Hell Atlas_Trading,

    Welcome to the NinjaTrader forums!

    I recommend reading through this support article on Getting Started with NinjaScript:



    You could do something like:

    Code:
    //in State.Configure
    AddDataSeries("NQ 03-24", Data.BarsPeriodType.Minute, 1);
    AddDataSeries("NQ 03-24", Data.BarsPeriodType.Minute, 240);
    
    //in OnBarUpdate()
    if ((Closes[2][0] > Opens[2][0]) && (Closes[1][0] > Closes[2][1]))
    EnterLong();​
    
    if ((Closes[2][0] > Opens[2][0]) && (Closes[1][0] < Closes[2][1]))
    EnterShort();


    Here are some Help Guide links that will help in understanding the code example above:















    Please let me know if I can assist further.
    Last edited by NinjaTrader_Gaby; 03-11-2024, 10:51 AM.

    Comment


      #3
      Thank you for the reply and the resources, this is very helpful. I'm getting a common error that I can't seem to solve with what I've found online. How to solve for "Error on 'OnBarUpdate' method on bar 1..." error with the below code? I believe this is bc I am using multi timeframe, but I only need the last fully formed bar on the 2nd Timeframe, not the live still-rendering bar.

      Code:
              namespace NinjaTrader.NinjaScript.Strategies
      {
          public class Directions : Strategy
          {​protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Enter the description for your new custom Strategy here.";
                      Name                                        = "Directions";
                      Calculate                                    = Calculate.OnBarClose;
                      EntriesPerDirection                            = 1;
                      EntryHandling                                = EntryHandling.AllEntries;
                      IsExitOnSessionCloseStrategy                = true;
                      ExitOnSessionCloseSeconds                    = 30;
                      IsFillLimitOnTouch                            = false;
                      MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                      OrderFillResolution                            = OrderFillResolution.Standard;
                      Slippage                                    = 0;
                      StartBehavior                                = StartBehavior.WaitUntilFlat;
                      TimeInForce                                    = TimeInForce.Gtc;
                      TraceOrders                                    = false;
                      RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                      StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                      BarsRequiredToTrade                            = 3;
                      // Disable this property for performance gains in Strategy Analyzer optimizations
                      // See the Help Guide for additional information
                      IsInstantiatedOnEachOptimizationIteration    = true;
                  }
                  else if (State == State.Configure)
                  {
                      AddDataSeries(Data.BarsPeriodType.Minute, 1);
                      AddDataSeries(Data.BarsPeriodType.Minute, 480);
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  if (BarsInProgress != 0)
                      return;
      
                  if (CurrentBar < 1)
                      return;
      
                  
      
      //Entry Logic
      if (Closes[1][0] > Closes[2][1]);
      EnterLong();​

      Comment


        #4
        Hello Atlas_Trading,

        What is the full error message? Is it "You are accessing an index with a value that is invalid since it is out-of-range"?

        If so, this message is indicating the specific index requested from a collection does not exist. Indexes must be a non-negative number and less than the size of the collection. The error may be indicating the index requested is larger than the number of elements in the collection.

        In the case of barsAgo values with Series, any barsAgo index must be less than CurrentBar (the total number of bars is the size of a Series collection).

        Code:
        // require BarsInProgress 0 to have 3 bars processed, require BarsInProgress 1 to have 4 bars processed
        if (CurrentBars[0] < 3 || CurrentBars[1] < 4)
        return;
        
        Print(Times[0][3]); // this does not cause an error as CurrentBars[0] is equal to or greater than 3
        
        Print(Times[1][4]); // this does not cause an error as CurrentBars[1] is equal to or greater than 4
        
        Print(Times[0][5]); // this line of code will cause an invalid index error. When CurrentBars[0] is 4 there is no bar 5 bars ago


        The help guide discusses ‘Make sure you have enough bars in the data series you are accessing’ .
        NinjaScript > Educational Resources > Tips > Make sure you have enough bars in the data series you are accessing


        Since you are adding Data Series, you'll need to add additional BarsInProgress checks for each series.

        Code:
        if (CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1)
        return;

        Please let me know if you have any further questions.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        52 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        130 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        70 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        44 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        49 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X