NexusFi: Find Your Edge


Home Menu

 





Sample Advanced Automated Strategy v1.0


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one dsraider with 36 posts (41 thanks)
    2. looks_two wgreenie with 21 posts (1 thanks)
    3. looks_3 gg80108 with 11 posts (6 thanks)
    4. looks_4 Ducman with 7 posts (0 thanks)
      Best Posters
    1. looks_one MXASJ with 3 thanks per post
    2. looks_two dsraider with 1.1 thanks per post
    3. looks_3 cbritton with 0.7 thanks per post
    4. looks_4 gg80108 with 0.5 thanks per post
    1. trending_up 49,619 views
    2. thumb_up 64 thanks given
    3. group 27 followers
    1. forum 117 posts
    2. attach_file 15 attachments




 
Search this Thread

Sample Advanced Automated Strategy v1.0

  #21 (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 Dave

My heartfelt thanks for sharing your strategy with us. I can tell the additional efforts you spent to make it simple and yet very comprehensive.

I've been studying it for almost 2 weeks now. As a newbie with minimal programming talents, my success as of this moment is having a BE condition triggered once the trade moves 8 ticks from my entry. All other efforts trying to include your Trail Stop code in different ways (that I could think of or tried) have not triggered a revised SL to protect some gained ticks even F5 compile successfully .. sigh ..

I am hoping maybe you will spare a few minutes going through my code and give me some tips on what I did wrong or how to make it work .. PLEASE!

 
Code
protectedoverridevoid Initialize()
{

EntriesPerDirection = 1; 
EntryHandling = EntryHandling.UniqueEntries;
CalculateOnBarClose = true;
}

privatevoid ManageOrders()
{
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, 15);
}

// If a long position is open, allow for stop loss modification to breakeven
if (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is greater than entry price +8 ticks, set stop loss to breakeven
if (Close[0] > Position.AvgPrice + 8 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\AutoTrail.wav");
}
}

// Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size
elseif (previousPrice != 0// SL is at Breakeven
&& Close[0] > previousPrice + trailProfitTrigger * TickSize)
{
newPrice = previousPrice + trailStepTicks * TickSize;
SetStopLoss(CalculationMode.Price, newPrice);
previousPrice = newPrice;
PrintWithTimeStamp("previousPrice = "+previousPrice);
PrintWithTimeStamp("newPrice = "+previousPrice);
PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\AutoTrail.wav");
}

// If a short position is open, allow for stop loss modification to breakeven
if (Position.MarketPosition == MarketPosition.Short)
{
// Once the price is smaller than entry price -8 ticks, set stop loss to breakeven
if (Close[0] < Position.AvgPrice - 8 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\AutoTrail.wav");
}
}

// Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size
elseif (previousPrice != 0// SL is at Breakeven
&& Close[0] < previousPrice + trailProfitTrigger * TickSize)
{
newPrice = previousPrice - trailStepTicks * TickSize;
SetStopLoss(CalculationMode.Price, newPrice);
previousPrice = newPrice;
PrintWithTimeStamp("previousPrice = "+previousPrice);
PrintWithTimeStamp("newPrice = "+previousPrice);
PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\AutoTrail.wav");
}
}
Thank you very much .. very much in advance!

Beth

Visit my NexusFi Trade Journal Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Exit Strategy
NinjaTrader
MC PL editor upgrade
MultiCharts
Trade idea based off three indicators.
Traders Hideout
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
33 thanks
Tao te Trade: way of the WLD
24 thanks
My NQ Trading Journal
14 thanks
HumbleTraders next chapter
11 thanks
GFIs1 1 DAX trade per day journal
11 thanks
  #22 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

Hi Beth,

I noticed you changed CalculateOnBarClose to true. When it's set to false, Close[0] means current price, which really helps with a trailing stop. When set to true, it has to wait until the bar closes before it looks to move the stop. That could be a problem. If you can explain to me how you want the trail to actually work, I'll take a shot at it.

Don't lose your mind. We'll figure it out.

