PDA

View Full Version : Stop Loss @ Low/High of Entry Bar minus one Bar


SamIam
08-26-2007, 01:23 AM
Greetings,

I have been trying to develop a strategy (using NinjaTrader Script) and want to add a condition of stop loss order trigger at 10 ticks below the low of the bar before the bar where the long/short position was opened.

"((Low(EntryBar()-1)-10) or ((High(EntryBar()-1+10)".

Can someone in this forum help me to convert the above condition into NinjaTrader Script code?

Thanks,

Sami

NinjaTrader_Josh
08-26-2007, 01:56 AM
Low[BarsSinceEntry()+1]-10*TickSize;
High[BarsSinceEntry()+1]+10*TickSize;
BarsSinceEntry() returns an int representing how many bars ago your strategy entered your position. (http://www.ninjatrader-support.com/HelpGuideV6/BarsSinceEntry.html)
You want to do BarsSinceEntry()+1 to reference the bar previous the Entry Bar. (an index of 0 references the current bar, 1 references the bar before the current bar, etc)
TickSize returns a double that represents the minimum price fluctuation of your instrument as defined in the Instrument Manager (http://www.ninjatrader-support.com/HelpGuideV6/helpguide.html)

mazachan
08-28-2007, 08:56 PM
Wouldn't it be easier to add that when you enter a trade? So you have the entry and set the stop to Low[1] or High[1]?

NinjaTrader_Josh
08-29-2007, 01:01 AM
The problem with doing it like that mazachan is that if you didn't hit your stop/target on the next bar your stop/target will keep getting modified. As each new bar is built, you end up moving your stop/target to the new respective Low[1] and High[1]. To keep the stop/target static (i.e. referencing the same Low/High bar value) regardless of how many bars have passed you need to add the BarsSinceEntry() into the parameter. Basically it acts as a kind of counter.

mazachan
08-29-2007, 02:20 AM
But if you say something like:


if (SomeCondition){
EnterLongPosition();
double stop = Low[1] - (10 * TickSize);
SetStopLoss(CalculationMode.Price, stop);
}

It should only be run when the trade is executed and should not be repeatedly updated, since that condition will only be hit on the entry bar.

NinjaTrader_Josh
08-29-2007, 03:55 AM
That would work too.:cool: