How To Use NinjaTrader To Test Simple Trading Strategies

by Mark Williams on February 9, 2009

My current trading platform that I am using is Ninjatrader. The enjoy the ease of use of the program, and the fact that this powerful little sumb**** of a program is free to use for charting and backtesting (but not for live trading).

Free. 100%. AND it’s powerful. You can’t beat that.

AND, it’s a lot better than some of other packages that I have played around with.

Anyway, the purpose of this article is give you a primer on how to create and back test strategies using this powerful program. So let’s get started.

First, our strategy:

The 3 Exponential Moving System 

  • This system uses 3 Exponential Moving Averages.

Entries are as follows:

If the Faster and Medium EMAs are above the Slowest EMA, then the only trades that are to be made will be long.

If the Faster and Medium EMAs are below the Slowest EMA, then the only trade that are to be made will be short.

A LONG trade will be made if the Faster EMA crosses Above the Medium Moving Average.

A SHORT trade will be made if the Faster EMA crosses Below the Medium Moving Average.

A LONG trade is exited when the Faster EMA crosses back below the Medium EMA.

A SHORT trade is exited when the Faster EMA crosses back above the Medium EMA.

My Variables are as follows:

  • Faster Period for EMAs
  • Medium Period For EMAs
  • SlowerPeriod For EMAs

3 Exponential Moving Average Example

Here is how we are going to make this happen in NinjaTrader:

Open Ninjatrader, and go to Tools ->New NinjaScript -> Strategy

Create a NinjaTrader Strategy

Hit Next:

Name and Description of NinjaTrader Strategy

From here, we are going to name the Strategy, and give a nice description of it.
Now, there is an option for calculate on bar close. What this means is that if your strategy is activated prior to the creation of the period bar that you are using (for example, the daily bar), then you will jump in immediately.

For example. Our FAST EMA line is about to cross the Medium EMA line. The current price of our security at this moment is 20 pts. If the security were to go to 30 pts, then the Fast EMA would cross the Medium EMA.

If we do not calculate our strategy on bar close, once the price crosses the 30pts line, we get in NOW, instead of waiting on the bar to finish up.

So, why would we ever set this for use on calculating the full bar?

Well, let’s say the current price does shoot above 30 pts, creating our cross of the Fast vs. Medium. If we have the calculate on bar close on, we are not going to do anything until the time period of this bar is over (in our example case, the market day).

So price shoots above 30pts, gives us our signal, but the bar is not done being calculated yet. Then, it crashes, and before the day is done, the bar’s current price drops to 15pts, and at the end of the day, we do not end up with a cross. Then the next day, the bar’s price is at 13pts, and it is continuing on a downward slope.

By calculating on bar close, we prevented ourselves from getting in to a losing trade. By not calculating on bar close, we got into the trade, but it didn’t work out as planned.

But, what if the price ended up at 50pts on the day?

If we calculated on bar close, we’d be getting in the trade at 50 pts. If we’d not calculated on bar close, we would have already been in the trade for a 30pts (unrealized) profit because we took the signal early.

Lets continue.

Now, we enter in the User Variables:
Trading Strategy User Variables

Once these are entered, we click on next and enter in our parameters.

New Strategy Basic Inputing of Logic

There are two spaces here. The top area is where we are going to put our logic statements. The bottom is where we put the actions that we want to accomplish.

So, the top area is where we let the program know, “hey, if everything in this box is true, do what ever is in the bottom box!”

When we click add, it opens up a menu where we select our parameters. So, we want to compare the 3 EMAs that we are using so, lets select them. We will start by adding our comparison of the Fast EMA vs. that of the Medium EMA:

Selecting the parameters of our strategy

We start by selecting EMA from the left pane.  Now, where it says PERIOD, instead of putting a static period, we are going to select our FAST EMA variable by selecting the 3 dots:

Selecting our Strategy parameters

And then selecting our parameter from the list.

Once we have done that, the logical operator is selected from the middle drop down list. You can select from the following:

  • Greater than >
  • Less than <
  • Greater than or equal to >=
  • Less than or equal to <=
  • Equals ==
  • Crosses Above
  • Crosses Below

