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
ISO Automated Strategy Script code for dynamic StopLoss
Collapse
X
-
ISO Automated Strategy Script code for dynamic StopLoss
Looking to see if anyone has some code willing to share that uses dynamic stoploss features. Such as Supertrend or Fibonacci pivots. Right now using e.order code to protect profit and loss. But that creates an instant stoploss and profit orders. What I am thinking is code to then adjust the initial stops to follow ie Supertrend pivots. So now I have ExitLongStopMarket(stoploss) and ExitLongLimit(Profit) on initial buy or sell entry. What would be the proper language to use to create an IF statement to then adjust that stop limit?Tags: None
-
Hello set2win,
Thank you for your post.
While I don't have an example of setting stops to Supertrend or Fibonacci pivots, I do have an example of a strategy that modifies its stops based on a multiple of the ATR, which may be of some help as a jumping off point. Please see this older forum post, which has an unlocked example:
Good afternoon, I would like through the strategy builder to be able to use the trailing stop, I don't know how to use it so that my stop decreases when I have advanced a certain number of ticks? Thanks
What you'd use for your if statement to control when the stop/target prices update would be dependent on when you want them to update - so for example, you could check that the current price is 5 ticks above the average entry price and then modify the stop/target.
Please let us know if we may be of further assistance to you.Kate W.NinjaTrader Customer Service
-
Yes, I am not necessarily looking for the condition code as in Supertrend or Fibonacci I am making sure I use the right code to remove the existing stoploss that was placed when order entry created and now use a new stop. I always want to cover myself in case computer breakdown or internet interruption so it needs to place the initial stop. So the code you referred to in the linked post I don't think it will do that. It will exit the trade and what I want is to put a new stoploss order.
Here is what I got now.
if( e.Order.OrderAction == OrderAction.Buy )
{
// Set stop loss
double stopOrderPrice = e.Order.AverageFillPrice - StopLoss * TickSize;
ExitLongStopMarket( 0, true, 3, stopOrderPrice, "Stop loss", e.Order.FromEntrySignal );
// Set profit target 1
double profitTarget = e.Order.AverageFillPrice + ProfitTarget * TickSize;;
ExitLongLimit( 0, true, 1, profitTarget, "Profit target", e.Order.FromEntrySignal );
}
That places the initial stoploss.
But as the trade progresses I want to change the existing stoploss to a new value based on conditions.
Hope this makes it clearer.
Comment
-
Hello set2win,
Thank you for your reply.
You should be able to just call whichever ExitLongLimit() or ExitLongStopMarket() again with the same Signal name and the revised price and that will update the order. You could do that within OnBarUpdate() as necessary to make changes to the price the stop or target is placed at.
Please let us know if we may be of further assistance to you.Kate W.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_Kate View PostHello set2win,
Thank you for your reply.
You should be able to just call whichever ExitLongLimit() or ExitLongStopMarket() again with the same Signal name and the revised price and that will update the order. You could do that within OnBarUpdate() as necessary to make changes to the price the stop or target is placed at.
Please let us know if we may be of further assistance to you.
I tried this method with no update to existing stopLoss
// Dynamic StopLoss
if (Close[0] > Close[1])
{
double stopOrderPriceNew = Low[0] - StopLoss * TickSize;
Print("New Stop Price: "+ stopOrderPriceNew );
ExitLongStopMarket(0,true,1,stopOrderPriceNew, "Stop loss", "Stop loss");
}
Output shows stoprice updating but it does not replace the existing stoploss order
Comment
-
Hello set2win,
Thank you for your note.
Could you provide the original line where you're setting the StopLoss? Right now, you have this one with the same "From Entry Signal" name as the signal name for this order - these values need to exactly match the original order when it was submitted for the order to be updated.
Thanks in advance; I look forward to assisting you further.Kate W.NinjaTrader Customer Service
Comment
-
Hello set2win,
Thank you for your reply.
I don't see that you're not saving the entry signal name to a variable so you can reuse it, and from your sample code I can't tell what the signal name for the entry might be since you're just using e.Order.FromEntrySignal. Here's an example from our help guide that goes over both initially submitting the stop loss and moving the stop to a different value:
Please let us know if we may be of further assistance to you.Kate W.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by ETFVoyageur, Today, 07:55 PM
|
0 responses
6 views
0 likes
|
Last Post
by ETFVoyageur
Today, 07:55 PM
|
||
Started by janio973, Today, 07:24 PM
|
1 response
7 views
0 likes
|
Last Post Today, 07:35 PM | ||
Started by aligator, 01-06-2022, 12:14 PM
|
4 responses
242 views
0 likes
|
Last Post
by john_44573
Today, 06:11 PM
|
||
Started by reynoldsn, Today, 05:56 PM
|
0 responses
13 views
0 likes
|
Last Post
by reynoldsn
Today, 05:56 PM
|
||
Started by bortz, 11-06-2023, 08:04 AM
|
51 responses
1,996 views
0 likes
|
Last Post
by aligator
Today, 05:50 PM
|
Comment