NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 06-13-2011, 12:53 AM   #1
Baseheadz
Senior Member
 
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
Default Moving Stop loss SetStopLoss

I am using the following code and my stop loss modifies on the first modification but does not modify on the second and third modifications.
For example: After 7 ticks profit, the stop is raised to entry price + 2 ticks, BUT after that, my other stoploss modifications do not appear to be working.
I looked in the message boards and I could not find a clear answer on how to do this. I have looked at the Sample Price Modification Script.

protected override void Initialize()
//PROFIT TARGET AND STOP LOSS
{
SetProfitTarget("", CalculationMode.Ticks, ProfitTarget);
SetStopLoss("", CalculationMode.Ticks, stoplossticks, false);
CalculateOnBarClose = true;
}

protected override void OnBarUpdate()
{
// Condition set 1
if ((myEntryOrder == null)
&& CrossAbove(Close, EMA(LengthMA), 1))

{
myEntryOrder = EnterLongLimit(0, true, DefaultQuantity, Close[0] + SlipTicks * TickSize, "Long Limit");

}

// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stoplossticks);
}

// If a long position is open, allow for stop loss modification
if (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is greater than entry price+trailtrigger, set stop loss to breakeven + trail
if (Close[0] >= (Position.AvgPrice + 7* TickSize))
{
SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 2* TickSize, true);
}
if (Close[0] >= (Position.AvgPrice + 9* TickSize))
{
SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 5* TickSize, true);
}
if (Close[0] >= (Position.AvgPrice + 11* TickSize))
{
SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 7* TickSize, true);
}
}
}
Baseheadz is offline  
Reply With Quote
Old 06-13-2011, 12:13 PM   #2
Baseheadz
Senior Member
 
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
Default

Anybody know why this won't work? Thanks in advance. Greatly appreciate any help.
Baseheadz is offline  
Reply With Quote
Old 06-13-2011, 04:57 PM   #3
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Hi Baseheadz,

The reason you are seeing this is because the condition for the first movement continues to evaluate true at the same time as your other movements. To code the desired sequence you can use bool flags or user variables. You should first start with a two-stage movement until adding additional complexity of the third. Example of this below:

Code:
#region Variables
private bool controlOne = true;
#endregion

if (Close[0] >= (Position.AvgPrice + 7* TickSize) && controlOne)
{
	SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 2* TickSize, true);
	controlOne = false;
}

if (Close[0] >= (Position.AvgPrice + 9* TickSize) && !controlOne)
{
	SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 5* TickSize, true);
}
			
//Reset your bool flag when flat. 
if (Position.MarketPosition == MarketPosition.Flat)
	controlOne = true;
NinjaTrader_RyanM is offline  
Reply With Quote
Old 06-14-2011, 09:49 AM   #4
Baseheadz
Senior Member
 
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
Default

Thanks Ryan, this worked very well. I have the 3-stage SetStopLoss working too.
Baseheadz is offline  
Reply With Quote
Old 06-16-2011, 11:30 AM   #5
Baseheadz
Senior Member
 
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
Default

Ryan, can I have different text print on the chart for each SetStopLoss modification?
What is the proper syntax for this?
Baseheadz is offline  
Reply With Quote
Old 06-20-2011, 04:16 PM   #6
Baseheadz
Senior Member
 
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
Default

Ryan,

Is it possible to have different names printed on the chart for each different SetStopLoss modification?
Baseheadz is offline  
Reply With Quote
Old 06-20-2011, 04:25 PM   #7
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Yes, this is possible. I'm not sure what happened to my previous reply here, sorry. Just add a separate DrawText() with a unique tag for each movement.

Code:
if (Close[0] >= (Position.AvgPrice + 7* TickSize) && controlOne)
{
	SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 2* TickSize, true);
	controlOne = false;
        DrawText("Mov1", "First stop loss movement", 0, High[0] + TickSize, Color.Black);

}

if (Close[0] >= (Position.AvgPrice + 9* TickSize) && !controlOne)
{
	SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 5* TickSize, true);
        DrawText("Mov2", "Second stop loss movement", 0, High[0] + TickSize, Color.Black);
}
NinjaTrader_RyanM is offline  
Reply With Quote
The following user says thank you to NinjaTrader_RyanM for this post:
Old 06-20-2011, 04:29 PM   #8
Baseheadz
Senior Member
 
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
Default

Thanks for that tip.
Baseheadz is offline  
Reply With Quote
Old 07-18-2011, 08:37 PM   #9
Baseheadz
Senior Member
 
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
Default

As stated before, the stop loss is is moving as planned.

In real time trading, I've noticed that the stop loss will not move until the bar closes at or above the trail trigger. For instance, if the trail trigger is Entry Price + 20 ticks, the trailing stop will not move until after the close of the bar prints Entry Price + 20.

How do I get the trailing stop to move once the price hits Entry Price + 20 Ticks?

Do I have to set COBC to false?

Thanks
Baseheadz is offline  
Reply With Quote
Old 07-18-2011, 11:47 PM   #10
trend747
Member
 
Join Date: Jul 2011
Posts: 39
Thanks: 11
Thanked 3 times in 2 posts
Default

Quote:
Originally Posted by Baseheadz View Post
As stated before, the stop loss is is moving as planned.

