Now my question: Can the horizontal line be cut off so it goes let's say only 5 bars back? (like to the right in the attached image). How would I need to address this coding wise?
sandman
// // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>. // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release. // #region Using declarations using System; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.ComponentModel; using System.Xml.Serialization; using NinjaTrader.Data; using NinjaTrader.Gui.Chart; #endregion // This namespace holds all indicators and is required. Do not change it. namespace NinjaTrader.Indicator { /// <summary> /// The HorizontalSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time. /// </summary> [Description("The HorizontalSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.")] public class HorizontalSMA : Indicator { #region Variables private int period = 14; [B] private int barsBack = 5; private Color smaLineColor = Color.Orange;[/B] #endregion /// <summary> /// This method is used to configure the indicator and is called once before any bar data is loaded. /// </summary> protected override void Initialize() { Add(new Plot([B]Color.Transparent[/B], "HorizontalSMA")); Overlay = true; } /// <summary> /// Called on each bar update event (incoming tick). /// </summary> protected override void OnBarUpdate() { if (CurrentBar == 0) Value.Set(Input[0]); else { double last = Value[1] * Math.Min(CurrentBar, Period); if (CurrentBar >= Period) Value.Set((last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period)); else Value.Set((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1)); } [B] if(CurrentBar > barsBack) DrawLine("HorizontalSMA", 0, Value[0], barsBack, Value[0], smaLineColor);[/B] } #region Properties /// <summary> /// </summary> [Description("Numbers of bars used for calculations")] [GridCategory("Parameters")] public int Period { get { return period; } set { period = Math.Max(1, value); } } [B] [Description("Numbers of bars back to draw the HorizontalSMA line")] [GridCategory("Parameters")] public int BarsBack { get { return barsBack; } set { barsBack = Math.Max(1, value); } } [XmlIgnore()] [Description("Color of the HorizontalSMA line")] [GridCategory("Parameters")] public Color SmaLineColor { get { return smaLineColor; } set { smaLineColor = value; } } [Browsable(false)] public string SmaLineColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(smaLineColor); } set { smaLineColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } }[/B] #endregion } } #region NinjaScript generated code. Neither change nor remove. // This namespace holds all indicators and is required. Do not change it. namespace NinjaTrader.Indicator { public partial class Indicator : IndicatorBase { private HorizontalSMA[] cacheHorizontalSMA = null; private static HorizontalSMA checkHorizontalSMA = new HorizontalSMA(); /// <summary> /// The HorizontalSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time. /// </summary> /// <returns></returns> public HorizontalSMA HorizontalSMA(int barsBack, int period, Color smaLineColor) { return HorizontalSMA(Input, barsBack, period, smaLineColor); } /// <summary> /// The HorizontalSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time. /// </summary> /// <returns></returns> public HorizontalSMA HorizontalSMA(Data.IDataSeries input, int barsBack, int period, Color smaLineColor) { if (cacheHorizontalSMA != null) for (int idx = 0; idx < cacheHorizontalSMA.Length; idx++) if (cacheHorizontalSMA[idx].BarsBack == barsBack && cacheHorizontalSMA[idx].Period == period && cacheHorizontalSMA[idx].SmaLineColor == smaLineColor && cacheHorizontalSMA[idx].EqualsInput(input)) return cacheHorizontalSMA[idx]; lock (checkHorizontalSMA) { checkHorizontalSMA.BarsBack = barsBack; barsBack = checkHorizontalSMA.BarsBack; checkHorizontalSMA.Period = period; period = checkHorizontalSMA.Period; checkHorizontalSMA.SmaLineColor = smaLineColor; smaLineColor = checkHorizontalSMA.SmaLineColor; if (cacheHorizontalSMA != null) for (int idx = 0; idx < cacheHorizontalSMA.Length; idx++) if (cacheHorizontalSMA[idx].BarsBack == barsBack && cacheHorizontalSMA[idx].Period == period && cacheHorizontalSMA[idx].SmaLineColor == smaLineColor && cacheHorizontalSMA[idx].EqualsInput(input)) return cacheHorizontalSMA[idx]; HorizontalSMA indicator = new HorizontalSMA(); indicator.BarsRequired = BarsRequired; indicator.CalculateOnBarClose = CalculateOnBarClose; #if NT7 indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256; indicator.MaximumBarsLookBack = MaximumBarsLookBack; #endif indicator.Input = input; indicator.BarsBack = barsBack; indicator.Period = period; indicator.SmaLineColor = smaLineColor; Indicators.Add(indicator); indicator.SetUp(); HorizontalSMA[] tmp = new HorizontalSMA[cacheHorizontalSMA == null ? 1 : cacheHorizontalSMA.Length + 1]; if (cacheHorizontalSMA != null) cacheHorizontalSMA.CopyTo(tmp, 0); tmp[tmp.Length - 1] = indicator; cacheHorizontalSMA = tmp; return indicator; } } } } // This namespace holds all market analyzer column definitions and is required. Do not change it. namespace NinjaTrader.MarketAnalyzer { public partial class Column : ColumnBase { /// <summary> /// The HorizontalSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time. /// </summary> /// <returns></returns> [Gui.Design.WizardCondition("Indicator")] public Indicator.HorizontalSMA HorizontalSMA(int barsBack, int period, Color smaLineColor) { return _indicator.HorizontalSMA(Input, barsBack, period, smaLineColor); } /// <summary> /// The HorizontalSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time. /// </summary> /// <returns></returns> public Indicator.HorizontalSMA HorizontalSMA(Data.IDataSeries input, int barsBack, int period, Color smaLineColor) { return _indicator.HorizontalSMA(input, barsBack, period, smaLineColor); } } } // This namespace holds all strategies and is required. Do not change it. namespace NinjaTrader.Strategy { public partial class Strategy : StrategyBase { /// <summary> /// The HorizontalSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time. /// </summary> /// <returns></returns> [Gui.Design.WizardCondition("Indicator")] public Indicator.HorizontalSMA HorizontalSMA(int barsBack, int period, Color smaLineColor) { return _indicator.HorizontalSMA(Input, barsBack, period, smaLineColor); } /// <summary> /// The HorizontalSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time. /// </summary> /// <returns></returns> public Indicator.HorizontalSMA HorizontalSMA(Data.IDataSeries input, int barsBack, int period, Color smaLineColor) { if (InInitialize && input == null) throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method"); return _indicator.HorizontalSMA(input, barsBack, period, smaLineColor); } } } #endregion
[COLOR=#000000][FONT=Tahoma][LEFT][LEFT] [FONT=Courier New][SIZE=2][COLOR=#0000FF]protected[/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000FF]override[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#0000FF]void[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]OnBarUpdate()[/SIZE][/FONT] [FONT=Courier New][SIZE=2] {[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#0000FF]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (CurrentBar > [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]14[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]){[/SIZE][/FONT] [FONT=Courier New][SIZE=2] DrawHorizontalLine([/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]"5BaragoSMALine"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] , SMA([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]14[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])[[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]5[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]], Color.Black); [/SIZE][/FONT] [FONT=Courier New][SIZE=2] }[/SIZE][/FONT] [FONT=Courier New][SIZE=2] }[/SIZE][/FONT][/LEFT] [/FONT][/COLOR]
[COLOR=#000000][FONT=Tahoma][LEFT][LEFT][LEFT][LEFT][LEFT][FONT=Courier New][SIZE=2] [/SIZE] [FONT=Courier New][SIZE=2][COLOR=#0000FF]protected[/SIZE][/FONT][FONT=Courier New][SIZE=2] [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000FF]override[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#0000FF]void[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]OnBarUpdate()[/SIZE][/FONT] [FONT=Courier New][SIZE=2] {[/SIZE][/FONT] [FONT=Courier New][SIZE=2] [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000FF]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (CurrentBar > [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]14[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]){[/SIZE][/FONT] [FONT=Courier New][SIZE=2] [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000FF]for[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] ([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000FF]int[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]i = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]; i < [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]5[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]; i++)[/SIZE][/FONT] [FONT=Courier New][SIZE=2]{[/SIZE][/FONT] [FONT=Courier New][SIZE=2]DrawLine( [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]"SMA"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] + i.ToString(), [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000FF]false[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] , i + [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2], SMA( [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]14[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])[i + [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]], i, SMA( [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]14[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] )[i], Color.Black, DashStyle.Solid, [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]);}[/SIZE][/FONT] [FONT=Courier New][SIZE=2]} }[/SIZE][/FONT][/LEFT] [/FONT][/COLOR]
#region Variables // Wizard generated variables private int period = 10; // Default setting for Period private int barsBack = 7; // Default setting for BarsBack private int wline = 3; // Default setting for Wline private Color downColor = Color.Red; private Color upColor = Color.Blue; private DataSeries Neutral; // User defined variables (add any user defined variables below) #endregion /// <summary> /// This method is used to configure the indicator and is called once before any bar data is loaded. /// </summary> protected override void Initialize() { Neutral = new DataSeries(this); Overlay = true; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { // Use this method for calculating your indicator values. Assign a value to each // plot below by replacing 'Close[0]' with your own formula. if(CurrentBar < period) return; Neutral.Set(LinReg(period)[0]); if(CurrentBar > period) { for (int i = 0; i < barsBack; i++) { DrawDot("string" , true, barsBack, Close[barsBack], Color.Black); if(Neutral[i] < Neutral[i+1]) { DrawLine("Line" + i, false, i + 1, LinReg(period)[i + 1], i, LinReg(period)[i], downColor, DashStyle.Solid, wline); } else { DrawLine("Line" + i, false, i + 1, LinReg(period)[i + 1], i, LinReg(period)[i], upColor, DashStyle.Solid, wline); } } } }
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by f.saeidi, Today, 12:12 AM
|
0 responses
3 views
0 likes
|
Last Post
by f.saeidi
Today, 12:12 AM
|
||
Started by Conall, Today, 12:06 AM
|
0 responses
5 views
0 likes
|
Last Post
by Conall
Today, 12:06 AM
|
||
Started by proptradingshop, 03-21-2024, 09:50 AM
|
6 responses
27 views
0 likes
|
Last Post Today, 12:02 AM | ||
Started by mly916, Yesterday, 09:48 PM
|
1 response
12 views
0 likes
|
Last Post Yesterday, 11:54 PM | ||
Started by Mikey_, Yesterday, 09:21 PM
|
0 responses
11 views
0 likes
|
Last Post
by Mikey_
Yesterday, 09:21 PM
|
Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing one’s financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.
CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.
This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates.
Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.
Comment