![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
|
Hi
I would like to put a trailing stop to my script when the market hit 2 ticks below my target, basically, if my target is 50, when the market hit 48, move up the stop to Position.AvgPrice + 2*TickSize i calculate my target with 2 times the ATR(10) (average true range) + the Position.AvgPrice, but the value of the ATR when the position is open here the part of my script: entryOrderAchat = EnterLongLimit(1, Close[0], "divergence haussiere 1"); atrAchat = CurrentBar; } if (Position.MarketPosition == MarketPosition.Long && High[0] > Position.AvgPrice + 2*ATR(10)[atrAchat] - 2*TickSize) { // Checks to see if our Stop Order has been submitted already if (stopOrderLong != null && stopOrderLong.StopPrice < Position.AvgPrice) { // Modifies stop-loss to breakeven + 2 tick stopOrderLong = ExitLongStop(0, true, stopOrderLong.Quantity, Position.AvgPrice+2*TickSize, "Trailing Stop div haussiere 1", "divergence haussiere 1"); } it is not working... i don't know if it is because the ATR is everytime a new value after each new bar or if it is anything else... the really weird stuff is that if i remove the 2* before ATR(10)[atrAchat] in the condition, it is look to take the trailing stop but with only 1 atr... can you help ? |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Mar 2012
Location: Denver, CO
Posts: 1,200
Thanks: 137
Thanked 183 times in 182 posts
|
Hello Thomas79,
So that I may further assist you could you answer the following questions. Do you see any error messages in the Log tab of the Control Center? Are you checking the values of the ATR to make sure that is the value that you would like set it to? Here is a link to our support forums that goes over how to debug your code which comes in handy if when writing custom code to view the values. http://www.ninjatrader.com/support/f...ead.php?t=3418 Note as well that you can enable TraceOrders to see the status of the orders that you try to submit, for more information see the following link. http://www.ninjatrader.com/support/f...ead.php?t=3627 You can also use the SetTrailStop() method to be able to set a trailing stop so that you would not have to calculate it manually. See our Help Guide on SetTrailStop from the link below. http://www.ninjatrader.com/support/h...ttrailstop.htm Happy to be of further assistance.
JC
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Senior Member
|
Hi
it is not working because the script take a different atr at each new bar, it is always take the new value of the ATR indicator... so because i don't find the solution to call the value of the ATR we the position is open... is it possible to call the value of the targeroder... for example if the targetorder value is 141.73, a way to call the value 141.73... like: if (Position.MarketPosition==MarketPosition.Long && High[0] >= "Value of targetorder" - 2*TickSize) |
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Mar 2012
Location: Denver, CO
Posts: 1,200
Thanks: 137
Thanked 183 times in 182 posts
|
Hello Thomas79,
You can create a new variable that will save the value of an order to be able to use it for calculation purposes. Alternatively, you can check the limit price of an order inside of OnOrderUpdate() if you would like. http://www.ninjatrader.com/support/h...rderupdate.htm Let us know if we can be of further assistance.
JC
NinjaTrader Customer Service
Last edited by NinjaTrader_JC; 09-12-2012 at 07:11 AM.
|
|
|
|
|
|
#5 |
|
Senior Member
|
Hi
The link than you gave me is the like of this post, is it a mistake ? Also, do you have a example to create a new variable that will save the value of an order to be able to use it for calculation purposes ? i don't see how to create this variable to save the value of the the order... |
|
|
|
|
|
#6 |
|
NinjaTrader Customer Service
Join Date: Mar 2012
Location: Denver, CO
Posts: 1,200
Thanks: 137
Thanked 183 times in 182 posts
|
Hello Thomas79,
I fixed the link on my previous reply. It would be like the atrAchat variable you created for use with the CurrentBar. Here you can see a reference about declaring variables that you may read on our Help Guide. http://www.ninjatrader.com/support/h...sic_syntax.htm There is also a forum post about creating a user defined variable as well that you may view. http://www.ninjatrader.com/support/f...ead.php?t=5782 Let me know if we can be of further assistance.
JC
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Senior Member
|
Hi
i don't understand where on this link http://www.ninjatrader.com/support/h...rderupdate.htm it is show how to call the level of a exitlonglimit().... if my exitlonglimit is 102.02, i would like to know how to call 102.02 my problem is that the calculation mode of 102.02 is with an indicator call ATR and i need to use the value of 102.02 for the condition of my trailing stop... If i call the value of the ATR when the position is opened, it is not working (as my first post) i when i create a variable like double atrAchat = ATR (10)[0] and after that i use atrAchat the script is always take the new value of the atr (what is logical for me)... but i want to call the value of the atr when the position is opened... So i would like to either call the value of the atr indicator when the position is opened (so in the past) or the value of the exitlonglimit order... and it is look like not possible |
|
|
|
|
|
#8 |
|
NinjaTrader Customer Service
Join Date: Mar 2012
Location: Denver, CO
Posts: 1,200
Thanks: 137
Thanked 183 times in 182 posts
|
Hello Thomas79,
Can you try the following code to get the value of the ATR since the position entry to see if it is going to be what you are looking for. Code:
ATR(10)[BarsSinceEntry()] http://www.ninjatrader.com/support/h...sinceentry.htm Happy to be of further assistance.
JC
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
Senior Member
|
Hi Ninja Trader JC
i find the way to call the limit price targetOrder.LimitPrice thx a lot for your help. |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 2 Level Bracket: One Hard Stop and a Trailing Stop | outstretchedarm | ATM Strategies (Discretionary Trading) | 4 | 07-01-2011 08:06 AM |
| Turning on and off the trailing stop feature of a stop loss set by an ATM | ohowie | ATM Strategies (Discretionary Trading) | 2 | 01-12-2011 03:19 AM |
| Trailing Profits - How to Reverse a Trailing Stop? | jeremymgp | Strategy Development | 1 | 05-05-2009 10:47 AM |
| Trailing Stop with a fixed 20 tick stop | Godzilla | Strategy Development | 3 | 03-18-2009 07:07 AM |
| NT 6.5 - Trailing stop not trailing | PocoDiablo | Miscellaneous Support | 3 | 03-19-2008 01:46 PM |