Finishing up the First Logical Parameter

So, in our example, I want it so that if the Fast EMA “Crosses Above” the Medium EMA, we are going to do something.

Now that we are done with the first logic parameter, we are going to move on to the second one, or more comparisons if you want to add more.

Now it is time to add our actions…

Selecting the Actions that are to be done

Selecting the Action of going long!

Now we have completed our first set of rules for our trading system we are testing: “If the Medium Exponential Moving Average is above the Slower Exponential Moving Average, and the Faster Average Crosses Above the Medium Average, let’s buy!”

Done with Part one

Done. Now let’s hit the Set 2 tab to do this for our short trades:

Set 2

(Ignore the Variable1 =1. That was a mistake)

Now, let’s exit our positions via our rules. Select Set 3 and do the following:

new_strategy_3ema11

So, instead of an indicator, we are now saying that if our market position is LONG or SHORT, then do X

Now we finish our LONG exit:

Our LONG exit criteria

And our Short Exit…

Exit our SHORT position

And we are done.

Next, we will go over actually running our strategy.

{ 11 comments }

How to Create And Test a Trading Strategy, Part III

by Mark Williams on February 4, 2009

So,

You’ve thought out your idea, fleshed it out, got a flowchart and summary going, now, it is time to create get your strategy created and backtested.

But, how do you go about getting the strategy created?

Well, there are two ways to go about this.

The first way, which is the HARDEST, is to code your strategy itself.

The second way is to hire someone to do it for you.

Let’s go over the second way in more detail today.

First, you need to know a little bit about your platform. What I mean by this, is what type of coding language does it use.

There are many languages out there that the more robust trading platforms use, but the MAJOR ones are EasyLanguage, Java, C++ and C# (C sharp).

Once you have this information, and armed with your flowchart and work summary, you can hire someone you know who is a programmer, or outsource this code to an outsourcing site.

I recommend the following sites:

Elance

Rent-A-Coder

Guru.com

Of the three, Rent-A-Coder has been the cheapest to use, but Elance has many protections in place in case someone tries to rip you off (although both rent-a-coder and Guru have come a long way in this regard)

These sites work like a reverse eBay. You state your project, your time line, and milestones, and people from all over the world will send out bids for the work. Then, you choose the price and person that you like, and you go from there.

Each site allows you to setup an escrow account that you can then use to pay the person, per milestone.

Now, if you believe that you have found the holy grail of all strategies, and you don’t want to people to find out about, you do not have to put up all the details of your project. However, having the flowchart handy and the written summary will help out the programmer who takes the assignment immensely

Also — and this may be the GREATEST piece of information you will EVER get from this site — DO NOT EVER go for the lowest bidder. End of story. Don’t do it, Don’t do it, Don’t do it, or you WILL get screwed over GUARANTEED!!!

Another piece of advice, and it’s a little tricky thing to do, it, at the end or at the beginning of your auction ad, write something along the lines of “This should be a cinch/really easy/piece of pie for someone who is versed/skilled in/got skills in/knows about/is an expert in <enter your trading platform or computer language of choice here>”

Example: “This should be a piece of cake for anyone who has made indicators and strategies before in NinjaTrader!”

I don’t know why (and I have tested this), but this statement has caused everyone bidding on my projects to drop the price of their bids BIG TIME!

Don’t ask me why it works, or why people even go for it, but it does.

Next, we will go over what you can do if you want to program the strategy yourself.

{ 1 comment }

How To Create And Test A Trading Strategy, Part II

by Mark Williams on January 29, 2009

In Part I of this series, we took a look at the beginning brainstorming phases of our project, to create and backtest our trading strategy. Today, we are going to take a look at fleshing out our ideas and making what we have easily available for us to code, OR have someone else code for us 😉

So, we have done our mindmap. Now, it is time to take that mindmap, and turn our idea from the abstract to the grounded — to give some order and guidance for our next step of the game.

Now, depending on the type of personality that you have you can do the following:

  • Either completely WRITE out your plan in detail, in ENGLISH (or whatever your native tongue is) or
  • Do a flowchart

