Hi , I’m trying to make a basic indicator , an EMA that changes color based on Momentum . It has 4 different colors . I am a complete newbie at this so I had Google AI code it but I’m getting an error "Plot does not contain a constructor that takes 2 , CS1729 , Line 44 , Column 17
line 44 i believe is , AddPlot(new Plot(Brushes.Black, “EMA_Plot”));
Does that need to be changed ? Thanks ,
try to use
AddPlot(Brushes.Black, “EMA_Plot”);
or
AddPlot(new Stroke(Brushes.Black, 2), PlotStyle.Line, “EMA_Plot”);
1 Like
Both those produced more errors .
So the problem was not in this line of code.
If you want, send the code of your indicator - I will fix it.
1 Like
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.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.NinjaScript;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript.DrawingTools;
namespace NinjaTrader.NinjaScript.Indicators
{
public class MomentumColorEMA : Indicator
{
private EMA ema;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"EMA that dynamically changes color based on price momentum strength.";
Name = "MomentumColorEMA";
Calculate = Calculate.OnBarClose;
IsOverlay = true; // Draws directly on the price chart
DisplayInDataBox = true;
// User Configuration Parameter Defaults
EMAPeriod = 20;
MomentumPeriod = 3;
WeakThreshold = 0.10;
MediumThreshold = 0.25;
StrongThreshold = 0.50;
// Add the primary plot line. Defaulting to Black thickness 2.
AddPlot(new Stroke(Brushes.Black, 2), PlotStyle.Line, "EmaPlot");
}
else if (State == State.DataLoaded)
{
// Initialize the built-in NinjaTrader EMA series
ema = EMA(Close, EMAPeriod);
}
}
protected override void OnBarUpdate()
{
// Ensure enough bars exist to calculate the EMA and look back at momentum
if (CurrentBar < Math.Max(EMAPeriod, MomentumPeriod))
return;
// 1. Calculate the current EMA value
double currentEmaValue = ema[0];
Value[0] = currentEmaValue;
// 2. Measure momentum absolute difference over the chosen lookback period
double priorEmaValue = ema[MomentumPeriod];
double absoluteMomentum = Math.Abs(currentEmaValue - priorEmaValue);
// 3. Evaluate thresholds and apply your specific color schema dynamically per bar
if (absoluteMomentum >= StrongThreshold)
{
PlotBrushes[0][0] = Brushes.Magenta; // Most powerful strength
}
else if (absoluteMomentum >= MediumThreshold)
{
PlotBrushes[0][0] = Brushes.Yellow; // Good strength
}
else if (absoluteMomentum >= WeakThreshold)
{
PlotBrushes[0][0] = Brushes.White; // Gaining some strength
}
else
{
PlotBrushes[0][0] = Brushes.Black; // Weak / Flat momentum
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="EMA Period", Description="Lookback bars for the underlying EMA.", Order=1, GroupName="Parameters")]
public int EMAPeriod { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Momentum Lookback", Description="Bars to look back to measure EMA change.", Order=2, GroupName="Parameters")]
public int MomentumPeriod { get; set; }
[NinjaScriptProperty]
[Range(0.0, double.MaxValue)]
[Display(Name="Weak Threshold (White)", Description="Momentum required to switch from Black to White.", Order=3, GroupName="Parameters")]
public double WeakThreshold { get; set; }
[NinjaScriptProperty]
[Range(0.0, double.MaxValue)]
[Display(Name="Medium Threshold (Yellow)", Description="Momentum required to switch from White to Yellow.", Order=4, GroupName="Parameters")]
public double MediumThreshold { get; set; }
[NinjaScriptProperty]
[Range(0.0, double.MaxValue)]
[Display(Name="Strong Threshold (Magenta)", Description="Momentum required to switch from Yellow to Magenta.", Order=5, GroupName="Parameters")]
public double StrongThreshold { get; set; }
#endregion
}
}
Thanks ! Actually this is a different code . Tried using Ai again to make the same indicator . Are you able to get this one to work ? Thanks ,
It Is a basic indicator , an EMA that changes color based on momentum . 4 different colors . Black (weak) White (gaining some strength) Yellow (good strength) Magenta ( most powerful strength)
Try this code.
I tested it: it compiles fine in NinjaScript Editor and doesn’t generate any errors.
#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.DrawingTools;
#endregion
namespace NinjaTrader.NinjaScript.Indicators
{
public class MomentumColorEMA : Indicator
{
private EMA ema;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"";
Name = "MomentumColorEMA";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
IsSuspendedWhileInactive = true;
// User Configuration Parameter Defaults
EMAPeriod = 20;
MomentumPeriod = 3;
WeakThreshold = 0.10;
MediumThreshold = 0.25;
StrongThreshold = 0.50;
// Add the primary plot line. Defaulting to Black thickness 2.
AddPlot(new Stroke(Brushes.Black, 2), PlotStyle.Line, "EmaPlot");
}
else if (State == State.DataLoaded)
{
// Initialize the built-in NinjaTrader EMA series
ema = EMA(Close, EMAPeriod);
}
}
protected override void OnBarUpdate()
{
// Ensure enough bars exist to calculate the EMA and look back at momentum
if (CurrentBar < Math.Max(EMAPeriod, MomentumPeriod))
return;
// 1. Calculate the current EMA value
double currentEmaValue = ema[0];
Value[0] = currentEmaValue;
// 2. Measure momentum absolute difference over the chosen lookback period
double priorEmaValue = ema[MomentumPeriod];
double absoluteMomentum = Math.Abs(currentEmaValue - priorEmaValue);
// 3. Evaluate thresholds and apply your specific color schema dynamically per bar
if (absoluteMomentum >= StrongThreshold)
{
PlotBrushes[0][0] = Brushes.Magenta; // Most powerful strength
}
else if (absoluteMomentum >= MediumThreshold)
{
PlotBrushes[0][0] = Brushes.Yellow; // Good strength
}
else if (absoluteMomentum >= WeakThreshold)
{
PlotBrushes[0][0] = Brushes.White; // Gaining some strength
}
else
{
PlotBrushes[0][0] = Brushes.Black; // Weak / Flat momentum
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="EMA Period", Description="Lookback bars for the underlying EMA.", Order=1, GroupName="Parameters")]
public int EMAPeriod { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Momentum Lookback", Description="Bars to look back to measure EMA change.", Order=2, GroupName="Parameters")]
public int MomentumPeriod { get; set; }
[NinjaScriptProperty]
[Range(0.0, double.MaxValue)]
[Display(Name="Weak Threshold (White)", Description="Momentum required to switch from Black to White.", Order=3, GroupName="Parameters")]
public double WeakThreshold { get; set; }
[NinjaScriptProperty]
[Range(0.0, double.MaxValue)]
[Display(Name="Medium Threshold (Yellow)", Description="Momentum required to switch from White to Yellow.", Order=4, GroupName="Parameters")]
public double MediumThreshold { get; set; }
[NinjaScriptProperty]
[Range(0.0, double.MaxValue)]
[Display(Name="Strong Threshold (Magenta)", Description="Momentum required to switch from Yellow to Magenta.", Order=5, GroupName="Parameters")]
public double StrongThreshold { get; set; }
#endregion
}
}