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
-
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.
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
-
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
-
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
-
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 owensd, 04-21-2024, 11:34 PM
|
9 responses
34 views
0 likes
|
Last Post Today, 11:59 AM | ||
Started by trilliantrader, 04-10-2024, 09:33 PM
|
7 responses
25 views
0 likes
|
Last Post Today, 11:56 AM | ||
Started by traderqz, Today, 12:06 AM
|
5 responses
11 views
0 likes
|
Last Post Today, 11:34 AM | ||
Started by Mongo, Today, 11:05 AM
|
2 responses
10 views
0 likes
|
Last Post
by Mongo
Today, 11:29 AM
|
||
Started by guillembm, Today, 11:25 AM
|
0 responses
5 views
0 likes
|
Last Post
by guillembm
Today, 11:25 AM
|
Comment