Either way it goes, you are going to have to do BOTH to continue on, but you should choose between the above two methods to get you and your mind started (yes, I know the above was REALLY bad english syntax).

So, let’s again take my mindmapped example:

So taking the above mind map, here is my whole strategy:

My variables are:

  • Account Size
  • Shorter Volatility Period
  • Longer Volatility Period
  • Number of bars to look for to get in for trade
  • % of account to trade on each trade (little did I know that this one factor would have the LARGEST effect on my overall gains)

Current Bar is defined as the time frame that I am using per update period. For example, if I am using daily as my time period, then current bar is the daily time period. If I am using minutes, and I want to have bars representing 15 min segments, I can do that as well.

I will use the following formula every bar :

  • Volatility Adjusted Bar = (My number of bars to look at) X (Shorter Volatility / Longer Volatility)
  • Longer Volatility is equal to the Average True Range Moving average of the Longer Volatility Period
  • Shorter Volatility is equal to the Average True Range Moving Average of the Shorter Volatility Period

(Note: the more volatile the security, the more days I want to have to determine breakouts therefore, I am not whipsawed as much due to having periods of higher volatility)

  • If the HIGHEST HIGH of the current bar is greater than the HIGHEST HIGH of the “my number of bars to look at”, then I want to buy ie. GO LONG.
  • If the LOWEST LOW of the current bar is greater than the LOWEST LOW of the “my number of bars to look at”, then I want to GO SHORT.
  • If I GO LONG, or GO SHORT, here is my buying criteria:

Number of share to buy = (Account Size X percentage of account)/ (current price of security X margin, if any)

Of course if number of shares to buy is less than zero, then we are not going SHORT or LONG.

Finally once I am LONG, if the current bar is less than the LOWEST LOW of “my number of bars to look for” DIVIDED by 2, then I will sell.

If I am SHORT, if the current bar is greater than the HIGHEST HIGH of “my number of bars to look for” DIVIDED by 2, then I will buy back the security.

Once I am out, I will update my Account Size with the P/L.

So, now I have taken it from the abstract to the linear. If you gave this to most programmers, they should be able to handle it.

However, there is one more thing you need to do to make sure that you have all of your ducks in a row — to make sure everything flows very well.

FLOWCHART!!!

A flow chart is an easy way for whomever is doing the coding of the strategy get a handle on what it is that you want to accomplish.

So, how do you go about making one?

Well try here.

Gliffy is a FREE online flowcharting software.

Okay, it’s not EXACTLY free. If you want to make more than 10 FLOWCHARTS, then you have to pay, but it is not too bad of a service if your job requires you to make a lot of flowcharts for organization /plug 😉

Here is my first flowchart of my strategy:

So, now that both the written statement and flowchart are made it is coding time. Time to hire a programmer, or crack your knuckles, dig in and do it your self.

In Part III, I will show you the different routes to go through to hire quality programmers, and give you advice on how to do the coding yourself.

My personal advice? If you are NOT BROKE, hire a programmer.

If you are broke, hunker down, and learn the coding of the platform that you are working from.

You are going to learn very quickly, that when it comes to the online world, those without time had better have money, and those WITHOUT MONEY, had better have a lot of time.

I use Ninjatrader and Genesis Trade Navigator, and these are where my examples are going to come from.

Ninjatrader is free, BTW — as long as you are not going to use it to do your actually trading. Once you are ready to trade, then you have to pay.

However, for something that is free, it is VERY powerful.

{ 0 comments }

How To Create And Test A Trading Strategy, Part I

by Mark Williams on January 26, 2009

You are finally ready to take the trading plunge. You have come up with an idea, or you decided to purchase a course, and are ready to get at it.

But, before you jumped right in, you wised up and decided to see if your idea actually works.

So, how do you go doing it?

Well, there are many ways for you to do this. I am going to show a you VERY low cost way to do this.

So, let’s start.

First, let’s go on ahead and get your idea fleshed out.

The example strategy that we are going to use is based on that of the famous Turtle Strategy created by Williams Eckhart and Richard Dennis. It was a strategy that they used from the late 70’s to the early 90’s that was very simple. If the security that they were trading went above the HIGHEST HIGH within X number of days, GO LONG.

