- Hi
I'd really appreciate some here here.
New to NT and trying to cobble together a few indicators. One of which, is that i want to show in an Analyzer window if Price closes above or below a pivot point. I've got this far, but i'm getting a
CS0200 error "Property or indexer 'NinjaTraderScript.NinjaScript.NinjaScriptBase.thi s[int]' cannot be assiggned to - is is read only.
Here's my code.
Thanks
Swing
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class cbCvsPPtest : Indicator
{
private SMA PPoint;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Indicator here.";
Name = "cbCvsPPtest";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
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;
AddPlot(Brushes.Orange, "Direction");
}
else if (State == State.DataLoaded)
{
PPoint[0] = (High[1]+Low[1]+Close[1])/3;
}
}
protected override void OnBarUpdate()
{
Direction[0] = Close[0]>PPoint[0] ? 1 : Close[0]<PPoint[0] ? -1 : 0;
}
I then tried this, which is maybe better, but i'm still getting the same error.
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class cbCvsPPtest2 : Indicator
{
private Series<double> PPoint;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Indicator here.";
Name = "cbCvsPPtest2";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
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;
AddPlot(Brushes.Orange, "Direction");
}
else if (State == State.DataLoaded)
{
PPoint = new Series<double>(this);
}
}
protected override void OnBarUpdate()
{
PPoint[0] = (High[1]+Low[1]+Close[1])/3;
Direction[0] = Close[0]>PPoint[0] ? 1 : Close[0]<PPoint[0] ? -1 : 0;
}
Any advice please?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Simple coding help for a newcomer please.
Collapse
X
-
Simple coding help for a newcomer please.
Tags: None
-
Hello Swing123,
Thanks for your post.
Your second code is correct however it would generate a run-time error when you apply it to a chart. When you run a script and it does not appear correct or at all, always check the "Log" tab of the control center for any related error messages. Likely you would see, in this case, an "error calling OnBarUpdate method on bar 0".
The error will occur because when you script is loaded it will start processing the first bar at the vert beginning of the data series. When the first bar loads your statement PPoint[0] = (High[1]+Low[1]+Close[1])/3 will generate the error because it is trying to access the previous bar [1] which at that point does not exist. To prevent this error you would add a check of the CurrentBar which is an system provided int variable that holds the bar number being processed. So you can delay your script from processing until the CurrentBar is more than 1. Typically this is added at the top of the OnBarUpdate() like this:
if (CurrentBar < 1) return; // do not process code below until we have 2 bars (Bar counting starts at 0).
NOTE: Regarding, "I then tried this, which is maybe better, but i'm still getting the same error." If you are seeing the same error, be aware that the compiler will compile all scripts together at once so an error in any script will show up as an error at the bottom of any script that is open in the editor. On the very left of the error message(s) will be the specific ninjascript that is the source of the error(s). If this is the case for you then you can either right mouse click on the file in the Ninjascript explorer and select "remove" or exclude from compilation", either selection would allow you to then recompile. Reference: https://ninjatrader.com/support/help...ile_errors.htm
-
Hi Paul
Thank you for the quick reply.
I think you might think i undeerstand more than i do.
When you say "1. Typically this is added at the top of the OnBarUpdate() like this:" I can't even find in my code where i would put this...
Can you please show me.
Thanks
Chris
Comment
-
Hello Swing123,
Thanks for your replies.
The 2nd set of code (cbCvsPPtest2) that you posted was what I was referring to, which does not include the SMA.
In the 2nd set of code(cbCvsPPtest2), here is what I am suggesting:
protected override void OnBarUpdate()
{
if (CurrentBar < 1) return; // do not process code below until we have 2 bars (Bar counting starts at 0).
PPoint[0] = (High[1]+Low[1]+Close[1])/3;
Direction[0] = Close[0]>PPoint[0] ? 1 : Close[0]<PPoint[0] ? -1 : 0;
}
Note: You should remove or exclude from compiling the file cbCvsPPtest.cs otherwise the same error will continue to show. All files are compiled at the same time and an error in any file will prevent a successful compilation.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
650 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
370 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
574 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
577 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment