Wednesday, July 14, 2010

Your first EA

I wrote a simple EA that trades with the help of Stochastic indicator.
The program is written in MQL4 for MetaTrader software.

int order_buy=-1;
int order_sell=-1;

int init() { return(0); }
int deinit() { return(0); }

int start()
{
double stochastic=iStochastic(Symbol(), PERIOD_H1, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);

if(order_buy==-1 && stochastic<20)
{
order_buy=OrderSend(Symbol(), OP_BUY, 0.1, NormalizeDouble(Ask, 4), 10, 0, 0);
}

if(order_sell==-1 && stochastic>80)
{
order_sell=OrderSend(Symbol(), OP_SELL, 0.1, NormalizeDouble(Bid, 4), 10, 0, 0);
}

if(order_buy!=-1 && stochastic>80)
{
OrderClose(order_buy, 0.1, NormalizeDouble(Bid, 4), 10);
order_buy=-1;
}

if(order_sell!=-1 && stochastic<20)
{
OrderClose(order_sell, 0.1, NormalizeDouble(Ask, 4), 10);
order_sell=-1;
}

return(0);
}

The algorithm of the program uses overbought state of the market to sell and vice-versa.
When market state changes on the contrary the program closes its order.

First we create 2 variables that represent orders.
In the main function we use built-in function to calculate stochastic on H1 chart using standart parameters for Fast Stochastic.

There 2 functions OrderSend and OrderClose for openning and closing orders.
With simple "if" operator we check the state of the market.

NormalizeDouble is used because of some problems with history. There were some rates with 4 numbers after comma and then data changed on 5 numbers. OrderSend and OrderClose couldn't handle them so I had to truncate all rates to 4 numbers.

There are 3 main functions: init, deinit and start. In this program first two aren't used.
Function start usually executes with every new tick, but when I test my EAs I use method based on fully built bars so the function start is executed every 1 hour as I set in in Strategy Tester.

The lot was used 0.1 which means 1 pip ~ 1$ . The 5th parameter of OrderSend is called slippage. It only matters when program runs in real-time meaning that rate could be more or less than 10 pips otherwise this function fails.

This program is used only for testing algorithm in Strategy Tester for H1 period on Gbp/Usd. Do not try it on real account! The testing period was since 01.01.2000.

I'm not going to get deeper in this code. If you have any questions just post a comment here.

Now the interesting part. The results showed us profit factor 0.97. The program was setup on 10000 USD and we lost 3044 USD. Total profit was 113091 USD and total 116135 USD. Actually its not a big difference. Order count was 4156.

With all that result I can tell you that this algorithm actually could make profit! The problem lays only in the size of the spread... Spread for Gbp/Usd is 4 pips. With a simple math we can count that 4156 orders * 4 pips = 16624 pips and is approx. 16624 USD. If we add this sum to our loss 3044 we could get 13580 USD.

Unfortunatly it is impossible to trade without spreads, although some of the brokers provide this opportunity with a cut of your profit. I haven't had experience with such brokers so I don't know for sure how it works.

No comments:

Post a Comment