Dave

Reply With Quote
Thanked by:
  #23 (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



dsraider View Post
Don't lose your mind. We'll figure it out.

Dave

Hi Dave

Your Swift response has certainly lightened me up! Days (incl 2 weekends) were spent trying to make a trail stop work. Every F5 compile successfully message gave me hope and yet it's a no no at the end. Very heart-breaking and I feel stupid when I've your master piece as my guidance.

I did try CalculateOnBarClose @ false and my supposed trail stop has not triggered anything when I backtested the strategy.


What I've been trying to do:

For example,

1. Long at 1000 with SL at 985 (initial 15 ticks SL) and BE when it moves 8 ticks from entry
2. Price moves to 1008
3. BE condition moves SL to 1000 (from 985) - that's how far I've accomplished
4. Price continues moving to 1010 (1008 + 2)
5. Trail stop kicks in with SL adjusts to 1002 (1000 + 2)
6. Price continues moving to 1012 (1010 + 2)
7. Trail stop to have SL adjusted to 1004 (1002 + 2)
.. ..

For every 2-tick move, SL will be adjusted 2 ticks to protect some gained profits.

++

I moved the trail stop code around dozens times and nothing has happened after each F5 compile successful. I'm running out of gas and that prompted to knock at your door. My heartfelt thanks for your GIANT lift!

Beth (now a little relieved )

Visit my NexusFi Trade Journal Reply With Quote
  #24 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

Can you try this?:

 
Code
                            
#region Variables
        
        
private int        breakEvenTicks        8;
        private 
int        plusBreakEven        0;
        private 
int     trailProfitTrigger     9;
        private 
int        trailStepTicks        2;
        
        private 
double    initialBreakEven    0;
        private 
double     previousPrice         0;
        private 
double     newPrice             0;

#endregion

protected override void OnBarUpdate()

#region Trail Stop
                    // Only allow entries if we have no current positions open
                        
switch (Position.MarketPosition)
                    {
                        case 
MarketPosition.Flat:
                            
SetStopLoss(""CalculationMode.Ticks15false);
                                                        
previousPrice 0;
                            break;
                        case 
MarketPosition.Long:
                            
// Once the price is greater than entry price+ breakEvenTicks ticks, set stop loss to breakeven
                            
if (Close[0] == Position.AvgPrice breakEvenTicks TickSize && previousPrice == 0)
                            {
                                
initialBreakEven Position.AvgPrice plusBreakEven TickSize
                                
SetStopLoss(""CalculationMode.PriceinitialBreakEvenfalse);
                                
previousPrice Position.AvgPrice;
                                
PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\AutoTrail.wav");
                                
PrintWithTimeStamp("previousPrice = "+previousPrice);
                                
PrintWithTimeStamp("newPrice = "+previousPrice);
                            }
                                                        
// Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size step
                            
else if (previousPrice    != ////StopLoss is at breakeven
                                    
&& Close[0] > previousPrice trailProfitTrigger TickSize)
                                
                            {
                                
newPrice previousPrice trailStepTicks TickSize;
                                
SetStopLoss(""CalculationMode.PricenewPricefalse);
                                
previousPrice newPrice;
                                
PrintWithTimeStamp("previousPrice = "+previousPrice);
                                
PrintWithTimeStamp("newPrice = "+previousPrice);
                                
PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\AutoTrail.wav");
                            }
                            break; 
I've obviously left some things out, but these are the parts needed for what you want to do. Remember to set CalculateOnBarClose back to false (that's why I use FirstTickOfBar in my entry code). I'm wondering if OnBarUpdate makes a difference as well, as I just realized you switched it to ManageOrders. Anyway, let me know how it goes.

Dave

Reply With Quote
Thanked by:
  #25 (permalink)
 
cbritton's Avatar
 cbritton 
Atlanta, Georgia
 
Experience: Intermediate
Platform: NT
Broker: DDT
Trading: ZN, ZB
Posts: 230 since Mar 2010
Thanks Given: 152
Thanks Received: 256

