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 gg80108 with 0.5 thanks per post
    4. looks_4 eman with 0.4 thanks per post
    1. trending_up 48,998 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

  #1 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

People of the interweb,

With the help of several sample strategies I found on the NT forum and a little ingenuity, I have put together a fully-automated, advanced strategy which does the following:

1. Only enters between 10:15 AM and 3:15 PM (EST) (allows for second time frame as well)
2. Shuts down if PnL is more than $1,000 or less than -$300
3. Enters limit orders for long and short based on signal
4. Covers three time frames but will only have one active position open at a time
5. Cancels entry order if market moves four ticks away from limit order without filling
6. Sets an initial stop at 12 ticks, then changes to breakeven at +4 ticks and trails, tick by tick, from there
7. Scales out of half position at +4 ticks (1st profit target) and covers second half position at +8 ticks (2nd profit target)

Good news: All parameters can be changed to suit your needs. Please feel free to add to, delete or change anything in the code. All that I ask is that you don't sell my hard work for profit.

Bad news: As I have been coding C# for roughly 2.5 weeks, the contents of the strat pretty much consists of everything I know about coding. Therefore, if you want something drasticly changed, I will most likely not know how.

I am creating this thread so that we may all help to make it better, if need be, so please keep all comments and requests to this thread. As of now, the only thing with which I am not thrilled is the CancelOrder() code. It USUALLY works fine, but sometimes it surprises me. I am open to suggestions for how to make that, or any other part, better.

Click below for the strat. To install: Control Panel ---> File ---> Utilitites ---> Import Ninjascript...

Thanks and happy trading,
Dave

SampleAdvancedAutomatedStratv1.1

UPDATE:
v.1.1 - 2/23/10
1. Fixed CancelOrder() code.
2. Added arrows and chart name plots (since plots will only show on main chart, this lets you know which time frame just took the trade).

If you cannot see your full chart name once plotted: Right-click on chart ---> Properties ---> Right Side Margin

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
Exit Strategy
NinjaTrader
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Are there any eval firms that allow you to sink to your …
Traders Hideout
Deepmoney LLM
Elite Quantitative GenAI/LLM
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
39 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
The Program
18 thanks
GFIs1 1 DAX trade per day journal
18 thanks
  #3 (permalink)
 
cclsys's Avatar
 cclsys 
Sydney, NS
 
Experience: Intermediate
Platform: Ninja
Broker: Zen-Fire
Trading: TF,S,GC
Posts: 605 since Nov 2009
Thanks Given: 248
Thanks Received: 393


Thank you very much. Having sample strategies like this is extremely helpful for those who don't know all the ins and outs of NT strategy programming.

One feature I would like to see an example of is very simple but don't know how to do it: I like to make stops and PT's variable by reference to expanding/contracting ATR versus fixed tick sizes.

Now in theory I think all one has to do is have a tick size PT input (for example) but then multiply it by an ATR ratio.

If the short-term ATR is greater than longer term ATR, then if that is divided by the longer term you get a ratio (say 1.25), so the PT tick input can be multiplied by 1.25 to get the current PT amount. As I said, very simple in concept.

But how to do that within strategy coding I am not sure. Now that I have your template I can try (when I have time). Also there is the Hurley sample strategy here on the forum thanks to BM. But if any other expert would like to add that in, I am sure I am not the only person who would find it a very helpful snippet to have handy. Good for stops, profit targets, entry conditions etc.

More importantly, makes it possible to test the same strategy on different tick/time/instrument charts to find bell-curve ratios that work best rather than having to optimize each instrument and tick/time chart each time which creates over-optimization type problems with system design in general, but especially with intraday systems using short term data/bars.

Ideally (imho) all you need to optimize on a system should be
a) profit-to-loss ratio
b) volatility quotient applied to
c) stops and profit targets.

a) is a simple ratio which is a double expressing ratio of PT to stop (say), i.e. 2 = PT is 2*stop.
b) is the Stop in ticks (one input) with a second input which is the volatility multiplier (i.e. test from .5 to 2.5).

This means there are only 3 inputs to test: the P-L ratio, the volatility multiplier, and the base stop price in ticks.

If you want to get it down to only 2 inputs to optimize then the stop itself is a function of the volatility multiplier so that one multiplier optimizes the stops and PT's whilst the second input is the PT-Stop ratio which expands and contracts the PT relative to the initial stop price.

Having only two inputs to optimize on any given system makes for far more robust systems.

At least in theory!

