This is a follow up article on our Introductory post Algorithmic Trading 101. I hope you understood the basic concepts of Algorithmic Trading and its benefits. Now, let’s gear up to build your own Trading system from scratch. This article would describe every step needed to create your first Algorithmic Trading system. We shall use our favorite tool Amibroker to create Trading Algorithm.
- Elementary knowledge of Technical Analysis.
- Hands on experience in Amibroker and AFL Coding.
Check out our Amibroker tutorial series here.
Step 1: Formulate your Trading Plan
The very first step would be to make a checklist of the parameters based on which you take your Trading decisions. These parameters should be something that can be formulated into an Algorithm, strictly avoiding elements of Gut feeling or speculation. It can be as simple as time based decisions like buying a a particular stock on the first day of every month, or decisions based on technical analysis like Trendline breakout with increasing volume. You should also plan your investment amount for each transaction, timeframe for trading, as well as your stoploss and targets. Once you have formulated your plan you should validate it against a bunch of stocks to see if it really works. This step is very important before you jump into the next steps. If your plan works for 50% of time, with a Risk-Reward ratio of atleast 1:2, then you are good to convert it into an Algorithm.
Also Read: Algorithmic Trading Courses for Beginners
Step 2: Convert your idea into an Algorithm
Next, you should start writing a code for your formulated trading plan. A code is nothing but a bunch of statements through which computer can understand your Buy/Sell logic. We would use Amibroker Formula Language (AFL) for writing Trading Algorithm. It’s a high-level programming language and very easy to understand if you start from basics. Even a person from non-programming background can learn AFL and avoid spending unnecessary on expensive ready-made AFL’s. Check this post for AFL tutorial from scratch. Let’s suppose you trade based on exponential moving average crossover in daily timeframe. You would buy a stock when 50 EMA crosses 200 EMA from below, and sell when 50 EMA crosses 200 EMA from above. For the sake of simplicity let’s consider it is a Buy only strategy. Below is the simple AFL code for this logic.
_SECTION_BEGIN("Simple Algorithmic Trading System"); //Parameters MALength1 = 50; MALength2 = 200; //Buy-Sell Logic Buy = Cross(ema( C, MALength1 ),ema( C, MALength2 )); Sell =Cross( ema( C, MALength2 ), ema( C, MALength1 )) ; Buy = ExRem( Buy, Sell ); Sell = ExRem( Sell, Buy ); Plot( Close, "Price", colorWhite, styleCandle ); Plot(ema( C, MALength1 ),"FastEMA",colorWhite); Plot(ema( C, MALength2 ),"SlowEMA",colorBlue); _SECTION_END();
This is how it looks like when applied in the chart:
Step 3: Backtest your Algorithm
Backtesting is a process to validate the performance of your Algorithm on Historical Data. This is something similar to what you did in Step 1 manually. Amibroker has a very powerful backtest engine that can do this in seconds. You just need to import Historical data of your favorite scrips into Amibroker. Check out this link to download Intraday 1 minute data for Nifty and Banknifty. In order to understand the detailed process of backtesting in Amibroker, please refer to the below link from official documentation:
To backtest this EMA Crossover strategy, we will use NSE Nifty as our preferred scrip, with the initial capital of 200000 Rupees. Let’s say we buy 2 Lots(150 nos) per transaction. Once you backtest this strategy you will get a detailed report which includes your Annual CAGR, Drawdown, Net Profit/Loss% etc. You can understand various parameters in Amibroker Backtest report here.
See below the summary of our initial backtest:
|Backtest Period||26-Mar-2002 to 23-July-2016|
|Net Profit %||418.83%|
|Annual Return %||10.45%|
|Number of Trades||11|
|Winning Trade %||54.55%|
|Average holding Period||227.91 days|
|Max consecutive losses||2|
|Max system % drawdown||-33.24%|
|Max Trade % drawdown||-29.94%|
Well, this is not bad, but there are still scopes of improvement. Drawdown is on a little higher side which can put retail investors in trouble.
Please refer the below link to continue reading this article: