protected override void OnBarUpdate() { Upper0.Set(MAX(High, LongPeriod)[0]); Lower0.Set(MIN(Low, LongPeriod)[0]); ShortStopATR.Set(Close[0] + (nAtr * ATR(8)[0])); LongStopATR.Set(Close[0] - (nAtr * ATR(8)[0])); }
Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
Index outside bounds error....
Collapse
X
-
Index outside bounds error....
I am receiving this error in my log when I am enabling my strategy that reverts to this indicator I have created. Not sure how to fix it. Can you help please? Thank you. See attached error and code.
Code:Tags: None
-
It does not work when I try to add just the indicator to the chart. When I put it in my strategy, I use the below code.
Code:Add(DonchianChannelATR(IndicatorSensitivity, LongEntryBand, NAtr));
Comment
-
Thanks for the reply. First step in troubleshooting this is getting the indicator to plot by itself, since the strategy doesn't actually use its values but only attempts to add for visualization.
Can you please share the indicator script you're having trouble plotting and we'll take a look?Ryan M.NinjaTrader Customer Service
Comment
-
Thanks Ryan.Code:#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 { [Description("")] public class DonchianChannelATR : Indicator { #region Variables private int longPeriod = 480; private double nAtr = 2.2; private int indicatorSensitivity = 8; #endregion protected override void Initialize() { Add(new Plot(Color.Blue, "Upper0")); Add(new Plot(Color.Blue, "Lower0")); Add(new Plot(Color.Green, "ShortStopATR")); Add(new Plot(Color.Red, "LongStopATR")); Overlay = true; } protected override void OnBarUpdate() { if (CurrentBar < LongPeriod) return; Upper0.Set(MAX(High, LongPeriod)[0]); Lower0.Set(MIN(Low, LongPeriod)[0]); ShortStopATR.Set(Close[0] + (nAtr * ATR(8)[0])); LongStopATR.Set(Close[0] - (nAtr * ATR(8)[0])); } #region Properties [Description("Numbers of bars used for calculations")] [GridCategory("Parameters")] public int LongPeriod { get { return longPeriod; } set { longPeriod = Math.Max(1, value); } } [Description("Numbers of deviations used for calcuations")] [GridCategory("Parameters")] public double NAtr { get { return nAtr; } set { nAtr = Math.Max(1, value); } } [Description("Indicator smoothing")] [GridCategory("Parameters")] public int IndicatorSensitivity { get { return indicatorSensitivity; } set { indicatorSensitivity = Math.Max(1, value); } } [Browsable(false)] [XmlIgnore()] public DataSeries Upper0 { get { return Values[0]; } } [Browsable(false)] [XmlIgnore()] public DataSeries Lower0 { get { return Values[1]; } } [Browsable(false)] [XmlIgnore()] public DataSeries ShortStopATR { get { return Values[4]; } } [Browsable(false)] [XmlIgnore()] public DataSeries LongStopATR { get { return Values[5]; } } #endregion } }
Comment
-
Thanks for posting the snippet. There are a couple things to check:
Make sure MaximumBarsLookBack is set to infinite, since your lookback is greater than 256.
Then in your properties region, you skip some Values. Please see the corrected properties region below for this, with Values going correctly from 0 - 3
Code:[Browsable(false)] [XmlIgnore()] public DataSeries Upper0 { get { return Values[COLOR="Red"][0][/COLOR]; } } [Browsable(false)] [XmlIgnore()] public DataSeries Lower0 { get { return Values[COLOR="red"][1][/COLOR]; } } [Browsable(false)] [XmlIgnore()] public DataSeries ShortStopATR { get { return Values[COLOR="red"][2];[/COLOR] } } [Browsable(false)] [XmlIgnore()] public DataSeries LongStopATR { get { return Values[COLOR="red"][3][/COLOR]; } }
Ryan M.NinjaTrader Customer Service
Comment
-
Let me ask you this... would there be a way to make both the LongStop and ShortStop online increase or decrease respectively? If you look at the plot, the stop line moves in accordance with price. When price moves down I would like to make the LongStop not move down with price. Is this possible? THanks again.
Comment
-
I want plots LongStopATR and ShortStopATR to act like a trailing stop. So once it enters a position upon the price hitting say a 5 day high, the ATR plot that I have set up will follow price upwards but not it's movement downwards. Doest that make sense?
Comment
-
Yes, that makes sense. It will require some custom coding to implement.
Math.Max() would be good for this to take the higher of either 1) the current value of the indicator 2) the highest value of the indicator since entry. Reverse for the short side. -Math.Min() and MIN()
Basic example using SMA below:
double myStopPrice = Math.Max(SMA(7)[0], MAX(SMA(7), BarsSinceEntry())[0]);
Relevant methods linked for this example:
Math.Max()
MAX()
BarsSinceEntry()Ryan M.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by WHICKED, 03-27-2024, 12:33 PM
|
4 responses
41 views
0 likes
|
Last Post Today, 12:28 AM | ||
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 |
Comment