PS: on looking at this noticed that most things are not inputs already so for myself I shall experiment with the Hurley since the times of day etc. are optimizable etc.

Of old the skilled first made themselves invincible to await the enemy's vincibility.
Invincibility lies in oneself. Vincibility lies in the enemy.
Thus the skilled can make themselves invincible.
They cannot cause the enemy's vincibility.
Thus it is said: 'Victory can be known; it cannot be made.'
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

cclsys,

First, thanks for your kind words. I'm a firm believer in giving back.

Second, I don't fully follow you but if you'd like to post a screenshot that helps me understand this in layman's terms, I'd be happy to give it a shot.

Thanks,
Dave

Reply With Quote
  #5 (permalink)
 
cclsys's Avatar
 cclsys 
Sydney, NS
 
Experience: Intermediate
Platform: Ninja
Broker: Zen-Fire
Trading: TF,S,GC
Posts: 605 since Nov 2009
Thanks Given: 248
Thanks Received: 393

Dsraider: cannot program NT strategies unfortunately so no screenshots. Just a thought. A little 'bee in my bonnet' with strategies that I think I have been harping on too much about past few days. Would just love to see a simple strategy with this capability. Had many in TS and always found it very helpful.

Of old the skilled first made themselves invincible to await the enemy's vincibility.
Invincibility lies in oneself. Vincibility lies in the enemy.
Thus the skilled can make themselves invincible.
They cannot cause the enemy's vincibility.
Thus it is said: 'Victory can be known; it cannot be made.'
Visit my NexusFi Trade Journal Reply With Quote
  #6 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

Don't know if it helps dsraider but I'm testing something now that uses a bool for trade times and session P&L. Whether the strategy looks at those can be switched on or off before you start the strategy.

In the variables area I have

private bool useTradeTimes = false;
private bool useSessionPnL = false;

And a bunch of user configurable parameters related to those two options.

Then in the OnBarUpdate area I have:

 
Code
                            
if (UseSessionPnL == true && Position.MarketPosition == MarketPosition.Flat && (Performance.AllTrades.TradesPerformance.Currency.CumProfit priorTradesCumProfit >= sessionProfitTarget
|| Performance.AllTrades.TradesPerformance.Currency.CumProfit priorTradesCumProfit <= (-1*sessionLossLimit)))
PrintWithTimeStamp("STRATEGY Erratic NT7: " Instrument.FullName " HALTED by Session P&L Logic");
return;
 
if (
UseTradeTime==true && ToTime(Time[0]) <= tradeStartTime || ToTime(Time[0]) >= tradeEndTime) return; 
I've lifted your Trail Stop code and I'm testing it now. I note you are not using ExitLongStop and ExitLongLimit in your code and are instead using SetStopLoss and SetProfitTarget. Any reason for that? This is the first time I've really looked at IOrders.

Reply With Quote
Thanked by:
  #7 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

cclsys - I actually meant a screenshot of your chart, only because I'm not familiar with that ATR formula. I was thinking that if I saw an example, I might be able to come up with a code snippet that does what you want (emphasis on MIGHT).

MXASJ - You're making me nervous. My time of day and PnL codes have been working perfectly for me. Did they not work for you? As far as using SetStopLoss/SetProfitTarget is concerned, as of now, I exit my trades solely based on how many ticks I want. I don't really have an actual exit strategy. If and when I do, I'll definitely use ExitLong at that point. Oh, for the trail to work properly, make sure you have CalculateOnBarClose set to false. This ensures that the "Close[0]" part of the trail means current price, so it will trail exactly how it's supposed to.

Thanks guys,
Dave

Reply With Quote
  #8 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800


dsraider View Post
MXASJ - You're making me nervous. My time of day and PnL codes have been working perfectly for me. Did they not work for you?

If it works for you leave it alone . I like the option of being able to turn that on/off without commenting out the code, but that's just me.

Reply With Quote
  #9 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

Ahh, okay. Just checking. I actually don't trust myself enough to have that power. I used to be really bad about revenge trading, etc, so this helps keep me from being my own worst enemy.

Hope your code works for you and thanks again for the suggestion. Let me know if you run into any issues with whatever you grab from mine. I'm STILL working on my CancelOrder code. Once that's done, I'll repost with a few other minor additions.

Dave

Reply With Quote
  #10 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800


Paying it forward, dsraider. My code is not as good as yours but you might get an idea or two out of it for further development (switching trade times and session P&L off, for example). Attached is NT7 only I'm afraid but here you go.

Attached Files
Elite Membership required to download: SimpleFramework.zip
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