Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to draw 5 minute swing indicator over 1 minute chart

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

    How to draw 5 minute swing indicator over 1 minute chart

    This code does not draw anything. What's missing?
    Code:
    #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 MySwing5Over1Min : Strategy
        {
    
            private Swing Swing5;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Draw 5 minute Strength = 1 over a 1 Minute Chart";
                    Name                                        = "MySwing5Over1Min";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    IsOverlay = true;
                    DrawOnPricePanel                             = true;
                    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;
                    BarsRequiredToPlot                            = 20;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = false;
                    //AddPlot(Brushes.Orange, "Swing5");
                }
                else if (State == State.Configure)
                {
                    AddDataSeries("", Data.BarsPeriodType.Minute, 5, Data.MarketDataType.Last);
                    AddPlot(new Stroke(Brushes.DarkCyan),  PlotStyle.Dot, "Swing5Upper");
                    AddPlot(new Stroke(Brushes.Goldenrod),  PlotStyle.Dot, "Swing5Lower");
    
                }
                else if (State == State.DataLoaded)
                {
                            Swing5                = Swing(BarsArray[1], 1) ;
                            Plots[0].PlotStyle = PlotStyle.Dot;
                            Plots[1].PlotStyle = PlotStyle.Dot;
                            Plots[0].Brush = Brushes.Red;
                            Plots[1].Brush = Brushes.White;
                            Plots[0].DashStyleHelper = DashStyleHelper.Dot;
                            Plots[1].DashStyleHelper = DashStyleHelper.Dot;
                            Plots[0].Width = 3;
                            Plots[1].Width = 3;
    
                }
    
    
            }
    
            protected override void OnBarUpdate()
            {
    
                if(CurrentBar < BarsRequiredToPlot) return;
    
                //AddChartIndicator(Swing5);
                    if (BarsInProgress == 1 )
                    {
                        Swing5Upper[0] = Swing5.SwingHigh[0];
                        Swing5Lower[0] = Swing5.SwingLow[0];
    
                    }    
                    if (BarsInProgress == 0)
                    {
                        Swing5Upper[0] = Swing5.SwingHigh[0];
                        Swing5Lower[0] = Swing5.SwingLow[0];
    
                        // if the secondary 5 minute series did not close, set the current bar's value to the previous bar's value to prevent gaps
                        if (!Swing5Upper.IsValidDataPoint(0))
                            Swing5Upper[0] = Swing5Upper[1];
    
                        if (!Swing5Lower.IsValidDataPoint(0))
                            Swing5Lower[0] = Swing5Lower[1];
                    }    
    
    
    
    
                //Add your custom strategy logic here.
            }
    
            #region Properties
    
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public Series<double> Swing5Upper
            {
                get { return Values[0]; }
            }
    
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public Series<double> Swing5Lower
            {
                get { return Values[1]; }
            }
    
    
    
            #endregion
    
        }
    }​

    #2
    Hello bobperez,

    First, note the SwingHighPlot / SwingLowPlot series are what is actually being plotted on the chart with the swing indicator. The SwingHigh / SwingLow series returns the swingHighSeries and swingLowSeries.

    The swing indicator changes it's historical plot values after a swing has been detected.

    If you are plotting only the current bar, and you are not looping through the older bars, you wouldn't be getting any of the updated previous bar values.

    You'll need the same logic the swing indicator has to loop through the previous bars and change their values. (See lines 147, 148, 161, 162, 189, 190, 203, 204)

    It may be helpful to check the SwingHighBar, and if its changed, loop back through to the swing bar and set all of the plot values again.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by cupir2, Yesterday, 06:49 PM
    0 responses
    11 views
    0 likes
    Last Post cupir2
    by cupir2
     
    Started by cupir2, Yesterday, 06:45 PM
    0 responses
    7 views
    0 likes
    Last Post cupir2
    by cupir2
     
    Started by mathfrick2023, 05-08-2025, 12:51 PM
    7 responses
    47 views
    0 likes
    Last Post mathfrick2023  
    Started by 1001111, Yesterday, 07:12 AM
    1 response
    15 views
    0 likes
    Last Post 1001111
    by 1001111
     
    Started by Pope_Finance_II, Yesterday, 02:19 PM
    0 responses
    10 views
    0 likes
    Last Post Pope_Finance_II  
    Working...
    X