How to Write A Best Forex Robot - Singapore Forex Trading, Singapore Forex Academy, Singapore Forex Association

Header Ads

How to Write A Best Forex Robot

There are many languages which would be suitable for writing an algorithm trading robot from scratch, but the complications that you will encounter are in fact with the API to direct market access that your stock broker will provide—that is how you will actually enable yourself to place the trade orders in the financial markets. 

Processing market data could be achieved in a plethora of languages, and probably to a faster extent than the MT4 MQL4 can run back tests (back tests are a way of testing your algorithm robot; more on this later). 

For the reasons of ease of functionality and general support for financial software,  I highly recommend using MQL4 (MetaQuotes Language 4), the native language of MetaTrader 4, to write your algorithm trading robot.

 The Job of a Trading Robot
A trading bot uses simple code to perform several basic takes. If you want to customize your own bot you will need to become comfortable programming:
  • Algorithmic trading strategy: this uses statistical models to predict market behavior
  • Develop a personal risk profile: this will depend on your time and knowledge commitment, as well as your trading capital
  • Backtesting: you will need to run your programs to test their success, these tests will be used to validate your bot’s programs

Trading Strategies

While your bot does the work, you need to ensure that it applies sound statistical models in order to build algorithmic trading strategies.
It is beneficial for your bot to take advantage of the following strategy type combinations:
  • Macroeconomic news (ex. non-farm payroll or interest rate changes)
  • Fundamental analysis (ex. using revenue data or earnings release notes)
  • Statistical analysis (ex. correlation or co-integration)
  • Technical analysis (ex. moving averages)
  • The market microstructure (ex. arbitrage or trade infrastructure)
MQL4 is similar in its form to PHP, C, C++ or VBScript. Below is an example of a function that will be called on every tick of market data:
01
02
03
04
05
06
07
08
09
10
void OnTick()
  {
       if(Bars<100 || IsTradeAllowed()==false)
          return;
       if(CalculateCurrentOrders(Symbol())==0){
          CheckForOpen();
       } else {
          CheckForClose();
       }
  }
Here we check if there has been enough market data loaded with Bars < 100. MQL4 sets predefined variables such as Bars (which contains the number of bars loaded into the chart).
Additionally, we check with an or || conditional for IsTradeAllowed(). This is a check function to check that the trading context is not currently busy.

Elements of MQL4 such as these predefined variables and chart operation functions like Symbol() make writing your strategy a walk in the park, and for me are why it is really preferable to code algorithm trading robots in MQL4 over other languages.
I recommend you take a look through the MQL4 Reference whenever you have time to get more confident in using MQL4 to further satisfy your needs.


I recommend using the built-in MetaEditor IDE that comes with the MT4 trading platform. To enter the editor, just right-click on an existing expert advisor in the left-hand navigator pane and select modify from the menu.
The MT4 Trading Terminal and Strategy Tester
The MetaEditor of MT4 will then open, and you will be able to create a new file. This offers the user syntax highlighting and debugging output. 

Important: You will have to compile your mq4 files into ex4 expert advisors using the MetaEditor if you are editing in an external editor. So getting familiar with the MetaEditor is a must.