14 September, 2010


Since we've created this EA, I've dump it into 1 of my demo account and test it out. So far so good. No major drawdown yet. I will update the EA again in near future. Have a look at the screenshot. I don't know how to make the picture larger in the blog post, if you know the way, kindly update me regarding this matter.

01 September, 2010

To change indicators

Sorry for not updating long. I've been having a tough time with my life lately. A lot of things going on, and the only constants I'm having right now is change! So, here I am again. I'm trying to put things together so we can continue to achieve financial freedom with our own build EA.

I have put up some codes from our previous project. I have removed the MACD indicators and replace it with Moving Averages.
I myself is more comfortable playing with Moving Averages crossing instead of reading the original MACD idea from the MACD Sample.mq4 provided by Meta Trader 4.

So, here we go again. Have a look at the screenshot below to find out how I modified them.


External variable has been removed and replaced with Fast EMA and Slow EMA.


And here is where you remove the MACD indicators. Replace them with 4 moving averages. Now, some might ask why there is 4 moving averages instead of 2. Here, let me tell you, EA does not read the lines. Instead, it need numbers in order to form an actual logic. For example EMA8 crosses EMA20 from below, normally we see this as an uptrend. EA will have to interpret it as current EMA8 is higher than EMA20, whereas previous bar it was EMA20 higher than EMA8. Therefore, we need 4 variable to hold the correct value of a MA cross.

The above screenshot is changing the trading condition from MACD to Moving Averages cross. Let's have the last screenshot in changing the closing trades condition. It's simple, just change buy to sell and sell to buy from the closing trade condition.



24 October, 2009

To back test

To have a legit and more accurate backtest, you got to have enough history data in your platform in order to perform a good backtest. Press F2 on your keyboard, MT4 will automatically direct you to a history centre. Select a symbol you wish you backtest, in this case we use GBPUSD as our test. Double click on GBPUSD, system will drop down several timeframe. Double click on 1 minute(M1), then hit Download button. MT4 will download as much history data as possible from Meta Quotes Corporation.

Once download completed, you'll be able to see something similar to the above. This can apply to other currency pair. If you wish to backtest with other symbol, simply repeat the procedure on other symbol's 1 minutes (M1) data. Click close button to close data center. Hold your "Control" button and press "R"(Ctrl+R), this will call out the tester window at the bottom of MT4.


Here is a brief introduction for each function in the tester:
Expert name: You can change any expert advisor to test with
Symbol: Currency pair you wish to test with
Model: Normally we use every tick as the quality of backtest is more reliable. You can choose other type of modeling, but less accurate.
Period: Time frame to test with (1 minute, 5 minutes, 15 minutes, 30 minutes, Hourly...etc)
Expert properties: Allow you to change settings on the EA
Symbol properties: To show you the testing environment of the currency pair you wish to test with
Open chart: Once backtest finish, by clicking this button, you'll be able to see all the trades placed on the chart.
Modify expert: MT4 will call out Meta Editor and immediately open the source code of the EA for your convenient to modify codes.

Below are screenshots for Expert Properties:


The above shows Initial deposit as 10000. It means the backtest should start with initial balance of 10k. Position Long & Short meaning both buy and sell are allowed. You can choose either buy only or sell only.


Above screenshot shows the input tab in Expert Properties. You can change the parameter of the EA for this particular backtest. In this case, we change the stop loss to 200pips from original 50pips. You can Save your parameter by clicking "Save button" or load a preset you've previously saved. Once completed setting up parameter, click OK button to exit.


Back to tester main window. Select the date of testing. From ~ To must be complete. Now, you can enable Visual mode. By enabling visual mode, you'll be able to view the trades on the chart whilst the tester is doing all the calculation at the background. Visual mode is obviously slower compare to non visual mode. Or you can choose to run a quick backtest without visual, and click on the Open Chart button later to view the trades offline. Click on Start button to begin backtest.

After the test, you can go to Report tab at the bottom of the tester window. You'll be able to see the full detail of the backtest. You can right click on the report and choose Save Report to get a full HTML report with equity graph in it. If you notice, there are 2 important column in report. Mismatched charts errors is the number of bars you downloaded from history center does not match with the visual on the chart. Many reason causes this, such as difference of brokerege firm's rollover time, data corruption, data overlapping and so on. With a bad history data, automatically your Modelling Quality will drop. Normally 90% is good enough and very acceptable. However, there are professionals that uses TICK DATA to produce 99% accuracy backtest result. There is a whole bunch of knowledge in collecting good quality tick data and how to import them into Meta Trader 4 to do backtest with Expert Advisors. I have not plan to extend our focus that far, let's just stick with this for the time being. Meanwhile, if you're interested in 99% backtest, you can search online.

The above equity graph are generated automatically from the backtester after I ran a 3 month backtest on GBPUSD.

