NexusFi: Find Your Edge


Home Menu

 





stoploss to breakeven to trailing


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one wgreenie with 4 posts (2 thanks)
    2. looks_two stephenszpak with 3 posts (1 thanks)
    3. looks_3 aslan with 2 posts (5 thanks)
    4. looks_4 Eloy with 2 posts (0 thanks)
      Best Posters
    1. looks_one aslan with 2.5 thanks per post
    2. looks_two cory with 1 thanks per post
    3. looks_3 wgreenie with 0.5 thanks per post
    4. looks_4 stephenszpak with 0.3 thanks per post
    1. trending_up 11,223 views
    2. thumb_up 10 thanks given
    3. group 7 followers
    1. forum 13 posts
    2. attach_file 2 attachments




 
Search this Thread

stoploss to breakeven to trailing

  #1 (permalink)
 
stephenszpak's Avatar
 stephenszpak 
Massachusetts (USA)
 
Experience: None
Platform: NinjaTrader
Trading: YM
Posts: 750 since Jun 2009
Thanks Given: 144
Thanks Received: 356

Hi

I have an idea regarding trading the YM. I am having a difficult time with the
coding of the stops. I think I know how to take the stop loss stop and turn
it into a breakeven stop. Unfortunately I don't know how to move the breakeven
up with each upward (downward) tick of the YM.

If the YM is above the breakeven by 10 points that is fine.
If the YM goes up one point I would need the breakeven to turn into a trailing
stop, with the YM still above the stop (in this case trailing) by 10 points.

Someone at another forum gave me some code, and some of that is here:

link: NinjaTrader Support Forum - View Single Post - Trailing stop not working on backtest

SLPrice=Position.AvgPrice;
SetStopLoss(CalculationMode.Price,SLPrice);

I realize I can keep incrementing the variable SLPrice if Close[0] increases.
Not positive how, but along these lines I think:


