Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

need help with strategy builder and script editor

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

    need help with strategy builder and script editor

    Hi, I am trying to build a strategy entering position on previous bar's break. Below is the code upon completion from Strategy Builder.

    Now I got a few questions :
    1) what does "if (BarsInProgress != 0) return; " do ?
    2) what does if (CurrentBars[0] < 1) return; do?
    3) what does ((BarsSinceEntryExecution(0, "", 0) >= 1)
    || (BarsSinceEntryExecution(0, "", 0) == -1))
    ​ mean here?
    4) under "condition" tab of Strategy Builder​​ (bottom graph below), what is the difference between "current position size" and current position and current market position ?
    5) under "Misc" tab, what should I set the value for "Bars in progress" and "Entry execution" ?

    sorry for so many questions and look forward to your answer.
    thanks a million !
    Peter



    ​​

    #2
    Hello judysamnt7,

    1) Strategy builder scripts can only process for the primary series, that prevents any secondary added series from being used for processing. You can only get prices from secondary series using the builder.
    2) this is a common check to make sure enough bars have processed before using a BarsAgo with a series.
    3) BarsSinceEntryExecution returns a negative 1 when no entry was found so that condition is making sure that an entry can happen at all, if you only check for a number of bars since an entry but it wasn't found then you won't get an initial entry.
    4) Current position size is the quantity of the position. current market position is a way to get the direction of the current position, market position is the way to select which direction of the position as a comparison
    5) That would relate to the entry you wanted to target. Bars in progress should always be 0 in the builder, this is just a required input. Entry Executions ago would be the instance of the entry you wanted, most often you would use 0 to get the most recent entry.


    Comment


      #3
      thanks for quick reply. does my script look correct to you ? my intention is to enter long position when current price is higher than previous bar high and short position when lower than previous bar low?

      ================================================== ============================
      region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.Indicators;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion

      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class candleHL3 : Strategy
      {
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "candleHL3";
      Calculate = Calculate.OnEachTick;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 300;
      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 = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      Quantity = 2;
      }
      else if (State == State.Configure)
      {
      }
      }

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 1)
      return;

      // Set 1
      if (
      // BarsSinceEntry Group
      ((BarsSinceEntryExecution(0, "", 0) >= 1)
      || (BarsSinceEntryExecution(0, "", 0) == -1))
      && (GetCurrentBid(0) > High[1]))
      {
      EnterLong(Convert.ToInt32(Quantity), "");
      }

      // Set 2
      if (
      // BarsSinceEntry Group
      ((BarsSinceEntryExecution(0, "", 0) >= 1)
      || (BarsSinceEntryExecution(0, "", 0) == -1))
      && (GetCurrentAsk(0) < Low[1]))
      {
      EnterShort(Convert.ToInt32(Quantity), "");
      }

      }

      region Properties
      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="Quantity", Order=1, GroupName="Parameters")]
      public int Quantity
      { get; set; }
      #endregion

      }
      }

      Comment


        #4
        Hello judysamnt7,

        Justs as a heads up, our support generally can't answer questions like "does this look right", we would redirect you to try the script and make sure it does what you asked of it. Testing the script is a very important part of the strategy creation process so that squarely falls on the developer to make sure it works as planned.

        I see that you are using a BarsAgo for the prices so that is potentially correct. You can run the script in realtime on the sim account and watch what it does to make sure it performs as you had expected it to. You may need to add additional conditions because checking if one price is higher or lower than another can remain true for many bars which could lead to many entries in the same direction if the condition remains true.

        Comment


          #5
          Originally posted by judysamnt7 View Post
          thanks for quick reply. does my script look correct to you ? my intention is to enter long position when current price is higher than previous bar high and short position when lower than previous bar low?

          ================================================== ============================
          region Using declarations
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.ComponentModel.DataAnnotations;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Windows;
          using System.Windows.Input;
          using System.Windows.Media;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Gui;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Gui.SuperDom;
          using NinjaTrader.Gui.Tools;
          using NinjaTrader.Data;
          using NinjaTrader.NinjaScript;
          using NinjaTrader.Core.FloatingPoint;
          using NinjaTrader.NinjaScript.Indicators;
          using NinjaTrader.NinjaScript.DrawingTools;
          #endregion

          //This namespace holds Strategies in this folder and is required. Do not change it.
          namespace NinjaTrader.NinjaScript.Strategies
          {
          public class candleHL3 : Strategy
          {
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "candleHL3";
          Calculate = Calculate.OnEachTick;
          EntriesPerDirection = 1;
          EntryHandling = EntryHandling.AllEntries;
          IsExitOnSessionCloseStrategy = true;
          ExitOnSessionCloseSeconds = 300;
          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 = 20;
          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          Quantity = 2;
          }
          else if (State == State.Configure)
          {
          }
          }

          protected override void OnBarUpdate()
          {
          if (BarsInProgress != 0)
          return;

          if (CurrentBars[0] < 1)
          return;

          // Set 1
          if (
          // BarsSinceEntry Group
          ((BarsSinceEntryExecution(0, "", 0) >= 1)
          || (BarsSinceEntryExecution(0, "", 0) == -1))
          && (GetCurrentBid(0) > High[1]))
          {
          EnterLong(Convert.ToInt32(Quantity), "");
          }

          // Set 2
          if (
          // BarsSinceEntry Group
          ((BarsSinceEntryExecution(0, "", 0) >= 1)
          || (BarsSinceEntryExecution(0, "", 0) == -1))
          && (GetCurrentAsk(0) < Low[1]))
          {
          EnterShort(Convert.ToInt32(Quantity), "");
          }

          }

          region Properties
          [NinjaScriptProperty]
          [Range(1, int.MaxValue)]
          [Display(Name="Quantity", Order=1, GroupName="Parameters")]
          public int Quantity
          { get; set; }
          #endregion

          }
          }
          ​
          Hello if you are still interested in this style of scripting a Youtuber named TradeSaber has writted scripts exactly the same as what you are inquiring about. He used the unique values you are inquiring about to prevent duplicate order etc...
          (873) TradeSaber - YouTube​

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          54 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          131 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          73 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