Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Builder syntax

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

    Strategy Builder syntax

    hI


    What I am actually trying to do is when conditions of my strategy trigger, I want to draw an arrow ( up or down ) as conditions dictate with price. That bit I have done ok.

    what I cant seem to mange is the close of 2 bars after the arrow is triggered I want to paint a Dot.

    Just cant get it to work !

    Using the Strategy Builder

    Thanks in advance for any help
    Last edited by fredfred123; 01-07-2018, 03:13 PM.

    #2
    Hello fredfred123

    The issue is this would need to math.

    The Strategy Builder is able to do simple comparisons and set values to individual indicators or other variable values, but is not able to do math with integer variables. (The only math available in the Strategy Builder is when using an Offset with a series)

    For this you would need to unlock the code.
    Before unlocking a script, its wise to copy it first so that the condition builder can still be used with the original.

    Below is a publicly available link to a video that demonstrates.
    https://www.youtube.com/watch?v=BA0W...utu.be&t=8m15s

    You can use a bool to set when the condition to place the dot is true, then increment an int variable on each new bar.

    In a second condition when the bool is true and the int is greater than your desired amount, trigger the second action.

    Code:
    // in the variables section
    private int counter;
    private bool isCounting;
    
    // in OnStateChange()
    if (State == State.DataLoaded)
    {
    counter = 0;
    isCounting = false;
    }
    
    // in OnBarUpdate()
    if (isCounting == false && /*action 1 conditions */)
    {
    // trigger action one such as drawing a dot
    counter = 0;
    isCounting = true;
    }
    
    // action two conditions
    if (isCounting == true && counter >= 2)
    {
    // trigger action two
    isCounting = false;
    }
    
    else if (isCounting == true)
    {
    counter++;
    }

    (Update March 1st, 2021 - NinjaTrader_PaulH was clever enough to use series with offsets that do subtraction to make a workaround for counting tin the Strategy Builder.
    https://ninjatrader.com/support/foru...38#post1078538)
    Last edited by NinjaTrader_ChelseaB; 03-01-2021, 11:25 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea

      I copied the code and had an error that was expression expected ?

      However I changed it to I thought would work and it complied ok but it is now on the list of strategies but is not on the list of strategies toload into a chart ? The Strategy is there for the Ninjascript Editor ?
      I have the correct name of the Strategy in the code.
      I have pasted the code below
      protected override void OnBarUpdate()


      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 1)
      return;

      // Set 1
      if (PricePivots1.MarketAnalyzerPlot[0] == 1)
      {
      Draw.ArrowDown(this, @"DOWN " + CurrentBars[0].ToString(), false, 0, (High[0] + (10 * TickSize)) , Brushes.Red);
      }

      if (isCounting == false)
      {
      Draw.ArrowDown(this, @"DOWN " + CurrentBars[0].ToString(), false, 0, (High[0] + (10 * TickSize)) , Brushes.Red);/*action 1 conditions */

      {


      // trigger action one such as drawing a dot
      Draw.Dot(this, @"UP " + CurrentBars[0].ToString(), false, 0, (Low[0] + (-10 * TickSize)) , Brushes.Blue);

      counter = 0;
      isCounting = true;
      }

      // action two conditions
      if (isCounting == true && counter >= 2)
      {
      // trigger action two
      isCounting = false;
      }

      else if (isCounting == true)
      {
      counter++;
      }

      Comment


        #4
        Hello fredfred123,

        Are there any errors appearing in the Log tab of the Control Center when you open the Strategies window from a chart?

        If so, what do these errors say?


        As a heads up, it looks like the curly braces may be used improperly.
        There is an opening curly brace with no closing curly brace.
        Code:
        if (isCounting == false)
        {
        	Draw.ArrowDown(this, @"DOWN " + CurrentBars[0].ToString(), false, 0, (High[0] + (10 * TickSize)), Brushes.Red);/*action 1 conditions */
        
        	[B]{   // <--- what is this curly brace for?[/B]
        		// trigger action one such as drawing a dot
        		Draw.Dot(this, @"UP " + CurrentBars[0].ToString(), false, 0, (Low[0] + (-10 * TickSize)), Brushes.Blue);
        
        		counter = 0;
        		isCounting = true;
        	}
        
        	// action two conditions
        	if (isCounting == true && counter >= 2)
        	{
        		// trigger action two
        		isCounting = false;
        	}
        
        	else if (isCounting == true)
        	{
        		counter++;
        	}
        [B]// < -- why is there no closing curly brace for the if statement?[/B]
        It appears you are trying to nest everything in the 'if (isCounting == false)' condition's action block. This is not what I am suggesting. These need to be separate conditions that are not nested as they are checked independently.
        Last edited by NinjaTrader_ChelseaB; 01-08-2018, 04:01 PM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I had not included all the code, I am sorry.

          The script doesn't show errors in the Ninjascript editor and compiled OK, I can't call it onto a chart as it is not on the available list?
          there are error in the log
          error on calling'OnStateChange" method:Object reference not set to an instance of an object

          it is as follows
          //This namespace holds Strategies in this folder and is required. Do not change it.
          namespace NinjaTrader.NinjaScript.Strategies
          {
          public class PivotPointsEdit : Strategy
          {
          private DynamicPricePivots QuantumDynamicPricePivots1;
          private int counter;
          private bool isCounting;



          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"PivotPointsEdit.";
          Name = "PivotPointsEdit";
          Calculate = Calculate.OnEachTick;
          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 = 20;
          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          }
          else if (State == State.Configure)
          {
          }
          else if (State == State.DataLoaded)
          {
          counter = 0;
          isCounting = false;
          }


          {
          DynamicPricePivots1 = DynamicPricePivots(Close, 10);
          DynamicPricePivots1.Plots[0].Brush = Brushes.Yellow;
          DynamicPricePivots1.Plots[1].Brush = Brushes.Yellow;
          DynamicPricePivots1.Plots[2].Brush = Brushes.Transparent;
          AddChartIndicator(QuantumDynamicPricePivots1);
          }
          }

          protected override void OnBarUpdate()


          {
          if (BarsInProgress != 0)
          return;

          if (CurrentBars[0] < 1)
          return;

          // Set 1
          if (DynamicPricePivots1.MarketAnalyzerPlot[0] == 1)
          {
          Draw.ArrowDown(this, @"DOWN " + CurrentBars[0].ToString(), false, 0, (High[0] + (10 * TickSize)) , Brushes.Red);
          }

          if (isCounting == false)
          {
          Draw.ArrowDown(this, @"DOWN " + CurrentBars[0].ToString(), false, 0, (High[0] + (10 * TickSize)) , Brushes.Red);/*action 1 conditions */

          {


          // trigger action one such as drawing a dot
          Draw.Dot(this, @"UP " + CurrentBars[0].ToString(), false, 0, (Low[0] + (-10 * TickSize)) , Brushes.Blue);

          counter = 0;
          isCounting = true;
          }

          // action two conditions
          if (isCounting == true && counter >= 2)
          {
          // trigger action two
          isCounting = false;
          }

          else if (isCounting == true)
          {
          counter++;
          }





          // Set 2
          if (DynamicPricePivots1.MarketAnalyzerPlot[0] == -1)
          {
          Draw.ArrowUp(this, @"UP " + CurrentBars[0].ToString(), false, 0, (Low[0] + (-10 * TickSize)) , Brushes.Blue);
          }
          }
          }
          }
          }

          Comment


            #6
            How to Add Breakeven Stop in Strategy Wizard

            Hello fredfred123,

            You have code that is not in an if or else if within OnStateChange.

            Code:
            { 
            DynamicPricePivots1 = DynamicPricePivots(Close, 10);
            DynamicPricePivots1.Plots[0].Brush = Brushes.Yellow;
            DynamicPricePivots1.Plots[1].Brush = Brushes.Yellow;
            DynamicPricePivots1.Plots[2].Brush = Brushes.Transparent;
            AddChartIndicator(QuantumDynamicPricePivots1);
            }
            This means that it will try to create these in every state since you have not specified which state you want this triggered in.

            You cannot call / initialize an indicator until there is data, you cannot add the indicator to the chart until its been initialized.


            For future reference, to export a script so that this can be shared with NinjaTrader 8, do the following:
            1. Click Tools -> Export -> NinjaScript...
            2. Click the 'add' link -> check the box(es) for the script(s) you want to include
            3. Click the 'Export' button
            4. Enter a unique name for the file in the value for 'File name:'
            5. Choose a save location -> click Save
            6. Click OK to clear the export location message

            By default your exported file will be in the following location:
            • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>


            Below is a link to the help guide on Exporting NinjaScripts.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              **Success**

              Helo Chelsea

              Made a fresh start this morning and looked at it again and your code then made sense to me, and it worked like a charm !! Brilliant . sorry I bungled it up at first !! and thank you very much

              It works exactly as it should draws a dot on the chart 2 bars after an arrow is drawn.

              **Great support**
              Last edited by fredfred123; 01-09-2018, 08:43 AM.

              Comment


                #8
                counter getting reset ?

                Hi Chelsea

                The code works a charm, the strategy triggers and draws an arrow, the counter starts counting bars till 2 then draws a dot.

                The counter gets upset when before the counter has finished counting to 2, price has reversed and the strategy triggers another arrow, this resets the counter before its finished.

                Then the following arrow draws an arrow and dot on the same bar, as the counter has been interrupted.

                Can I get the script to ignore the second interrupting arrow trigger and allow the counter to finish?

                Comment


                  #9
                  Hello fredfred123,

                  The code I have suggested resets the counter when the first action is taken.

                  Do you have multiple actions that are resetting the counter?

                  Are you using multiple counters?

                  Are you requiring the bool to be false to trigger the first action as shown in my suggested code?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chelsea

                    Your questions are telling me what's amiss.

                    The answers are yes, I have followed the logic of the Strategy builder ie, set 1 condition long draw arrow up, and set 2 condition short draw arrow down.

                    so having 2 sets means I am repeating the counter. I see that this is wrong!

                    There are 2 conditions that create arrows. Arrows up, and Arrows down depending on each being triggered.

                    The dots can be one condition as they both mean the same = exit trade up or down.

                    My problem is how do I separate the 2 conditions that produce Long arrows and Short arrows but at the same time combine them for the one counter ?

                    Is there a similar snippet I could follow that does this?

                    Comment


                      #11
                      Hello fredfred123,

                      If both actions reset the counter at the same time, then only allow one of them to reset it.

                      I'm not aware of a pre-written line of code that does this specific set of actions.
                      Last edited by NinjaTrader_ChelseaB; 01-15-2018, 08:20 AM.
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by jaybedreamin, Today, 05:56 PM
                      0 responses
                      2 views
                      0 likes
                      Last Post jaybedreamin  
                      Started by DJ888, 04-16-2024, 06:09 PM
                      6 responses
                      18 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by Jon17, Today, 04:33 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post Jon17
                      by Jon17
                       
                      Started by Javierw.ok, Today, 04:12 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post Javierw.ok  
                      Started by timmbbo, Today, 08:59 AM
                      2 responses
                      10 views
                      0 likes
                      Last Post bltdavid  
                      Working...
                      X