NexusFi: Find Your Edge


Home Menu

 





How to set a dinamic stop loss based on percent


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Astrogirl with 5 posts (2 thanks)
    2. looks_two ratfink with 4 posts (2 thanks)
    3. looks_3 gregid with 2 posts (2 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 6,535 views
    2. thumb_up 6 thanks given
    3. group 3 followers
    1. forum 11 posts
    2. attach_file 0 attachments




 
Search this Thread

How to set a dinamic stop loss based on percent

  #1 (permalink)
Astrogirl
Milan/Italy
 
Posts: 51 since Oct 2013
Thanks Given: 25
Thanks Received: 12

I'm new on this forum and this is my first message so hello everyone!

I am a newbie of the C# and the NinjaTrader scripting; I am from Italy so sorry for my poor english. Hope the theme that follows were not already studied and if it will be redundant, sorry again.

Coming to us: I am trying to set a stop loss on an open position when a determinated unrealized profit is reached.

Example: my enter price is 10000 and it grows to 10050; at this point my script set a new stop loss to 10040. Actually I am able to do that using this code:

// Dinamic stop loss: Set a new stop loss when a determinated unrealized profit is reached

if (Position.MarketPosition == MarketPosition.Long
&& Median[0] == Position.AvgPrice + 50 * TickSize)
{
SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 40 * TickSize, false);
}


It works well but I would need to set a new stop loss based not more on a prefixed ticks value but on a percent value of the enter price (the Position.AvgPrice).

Example: my enter price is 10000 and it grows of the 1% to 10100; at this point my script should set a new stop loss of the 0.8% to 10080.

I spent a lot of time and energies trying all possible solution without any result... it seems like any double value doesnt' work.

Where am I wrong?! Can anyone help me please?

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Build trailing stop for micro index(s)
Psychology and Money Management
Online prop firm The Funded Trader (TFT) going under?
Traders Hideout
Are there any eval firms that allow you to sink to your …
Traders Hideout
Ninja Mobile Trader VPS (ninjamobiletrader.com)
Trading Reviews and Vendors
Better Renko Gaps
The Elite Circle
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
59 thanks
Funded Trader platforms
37 thanks
NexusFi site changelog and issues/problem reporting
24 thanks
GFIs1 1 DAX trade per day journal
22 thanks
The Program
19 thanks
  #3 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425



Astrogirl View Post
I'm new on this forum and this is my first message so hello everyone!

I am a newbie of the C# and the NinjaTrader scripting; I am from Italy so sorry for my poor english. Hope the theme that follows were not already studied and if it will be redundant, sorry again.

Coming to us: I am trying to set a stop loss on an open position when a determinated unrealized profit is reached.

Example: my enter price is 10000 and it grows to 10050; at this point my script set a new stop loss to 10040. Actually I am able to do that using this code:

// Dinamic stop loss: Set a new stop loss when a determinated unrealized profit is reached

if (Position.MarketPosition == MarketPosition.Long
&& Median[0] == Position.AvgPrice + 50 * TickSize)
{
SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 40 * TickSize, false);
}


It works well but I would need to set a new stop loss based not more on a prefixed ticks value but on a percent value of the enter price (the Position.AvgPrice).

Example: my enter price is 10000 and it grows of the 1% to 10100; at this point my script should set a new stop loss of the 0.8% to 10080.

I spent a lot of time and energies trying all possible solution without any result... it seems like any double value doesnt' work.

Where am I wrong?! Can anyone help me please?

It should be possible with something like:

 
Code
stopPrice = myEntryPrice + ((Position.AvgPrice - myEntryPrice) *0.8);

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
  #4 (permalink)
Astrogirl
Milan/Italy
 
Posts: 51 since Oct 2013
Thanks Given: 25
Thanks Received: 12


ratfink View Post
It should be possible with something like:

 
Code
stopPrice = myEntryPrice + ((Position.AvgPrice - myEntryPrice) *0.8);

Thanks Ratfink!

Unfortunately that code doesnt' seem to work; I suppose that the matter is about the variable type so when the output is a double it doesnt' work; I am actually working on convert a double variable in a int variable... I hope I am not still wasting my time on the wrong assuption!

Reply With Quote
Thanked by:
  #5 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


Astrogirl View Post
Thanks Ratfink!

Unfortunately that code doesnt' seem to work; I suppose that the matter is about the variable type so when the output is a double it doesnt' work; I am actually working on convert a double variable in a int variable... I hope I am not still wasting my time on the wrong assuption!

Just another thought - maybe you need to use the Round2TickSize function as well?

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #6 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


ratfink View Post
Just another thought - maybe you need to use the Round2TickSize function as well?

So it would actually be stopPrice = Instrument.MasterInstrument.Round2TickSize (stopPrice); for example.

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #7 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623

Just in case it is not obvious:

To use ratfink's code:
 
Code
stopPrice = myEntryPrice + ((Position.AvgPrice - myEntryPrice) *0.8);
you would first have to declare the variables (something like this):
 
Code
double myEntryPrice = 12341234.12;
double stopPrice = myEntryPrice + ((Position.AvgPrice - myEntryPrice) *0.8);
When you say the code doesn't work do you mean it doesn't compile (compilation errors) or simply doesn't return proper value. If you have compilation errors post them here so it will be easier for someone to assist you.

Reply With Quote
Thanked by:
  #8 (permalink)
Astrogirl
Milan/Italy
 
Posts: 51 since Oct 2013
Thanks Given: 25
Thanks Received: 12

It works! You two are great, thank you friends!

My assumption was correct and your solutions simply perfect!

Reply With Quote
Thanked by:
  #9 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


Astrogirl View Post
It works! You two are great, thank you friends!

My assumption was correct and your solutions simply perfect!

Great - and welcome to futures.io (formerly BMT)! It's not a bad place to hang out.

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
  #10 (permalink)
Astrogirl
Milan/Italy
 
Posts: 51 since Oct 2013
Thanks Given: 25
Thanks Received: 12


Now I'm trying to use a trailing stop instead a stop loss. I am using this code:


else if (Position.MarketPosition == MarketPosition.Long && Median[0] == targetstep1)

{
SetTrailStop("", CalculationMode.Price, trailing1, false);
}


instead of:

{
SetStopLoss("", CalculationMode.Price, trailing1, false);
}


("targetstep1" is a determineted target price level and "trailin1" is an integer variable of a stop price)

It doesn't work... what's wrong with me?!

Reply With Quote




Last Updated on October 20, 2013


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts