View Full Version : Variable Declaration?
GreenTrade
06-26-2008, 03:21 PM
I would like to only access a variable if market position is flat. When I code the following I get the error message "Embedded statement cannot be a declaration or labeled statement."
protected override void OnBarUpdate()
{
if(Position.MarketPosition==MarketPosition.Flat)
double valuex = EMA(20);
//other random code//
}
Any ideas how to fix?
Thanks
GT
NinjaTrader_Ray
06-26-2008, 03:33 PM
double valuex = EMA(20)[0];
GreenTrade
06-29-2008, 01:48 PM
OK that was pretty dumb by me. Thanks for the quick fix. But what about declaring a variable with an equation? i.e.
protected override void OnBarUpdate()
{
//various entry logic
if(Position.MarketPosition==MarketPosition.Long)
double LongExit = ((Position.AvgPrice - MIN(Low, 20)[BarsSinceEntry)*3))+Position.AvgPrice;
SetProfitTarget(CalculationMode.Price, LongExit);
}
1) I am receiving the same error message listed in the earlier post. Any thoughts?
2) Do I need to use OnPositionUpdate for the profit target to begin working immediately upon entry or does the Position.MarketPosition==MarketPosition.Long accomplish that?
Thanks.
GT
NinjaTrader_Josh
06-29-2008, 03:54 PM
double LongExit = ((Position.AvgPrice - MIN(Low, 20)[BarsSinceEntry]*3))+Position.AvgPrice;
GreenTrade
07-03-2008, 09:44 AM
Two obvious typos in a row. Sorry and thanks. I was still getting the same error message until I "fixed" the code by adding brackets (shown below) but now the profit target always keeps the value from the initial long position on the chart which a lot of the time is way under the market. So when it enters a long position it exits on the same bar way under the actual market.
if(Position.MarketPosition==MarketPosition.Long)
{
double LongExit = ((Position.AvgPrice - MIN(Low,20)[BarsSinceEntry()] * 3)) + Position.AvgPrice;
SetProfitTarget(CalculationMode.Price, LongExit);
}
How do I reset/recalculate the profit target when I enter a new long position? Thanks.
GT
NinjaTrader_Ray
07-03-2008, 09:50 AM
You could add at the top of OnBarUpdate() something like:
if (Position.MarketPosition == MarketPosition.Flat)
SetProfitTarget(CalculationMode.Ticks, someReasonableOffsetValue);
Then once you have entered your position, call the method again and set the specific price you want.
GreenTrade
07-03-2008, 12:51 PM
Thanks again for the quick reply. When I try putting the code in the top of the OnBarUpdate() section, once the position is entered the profit target never recalculates with the code under if(Position.MarketPosition==Market.Position==Long) . I also took the variable LongExit out and added the calculation code into the SetProfitTarget line. If I leave the suggested code out it calculates correctly for only the first position. If I leave it in it never recalculates. Any more ideas on how to get this to recalculated for each new position? Code now looks like this:
if(Position.MarketPosition==MarketPosition.Long)
SetProfitTarget(CalculationMode.Price, ((Position.AvgPrice - MIN(Low,20)[BarsSinceEntry()] * 3)) + Position.AvgPrice) ;
Thanks again,
GT
NinjaTrader_Ray
07-03-2008, 01:34 PM
You likely will have to debug your code. Here is a reference sample that illustrates the basic concepts.
http://www.ninjatrader-support.com/vb/showthread.php?t=3222