View Full Version : Stop Loss Offset
CraigC
06-28-2008, 11:36 PM
How would one put a stop loss 2 points under the lowest low for the last 5 bars (for a long)?
NinjaTrader_Josh
06-29-2008, 01:03 AM
Try SetStopLoss(MIN(Low, 5)[0] - 2 * TickSize)
If TickSize is not what you want you can also try Instrument.MasterInstrument.PointValue (http://www.ninjatrader-support.com/HelpGuideV6/InstrumentMasterInstrumentPointValue.html).
CraigC
06-29-2008, 02:15 AM
Do I call that once in Initialize, or just before I enter the long?
Edit: Nix that, I have RTFM.
CraigC
06-29-2008, 03:00 AM
I now get 'buy stop or buy stop limit orders can't be placed below market'
NinjaTrader_Josh
06-29-2008, 03:26 AM
Right buy stop orders cannot be placed there. Please see this tip: http://www.ninjatrader-support.com/vb/showthread.php?t=3271
CraigC
06-29-2008, 01:57 PM
I think I sorted it, the proper syntax is
SetStopLoss(CalculationMode.Price, MAX(High, 5)[0] + stopLoss * TickSize);
Red Fish
09-03-2008, 08:45 AM
Hey Craig and Josh, thanks for posting up how to do this, I've been sweating over this for two full days...Now I've tried your syntax and I can't quite work out why it doesn't do the calculation it should. I have
SetStopLoss(CalculationMode.Price, MIN(Low, 5)[0] - 6 * TickSize);
So theoretically it should take the lowest low for the last five candles and then subtract a further 6 pips to set the stop loss - what I want is one pip below the lowest low of the last five candles and then I'm adding the 5 pips broker's spread on - which gives me my 6 below. But when I run it and read off the charts, for example, a buy is marked and the low of the candle before it is 1.8364, but the stop loss exits the trade at 1.8365. That's 1 pip above the low of the last candle not 6 pips below it. I'm sure there's a good explanation, but I can't fathom it right now. Can you help?
NinjaTrader_Josh
09-03-2008, 09:03 AM
Hi Red Fish,
Best way to check is to print values instead of trying to visualize it from the chart. Reason for printing is so you know for sure what the system thinks the value is.
Try printing out the value for your MIN(Low, 5)[0] - 6 * TickSize and then comparing it to the value that your stop loss is at.
Red Fish
09-03-2008, 09:11 AM
Hi Josh, thanks for your reply! I'm sorry to sound a bit dumb. I wrote this
Print(MIN(Low, 5)[0] - 6 * TickSize); below the SetStopLoss line, but I don't get anything in the output window. What am I doing wrong?
Red Fish
09-03-2008, 09:17 AM
Sorry, I cleared the output window and I reapplied the strategy to the charts again, now it is showing me a string of numbers. So how do I judge these numbers...what am I looking for??
NinjaTrader_Josh
09-03-2008, 09:22 AM
Do this.
Print(Time[0] + " " + MIN(Low, 5)[0] + " " + TickSize);
You will get two numbers. Use your calculator to find your stop loss value between those values. Then figure out the time that your stop loss is submitted and compare the value of your stop loss to the value that your prints come out as. They should match.
Red Fish
09-03-2008, 09:31 AM
Thanks Josh! So I can confirm the values are right, phew! So really it's the visualisation that is off? Does that mean that the numbers will come out right in the end then?
NinjaTrader_Josh
09-03-2008, 09:35 AM
Visualization is not necessarily wrong. It may just be that you are correlating values from the wrong bar to be what you think the stop loss should be.
What you want to do is find your entry trade. Look at the chart and determine what you wish the stop loss should be in theory. Then look at the values you did earlier and see if those values match up with what you want them to be. If they match you are golden. You may be off by a bar to the left or right due to miscounting when you say "5 bars back". Just come back to your code and adjust the 5 accordingly to match up the stop loss with what you want.
Red Fish
09-08-2008, 02:08 AM
Josh, now I've put in my other conditions and try and run this from the strategy analyser it gives me a blank screen when I select this strategy. If I set my stop loss to 25 ticks
SetStopLoss(CalculationMode.Ticks, 25);
it gives me the normal screen to fill in my parameters. But if I alter the same test strategy to
SetStopLoss(CalculationMode.Price, MIN(Low, 5)[0] - 6 * TickSize);
it gives me a blank screen. If I try and run it with the blank screen it tells me NT needs to close down. I have even uninstalled and reinstalled NT and did it again but it does the same thing. If I then amend it back to a simple 25 ticks stop loss it allows me to run it.
NinjaTrader_Dierk
09-08-2008, 02:22 AM
>>SetStopLoss(CalculationMode.Price, MIN(Low, 5)[0] - 6 * TickSize);
You can not access an indicator value in Initialize() method. Please see resulting error(s) in the log. Please try moving your logic to OnBarUdpate() method.
Red Fish
09-08-2008, 03:01 AM
Thanks Dierk, that works!
I have one other problem, it seems to give me results as if I have moved my stop...I am testing an exit at 60 pips, I have not put any trailing stop code in, but it seems to give me profit results which are positive based on exiting at the stop, i.e. not 60 pips but a variety of amounts below 60 pips. Is there an inbuilt trailing stop at work?
NinjaTrader_Dierk
09-08-2008, 05:06 AM
>> Is there an inbuilt trailing stop at work?
No, unless you coded it. I suggest tracing your strategy as per here to understand what's going on: http://www.ninjatrader-support.com/vb/showthread.php?t=3627
Red Fish
09-08-2008, 09:41 AM
What I've found out is that it's reading the stop calculation as a moving stop - recalculating on every new bar update it so that it moves with price, similar to a trailing stop - I've looked at the charts and it stops out on a retracement, rather than continuing to the profit target. I guess this means a price action entry where you place stops one pip under/above the recent low or high is not possible to test for.
NinjaTrader_Josh
09-08-2008, 10:01 AM
The reason your stop keeps moving while in the OnBarUpdate() is because it is constantly submitting modifications to your stop price. If this is not intended behavior you need to also have a limiting condition that only submits your stop once at given price for your entry.
Red Fish
09-08-2008, 10:07 AM
Yes Josh, that's what it looks like. What kind of limiting condition could I put in that "only submits your stop once at given price for your entry"? I only want the stop to do its calculations for the entry bar and stick there.
NinjaTrader_Josh
09-08-2008, 10:26 AM
For one, you could throw it with the condition for entry.
if (some entry condition)
{
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(...);
EnterLong();
}
}Keep in mind that SetStopLoss line should be before the EnterLong line. This is untested code, but should work in theory.
You may get some other ideas for other things you can do with this reference sample: http://www.ninjatrader-support.com/vb/showthread.php?t=3222