If the security they were trading went lower than the LOWEST LOW within X number of days, then GO SHORT.

I thought about taking this strategy and seeing if I could make it Auto adjust based on Volatility. The more volatile the security, the more number of days I would allow before I would take a signal. The less volatile, the less number of days.

The way I went about fleshing out this idea was starting with a mind map.

Marks Mind Map showing his strategy of Adaptive Donchanian Channel

Mark's Mind Map showing his strategy of Adaptive Donchian Channels

The way a mind maps works is that you take a sheet of paper, and place a circle in the middle.

The name of circle could be anything, as long as it helps get you started.

My center is just the name of my strategy (before I changed it),  a “Variable Turtle Strategy.”

From there, just let you ideas flow.

For each idea, add a branch and a circle.

You’ll notice that in my mind map, I could not decide how I was going to go about getting the volatility of a security, so I listed every way that I did know.

In each circle try to use action verbs and nouns — you are just brainstorming for now.

However, as you run through and do this, leave nothing behind. Hell, if you notice on my mind map I have many terms for my strategy listed multiple times.

I also feel that the messier, the better.

There is Mind Mapping software out there for those that prefer it. My personnel favorite for mind mapping software is Free Mind.

Once have brainstormed your strategy, create a bubble called “Variables.” In this bubble, after going back through your strategy, you will place the variables that you’d like to test. Using my Adaptive Turtle Strategy, I realized that I wanted to use the AVERAGE TRUE RANGE of the security over a short term period (to be controlled by me), and that of a longer term period (to also be determined). I also realized that I wanted to have a counter  for Account Size, since I wanted to accumulated total of this strategy over a long period of time.

So, once our mind map is done, it is time for Part II of our series creating flowcharts.

Why do we need to create flowchart?

Well, it makes  it easier to program our strategy since will now have a map of what to do…

And…

If you are a non programmer, then having a flowchart and a detailed description of your strategy is essential if you want to have someone do the programming for you!

More to come!

{ 1 comment }

The 3 Keys To Determining Your Trading Edge

by Mark Williams on January 21, 2009

You need to have an edge in your trades. Without it, there is no way you will be able to make money long term with trading.

The reason for this is the true secret to trading: You Want To Avoiding Giving Back More Than What You Keep!

So, you may wonder, how does one go about creating this mythical, magical thing called an EDGE (trading edge)?

Well, in trading, you can gain an edge by

  • Choosing a proper portfolio of securities to trade(EUR/USD pair, S&P Eminis, Options, Crude contracts, AAPL, etc).
  • Choosing the trading system (Bollinger Bands? The Turtle system? Donchian Channels? RSI/Stochastics)

and most importantly…

  • Money Management (Where are your stops? When do you get out of a trade? How much are you putting into a trade? How many contracts/shares? How much of you account should you risk on a trade?)

Of these, the most important one here is money management. You could be trading the best system in the world, but without proper money management, you WILL blowup your account (a blowup is a massive string of losses that end your ability to trade).

{ 0 comments }

Let’s play a game.

It’s a simple game mind you, just your standard ole, heads or tails, coin flip.

But, let’s put a wager on it.

If it lands on heads, I give you $200 dollars.

If it lands on tails, I will take away $200.

How much money would you expect make after about 10 mins of play?

Who friggins knows, you’ve only been playing for ten minutes, anything can happen.

What about after 15 hours of play.

By then, we should have had enough coin flips for the results to be statistically relevant.

Well, with a simple coin flip, the odds are 50-50 (1 to 1).

And, even though you may catch a hot streak, and get 4 or 5 heads in the row, because of the mechanics and distribution of the game, you WILL be giving that money back — you will revert back to the mean.

So, the longer that you play this game, closer your bank account will be to zero.

So…how COULD you make money in this game?

What variables in this game could you change so that could make money? How could you make it so that you have a POSITIVE expectancy to make money?

Well, you could change the dollar amount that you make…

OR…

You could change the expectancy so that you get heads more often as tails.

Let’s explore changing the dollar amount.