I think you want this instead:

 
Code
                            
...
  
// Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size step
                            
else if (previousPrice    != ////StopLoss is at breakeven
                                    
&& Close[0] > previousPrice trailProfitTrigger TickSize)
                                
                            {
                                
newPrice previousPrice trailStepTicks TickSize;
                                
SetStopLoss(""CalculationMode.PricenewPricefalse);
                                
// print the previous price before it's changed
                                
PrintWithTimeStamp("previousPrice = "+previousPrice);
                                
previousPrice newPrice;
                                
PrintWithTimeStamp("newPrice = "+previousPrice);
                                
PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\AutoTrail.wav");
                            }
                            break;
... 
Otherwise you won't see the previous price being printed

“Strategy without tactics is the slowest route to victory. Tactics without strategy is the noise before defeat.” - Sun Tzu
Reply With Quote
Thanked by:
  #26 (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

BIG BIG Thank You to Dave and cbritton for your prompt code suggestion. I have them in the strategy (+ CalculateOnBarClose at false) and it's running on sim now. For backtesting, the revised strategy has generated the same results (same trades etc) as the one without the trail stop code?!!

Maybe trail stop only works when it's on live. Will find out later today and please stay tuned to my report.

Have a Great day.

Beth (grateful for your guidance)

Visit my NexusFi Trade Journal Reply With Quote
  #27 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

Beth,

The trail should would on the sim. If it isn't, we have an issue. I may have to take a peek at your actual strat if we can't figure it out here, as I'm flying slightly blind. Maybe you can change around a few things if you'd like to keep your actual signal to yourself. Good luck for now. I'll check back after market close.

Dave

Reply With Quote
Thanked by:
  #28 (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 Dave. No rush as I've been working on this for 2 weeks now. The strategy with trail got a little crazy after a few minutes. You will see from the enclosed log that it started to open and close a position within a second after Trade No 1. Just stopped it running.

I have a thread on my automation efforts here. I am trying to automate The Wizard's systems and the signals are well-known in the Big Mike community. Please let me know later tonight what will you need to find out what I did wrong. Thank You .. Thank You again from the bottom of my petite heart for your help!

Beth

Attached Thumbnails
Click image for larger version

Name:	YM Trade Log w Trail.JPG
Views:	385
Size:	250.6 KB
ID:	9489  
Visit my NexusFi Trade Journal Reply With Quote
  #29 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

I'm guessing you will need to add FirstTickOfBar to your entry code and set your indicator conditions a bar back. So, instead of if(Close[0] > Open[0]) go long, you would use if(FirstTickOfBar && Close[1] > Open[1]) go long. Since CalculateOnBarClose = false makes Close[0] = current price, this allows you use close as a true close and prevents the strat from taking trades on every tick, like yours seems to be doing.

If you'd like to PM me the strat or post it here, I'll have a look. It's hard to tell what will work without knowing how you want to enter.

D

Reply With Quote
Thanked by:
  #30 (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 Dave for your swift suggestion. I changed the entry conditions with FirstTickOfBar and [0] to [1] etc. Backtest results were different from the strategy without the trail (finally .. smile). Have it run on sim now and so far not generating trades every second - great news! Thanks to you.

Need to pick your brains on the following please:

1. trailProfitTrigger = 9;

8 + 1 formula with 8 being the breakEvenTicks? The trade needs to be 9 ticks away before the system starts adjusting the BE stoploss to BE + 2 ticks regularly (with 2 being the trailStepTicks), right?

2. With CalculateOnBarClose at false, I also changed the Exit condition. I'm not sure if I did correctly on the following CrossBelow:

(CrossBelow(d9ParticleOscillatorWVertLineR(7, 0).RawTrend, 0, 0)
which was (CrossBelow(d9ParticleOscillatorWVertLineR(7, 0).RawTrend, 0, 1)

Appreciate your advice. Thank you.

Beth

Visit my NexusFi Trade Journal Reply With Quote




Last Updated on November 16, 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