NinjaTrader Support Forum  
X

Attention!

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


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 09-05-2012, 03:13 PM   #1
Thomas79
Senior Member
 
Join Date: Apr 2009
Location: Paris
Posts: 146
Thanks: 11
Thanked 0 times in 0 posts
Send a message via MSN to Thomas79
Default trailing stop

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 ?
Thomas79 is offline  
Reply With Quote
Old 09-05-2012, 03:39 PM   #2
NinjaTrader_JC
NinjaTrader Customer Service
 
NinjaTrader_JC's Avatar
 
Join Date: Mar 2012
Location: Denver, CO
Posts: 1,200
Thanks: 137
Thanked 183 times in 182 posts
Default

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.
NinjaTrader_JC is offline  
Reply With Quote
Old 09-11-2012, 01:44 PM   #3
Thomas79
Senior Member
 
Join Date: Apr 2009
Location: Paris
Posts: 146
Thanks: 11
Thanked 0 times in 0 posts
Send a message via MSN to Thomas79
Default

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)
Thomas79 is offline  
Reply With Quote
Old 09-11-2012, 02:18 PM   #4
NinjaTrader_JC
NinjaTrader Customer Service
 
NinjaTrader_JC's Avatar
 
Join Date: Mar 2012
Location: Denver, CO
Posts: 1,200
Thanks: 137
Thanked 183 times in 182 posts
Default

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.
Last edited by NinjaTrader_JC; 09-12-2012 at 07:11 AM.
NinjaTrader_JC is offline  
Reply With Quote
Old 09-12-2012, 06:20 AM   #5
Thomas79
Senior Member
 
Join Date: Apr 2009
Location: Paris
Posts: 146
Thanks: 11
Thanked 0 times in 0 posts
Send a message via MSN to Thomas79
Default

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...
Thomas79 is offline  
Reply With Quote
Old 09-12-2012, 07:38 AM   #6
NinjaTrader_JC
NinjaTrader Customer Service
 
NinjaTrader_JC's Avatar
 
Join Date: Mar 2012
Location: Denver, CO
Posts: 1,200
Thanks: 137
Thanked 183 times in 182 posts
Default

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.
NinjaTrader_JC is offline  
Reply With Quote
Old 09-12-2012, 11:39 AM   #7
Thomas79
Senior Member
 
Join Date: Apr 2009
Location: Paris
Posts: 146
Thanks: 11
Thanked 0 times in 0 posts
Send a message via MSN to Thomas79
Default

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
Thomas79 is offline  
Reply With Quote
Old 09-12-2012, 01:47 PM   #8
NinjaTrader_JC
NinjaTrader Customer Service
 
NinjaTrader_JC's Avatar
 
Join Date: Mar 2012
Location: Denver, CO
Posts: 1,200
Thanks: 137
Thanked 183 times in 182 posts
Default

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()]
This is a link to our Help Guide that explains what BarsSinceEntry() will return.
http://www.ninjatrader.com/support/h...sinceentry.htm

Happy to be of further assistance.
NinjaTrader_JC is offline  
Reply With Quote
Old 09-13-2012, 01:24 PM   #9
Thomas79
Senior Member
 
Join Date: Apr 2009
Location: Paris
Posts: 146
Thanks: 11
Thanked 0 times in 0 posts
Send a message via MSN to Thomas79
Default

Hi Ninja Trader JC

i find the way to call the limit price

targetOrder.LimitPrice

thx a lot for your help.
Thomas79 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
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


All times are GMT -6. The time now is 06:56 PM.