Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using declarations
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.Diagnostics.CodeAnalysis;
- using System.Windows;
- using System.Reflection;
- using System.Windows.Media;
- using System.Xml.Serialization;
- using NinjaTrader.Cbi;
- using NinjaTrader.Data;
- using NinjaTrader.Gui;
- using NinjaTrader.Gui.Chart;
- using NinjaTrader.Gui.Tools;
- using NinjaTrader.NinjaScript.DrawingTools;
- using NinjaTrader.NinjaScript.Indicators;
- using NinjaTrader.Gui.Tools;
- #endregion
- namespace NinjaTrader.NinjaScript.Indicators
- {
- public class YR_PivotPoints : Indicator
- {
- private int i=0;
- protected override void OnStateChange()
- {
- if (State == State.SetDefaults)
- {
- Name = "YR_PivotPoints";
- Description = "Rolling Hourly Pivots";
- Calculate = Calculate.OnBarClose;
- //IsOverlay = true;
- DisplayInDataBox = true;
- DrawOnPricePanel = true;
- DrawHorizontalGridLines = true;
- //DrawVerticalGridLines = true;
- PaintPriceMarkers = true;
- ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
- //Disable this property if your indicator requires custom values that cumulate with each new market data event.
- //See Help Guide for additional information.
- IsSuspendedWhileInactive = true;
- //customs
- Initialize();
- }
- else if (State == State.Configure)
- {
- }
- else if (State == State.DataLoaded)
- {
- sessionIterator = new SessionIterator(Bars);
- }
- else if (State == State.Terminated)
- {
- }
- }
- private void Initialize()
- {
- AddPlot(new Stroke(Brushes.White), PlotStyle.Hash, "PP");
- //S
- for(i=1; i<=8; i++)
- AddPlot(new Stroke(Brushes.Green), PlotStyle.Hash, "S"+i.ToString() );
- //R
- for(i=1; i<=8; i++)
- AddPlot(new Stroke(Brushes.Red), PlotStyle.Hash, "R"+i.ToString() );
- //S middle
- for(i=1; i<=8; i++)
- AddPlot(new Stroke(Brushes.Green), PlotStyle.Cross, "S"+(i-1).ToString()+(i).ToString() );
- //R middle
- for(i=1; i<=8; i++)
- AddPlot(new Stroke(Brushes.Red), PlotStyle.Cross, "R"+(i-1).ToString()+(i).ToString() );
- AddPlot(new Stroke(Brushes.Cyan), PlotStyle.Line, "PrevHigh" );
- AddPlot(new Stroke(Brushes.Cyan), PlotStyle.Line, "PrevLow" );
- AddPlot(new Stroke(Brushes.Cyan), PlotStyle.Line, "PrevClose" );
- Calculate = Calculate.OnEachTick;
- IsOverlay = true;
- // NT8 REMOVED: PriceTypeSupported = false;
- IsAutoScale = false;
- }
- private DateTime currentDate = Core.Globals.MinDate;
- private double currentOpen = double.MinValue;
- private double currentHigh = double.MinValue;
- private double currentLow = double.MaxValue;
- private double currentClose = double.MinValue;
- private DateTime lastDate = Core.Globals.MinDate;
- private double lastOpen = double.MinValue;
- private double lastHigh = double.MinValue;
- private double lastLow = double.MaxValue;
- private double lastClose = double.MinValue;
- private Data.SessionIterator sessionIterator;
- private SimpleFont font = (new SimpleFont("Arial", 12));
- private string labelType = "";
- private string dayNum;
- protected override void OnBarUpdate()
- {
- if (CurrentBar < 2) return;
- if (Bars == null)
- return;
- //if (!Bars.BarsType.IsIntraday) return;
- //if (Bars.Period.Id == BarsPeriodType.Minute && Bars.Period.Value > 30) return;
- bool should_plot = false;
- bool is_Line_Transition=false;
- dayNum = Time[0].DayOfYear.ToString();
- if (PivotMode == PivotModeEnums.Daily)
- {
- double curTime= ToTime(Time[0])/100;
- double prevTime= ToTime(Time[1])/100;
- if ( curTime >= SessionStart && curTime < SessionEnd)
- {
- should_plot = true;
- labelType = "d";
- //if (!Bars.BarsType.IsIntraday) return;
- lastDate = currentDate;
- currentDate = sessionIterator.GetTradingDay(Time[0]);
- if (lastDate != currentDate || currentOpen == double.MinValue)
- {
- if(lastDate != currentDate)
- {
- lastOpen = currentOpen;
- lastHigh = currentHigh;
- lastLow = currentLow;
- lastClose = Time[0].DayOfYear != Time[1].DayOfYear ? Close[1] : Open[0];
- is_Line_Transition = true;
- }
- currentOpen = Open[0];
- currentHigh = High[0];
- currentLow = Low[0];
- currentClose = Close[0];
- }
- else
- {
- currentOpen = Open[0];
- currentHigh = Math.Max(currentHigh, High[0]);
- currentLow = Math.Min(currentLow, Low[0]);
- currentClose = Close[0];
- }
- }
- if(ShowSessionVerticalLines)
- {
- if ( curTime >= SessionStart && prevTime < SessionStart)
- {
- Draw.VerticalLine(this, "Vline_start"+dayNum, Time[0], Brushes.Green);
- }
- if ( curTime >= SessionEnd && prevTime < SessionEnd)
- {
- Draw.VerticalLine(this, "Vline_end"+dayNum, Time[0], Brushes.Red);
- }
- }
- }
- //if we should plot it or not
- if (should_plot)
- {
- double P = (lastHigh + lastLow + lastClose) / 3;
- double A = (lastHigh + lastLow)/2;
- double HL= (lastHigh - lastLow);
- if (ShowPivotLine)
- {
- PP[0] = P;
- }
- double remn = P-A; //shortcut
- if (ShowResistanceLine)
- {
- R8[0] = P+HL*4.55 + (remn>0 ? HL*14/2 : HL*50/5);
- R7[0] = P+HL*3.28 + (remn>0 ? HL*12/2 : HL*35/5);
- R6[0] = P+HL*2.33 + (remn>0 ? HL*8/2 : HL*25/5);
- R5[0] = P+HL*1.6 + (remn>0 ? HL*6/2 : HL*15/5);
- R4[0] = P+HL*1.08 + (remn>0 ? HL*4/2 : HL*10/5);
- R3[0] = P+HL*0.68 + (remn>0 ? HL*3/2 : HL*6/5);
- R2[0] = P+HL*0.382 + (remn>0 ? HL*2/2 : HL*3/5);
- R1[0] = P+HL*0.1565 + (remn>0 ? HL*1/2 : HL*1/5);
- }
- if (ShowSupportLine)
- {
- //as of the questionable request of client, I am doing this.
- if (SymetricLogic)
- {
- S1[0] = P-HL*0.1565 - (remn>0 ? HL*1/2 : HL*1/5) ;
- S2[0] = P-HL*0.382 - (remn>0 ? HL*2/2 : HL*3/5) ;
- S3[0] = P-HL*0.68 - (remn>0 ? HL*3/2 : HL*6/5) ;
- S4[0] = P-HL*1.08 - (remn>0 ? HL*4/2 : HL*10/5);
- S5[0] = P-HL*1.6 - (remn>0 ? HL*6/2 : HL*15/5);
- S6[0] = P-HL*2.33 - (remn>0 ? HL*8/2 : HL*25/5);
- S7[0] = P-HL*3.28 - (remn>0 ? HL*12/2 : HL*35/5);
- S8[0] = P-HL*4.55 - (remn>0 ? HL*14/2 : HL*50/5);
- }
- else
- {
- S1[0] = P-HL*0.1565 + (remn<0 ? HL*1/2 : HL*1/5) ;
- S2[0] = P-HL*0.382 + (remn<0 ? HL*2/2 : HL*3/5) ;
- S3[0] = P-HL*0.68 + (remn<0 ? HL*3/2 : HL*6/5) ;
- S4[0] = P-HL*1.08 + (remn<0 ? HL*4/2 : HL*10/5);
- S5[0] = P-HL*1.6 + (remn<0 ? HL*6/2 : HL*15/5);
- S6[0] = P-HL*2.33 + (remn<0 ? HL*8/2 : HL*25/5);
- S7[0] = P-HL*3.28 + (remn<0 ? HL*12/2 : HL*35/5);
- S8[0] = P-HL*4.55 + (remn<0 ? HL*14/2 : HL*50/5);
- }
- }
- if(ShowPreviousDayLines)
- {
- PrevHigh[0]= lastHigh;
- PrevLow[0]= lastLow;
- PrevClose[0]= lastClose;
- }
- AssignLabels(0);
- }
- }
- private void AssignLabels(int displace)
- {
- // show labels
- if (ShowLineLabels)
- {
- if (true) //(is_Line_Transition)
- {
- Series<double> val;
- for (i=1; i<=8; i++)
- {
- foreach(string R_S in (new string[]{"R","S"}) )
- {
- if ( (R_S=="R" && ShowResistanceLine) || (R_S=="S" && ShowSupportLine) )
- {
- val = (Series<double>) this.GetType().GetProperty(R_S+i.ToString()).GetValue(this, null);
- Draw.Text(this, labelType+R_S+ i.ToString() + "_"+dayNum, true, labelType+R_S+ i.ToString(), Time[0], val[displace], 0, Brushes.White, font, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
- }
- }
- }
- if (ShowPivotLine)
- {
- Draw.Text(this, labelType+"P"+"_"+dayNum, true, labelType+"P", Time[0], PP[displace], 0, Brushes.White, font, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
- }
- }
- }
- }
- #region Properties
- public enum PivotModeEnums
- {
- Daily,
- Weekly,
- Monthly
- }
- [NinjaScriptProperty]
- [Display(GroupName="User - Parameters", Order=10, Name="Pivot Mode")]
- public NinjaTrader.NinjaScript.Indicators.YR_PivotPoints.PivotModeEnums _PivotMode
- {
- get { return PivotMode; }
- set { PivotMode = value; }
- }
- private NinjaTrader.NinjaScript.Indicators.YR_PivotPoints.PivotModeEnums PivotMode = NinjaTrader.NinjaScript.Indicators.YR_PivotPoints.PivotModeEnums.Daily;
- [NinjaScriptProperty]
- [Display(GroupName="User - Parameters", Order=20, Name="Session start")]
- public int _SessionStart
- {
- get { return SessionStart; }
- set { SessionStart = value; }
- }
- private int SessionStart = 0600;
- [NinjaScriptProperty]
- [Display(GroupName="User - Parameters", Order=30, Name="Session end")]
- public int _SessionEnd
- {
- get { return SessionEnd; }
- set { SessionEnd = value; }
- }
- private int SessionEnd = 1700;
- [NinjaScriptProperty]
- [Display(GroupName="User - Parameters", Order=40, Name="Show Middle Lines")]
- public bool _ShowMiddleLines
- {
- get { return ShowMiddleLines; }
- set { ShowMiddleLines = value; }
- }
- private bool ShowMiddleLines = true;
- [NinjaScriptProperty]
- [Display(GroupName="User - Logic", Order=30, Name="Symetric Logic")]
- public bool _SymetricLogic
- {
- get { return SymetricLogic; }
- set { SymetricLogic = value; }
- }
- private bool SymetricLogic = true;
- [NinjaScriptProperty]
- [Display(GroupName="User - Visuals", Order=20, Name="Show Pivot Line")]
- public bool _ShowPivotLine
- {
- get { return ShowPivotLine; }
- set { ShowPivotLine = value; }
- }
- private bool ShowPivotLine = true;
- [NinjaScriptProperty]
- [Display(GroupName="User - Visuals", Order=20, Name="Show Resistance Lines")]
- public bool _ShowResistanceLine
- {
- get { return ShowResistanceLine; }
- set { ShowResistanceLine = value; }
- }
- private bool ShowResistanceLine = true;
- [NinjaScriptProperty]
- [Display(GroupName="User - Visuals", Order=30, Name="Show Support Lines")]
- public bool _ShowSupportLine
- {
- get { return ShowSupportLine; }
- set { ShowSupportLine = value; }
- }
- private bool ShowSupportLine = true;
- [NinjaScriptProperty]
- [Display(GroupName="User - Visuals", Order=40, Name="Show Line-Labels")]
- public bool _ShowLineLabels
- {
- get { return ShowLineLabels; }
- set { ShowLineLabels = value; }
- }
- private bool ShowLineLabels = true;
- [NinjaScriptProperty]
- [Display(GroupName="User - Visuals", Order=40, Name="Show Previous Day lines")]
- public bool _ShowPreviousDayLines
- {
- get { return ShowPreviousDayLines; }
- set { ShowPreviousDayLines = value; }
- }
- private bool ShowPreviousDayLines = true;
- [NinjaScriptProperty]
- [Display(GroupName="User - Visuals", Order=40, Name="Show Session Vertical Lines")]
- public bool _ShowSessionVerticalLines
- {
- get { return ShowSessionVerticalLines; }
- set { ShowSessionVerticalLines = value; }
- }
- private bool ShowSessionVerticalLines = true;
- [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> PP
- {
- get { return Values[0]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S1
- {
- get { return Values[1]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S2
- {
- get { return Values[2]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S3
- {
- get { return Values[3]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S4
- {
- get { return Values[4]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S5
- {
- get { return Values[5]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S6
- {
- get { return Values[6]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S7
- {
- get { return Values[7]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S8
- {
- get { return Values[8]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R1
- {
- get { return Values[9]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R2
- {
- get { return Values[10]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R3
- {
- get { return Values[11]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R4
- {
- get { return Values[12]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R5
- {
- get { return Values[13]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R6
- {
- get { return Values[14]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R7
- {
- get { return Values[15]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R8
- {
- get { return Values[16]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S01
- {
- get { return Values[17]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S12
- {
- get { return Values[18]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S23
- {
- get { return Values[19]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S34
- {
- get { return Values[20]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S45
- {
- get { return Values[21]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S56
- {
- get { return Values[22]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S67
- {
- get { return Values[23]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> S78
- {
- get { return Values[24]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R01
- {
- get { return Values[25]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R12
- {
- get { return Values[26]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R23
- {
- get { return Values[27]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R34
- {
- get { return Values[28]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R45
- {
- get { return Values[29]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R56
- {
- get { return Values[30]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R67
- {
- get { return Values[21]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> R78
- {
- get { return Values[32]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> PrevHigh
- {
- get { return Values[33]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> PrevLow
- {
- get { return Values[34]; }
- }
- [Browsable(false)]
- [XmlIgnore()]
- public Series<double> PrevClose
- {
- get { return Values[35]; }
- }
- #endregion
- }
- }
- #region NinjaScript generated code. Neither change nor remove.
- namespace NinjaTrader.NinjaScript.Indicators
- {
- public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
- {
- private YR_PivotPoints[] cacheYR_PivotPoints;
- public YR_PivotPoints YR_PivotPoints(NinjaTrader.NinjaScript.Indicators.YR_PivotPoints.PivotModeEnums _pivotMode, int _sessionStart, int _sessionEnd, bool _showMiddleLines, bool _symetricLogic, bool _showPivotLine, bool _showResistanceLine, bool _showSupportLine, bool _showLineLabels, bool _showPreviousDayLines, bool _showSessionVerticalLines)
- {
- return YR_PivotPoints(Input, _pivotMode, _sessionStart, _sessionEnd, _showMiddleLines, _symetricLogic, _showPivotLine, _showResistanceLine, _showSupportLine, _showLineLabels, _showPreviousDayLines, _showSessionVerticalLines);
- }
- public YR_PivotPoints YR_PivotPoints(ISeries<double> input, NinjaTrader.NinjaScript.Indicators.YR_PivotPoints.PivotModeEnums _pivotMode, int _sessionStart, int _sessionEnd, bool _showMiddleLines, bool _symetricLogic, bool _showPivotLine, bool _showResistanceLine, bool _showSupportLine, bool _showLineLabels, bool _showPreviousDayLines, bool _showSessionVerticalLines)
- {
- if (cacheYR_PivotPoints != null)
- for (int idx = 0; idx < cacheYR_PivotPoints.Length; idx++)
- if (cacheYR_PivotPoints[idx] != null && cacheYR_PivotPoints[idx]._PivotMode == _pivotMode && cacheYR_PivotPoints[idx]._SessionStart == _sessionStart && cacheYR_PivotPoints[idx]._SessionEnd == _sessionEnd && cacheYR_PivotPoints[idx]._ShowMiddleLines == _showMiddleLines && cacheYR_PivotPoints[idx]._SymetricLogic == _symetricLogic && cacheYR_PivotPoints[idx]._ShowPivotLine == _showPivotLine && cacheYR_PivotPoints[idx]._ShowResistanceLine == _showResistanceLine && cacheYR_PivotPoints[idx]._ShowSupportLine == _showSupportLine && cacheYR_PivotPoints[idx]._ShowLineLabels == _showLineLabels && cacheYR_PivotPoints[idx]._ShowPreviousDayLines == _showPreviousDayLines && cacheYR_PivotPoints[idx]._ShowSessionVerticalLines == _showSessionVerticalLines && cacheYR_PivotPoints[idx].EqualsInput(input))
- return cacheYR_PivotPoints[idx];
- return CacheIndicator<YR_PivotPoints>(new YR_PivotPoints(){ _PivotMode = _pivotMode, _SessionStart = _sessionStart, _SessionEnd = _sessionEnd, _ShowMiddleLines = _showMiddleLines, _SymetricLogic = _symetricLogic, _ShowPivotLine = _showPivotLine, _ShowResistanceLine = _showResistanceLine, _ShowSupportLine = _showSupportLine, _ShowLineLabels = _showLineLabels, _ShowPreviousDayLines = _showPreviousDayLines, _ShowSessionVerticalLines = _showSessionVerticalLines }, input, ref cacheYR_PivotPoints);
- }
- }
- }
- namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
- {
- public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
- {
- public Indicators.YR_PivotPoints YR_PivotPoints(NinjaTrader.NinjaScript.Indicators.YR_PivotPoints.PivotModeEnums _pivotMode, int _sessionStart, int _sessionEnd, bool _showMiddleLines, bool _symetricLogic, bool _showPivotLine, bool _showResistanceLine, bool _showSupportLine, bool _showLineLabels, bool _showPreviousDayLines, bool _showSessionVerticalLines)
- {
- return indicator.YR_PivotPoints(Input, _pivotMode, _sessionStart, _sessionEnd, _showMiddleLines, _symetricLogic, _showPivotLine, _showResistanceLine, _showSupportLine, _showLineLabels, _showPreviousDayLines, _showSessionVerticalLines);
- }
- public Indicators.YR_PivotPoints YR_PivotPoints(ISeries<double> input , NinjaTrader.NinjaScript.Indicators.YR_PivotPoints.PivotModeEnums _pivotMode, int _sessionStart, int _sessionEnd, bool _showMiddleLines, bool _symetricLogic, bool _showPivotLine, bool _showResistanceLine, bool _showSupportLine, bool _showLineLabels, bool _showPreviousDayLines, bool _showSessionVerticalLines)
- {
- return indicator.YR_PivotPoints(input, _pivotMode, _sessionStart, _sessionEnd, _showMiddleLines, _symetricLogic, _showPivotLine, _showResistanceLine, _showSupportLine, _showLineLabels, _showPreviousDayLines, _showSessionVerticalLines);
- }
- }
- }
- namespace NinjaTrader.NinjaScript.Strategies
- {
- public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
- {
- public Indicators.YR_PivotPoints YR_PivotPoints(NinjaTrader.NinjaScript.Indicators.YR_PivotPoints.PivotModeEnums _pivotMode, int _sessionStart, int _sessionEnd, bool _showMiddleLines, bool _symetricLogic, bool _showPivotLine, bool _showResistanceLine, bool _showSupportLine, bool _showLineLabels, bool _showPreviousDayLines, bool _showSessionVerticalLines)
- {
- return indicator.YR_PivotPoints(Input, _pivotMode, _sessionStart, _sessionEnd, _showMiddleLines, _symetricLogic, _showPivotLine, _showResistanceLine, _showSupportLine, _showLineLabels, _showPreviousDayLines, _showSessionVerticalLines);
- }
- public Indicators.YR_PivotPoints YR_PivotPoints(ISeries<double> input , NinjaTrader.NinjaScript.Indicators.YR_PivotPoints.PivotModeEnums _pivotMode, int _sessionStart, int _sessionEnd, bool _showMiddleLines, bool _symetricLogic, bool _showPivotLine, bool _showResistanceLine, bool _showSupportLine, bool _showLineLabels, bool _showPreviousDayLines, bool _showSessionVerticalLines)
- {
- return indicator.YR_PivotPoints(input, _pivotMode, _sessionStart, _sessionEnd, _showMiddleLines, _symetricLogic, _showPivotLine, _showResistanceLine, _showSupportLine, _showLineLabels, _showPreviousDayLines, _showSessionVerticalLines);
- }
- }
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement