Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding Bollinger to Strategy Analyzer chart.

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

    Adding Bollinger to Strategy Analyzer chart.

    I am trying to add Bollinger Bands to a strategy. I create a BB object and call AddChartIndicator. The only thing that appears on the chart is a series of jagged lines reflected above and below the prices. Is there a way to make this work? Thanks.

    #region Using declarations
    ...
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class goldreturn : Strategy
    {

    private Bollinger bb;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "goldreturn";
    Calculate = Calculate.OnPriceChange;
    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)
    {
    rsi = RSI(14, 1);
    adx = ADX(14);
    // bbu = Bollinger(20,2).Upper();
    bbl = Bollinger(20,2).Lower;
    bb = Bollinger(20,2);

    // Add RSI and ADX indicators to the chart for display
    // This only displays the indicators for the primary Bars object (main instrument) on the chart

    AddChartIndicator(bb);
    }
    }

    protected override void OnBarUpdate()
    {
    //Add your custom strategy logic here.


    if (Input[0] > Bollinger(2,20).Upper[0] + 0.5 ) {
    EnterShort(10,"shortgold");
    }
    if (Input[0] < Bollinger(2,20).Upper[0])
    ExitShort("shortgold");


    if (Input[0] < Bollinger(2,20).Lower[0] - 0.5 ) {
    EnterLong(10,"longgold");
    }
    if (Input[0] > Bollinger(2,20).Lower[0])
    ExitLong("longgold");
    }


    }

    #2
    Hello bjmoose,

    Thanks for your post.

    This line in your code: bb = Bollinger(20,2); is specifying at 20 standard deviation of a 2 period Bollinger which would lead to plots that quickly change direction. I think you just switched the sequence which should be: bb = Bollinger(2, 20);
    Reference: https://ninjatrader.com/support/help...nger_bands.htm

    In your OnBarUpdate() you are directly adding the Bollinger on each line which is not needed as you created a private Bollinger called bb. You can use that local bollinger in place of the direct calls, for example:

    if (Input[0] > bb.Upper[0] + 0.5 ) {
    EnterShort(10,"shortgold");
    Last edited by NinjaTrader_PaulH; 02-20-2019, 07:06 AM. Reason: Added NT8 help guide reference to Bollinger

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    65 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    139 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    75 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    50 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X