I'm trying to create an EMA of a variable and keep getting error messages. Any help u could provide would be greatly appreciated.
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
EMA code
Collapse
X
-
please , have picture of the error message and error code or the programming line of the added variable.
If you are unsure as to what the error is indicating, please post a screenshot of the NinjaScript File column and Error column fully visible (not to be confused with the Code column)- To send a screenshot with Windows 7 or newer I would recommend using the Windows Snipping Tool.
- Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save as a jpeg file and send the file as an attachment.
Last edited by Emma1; 06-15-2022, 05:44 AM.
-
Hello JWeather,
Thank you for writing in.
Before providing any further information, I wanted to verify if you were working on a NinjaScript Strategy that you are creating yourself or if you are working with a NinjaScript Strategy that you received from someone else or a 3rd party developer.- This post is in the Platform Technical Support section of the Forum vs. the "Strategy Development" or "Indicator Development" section of the forum so I want to make sure
Clayton G.NinjaTrader Customer Service
Comment
-
I copied the LInear Regression code from NT8 which is where the Value is calculating. I was trying to bound the indicator and smooth it out with the EMA. The error message I'm getting is that "Cannot implicity convert EMA to double".
Value[0] = CurrentBar == 0 ? input0 : (intercept + slope * (myPeriod - 1));
}
double Value0 = Value[0];
double Value1 = Value[1];
if (Value0 > Value1)
{double LRR = 1.0;
}
else
{double LRR = -1.0;
}
{
double JLRR = EMA(LRR,14);
}}
Comment
-
HI JWeather, I would need you to post the rest of your script for me to know whats going on. Use Print(); in your script to see what the value of everything is e.g.
Print(Value[0]);
Print(JLRR );
Print(Time[0]);
Also check the Log tab of the Control Center for any errors.Chris L.NinjaTrader Customer Service
Comment
-
LRR needs to be a Series<double>, not a Double variable.
Then:
if (Value0 > Value1)
{LRR[0] = 1.0;
}
else
{LRR[0] = -1.0;
}
{
double JLRR = EMA(LRR,14);Last edited by eDanny; 06-16-2022, 11:12 AM.
Comment
-
Chris,
I'm getting an error message of:
Indicator 'JWLRR': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
I've copied the script as you suggested. I'm obviously a novice at this NT code - any help you could provide would be greatly appreciated.
/// </summary>
public class JWLRR : Indicator
{
private Series<double> LRR;
private double avg;
private double divisor;
private double intercept;
private double myPeriod;
private double priorSumXY;
private double priorSumY;
private double slope;
private double sumX2;
private double sumX;
private double sumXY;
private double sumY;
private SUM sum;
private double JLRR;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{//Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionNameJWLRR;
Name = "JWLRR";
Calculate = Calculate.OnBarClose;
//DisplayInDataBox = true;
//DrawonPricePanel = false;
IsOverlay = false;
IsSuspendedWhileInactive = true;
Period = 14;
AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meLinReg);
AddPlot(Brushes.DodgerBlue, "MyPlot");
}
else if (State == State.Configure)
{
avg = divisor = intercept = myPeriod = priorSumXY
= priorSumY = slope = sumX = sumX2 = sumY = sumXY = 0;
}
else if (State == State.DataLoaded)
{
sum = SUM(Inputs[0], Period);
}
}
protected override void OnBarUpdate()
{
if (BarsArray[0].BarsType.IsRemoveLastBarSupported)
{
double sumX = (double)Period * (Period - 1) * 0.5;
double divisor = sumX * sumX - (double)Period * Period * (Period - 1) * (2 * Period - 1) / 6;
double sumXY = 0;
for (int count = 0; count < Period && CurrentBar - count >= 0; count++)
sumXY += count * Input[count];
double slope = ((double)Period * sumXY - sumX * SUM(Inputs[0], Period)[0]) / divisor;
double intercept = (SUM(Inputs[0], Period)[0] - slope * sumX) / Period;
Value[0] = intercept + slope * (Period - 1);
}
else
{
if (IsFirstTickOfBar)
{
priorSumY = sumY;
priorSumXY = sumXY;
myPeriod = Math.Min(CurrentBar + 1, Period);
sumX = myPeriod * (myPeriod - 1) * 0.5;
sumX2 = myPeriod * (myPeriod + 1) * 0.5;
divisor = myPeriod * (myPeriod + 1) * (2 * myPeriod + 1) / 6 - sumX2 * sumX2 / myPeriod;
}
double input0 = Input[0];
sumXY = priorSumXY - (CurrentBar >= Period ? priorSumY : 0) + myPeriod * input0;
sumY = priorSumY + input0 - (CurrentBar >= Period ? Input[Period] : 0);
avg = sumY / myPeriod;
slope = (sumXY - sumX2 * avg) / divisor;
intercept = (sum[0] - slope * sumX) / myPeriod;
Value[0] = CurrentBar == 0 ? input0 : (intercept + slope * (myPeriod - 1));
}
double Value0 = Value[0];
double Value1 = Value[1];
if (Value0 > Value1)
{LRR[0] = 1.0;
}
else
{LRR[0] = -1.0;
}
{
double JLRR = EMA(LRR,14)[0];
}
{ MyPlot[1] = JLRR;
}
Print(Value[0]);
Print(Value1);
Print(LRR);
}
#region Properties
[Range(2, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
public int Period
{ get; set; }
[Browsable(false)]
[XmlIgnore]
public Series<double> MyPlot
{
get { return Values[1]; }
}
Comment
-
Hi JWeather, thanks for posting that. The First thing I notice is the use of if (IsFirstTickOfBar) in a strategy that runs OnBarUpdate. That value will only vary if the strategy runs OnEachTick or OnPriceChange. The indexing error can be found using Visual Studio debugging. You can attach Visual Studio to the NinjaTrader process, run the code, and Visual Studio will break at the line that caused the exception.
Kind regards,
-ChrisLChris L.NinjaTrader Customer Service
- Likes 1
Comment
-
I've been able to get the code to calculate an EMA of a Value by assigning a double Average to the EMA;
double Average = EMA(Value,14)[0]
but I need to index the EMA from the prior bar. As I understand it, you can only index Series <double> so how would I do that?
Thanks,
JWeather
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by warreng86, 11-10-2020, 02:04 PM
|
7 responses
1,360 views
0 likes
|
Last Post Today, 09:41 PM | ||
Started by Perr0Grande, Today, 08:16 PM
|
0 responses
5 views
0 likes
|
Last Post
by Perr0Grande
Today, 08:16 PM
|
||
Started by elderan, Today, 08:03 PM
|
0 responses
9 views
0 likes
|
Last Post
by elderan
Today, 08:03 PM
|
||
Started by algospoke, Today, 06:40 PM
|
0 responses
10 views
0 likes
|
Last Post
by algospoke
Today, 06:40 PM
|
||
Started by maybeimnotrader, Today, 05:46 PM
|
0 responses
14 views
0 likes
|
Last Post Today, 05:46 PM |
Comment