if(Close[0]>Position.AvgPrice+11
{
SLPrice=Position.AvgPrice;
SLPrice=Position.AvgPrice+
1;

SetStopLoss(CalculationMode.Price,SLPrice);
}

First the coding might be wrong, but besides that there would have to
be a few hundred lines of code (just for a bull move) as the YM moves
up. A little block something like the above for every tick up. What if
the YM moves up 50 points?!

Anyway I was hoping that someone already had this problem and had it
answered, or at least maybe someone has an idea to create a loop
(or equivalent) with NinjaScript to avoid writing such a long program.

(Back tomorrow if possible.)

- Stephen

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
How to apply profiles
Traders Hideout
ZombieSqueeze
Platforms and Indicators
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
MC PL editor upgrade
MultiCharts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
34 thanks
Tao te Trade: way of the WLD
24 thanks
GFIs1 1 DAX trade per day journal
16 thanks
Vinny E-Mini & Algobox Review TRADE ROOM
13 thanks
My NQ Trading Journal
12 thanks
  #2 (permalink)
Eloy
Moscow
 
Posts: 12 since Jan 2010
Thanks Given: 1
Thanks Received: 8

There is no problem with a long program if you post your code into OnBarUpdate() event - the code will execute every incoming tick.

Reply With Quote
  #3 (permalink)
 
stephenszpak's Avatar
 stephenszpak 
Massachusetts (USA)
 
Experience: None
Platform: NinjaTrader
Trading: YM
Posts: 750 since Jun 2009
Thanks Given: 144
Thanks Received: 356


Thanks for the reply.

Started this thread Reply With Quote
  #4 (permalink)
Eloy
Moscow
 
Posts: 12 since Jan 2010
Thanks Given: 1
Thanks Received: 8

And remember set CalculateOnBarClose = false; to run OnBarUpdate() on each tick - https://www.ninjatrader-support.com/HelpGuideV6/helpguide.html?CalculateOnBarClose1

Reply With Quote
  #5 (permalink)
 
Trader.Jon's Avatar
 Trader.Jon 
Near the BEuTiFULL Horse Shoe
 
Experience: Beginner
Platform: NinjaTrader
Broker: MBTrading Dukascopy ZenFire
Trading: $EURUSD when it is trending
Posts: 473 since Jul 2009
Thanks Given: 401
Thanks Received: 184

Stephen,

This code was written by a consultant for another project of mine. I have parsed out specifics for stops: perhaps it might be useful as you develop your code. Sorry, I cant support it as I am barely at the point of understanding what parts of the strategy go where, but I am trying!

Attached Files
Elite Membership required to download: BE_trailingSTOP.txt
Reply With Quote
Thanked by:
  #6 (permalink)
 wgreenie 
Cambridge, Ontario
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Zen-Fire
Trading: 6E
Posts: 1,778 since Dec 2009
Thanks Given: 1,258
Thanks Received: 1,203

Hi Stephen

I also tried for weeks to have a trail code "functioning" in my strategies. Can certainly understand the stress and frustration when days and nights were spent and nothing seemed working.

Just got the enclosed sample and hope it may help you. Good Luck!

Beth

Attached Files
Elite Membership required to download: ProfitTargetTrailingStop_1[1].01b.cs
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #7 (permalink)
 wgreenie 
Cambridge, Ontario
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Zen-Fire
Trading: 6E
Posts: 1,778 since Dec 2009
Thanks Given: 1,258
Thanks Received: 1,203


Eloy View Post
And remember set CalculateOnBarClose = false; to run OnBarUpdate() on each tick - https://www.ninjatrader-support.com/HelpGuideV6/helpguide.html?CalculateOnBarClose1

Sorry to throw in another idea here which is opposite to yours. I learned the following from a more experienced member:

Quote
Due to its inherent design, Ninja forces limitations upon your strategy. As an example, Ninja requires that you set OnBarClose to true in order to have any chance at success. That alone limits your design tremendously.
Unquote

Based on my experiences, when I had CalculateOnBarClose @false, sim-trade and Market Replay results had looping trades, ie 1 smooth short trade became choppy trades in-and-out within a second. Not able to identify the cause for sure though with CalculateOnBarClose @true there're no looping trades.

Beth

Visit my NexusFi Trade Journal Reply With Quote
  #8 (permalink)
 
aslan's Avatar
 aslan 
Madison, WI
 
Experience: Advanced
Platform: ALT
Trading: ES
Posts: 625 since Jan 2010
Thanks Given: 356
Thanks Received: 1,127


wgreenie View Post
Quote
Due to its inherent design, Ninja forces limitations upon your strategy. As an example, Ninja requires that you set OnBarClose to true in order to have any chance at success. That alone limits your design tremendously.
Unquote

Sorry, I don't mean to burst your bubble, but this is just not true.

There are two models that you have to choose between. You can either process data at the end of each bar (which is really the beginning of the next bar depending on the bar type) or on each incoming tick. The former is when CalcOnBarClose=true and the latter is when CalcOnBarClose=false.

With either model, you need to understand what Ninja is actually doing behind the scenes when you are calling the enter/exit/setStop/etc routines. Sadly, the doc almost helps (all of the little warnings that this is advanced coding should be a red flag).

Unfortunately, it is pretty easy to write code that works well in backtests, but fails when you run live. This is usually a problem in the strategy coding, not some limitation in Ninja. If you want to be accurate, you need to use COBC=false, but that means you need to dig in and really understand what is going on.

Reply With Quote
  #9 (permalink)
 wgreenie 
Cambridge, Ontario
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Zen-Fire
Trading: 6E
Posts: 1,778 since Dec 2009
Thanks Given: 1,258
Thanks Received: 1,203

Thanks aslan. That's good news that CalculateOnBarClose @ false will work IF one knows what's going on! Unfortunately not me as for some unknown reasons seemed it might have triggered looping trades PLUS for weeks now I still have a super-mega big head trying to make a simple strategy with a trail code to work!!

Good Luck to Stephen and me!

Beth

Visit my NexusFi Trade Journal Reply With Quote
  #10 (permalink)
 
cory's Avatar
 cory 
virginia
 
Experience: Intermediate
Platform: ninja
Trading: NQ
Posts: 6,098 since Jun 2009
Thanks Given: 877
Thanks Received: 8,090


cut & paste this into goggle box , remember to take out a space between .

Trailing Stop strategy site:www .ninjatrader-support2 .com

Reply With Quote
Thanked by:




Last Updated on October 13, 2014


© 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