This is the continuation to our previous post on Algorithmic Trading System development. If you have missed reading it, refer the below post:
Build your own Algorithmic Trading System: Step by Step Tutorial- Part 1
Step 4: Optimize your Algorithm Parameters
Optimization is the process of iteratively finding the best parameters for your Trading System. For ex: in our example we have used 50 and 200 as the EMA periods, now we would try to optimize these parameters to see if there is any other Moving average combination which can give better results with least drawdowns. You need not to input these parameters manually and record backtest results. Amibroker optimization engine would make this task simpler for your. It will iterate through the range of parameters you specify and evaluate your system performance for each set of parameters. To understand Optimization process in detail, go through the below link from the official documentation:
We’ll add below two lines in our AFL code
MALength1 = Optimize("MALength1",50,5,150,5); MALength2 = Optimize("MALength2",200,10,200,10);
Here we have used a function called ‘Optimize‘. This function instructs Amibroker that the variable needs to be optimized. Below is the signature of this function:
optimize( “Description“, default, min, max, step );
where,
Description: Parameter description that we are optimizing
Default: Default Value of the parameter to be shown if plotted in chart
Min: minimum value of the variable being optimized
Max: maximum value of the variable being optimized
Step: An interval used for increasing the value from min to max
Go to Analysis–>Optimize. Make sure that your AFL file is selected in the Optimization window. This process can take few minutes depending on the amount of Historical data. Once Optimization is completed, Amibroker would display the detailed results automatically sorted from best to worst. Below is the snapshot of Optimization result:
Clearly 5/30 combination of EMA’s gives the best results with minimum drawdowns. Net profit has increased from 418% to 585%, also drawdown has decreased from 29% to 13%. You should try the same combination on different symbols and time-frames to see how robust is your Trading system.
Step 5: Risk Management
It’s not enough to build a successful Trading system which gives decent results. You definitely need to incorporate Risk Management which would help you overcome unpredictable market risks. Check out our blog post which outlines the detailed Risk Management strategies for Traders:
Risk Management strategies for Traders
You might have noticed that the simple Algorithmic system we have developed does not have any Stop loss component. All the long positions are squared off based on the Sell signal. Let’s try to add Stop Loss in this system. Add the below piece of line in your code:
Stoploss=2; ApplyStop(Type=0,Mode=1,Amount=StopLoss);
ApplyStop is a Function which instructs Amibroker to exit the trade when a predefined Stoploss or Target condition is met. Below is the signature of this function.
ApplyStop( type, mode, amount)
type =
0 = stopTypeLoss – maximum loss stop,
1 = stopTypeProfit – profit target stop,
2 = stopTypeTrailing – trailing stop,
3 = stopTypeNBar – N-bar stop
mode =
0 – disable stop (stopModeDisable),
1 – amount in percent (stopModePercent), or number of bars for N-bar stop (stopModeBars),
2 – amount in points (stopModePoint);
3 – amount in percent of profit (risk)
amount =
percent/point loss/profit trigger/risk amount.
This could be a number (static stop level) or an array (dynamic stop level)
Step 6: Monte Carlo Analysis
Monte Carlo simulation is one of the most important steps in Trading system development and optimization. s a process which performs repeated execution of pre-defined set of steps by adding randomness to the input parameters at each iteration. The results are noted down at the end of each iteration which forms the basis of probabilistic analysis of the desired result. In Trading terms, Monte Carlo simulation is performed to forecast the success of a backtested trading system. In order to ensure that your trading system is robust, backtesting should be performed multiple times by adding variations to your trading rules or data. If it gives consistent result each time, then it has a higher probability of generating profit. The below post explains the detailed process of performing Monte Carlo Analysis in Amibroker:
Monte Carlo Analysis in Amibroker
See the snapshot of Monte Carlo Analysis report for our example Trading System:
It is a well-known fact that ‘Markets are Random’, so Monte Carlo simulation is a method to absorb this randomness in your Trading system. If your system performs well in random market conditions, then it has a huge probability of success. At the end, everything boils down to probability and that is actually the basis of all profitable Trading systems. It’s really not enough to believe in the Trading system just based on profitable backtest reports. Monte Carlo analysis weighs equally while designing a system.
Step 7: Automate your Trading System
We strictly recommend that you should trade manually for at-least 6 months before you go for fully automated trading. Once you have full conviction on the performance of your Trading System, you should start exploring automation. Automatic or semi-automatic trading do not let emotions affect your trading decisions, it directly places the order in terminal when your strategy gives signals. There are many ready-made plugins and softwares available in market which can interpret buy/sell orders from Amibroker and place it into your Trading terminal. Check out the below post for list of such softwares:
Auto Trading Softwares and Plugins
In order to completely automate your strategy, you will need a dealer terminal. Exchanges don’t allow completely automated platform on a retail terminal.To get a dealer terminal for NSE/BSE Segments, you will need to become an authorized person with your broker and also clear the NISM-Series-VIII – Equity Derivatives Certification Examination.Whereas there is no such procedure for MCX. There will be a one time cost to register as an Authorized person(need to check with your broker) and a rental for dealer terminal of Rs 250/segment/exchange.
Step 8: Observe the performance
So you have developed your system, convinced with its performance an/or automated it. Unfortunately the story doesn’t end here. The markets are always changing, and so should your strategy. You have to observe you system performance regularly and note down anything that doesn’t happen as expected. Always remember that you would never be able to devise a holy grail system that performs in all market conditions.
Download the example AFL Trading system that we discussed throughout this article in the below link:
Where can I find all AFL functions and its parameters in details?
Please check out the below link:
https://www.amibroker.com/guide/a_funref.html
Can it be integrated in zerodha kite or pi
Hi Arun,
Yes, you can automate your AFL algorithms using Pi bridge offered by zerodha
Hello I m newbie to trading, how do I select which stocks to trade ?
If I select my risk reward 1:3 ratio?
Can technical helpin monthly trading ??
Is it possible to have a target of earning each day ??
Hi Arun,
Please go through the below articles in that sequence. It may help you to answer your questions. Let me know if some of them are still unanswered.
https://tradingtuitions.com/7-steps-to-become-a-successful-trader/
https://tradingtuitions.com/how-and-where-to-learn-about-stock-market/
https://tradingtuitions.com/how-to-pick-stocks-for-intraday-trading/
hi , i am looking the programmer to develop auto robot plugin for mt4 and trading stratigies. any one can help us . call /whatsapp 9911820267