In real time trading, I've noticed that the stop loss will not move until the bar closes at or above the trail trigger. For instance, if the trail trigger is Entry Price + 20 ticks, the trailing stop will not move until after the close of the bar prints Entry Price + 20.

How do I get the trailing stop to move once the price hits Entry Price + 20 Ticks?

Do I have to set COBC to false?

Thanks
Yes, then you can use GetCurrentBid() and GetCurrentAsk() to do touches...
trend747 is offline  
Reply With Quote
The following 2 users say thank you to trend747 for this post:
Old 07-19-2011, 08:09 AM   #11
Baseheadz
Senior Member
 
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
Default

That worked. Nice.
Baseheadz is offline  
Reply With Quote
Old 07-30-2011, 09:17 AM   #12
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 747
Thanks: 23
Thanked 14 times in 9 posts
Default

Hello,

I have my stop

stop1=DonchianChannel(4).Upper[0];

SetStopLoss("S1", CalculationMode.Price, stop1, false);

What do I have to change for my shorts so that stoploss is only realized when CLOSE is above stop1 (and not current price)? As I us Renkos, this is important. Do I understand in this thread that its enough to set COBC=true?

Thanks
Tony
tonynt is offline  
Reply With Quote
Old 07-30-2011, 08:03 PM   #13
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 90 times in 82 posts
Default

Tony, please do not post your question multiple times. I have already answered it on another thread.
NinjaTrader_Austin is offline  
Reply With Quote
Old 07-31-2011, 01:08 AM   #14
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 747
Thanks: 23
Thanked 14 times in 9 posts
Default

Thanks and sorry.

I didnīt see my post in the overview, so I thought its not OK because it was to an older thread.


Quote:
Originally Posted by NinjaTrader_Austin View Post
Tony, please do not post your question multiple times. I have already answered it on another thread.
tonynt is offline  
Reply With Quote
Old 02-03-2012, 06:52 AM   #15
ggerstein
Junior Member
 
Join Date: Oct 2011
Posts: 10
Thanks: 0
Thanked 0 times in 0 posts
Default

The posts on setStopLoss were really helpful, but I am having a different problem in getting the setStopLoss function to work. The protective stop loss order never triggers when I am running this strategy in Market replay and I can't figure out why. I have the following variable to control where the stop loss price is set at any point and where the order is at. I also track a variable called "currentEntry" which is just a price at to which I can compare the modified stop loss value so I know when to modify it. The idea of the stop loss strategy is that if we enter say on a long entry and then the next bars low exceeds the entry price, then modify the stop loss to the low of the new bar minus some number of trailing ticks. In other words I only want to modify the stop loss when the bars do not overlap.

Here is the stop loss code. I call a seperate function to manage the stop from within the OnBarUpdate method so here is that first part.

private String entryName = "filledOrder";
private double barHigh;
private double barLow;
private double barOpen;
private double barClose;
private double lastStopLoss = 0.0;
private double currentEntry = 0.0;
private double defaultStopLoss = 10;
private int trailingStopTicks = 1;
private IOrder runningOrder ;

protected override void OnBarUpdate()
{

barHigh = High[0];
barLow = Low[0];
barOpen = Open[0];
barClose = Close[0];


if(Position.MarketPosition == MarketPosition.Flat && runningOrder == null)
{

SetStopLoss(CalculationMode.Ticks, lastStopLoss);

//for brevity
if(SMA(20) > SMA(5))
{
lastStopLoss = barLow - (1 * TickSize);
currentEntry = Close[0] + (1 * TickSize);
SetStopLoss(CalculationMode.Price, lastStopLoss);
runningOrder = EnterLongStop(DefaultQuantity, currentEntry, entryName);
}
}
else
{
//run protective stop functions if in trade
if(Position.MarketPosition == MarketPosition.Long && runningOrder != null)
{
setStopLossStrat2( trailingStopTicks );
}
}
}

private void setStopLossStrat2(int trailingValue)
{
if(barLow > currentEntry)
{

lastStopLoss = barLow - ( trailingValue * TickSize );
currentEntry = Close[0];

SetStopLoss(entryName, CalculationMode.Price, lastStopLoss, true );
}
else
{
SetStopLoss(entryName, CalculationMode.Price, lastStopLoss, true );
}
Print(runningOrder.toString());
}

The stop loss order never gets triggered. Can't figure it out. It traces the stop order on each bar and the values are correct, but then it never triggers. I was reading about order management and it said that a setStopLoss(); may be ignored if the order is not simulated and was actually placed by setting the simulation parameter to false, but even when I set it to true it is failing to trigger the protective stop order. Any help is appreciated. Thank you.
Last edited by ggerstein; 02-03-2012 at 04:56 PM.
ggerstein is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I change the stop-loss order into a stop-loss-limit order? op684 ATM Strategies (Discretionary Trading) 10 01-23-2011 03:02 AM
Moving Stop Loss Order BrandonLG83 ATM Strategies (Discretionary Trading) 2 07-30-2009 11:21 AM
Is it possible to use the SetStopLoss() also as a Trail Stop? stefy Strategy Development 1 01-12-2009 07:32 AM
Manually moving stop loss Jurase ATM Strategies (Discretionary Trading) 2 11-14-2008 04:41 AM
Moving Stop Loss and Enter Reverse kgillis23 Strategy Development 2 03-11-2008 10:14 PM


All times are GMT -6. The time now is 12:25 AM.