Let’s make it so if it is heads you make $200, but if it is tails you LOSE $150.

How did that change the game?

The coin flip’s odds are still 1 to 1, so for every HEADS, there will be a TAILS, but now for every flip of the coin you make, your monetary expectancy is now 1.3 (200/150).

If I knew that for every coin flip made, I was going to make $1.30, you’d best believe I’d a coin flipping mo-fo!!!

By changing one variable in the game, you changed it from a null expectancy game to one with a postive expectancy.

And that, ladies and gentleman is what we call an EDGE — a change in the probability of an outcome in the favor of the person.

Another example.

You and your coworkers all live on the same street. You have to get to work in the morning.

@7:25AM every morning, you AND your co-workers all leave the house at the exact same time to head to work.

One day, you have calculated, because you got bored, that 25% of the time you are the first to arrive at work.

What can you do to increase your edge so that you get to work first, more often?

You could buy a faster car…

You could run all your co-workers off the road 😉 …

You could slash all of their tires every night…

Or, you could just get up earlier. If everyone is leaving at 7:25AM, what if you left at 7:20AM?

That one variable change would be enough to give you an EDGE in getting to work FIRST.

Now, will you always get to work first by leaving five minutes sooner?

No. You could get in accident. Traffic could be crappy, and you happen to get into the lane that is not moving. Fog could imped you vision long enough for your co-workers to catch up.

But, in the end you still have an EDGE, therefore, in the long run YOU WILL GET TO WORK FIRST!

And that is EDGE!

So…why do you need to have it?

The better your EDGE, the higher the expectancy that you will MAKE MONEY — in the long term. And that is what all traders want — systems that make them money.

{ 0 comments }

I was a new sailor onboard USS Enterprise (CVN-65, no Star Trek jokes in the comments please) when I first met T.I.

T.I. could be described as one of those only in the military stories, one of those guys that there is no way in hell that they could ever exist in the “civilian” world.

You see, T.I. grew up in a racist family. No one knows why he joined the military, but here he was now surrounded by people of different backgrounds, ideals…

Races.

There is a trading lesson here, trust me.

Anyway, T.I. was on his way out, but because of his “seniority”, all new guys, including myself had to suffer — I mean — listen to him.

However, something happened to T.I. in which we will find our lesson:

T.I. found out that he was adopted.

He then found his birth parents…and discovered that he was not white, but Puerto Rican.

It gets better…

After he found his birth parents, a major change happened in T.I.

He became a “loc down GANSTA ESSE!”

I guess he thought that to be Puerto Rican meant to be your stereotypical latin gangbanger from the movies, so that is the persona he decided to take.

He even became fluent in Spanish!

But wait, there is more, and there is a lesson to be learned here.

From there, I guess he thought that if everything that he learned from his racist pseudo-baptist upbringing was wrong, hell let’s go wild!

He tried hanging out at goth clubs, rave clubs — doing anything and everything to try and find out who he REALLY was.

He even got himself a boyfriend!

In the end, the military finally kicked him out on a psychological discharge — he had gone that far down the deep end.

So, what does this have to do with you?

Well…

When many of us started out in the trading game, it is usually as a “buy and hold”-er — a wannabe Warren Buffet type.

And, (especially the returns that the market is currently giving) we eventually wake up from that premise and begin to really explore how to make money in the markets.

Eventually, we discover trading, starting with a few indicators.

Then, more and more and more indicators are used.

Eventually, we end up with 17 or more indicator monstrosity that gives conflicting signals.

Eventually we end up making up rules, like “I will listen to this indicator only if the market is trending, the low tide has come in, and only during the winter solstice”…

And then wonder why we are LOSING MONEY!

The best trading strategies have the following:

  • They are simple. The more complicated, the worse off you will be.
  • They have an EDGE.
  • They are clearly defined.

Simple. But then again, who was it that said common is sense is not so common?

{ 1 comment }

It took a while, but I had finally done it.

I had the results from backtest of the trading system that I had been using FOR YEARS there in the palm of my hand.

Did I truly find the holy grail system, I had thought, or was my system just doomed to failure, but I just did not know it yet?

