![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
|
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); } } } |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
|
Anybody know why this won't work? Thanks in advance. Greatly appreciate any help.
|
|
|
|
|
|
#3 |
|
NinjaTrader Customer Service
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
|
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;
Ryan M
NinjaTrader Customer Service |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
|
Thanks Ryan, this worked very well. I have the 3-stage SetStopLoss working too.
|
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
|
Ryan, can I have different text print on the chart for each SetStopLoss modification?
What is the proper syntax for this? |
|
|
|
|
|
#6 |
|
Senior Member
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
|
Ryan,
Is it possible to have different names printed on the chart for each different SetStopLoss modification? |
|
|
|
|
|
#7 |
|
NinjaTrader Customer Service
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
|
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);
}
Ryan M
NinjaTrader Customer Service |
|
|
|
|
The following user says thank you to NinjaTrader_RyanM for this post: |
|
|
|
#8 |
|
Senior Member
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
|
Thanks for that tip.
|
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#10 | |
|
Member
Join Date: Jul 2011
Posts: 39
Thanks: 11
Thanked 3 times in 2 posts
|
Quote:
|
|
|
|
|
|
The following 2 users say thank you to trend747 for this post: |
|
|
|
#11 |
|
Senior Member
Join Date: Mar 2005
Location: , ,
Posts: 104
Thanks: 3
Thanked 0 times in 0 posts
|
That worked. Nice.
|
|
|
|
|
|
#12 |
|
Senior Member
Join Date: Jun 2009
Posts: 747
Thanks: 23
Thanked 14 times in 9 posts
|
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 |
|
|
|
|
|
#13 |
|
NinjaTrader Customer Service
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 90 times in 82 posts
|
Tony, please do not post your question multiple times. I have already answered it on another thread.
Austin
NinjaTrader Customer Service |
|
|
|
|
|
#14 |
|
Senior Member
Join Date: Jun 2009
Posts: 747
Thanks: 23
Thanked 14 times in 9 posts
|
|
|
|
|
|
|
#15 |
|
Junior Member
Join Date: Oct 2011
Posts: 10
Thanks: 0
Thanked 0 times in 0 posts
|
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.
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |