NexusFi: Find Your Edge


Home Menu

 





StopLoss as fraction of ATR


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one FArias with 3 posts (0 thanks)
    2. looks_two Big Mike with 1 posts (0 thanks)
    3. looks_3 Luger with 1 posts (0 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 4,849 views
    2. thumb_up 0 thanks given
    3. group 1 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread

StopLoss as fraction of ATR

  #1 (permalink)
 FArias 
Germany
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Futures
Posts: 9 since Feb 2011
Thanks Given: 34
Thanks Received: 2

Hello all,

I am trying to program a simple stop loss and profit target based on a fraction of the ATR value at the bar where the trade was placed, but can not get it to work. Any help would be much welcome!!!

I am using calculatemode.price for the stops and using an if(BarsSinceEntry()==0) in order to set the value of my stops directly after the trade is placed. The code looks like this:

 
Code
if (Close[0] > SMA(periodoMA)[0])
            { EnterLong("Long");}
 
if(Position.MarketPosition==MarketPosition.Long)
 
   { if(BarsSinceEntry(0,"Long",0)==0)
{ SetStopLoss("Long", CalculationMode.Price, Position.AvgPrice-ATR(PeriodoATR)[0]*fractionstop);
SetProfitTarget("Long",CalculationMode.Price,Position.AvgPrice+ATR(PeriodoATR)[0]*fractiontarget);}
}
Any ideas on how this could be implemented?

Thanks in advance!

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Exit Strategy
NinjaTrader
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Futures True Range Report
The Elite Circle
NexusFi Journal Challenge - April 2024
Feedback and Announcements
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
 
  #3 (permalink)
 Luger 
Nashville, TN
 
Experience: Intermediate
Platform: NinjaTrader
Broker: IB
Trading: NQ ES
Posts: 468 since Feb 2011
Thanks Given: 323
Thanks Received: 543


You can do your "SetStopLoss" line without the Position check. You effectively are attaching the StopLoss to the Entry Order. However, if your entry is market, then you must figure the price from which you want to calculate your target before execution. Such as Close[0] or Close[0] + assumed slippage - StopLoss.

With a bit more trouble, you can use the following section:

protected override void OnOrderUpdate

And when the order is updated to "filled", only then send out the appropriate StopLoss after getting the confirmed execution price.

There is a section in the NT Help File that discusses this further so that you can make a practical implementation.

Reply With Quote
  #4 (permalink)
 FArias 
Germany
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Futures
Posts: 9 since Feb 2011
Thanks Given: 34
Thanks Received: 2

Hello Luger,

Thanks for the info, I will read through OnOrderUpdate. However, I think I am already getting the correct entry price with Position.AvgPrice. I am substracting from that entry price the ATR. But the StopLoss is not executing.

I have seen on some examples that you have to reset the StopLoss / TargetProfit after you are flat??. I will give it a try.

Started this thread Reply With Quote
  #5 (permalink)
 FArias 
Germany
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Futures
Posts: 9 since Feb 2011
Thanks Given: 34
Thanks Received: 2

I think I got it to work... but I do not know exactly why.

I added some code to set the stoploss and targetprofit to 1 and 10000 if the market position is flat. I could have used other values, I just wanted to make sure they would not be triggered by price action in the next bar(s).

Here is the code:

 
Code
 if (Close[0] > SMA(periodoMA)[0])
             {
                EnterLong("Entrada Largos");
             }
 
 
   if(Position.MarketPosition==MarketPosition.Flat)
  {
   SetStopLoss("Entrada Largos",CalculationMode.Price,1,false);
   SetProfitTarget("Entrada Largos",CalculationMode.Price,10000);
     }
 
   if(Position.MarketPosition==MarketPosition.Long)
    { 
   double Variable1 = Instrument.MasterInstrument.Round2TickSize(Position.AvgPrice-ATR(PeriodoATR)[0]*stopPerdidas);
   double Variable2 = Instrument.MasterInstrument.Round2TickSize(Position.AvgPrice+ATR(PeriodoATR)[0]*objBeneficio);
   SetStopLoss("Entrada Largos",CalculationMode.Price, Variable1,false);
   SetProfitTarget("Entrada Largos",CalculationMode.Price,Variable2));
}
What I do not understand has to do with the positioning of this new code. At first, I placed the reseting of the StopLoss and TargetProfit, that is, everything under if(...=MarketPosition.Flat), after the if(...MarketPosition.Long) code and it did not work. But placing it as it is now, it is working. Does anyone know why?

BTW, how can I use the C# code highlighting on my messages?

Started this thread Reply With Quote
  #6 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,398 since Jun 2009
Thanks Given: 33,173
Thanks Received: 101,537


FArias View Post
BTW, how can I use the C# code highlighting on my messages?

Instead of using CODE block use PHP block for syntax highlighting on posts on futures.io (formerly BMT). Sorry no time to answer the rest, but I'm sure someone will be nice enough to help you.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote




Last Updated on February 23, 2011


© 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