A variation on the theme is actually available in the Samples subForum. ref: http://www.ninjatrader.com/support/f...ead.php?t=6651
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Referring to MFE in automated trading startegy
Collapse
X
-
Technically, NT does not allow Strategies to communicate with Indicators, but it is all just C#, so conceptually, you just need the strategy to update a double value in the indicator, and use that value to set the Plot. You would perform the update through the publicly exposed properties of the double, using the setter.Originally posted by pandyav View Post
A variation on the theme is actually available in the Samples subForum. ref: http://www.ninjatrader.com/support/f...ead.php?t=6651Last edited by koganam; 01-02-2014, 05:12 PM.
-
-
Originally posted by NinjaTrader_JC View PostHello koganam,
It is changing the Plot Style of the StrategyPlot Indicator just like changing the color. The reason why I referenced it like that is because since PlotStyle is a Indicator Object you cannot simply use PlotStyle.Bar to reference this is all.
Oops. Did not see that you were referencing StrategyPlot in the first place. Mea culpa.
Comment
-
Hi Koganam,
I took the original code you had provided to identify the max profit of a trade, but I keep receiving the compiling errors.
Here is what I have under OnBarUpdate:
if (Position.MarketPosition == MarketPosition.Long)
{
double CurrentBarHighestGain = High[0] - Position.AvgPrice;
}
if (Position.MarketPosition == MarketPosition.Short)
{
double CurrentBarHighestGain = Position.AvgPrice-Low[0];
}
double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);
For some reason, I received the following errors:
- Use of unassigned local variable 'HighestGain'
- The name 'CurrentBarHighestGain' does not exist in the current context
Can you please help to understand what could be wrong? I have not made any changes except trying to calculate HighestGain for both long and short sides.
Any ideas what could be wrong here?
Very much appreciate your help Koganam.
Comment
-
Quite to the contrary, you have made multiple changes that are causing your issues.Originally posted by pandyav View PostHi Koganam,
I took the original code you had provided to identify the max profit of a trade, but I keep receiving the compiling errors.
Here is what I have under OnBarUpdate:
if (Position.MarketPosition == MarketPosition.Long)
{
double CurrentBarHighestGain = High[0] - Position.AvgPrice;
}
if (Position.MarketPosition == MarketPosition.Short)
{
double CurrentBarHighestGain = Position.AvgPrice-Low[0];
}
double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);
For some reason, I received the following errors:
- Use of unassigned local variable 'HighestGain'
- The name 'CurrentBarHighestGain' does not exist in the current context
Can you please help to understand what could be wrong? I have not made any changes except trying to calculate HighestGain for both long and short sides.
Any ideas what could be wrong here?
Very much appreciate your help Koganam.
HighestGain was defined as a class variable. You have changed it to a local variable that you tried to use before you initialized it. If it is to survive each tick update, it must be declared as a class variable. Moreover, the very act of declaring it as a class variable, would have initialized it, yet I specifically initialized it for redundancy.
Your CurrentBarHighestGain is in each case defined as a local variable, delimited by your scope braces. You can do what you are trying to do, but in that case declare and initialize the variable before and outside any of the blocks that will use it, and just assign a value in your position filter blocks. As the positions are mutually exclusive, you can use the same variable for both sides, just as you have used it now for both sides. You just have to declare it with proper scope.
OTOH, if you really prefer to use your current scheme, where CurrentBarHighestGain is a local variable, then you want to calculate HighestGain in the same block. HighestGain will still have to be declared with class scope.
Comment
-
Thanks Koganam,
Trying my best to follow your advise here. This is what I have now:
Variables:
private double HighestGain = 0;
OnBarUpdate:
if (Position.MarketPosition == MarketPosition.Long)
{
double CurrentBarHighestGain = High[0] - Position.AvgPrice;
double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);
}
if (Position.MarketPosition == MarketPosition.Short)
{
double CurrentBarHighestGain = Position.AvgPrice-Low[0];
double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);
}
I get the following error message: Use of unassigned local variable 'HighestGain'
At least I was able to remove one of the prior error messages. This time I've put the calculation of HighestGain within the same block as CurrentBarHighestGain. Just now sure what else is wrong?
Any thoughts?
Comment
-
You want to use the class variable for holding the HighestGain, as it must survive calls to the event handler. You are again trying to declare it as local. Remove the attempt to declare it again as local: remove double from in front of it.Originally posted by pandyav View PostThanks Koganam,
Trying my best to follow your advise here. This is what I have now:
Variables:
private double HighestGain = 0;
OnBarUpdate:
if (Position.MarketPosition == MarketPosition.Long)
{
double CurrentBarHighestGain = High[0] - Position.AvgPrice;
double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);
}
if (Position.MarketPosition == MarketPosition.Short)
{
double CurrentBarHighestGain = Position.AvgPrice-Low[0];
double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);
}
I get the following error message: Use of unassigned local variable 'HighestGain'
At least I was able to remove one of the prior error messages. This time I've put the calculation of HighestGain within the same block as CurrentBarHighestGain. Just now sure what else is wrong?
Any thoughts?
Comment
-
So I was able to compile the code by removing double. The next task I took on was to plot the values of HighestGain in a form of a bar by using the sample example link you forwarded earlier.
This is what I have
Initialize:
Add(StrategyPlot(0));
Add(StrategyPlot(1));
// Set the color for the indicator plots
StrategyPlot(0).Plots[0].Pen.Color = Color.Blue;
StrategyPlot(1).Plots[0].Pen.Color = Color.YellowGreen;
StrategyPlot(0).Plots[0].PlotStyle = PlotStyle.Bar;
StrategyPlot(1).Plots[0].PlotStyle = PlotStyle.Bar;
StrategyPlot(0).Plots[0].Pen.Width = 4;
StrategyPlot(1).Plots[0].Pen.Width = 4;
// Set the panel which the plots will be placed on. 1 = price panel, 2 = panel under the price panel, etc.
StrategyPlot(0).PanelUI = 2;
StrategyPlot(1).PanelUI = 2;
OnBarUpdate
StrategyPlot(0).Value.Set(HighestGain);
StrategyPlot(1).Value.Set(HighestGain);
As you can see in the attached chart, all I get is a single value of HighestGain and it does not change as the position moves in my favor. Should I be placing StrategyPlot(0) and (1) in some different fashion?
What am I missing here? Thank you again for all your help so far.
Comment
-
Without knowing exactly where you placed your code, it would not be possible to determine the exact cause of the issue here. You will want to Print() out the values that you are trying to use, in order to be sure that the code is doing what you want it to do. Print() will send output to the Output Window.Originally posted by pandyav View PostSo I was able to compile the code by removing double. The next task I took on was to plot the values of HighestGain in a form of a bar by using the sample example link you forwarded earlier.
This is what I have
Initialize:
Add(StrategyPlot(0));
Add(StrategyPlot(1));
// Set the color for the indicator plots
StrategyPlot(0).Plots[0].Pen.Color = Color.Blue;
StrategyPlot(1).Plots[0].Pen.Color = Color.YellowGreen;
StrategyPlot(0).Plots[0].PlotStyle = PlotStyle.Bar;
StrategyPlot(1).Plots[0].PlotStyle = PlotStyle.Bar;
StrategyPlot(0).Plots[0].Pen.Width = 4;
StrategyPlot(1).Plots[0].Pen.Width = 4;
// Set the panel which the plots will be placed on. 1 = price panel, 2 = panel under the price panel, etc.
StrategyPlot(0).PanelUI = 2;
StrategyPlot(1).PanelUI = 2;
OnBarUpdate
StrategyPlot(0).Value.Set(HighestGain);
StrategyPlot(1).Value.Set(HighestGain);
As you can see in the attached chart, all I get is a single value of HighestGain and it does not change as the position moves in my favor. Should I be placing StrategyPlot(0) and (1) in some different fashion?
What am I missing here? Thank you again for all your help so far.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
110 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
59 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
37 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
41 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
78 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment