NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 07-26-2011, 06:52 AM   #1
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 735
Thanks: 19
Thanked 8 times in 8 posts
Default stoploss - double

Hello,

I´m using stoploss with Donchian and I want to ask when the stoploss moves and changes his value? Can SetTrailStop be used for variable like Donchian? I remember in another thread there was the information that SetTrailStop doesn´t work with Mode.Price.

So what do I have to code for having the stoploss each bar the actual value of Donchian?

Thanks
Tony
Last edited by tonynt; 07-26-2011 at 06:52 AM. Reason: typing error
tonynt is offline  
Reply With Quote
Old 07-26-2011, 07:08 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

Correct Tony, you can't use SetTrailStop then - you would call SetStopLoss again in your OnBarUpdate() if you would like to change the stop loss price, so basically each bar update as your Donchian value changes then.

http://www.ninjatrader.com/support/f...ead.php?t=3222
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 07-26-2011, 07:38 AM   #3
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 735
Thanks: 19
Thanked 8 times in 8 posts
Default

Thanks Bertrand,

but it doesn´t change each bar!. It changes only when the if condition is filled (here the 90 ticks) - referring to the samplepricemodification. This is my coding - I think it should be correct as done same as in the sample.

if (BarsInProgress !=0)
return;

stop1=DonchianChannel(3).Upper[0]+5*TickSize;
stop2=DonchianChannel(5).Upper[0]+5*TickSize;
stop3=DonchianChannel(6).Upper[0]+5*TickSize;

if(Position.MarketPosition == MarketPosition.Flat)
{ SetStopLoss("SG1", CalculationMode.Price, stop1, false);
SetStopLoss("SG2", CalculationMode.Price, stop1, false);
SetStopLoss("SG3", CalculationMode.Price, stop1, false);

if (Position.MarketPosition == MarketPosition.Short
&& Close[0] < Position.AvgPrice - 90 * TickSize)

{ SetStopLoss("SG1", CalculationMode.Price, stop1, false);
SetStopLoss("SG2", CalculationMode.Price, stop2, false);
SetStopLoss("SG3", CalculationMode.Price, stop3, false);
}

Why does stop move only with the if condition and not from the beginning each bar?
I´m in the direction of the trade maybe 80 ticks and Donchians moves with my bars but the initial stoploss stays same value.

Thanks
Tony
tonynt is offline  
Reply With Quote
Old 07-26-2011, 08:32 AM   #4
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

Tony, the stop is only moving with the if conditions because you've placed it in the if statement code block.

It will also not move until the trade is 90 ticks in profit.

Please try simplifying your strategy to one position and one moving stop loss and once you get that figured out you can add the additional positions and stops.
NinjaTrader_Austin is offline  
Reply With Quote
Old 07-26-2011, 08:47 AM   #5
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 735
Thanks: 19
Thanked 8 times in 8 posts
Default

Austin,

thank you for your reply. You are right of course with the way to debug. But the "stop1" is also in the "if flat" statement. No? There´s no other difference in the both statements as if 90 ticks then change the donchian values for 2nd and 3rd entry.
tonynt is offline  
Reply With Quote
Old 07-26-2011, 08:52 AM   #6
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

If there is no position, then there is no stop loss able to be changed. Setting the stop loss when flat is just for resetting the stop losses, it won't assign a stop loss to your positions.
NinjaTrader_Austin is offline  
Reply With Quote
Old 07-26-2011, 09:03 AM   #7
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 735
Thanks: 19
Thanked 8 times in 8 posts
Default

Oh, big mistake. Thank you! Sorry for the question.

Quote:
Originally Posted by NinjaTrader_Austin View Post
If there is no position, then there is no stop loss able to be changed. Setting the stop loss when flat is just for resetting the stop losses, it won't assign a stop loss to your positions.
tonynt is offline  
Reply With Quote
Old 07-26-2011, 09:19 AM   #8
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

Not a problem, Tony. Let us know what you get figured out.
NinjaTrader_Austin is offline  
Reply With Quote
Old 07-26-2011, 09:58 AM   #9
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 735
Thanks: 19
Thanked 8 times in 8 posts
Default

Austin, referring to you information I have changed now to the following coding but this does´t work

if (BarsInProgress !=0)
return;

stop1=DonchianChannel(3).Upper[0]+5*TickSize;
stop2=DonchianChannel(5).Upper[0]+5*TickSize;
stop3=DonchianChannel(6).Upper[0]+5*TickSize;

if(Position.MarketPosition == MarketPosition.Flat)
{ SetStopLoss("SG1", CalculationMode.Price, stop1, false);
SetStopLoss("SG2", CalculationMode.Price, stop1, false);
SetStopLoss("SG3", CalculationMode.Price, stop1, false); }

if (Position.MarketPosition == MarketPosition.Short
&& Close[0] < Position.AvgPrice - 30 * TickSize
&& Close[0] > Position.AvgPrice - 90 * TickSize)

{ SetStopLoss("SG1", CalculationMode.Price, stop1, false);
SetStopLoss("SG2", CalculationMode.Price, stop1, false);
SetStopLoss("SG3", CalculationMode.Price, stop1, false); }



if (Position.MarketPosition == MarketPosition.Short
&& Close[0] < Position.AvgPrice - 90 * TickSize)

{ SetStopLoss("SG1", CalculationMode.Price, stop2, false);
SetStopLoss("SG2", CalculationMode.Price, stop2, false);
SetStopLoss("SG3", CalculationMode.Price, stop3, false); }

How can I change to have a variable in the stoploss untill a certain price and then changing to another variable from this certain price?

Thanks
tonynt is offline  
Reply With Quote
Old 07-26-2011, 12:08 PM   #10
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

Hi Tony, you could for example add some boolean flags / user variables to your code to control the flow of the statements used better. A related sample script could be used here - http://www.ninjatrader.com/support/f...ad.php?t=15417

Think of the this functionality like switches that would have two values (true / false) to control which action is taken and in your case then which stop value / calculation is used at which time in the trade.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 07-28-2011, 03:02 AM   #11
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 735
Thanks: 19
Thanked 8 times in 8 posts
Default

Thank you for your reply. I tried to do so but it doesn´t work as stoploss always jumps back to first condtion that is true also with condtion 2 and 3. What am I doing wrong? Thank you

if(Position.MarketPosition == MarketPosition.Flat)
{SetStopLoss("SG1", CalculationMode.Ticks, 70, false);
Variable1 = 0;}

if (Position. MarketPosition==MarketPosition.Short
&& Close[0]<Position.AvgPrice - 60*TickSize)
{Variable1 = 1;}

if (Variable1 == 1)
{SetStopLoss("SG1", CalculationMode.Price, stop1,false);}

if (Position. MarketPosition==MarketPosition.Short
&& Close[0]<Position.AvgPrice - 90*TickSize)
{Variable1 = 2;}

if (Variable1 == 2)
{SetStopLoss("SG1", CalculationMode.Price, stop2,false);}

if (Position. MarketPosition==MarketPosition.Short
&& Close[0]<Position.AvgPrice - 150*TickSize)
{Variable1 = 3;}

if (Variable1 == 3)
{SetStopLoss("SG1", CalculationMode.Price, stop3,false);}
tonynt is offline  
Reply With Quote
Old 07-28-2011, 03:40 AM   #12
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

Tony, you would also need a check what your variable state is here in the first condition, so it would not always evaluate to true and force changes in your stoploss but only if your variable is reading a certain state where you would like this to to triggered again.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 07-28-2011, 04:13 AM   #13
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 735
Thanks: 19
Thanked 8 times in 8 posts
Default

????????????
tonynt is offline  
Reply With Quote
Old 07-28-2011, 05:01 AM   #14
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

Tony, by your reply, I was unfortunately not sure which part was unclear to you, but I hope the attached strategy can help shed some more light on the topic.

Good luck,
Attached Files
File Type: cs SamplePriceModification2.cs (3.7 KB, 10 views)
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 07-28-2011, 07:56 AM   #15
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 735
Thanks: 19
Thanked 8 times in 8 posts
Default

Thank you!
tonynt 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
stoploss tonynt Automated Trading 5 08-08-2011 03:15 AM
double to dataseries neb1998 Automated Trading 1 01-17-2011 11:38 PM
stoploss mballagan Strategy Development 1 11-19-2009 10:15 AM
Double into int Gepetto Automated Trading 5 11-18-2009 11:16 AM
Double moving average ,double time frame Mauro60 Indicator Development 3 01-23-2007 02:11 AM


All times are GMT -6. The time now is 09:43 PM.