PDA

View Full Version : Profit and Stop


Michael1111
10-23-2009, 02:48 AM
Hi,

I'm wanting to place a profit target and stop loss when I enter into the market.
I have been playing around with this and looking in the forum but I cant quite script it in the correct way.

- I want my profit target to = ATR20 on bar "0".
SetProfitTarget(????????????????)

- And I want my Stop loss to = Low of bar "0", -2 ticks.
SetStopLoss(??????????????????)

Can you please let me know how to script this and also let me know if it goes under Initialize() or OnBarUpdate()


Many Thanks,

Michael

NinjaTrader_Bertrand
10-23-2009, 05:19 AM
Michael, please take a look at this sample here - http://www.ninjatrader-support2.com/vb/showthread.php?t=3222

You would need to set some default value in the Initialize() for both Target and Stop, then revert to this when you strategy is flat, as in the above sample.

For your dynamic adjustments to those, the OnBarUpdate() is the correct location - when you filled (Market Position change), you can then set to both as needed.

Michael1111
10-23-2009, 06:21 AM
Hi Bertrand,

Thanks for the reply I have viewed that example, I'm just struggling to write the correct dynamic adjustments in the OnBarUpdate() section?

Can you please help me by filling in the ?????????? below?

- I want my profit target to = ATR20 on bar "0".
SetProfitTarget(????????????????)

- And I want my Stop loss to = Low of bar "0", -2 ticks.
SetStopLoss(??????????????????)


Many Thanks,

Michael

NinjaTrader_Bertrand
10-23-2009, 06:27 AM
Michael, for example for the stop you could start working with -


if (Close[0] > Close[5]) // some entry trigger to get us going
{
EnterLong(1, "");
myStopValue = Close[0] - 2 * TickSize;
}

if (Position.MarketPosition == MarketPosition.Long)
SetStopLoss(CalculationMode.Price, myStopValue);


Same would work for the target, just use some ATR offset instead of TickSize then...

Michael1111
10-23-2009, 07:28 AM
Hi Bertrand,

Thanks for that, It seems I have everything in order apart form this line
myStopValue = Close[0] - 2 * TickSize;

The Error is saying - "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)' Code CS0266 and it has no extra info on the Error?
Can you please help me correct this Error?

Thanks,

Michael

NinjaTrader_Josh
10-23-2009, 07:51 AM
myStopValue needs to be a double variable not an int.

Michael1111
10-23-2009, 08:15 AM
Hi Josh,

Thanks for that,

Can you please show me how to make it a double variable?
myStopValue = Close[0] - 2 * TickSize;

Many Thanks,

Michael

NinjaTrader_Bertrand
10-23-2009, 08:21 AM
You want to declare it as double in the Initialize() Michael -


private double myStopValue;

Michael1111
10-23-2009, 08:39 AM
Great, Thanks Bertrand,
That gives me something to work with now.
I may have some more questions on this at a later stage, but thanks again for your help so far :)


Michael