Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
Moving multiple stop Loss Orders
Collapse
X
-
Moving multiple stop Loss Orders
Hi there. Trying to move multiple stop losses in a strategy (effectively using them as a trailing stop). The problem is I seem to get looping. Moving the first one to breakeven works ok but then the next ones are looping so basically in the log you see the stops being moved continuously as orders. So I modified the example here with just a single order and the same thing happens. The first one to BE works when the market moves up 30 ticks but when the market moves up 40 or 60 ticks the stop moves but in a continuous loop. Any ideas what is causing this would be appreciated. ThanksLast edited by djkiwi; 11-23-2020, 07:43 PM.Tags: None
-
Here is the example code for the multiple orders. Both behave in this looping fashion.
{
EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), @"Long1");
EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), @"Long2");
EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), @"Long3");
}
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, Stoploss1);
SetStopLoss(CalculationMode.Ticks, Stoploss2);
SetStopLoss(CalculationMode.Ticks, Stoploss3);
}
else if
((Position.MarketPosition == MarketPosition.Long)
&& (Close[0] > Position.AveragePrice+10 * TickSize))
{
SetStopLoss(@"Long1",CalculationMode.Price, Position.AveragePrice,false);
SetStopLoss(@"Long2",CalculationMode.Price, Position.AveragePrice,false);
SetStopLoss(@"Long3",CalculationMode.Price, Position.AveragePrice,false);
Print(Time[0].ToString() + " not first tick of bar Long");
}
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, Stoploss1);
SetStopLoss(CalculationMode.Ticks, Stoploss2);
SetStopLoss(CalculationMode.Ticks, Stoploss3);
}
else if ((Position.MarketPosition == MarketPosition.Long)
&& (Close[0] > Position.AveragePrice+30 * TickSize))
{
//SetTrailStop(@"Long1", CalculationMode.Ticks, 12,false);
//SetTrailStop(@"Long1", CalculationMode.Ticks, 12,false);
//SetTrailStop(@"Long1", CalculationMode.Ticks, 12,false);
SetStopLoss(@"Long1",CalculationMode.Price, Position.AveragePrice+10*TickSize,false);
SetStopLoss(@"Long2",CalculationMode.Price, Position.AveragePrice+10*TickSize,false);
SetStopLoss(@"Long3",CalculationMode.Price, Position.AveragePrice+10*TickSize,false);
Print(Time[0].ToString() + " second");
}
// If a long position is open, allow for stop loss modification to breakeven
//2. Shorts
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(@"Long1", CalculationMode.Ticks, Stoploss1, false);
SetStopLoss(@"Long2", CalculationMode.Ticks, Stoploss2, false);
SetStopLoss(@"Long3", CalculationMode.Ticks, Stoploss3, false);
SetStopLoss(@"Short1", CalculationMode.Ticks, Stoploss1, false);
SetStopLoss(@"Short2", CalculationMode.Ticks, Stoploss2, false);
SetStopLoss(@"Short3", CalculationMode.Ticks, Stoploss3, false);
}
-
Hello djkiwi,
Thank you for the post.
When you say that the stop moves in a loop what do you mean? Are you seeing that its moved multiple times? Are you expecting that it only happens one time?
The code as you have it should all happen at once when the price increases, you are not using any else statements with your if conditions. If the price is + 60 * TickSize you will also be setting the stop for the 40 and 60 because those are true as well at that point.
You do have one else statement but its only going to apply to a single one of the if statements below it, you are not using any curly braces to contain the code you want the else to apply to. If and else statements will only apply to the following 1 line if you don't use { }
I look forward to being of further assistance.JesseNinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Padan, Today, 02:02 AM
|
9 responses
21 views
0 likes
|
Last Post Today, 03:27 PM | ||
Started by spottysallrite, Today, 11:31 AM
|
7 responses
22 views
0 likes
|
Last Post Today, 03:26 PM | ||
Started by nandhumca, Today, 03:15 PM
|
1 response
3 views
0 likes
|
Last Post Today, 03:23 PM | ||
Started by PopovDS, 03-11-2024, 12:49 PM
|
3 responses
17 views
0 likes
|
Last Post
by PopovDS
Today, 03:16 PM
|
||
Started by Lele2k24, Yesterday, 11:24 AM
|
12 responses
37 views
0 likes
|
Last Post Today, 03:07 PM |
Comment