Like a kid at Christmas, I watched the system compile all of the data (very quickly I might add), and it finally revealed what I had been waiting for…

A little background first.

The system that I had been using had been a derivative of another system that I learned from a workshop many years ago, you know, one of those $1000-4000 ones. I had been using it for a while, but somewhat moderate success (I found another system by accident that I had been using more).

I had always wondered by that system I had used was so wishy-washy with the results I that I was getting. You see, when every was good, it was REAL good. But when everything went bad, it went REAL bad.

And now I know why…

Anyway, back to the results.

With the system that I had been using for stock trading, continued usage would have resulted in a 87.6% drawdown (over historical data).

87%

87%!!!

Using the system on the S&P 500 stocks only resulted in 6 out of the 500 stocks being profitable over time, and a Risk to Reward ratio of 11:1

And this is over a time period of 20 years (1988-2008).

I will not reveal the company that I learned from, nor will I tell you the rest of the results (using this systwm with commodities and forex would have resulted in heart palpitations, nausea, and thoughts of suicide).

So, what’s my point?

You need to learn how to test your trading strategy.

Period, end of story, just do it.

Too lazy? Well, if you do decide to take one of those super wham-a-dine trading systems courses, make sure that they provide you with backtested results of said system.

If they do not, then get the hell out of there.

Now, I am not knocking taking seminars on trading. This post is actually far from that.

If anything, taking a training seminar on trading is one of the FASTEST ways to learn how to trade.

But, if said course says “Use this system, because it has given us X return, over such and such a time,” then make them prove it…unless you like losing money.

There will be more to come here about how to test, what to test for, and how to understand all the information that you have received from said test.

I will also show you how to avoid the common traps associated with backtesting, and tricks that people use tswindle a person into thinking that the system that they invented is THE holy grail.

{ 0 comments }

Great traders are MADE, not born!

And if you want to be one, you have to learn to how think like one.

I can hear the whining coming from you right now, about not having any time for reading, this, or you don’t read, or you hate it, but you know what?

Who cares! If you can’t think like the pros — you know, get into their mindset  — then you should just give up now, shut down your computer, turn off the lights, and cry like the little victim that you want to be!

Plus, many of these books are available in AUDIO format. As a matter of fact, if you can get an audiobook, that is even better.

Listening is a passive activity. With the audiobooks, you can learn in your car on your way to work, while taking a jog, walking the dog — you get the idea…

Still here? Good, here’s the list:

1. Market Wizards by Jack Schwager. This book is slightly old, but the information contained inside is remarkable. Jack Schwager interviewed many great traders in this book during the late 80s and got insight into their trading styles, ambitions, and trading strengths. From PROFITING from the 1987 crash to making 15,000% profits on trades, the knowledges nuggets tossed around in this book are…are…JUST GET THE DAMN THING ALREADY!

2. How to Make Money In Stocks by Williams J. O’Neil. This book is a great primer on how to read and interpret the fundamentals of stocks.

3. Hot Commodities: How Anyone Can Invest Profitably in the World’s Best Market by Jim Rogers

4. Bollinger on Bollinger Bands by John Bollinger. Everything you need to know about my (and soon to be yours) favorite indicator, the Bollinger Band.

5. Rich Dad, Poor Dad by Robert Kiyosaki.

{ 0 comments }

The Inaugural Post To Get Things Started!

by Mark Williams on December 7, 2008

There is a reason that you have arrived at this site.

It could be that you stumbled on to it by accident via a Google search…

It could be that you were playing around in the toolbar and found this site…

Or it could be that you are down and out — sick and tired of losing money in the market — and are on your last rope just looking for something (or someone) to help you.

Regardless of your reason for being here, I am here to help you gain the edge that you need in the market.

From choosing the proper charting or trading suite, to trading system development (to help you find your holy grail), to testing to see if your system that you developed (or bought) even works, I will go over everything that the PROs out there do not tell you…or even want you to know about.

…Plus, I like to give away free stuff too!

{ 0 comments }

Disclaimer | Privacy Policy © 2007-2008 YouSuckAtTrading.Com All Rights Reserved.