18 October, 2009

Magic number

OK. Let's talk about trade handling. This EA was made to trade alone, and not suitable when there is human interference or other EA trading on the same account. When you try to place a trade on the account with MACD Sample trading at the same time, the EA will take over your position and modify your stop loss according to the TrailingStop you've declared in the EA earlier. Which is why a Magic Number is important. We need a special tag for a robot to recognize which are the trades that they should handle, instead of modify all stop losses for all the trades on the same platform. So, here we go...


Place a new external variable for user to key in Magic Number for the EA to recognize it. Magic Number is a special tag that doesn't show on your trading statement because its hidden. Since we're doing Magic Number, I've take another step to pull out the trade comment. By putting the trade comment as a variable, this can help us trace our trading history. In example, while we're modifying the EA from version 001 until current version 004, we have trouble to figure out from where our EA evolved. By looking at the trading statement, scroll all the way down, all the trade comment saids the trades are triggered by "macd sample". Therefore, in the future we can change the trade comment directly from the variable everytime we modify the EA.

Magic number had taken over the original fixed magic number 16384. Meanwhile, trade comment will automatically refer to the variable we've declared earlier.

Added new filter here. Whilst the EA trying to filter out all the trades from the same pair(OrderSymbol()), we've added new Magic Number as new filter for the EA to recognize which are the trades needs to handle instead of handle all the trades from the same pair.

04 October, 2009

Been busy lately

Hello there. I've been busy lately. Have not been able to update the blog. I'm sorting things out and will continue to blog as soon as possible. Here is what happened to me while I'm not around:

18 July, 2009

An efficient way to declare SL and TP

Previously we have discussed about how to put down a stop loss in the EA. Now, it is time to further improve it. Originally, the EA came with fraction variable declared on its Take Profit column. The reason for using "double" in the code is to hold the value of market price, such as bid on EURUSD as 1.4002. Previous code explanation as below:

Ask+TakeProfit*Point = Actual take profit
1.4002 + (50pips * 0.0001) = 1.4052

And therefore the EA will put down 1.4052 as our take profit. Here is the problem. What if we revalue the parameter of Take Profit as ZERO? Further elaboration as below:

Ask+TakeProfit*Point = Actual take profit
1.4002 + (ZERO * 0.0001) = 1.4002

Now, I doubted what is the purpose for you to trade something stupid like opening and close trade at breakeven? I even doubted whether your broker is going to allow you to do so. Most broker has the minimum 5pips limit between their order open price and their take profit/stop loss... hmmm... here is what I did. Change it from "double" to "int", add new variable as SL and TP to accommodate the actual value of the price we want to take profit and stop loss.



Screenshot above is showing how I declare my new variable. I've added "//" to remark the old codes.


2nd screenshot. I've removed the TakeProfit checking routine. We will check the take profit value later.



Here is the screenshot I added the new set of codes to allow 0 stop loss and 0 take profit to the system. The logic as below:

StopLoss=50
if StopLoss>0
Ask-StopLoss*Point = SL
1.4002 - (50pips * 0.0001) = 1.3952
Otherwise
SL = 0

Now we have a real 0 Stop Loss.



Here is the set of code I added to sell position.

27 May, 2009

To put down a stop loss

MACD Sample EA is a swing trading system. However, this swing trading system doesn't come with a stop loss. Its either you get it right on your buy position, you sell off at higher price to make money; or you got it wrong on your buy position, then you get a sell signal at lower price. So, you can choose to put down a stop loss in order to limit your losses when thing goes wrong.
Place these codes into your MACD Sample EA.

By adding extern in the code, you'll be able to control the external panel. StopLoss is the variable name. double in the code will allow the StopLoss variable to store fractional figures. We temporary put 50point as the default figure. Anytime when you load the EA on the chart, your default TakeProfit and StopLoss will be 50.



We have defined the parameter. Now we add an extra code into the OrderSend() function. OrderSend fucntion work with these element:
Option = OP_BUY,OP_SELL,OP_BUYSTOP,OP_SELLSTOP,OP_BUYLIMIT,OP_SELLLIMIT
Lot size = Any figure your account able can trade
Order open price = Ask for buy, Bid for sell, or put down a figure if you want to use pending order
Slippage = How many point of slippage allowed
Stop loss = The price when you decided to cut loss
Take profit = The price when you liquidate your profit
Trade comment = To put down a comment on your trade in order to recognize them traded from which trading system
Magic number = Any integer figure. This is for the EA to recognize its an auto trade instead of trader's manual position
Expiry date = Trade will be close after exceeded this amount of time.
Color marking = Trade will be indicate on the chart. Choose a color to represent your buy/sell position, inorder to recognize them in the future.
Original code came with 0 stop loss, so we added some code for this system to recognize a new stop loss we added earlier at the external panel.