Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error when multiplying ATR indicator with double user input variable

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

    Error when multiplying ATR indicator with double user input variable

    I get a compilation error on line 37 when I multiply an indicator ATR(20)*TrStopATR (a double user input).

    Error is line 37 - "Operator * cannot be applied to NinjaTrader.Indicator.ATR and double"
    Line 37 - SetTrailStop("", CalculationMode.Price, ATR(20)*TrStopATRs, false);

    Strategy code below
    Pl advise what i'm doing wrong?

    thanks
    Kiran



    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// BB
    /// </summary>
    [Description("BB ")]
    public class BBcross0TrStop : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int bBperiod = 20; // Default setting for BBperiod
    private double bBstdev = 1; // Default setting for BBstdev
    private double trStopATRs = 3; // Default setting for TrStopATRs
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    SetTrailStop("", CalculationMode.Price, ATR(20)*TrStopATRs, false);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(Close, Bollinger(BBstdev, BBperiod).Lower, 1))
    {
    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (CrossBelow(Close, Bollinger(BBstdev, BBperiod).Lower, 1))
    {
    EnterShort(DefaultQuantity, "");
    }
    }

    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public int BBperiod
    {
    get { return bBperiod; }
    set { bBperiod = Math.Max(10, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double BBstdev
    {
    get { return bBstdev; }
    set { bBstdev = Math.Max(0.4, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double TrStopATRs
    {
    get { return trStopATRs; }
    set { trStopATRs = Math.Max(1.5, value); }
    }
    #endregion
    }

    #2
    Hello Kiran,

    This is because "ATR(20)" is going to return a Indicator not a value like a double. You will want to use the "[]" brackets to reference the double value of the Plot. Here is a thread that you may view that will go over how to use "[]" brackets.



    Note if you are going to use the SetTrailingStop() to a price value of based on the ATR you will want to move that into your Entry Conditions. The reason for this is the inside of Initialize() the ATR is not going to have any data loaded and is only going to get called when the Strategy is first started up.

    If you would like to see how you could use this inside of the strategy wizard you may view the following post.

    Support for the development of custom automated trading strategies using NinjaScript.
    JCNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    628 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    359 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    105 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    562 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    568 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X