Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

My First Strategy has gone wrong

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

    My First Strategy has gone wrong

    Hi I am having a go at writing a strategy. The goal is really just to have a go at compiling a strategy and see it working before I attempt to map out some more complex.

    To build the test example, I used the strategy builder to build a simple Bollinger band test looking for price crossing above or below the bands, with the goal of painting arrows on a chart at these points.

    When I went to compile the strategy I get errors: See the attached image.

    I am assuming that I need to transpose the double into a whole number before I can add pips or ticks to it?

    Thoughts great appreciated.
    Attached Files

    #2
    Originally posted by EastLondonKiwi View Post
    Hi I am having a go at writing a strategy. The goal is really just to have a go at compiling a strategy and see it working before I attempt to map out some more complex.

    To build the test example, I used the strategy builder to build a simple Bollinger band test looking for price crossing above or below the bands, with the goal of painting arrows on a chart at these points.

    When I went to compile the strategy I get errors: See the attached image.

    I am assuming that I need to transpose the double into a whole number before I can add pips or ticks to it?

    Thoughts great appreciated.
    As it is telling you, you cannot compare a Series to a double. You need to specify the index on the Series; said index being the identifier for the double that is held in that index position. The index of the immediate CurrentBar is zero.
    Last edited by koganam; 09-10-2016, 04:05 PM.

    Comment


      #3
      Hi kogaman

      Can you give me an example by rewriting line 71?

      Kind regards

      Duncan

      Comment


        #4
        Originally posted by EastLondonKiwi View Post
        Hi kogaman

        Can you give me an example by rewriting line 71?

        Kind regards

        Duncan
        You asked for a crossing of price above the Bollinger band. That is simply
        Code:
        if (CrossAbove(Close, Bollinger1.Lower, 1)){//do something here}
        which in this instance may not make sense because your statement is inexact. I only put that there to show that if your own statement is not exact, then nobody, including you can write correct statements. There are 3 lines in a Bollinger Band indicator, so "price crossing above ..." is totally ambiguous. Then I also see that you have a tick offset in your statement, that is not mentioned in your description. What exactly do you want to write?
        Last edited by koganam; 09-10-2016, 04:07 PM.

        Comment


          #5
          Hi you are absolutely right of course.

          And maybe that is where the Strategy builder fails. Because I used the default questions it throws up to define the logic.

          It also did not let me define what the parameters of the Bollinger bands are, which at the time I thought was odd.

          Any way back to the question. The logic I am try to write is this.

          There are three bands in a Bollinger Band as you have identified, CentreBand, UpperBand, and LowerBand.

          I am looking for when Price crosses and closes above the UpperBand, and the next candle is a higher close to that cross, then I want a buy signal (in this case that is simply a arrow pointing up).

          If Price crosses and closes below the LowerBand, and the next candle is a lower close to that cross, then I want a sell signal (in this case that is simply a arrow pointing down).

          I'll look at the Bollinger Band code in the Script Editor library and see if I can set up the Bollinger variables better than my current code has.

          but your help on the cross is great.

          Kind regards

          Duncan

          Comment


            #6
            Ok here is a go at cleaning things up. This code has errors in it. But hopefully it is starting to make a bit more sense.

            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"This Straegy if looking for Bollinger Band Crosses in a trending market";
            Name = "BollingerCross";
            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;
            //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)
            {
            Bollinger1 = Bollinger(2, 20);
            //SetProfitTarget(@"", CalculationMode.Pips, 50);
            //SetStopLoss("", CalculationMode.Pips, 50, false);
            bbUpper = Bollinger.Upper;
            bbLower = Bollinger.Lower;
            bbCross = CurrentBar[1];
            double hhUp = NBarsUp(1, true, true, true)[0];
            double llDown = NBarsDown(1, true, true, true)[0];


            }
            }

            protected override void OnBarUpdate()
            {
            if (CurrentBars[0] < BarsRequired)
            return;

            //
            if ((bbCross == CrossBelow(Close, bbLower, 1)) && llDown ==1);
            {
            Draw.ArrowDown(this, @"BollingerCrossArrow down", false, 0, 0, Brushes.Red);
            }
            // Set 1
            if ((bbCross == CrossAbove(Close, bbUpper, 1)) && hhUp ==1);
            {
            Draw.ArrowUp(this, @"BollingerCrossArrow up", false, 0, 0, Brushes.DodgerBlue);
            }

            }
            }
            }

            Comment


              #7
              Ok I seem to have cleared all the errors, but now if I try and run the strategy in the Strategy Analyzer I get and Error

              "Value of property 'BarCount' of NinjaScript 'N bars up' is 1 and not in valid range between 2 and 2147483647."

              Current Code attached

              #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 BollingerCross : Strategy
              {
              private Bollinger Bollinger1;



              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = @"This Straegy if looking for Bollinger Band Crosses in a trending market";
              Name = "BollingerCross";
              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;
              //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)
              {
              Bollinger1 = Bollinger(2, 20);
              }
              //SetProfitTarget(@"", CalculationMode.Pips, 50);
              //SetStopLoss("", CalculationMode.Pips, 50, false);

              }



              protected override void OnBarUpdate()
              {
              double bbUpper = Bollinger(Close, 2, 20).Upper[0];
              double bbLower = Bollinger(Close, 2, 20).Lower[0];
              //public double bbCross = CurrentBar[1];
              double hhUp = NBarsUp(1, true, true, true)[0];
              double llDown = NBarsDown(1, true, true, true)[0];

              if (CurrentBar < BarsRequiredToTrade)
              {
              return;
              }

              //
              if (CrossBelow(Close, bbLower, 1) && llDown ==1);
              {
              Draw.ArrowDown(this, @"BollingerCrossArrow down", false, 0, 0, Brushes.Red);
              }
              // Set 1
              if (CrossAbove(Close, bbUpper, 1) && hhUp ==1);
              {
              Draw.ArrowUp(this, @"BollingerCrossArrow up", false, 0, 0, Brushes.DodgerBlue);
              }
              }
              }

              }

              Comment


                #8
                Hello EastLondonKiwi,

                Thanks for your posts.

                To answer your latest post, in your code you have:

                double hhUp = NBarsUp(1, true, true, true)[0];
                double llDown = NBarsDown(1, true, true, true)[0];

                Where the 1 is specified as the BarCount. The error message "Value of property 'BarCount' of NinjaScript 'N bars up' is 1 and not in valid range between 2 and 2147483647." is advising you to set that to a minimum of 2. Please replace the 1 with 2 and recompile and test.
                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by gravdigaz6, Today, 11:40 PM
                0 responses
                0 views
                0 likes
                Last Post gravdigaz6  
                Started by MarianApalaghiei, Today, 10:49 PM
                3 responses
                9 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by XXtrader, Today, 11:30 PM
                0 responses
                3 views
                0 likes
                Last Post XXtrader  
                Started by love2code2trade, Yesterday, 01:45 PM
                4 responses
                28 views
                0 likes
                Last Post love2code2trade  
                Started by funk10101, Today, 09:43 PM
                0 responses
                9 views
                0 likes
                Last Post funk10101  
                Working...
                X