This is my first post in this forum. I am quite new into forex trading - about 2 months maybe.
I study computer science and so I have some skills in programming - at least, I should have
When I first read that it's possible to programm the MT4 with a C-style like syntax, I wanted to give it a try - so I started reading the MQL-API and studied those EAs that came with MT4.
...that was yesterday. Today I want to present my very first EA.
Maybe it is usefull for some of you. Any feedback is welcome - wether it's coding style or discussion of the trading system.
So here's my first EA - very simple, very basic:
//+------------------------------------------------------------------+
//| EA #1 |
//| ===== |
//| Date : 02.08.2008 |
//| Author: AdamP |
//+------------------------------------------------------------------+
#define MAGICNUMBER 01082008
extern double Lots = 1.0;
extern double TakeProfit = 80;
extern double StopLoss = 60;
//+------------------------------------------------------------------+
//| Count open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders()
{
int buys=0,sells=0;
// Go through all orders
for(int i=0;i<OrdersTotal();i++)
{
// We're only interested in trades that are in the trading pool
// Closed or canceled orders are not interesting for us
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
// Count positions that were opened by us for the current currency
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICNUMBER)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
// Return open positions
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
return(Lots);
}
//+------------------------------------------------------------------+
//| Check for bullish market conditions |
//+------------------------------------------------------------------+
bool isBullishMarket()
{
// Calculate current and previous MACD and and current signal line
double SignalCurrent=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
double MacdCurrent=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
double MacdPrevious=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
double RsiCurrent = iRSI(Symbol(),PERIOD_W1,14,PRICE_CLOSE,1);
return ((RsiCurrent > 50.0) && (MacdPrevious < MacdCurrent) && (MacdCurrent > SignalCurrent));
}
//+------------------------------------------------------------------+
//| Check for bearish market conditions |
//+------------------------------------------------------------------+
bool isBearishMarket()
{
// Calculate current and previous MACD and and current signal line
double SignalCurrent=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
double MacdCurrent=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
double MacdPrevious=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
double RsiCurrent = iRSI(Symbol(),PERIOD_W1,14,PRICE_CLOSE,1);
return ((RsiCurrent < 50.0) && (MacdPrevious > MacdCurrent) && (MacdCurrent < SignalCurrent));
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double TP, SL;
// Only trade on beginning of a new bar
if (Volume[0] > 1) return;
// Check for buy condition
if (isBullishMarket())
OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",MAGICNUMBER,0,Green);
// Check for sell condition
if (isBearishMarket())
OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",MAGICNUMBER,0,Red);
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
// Make sure we have enough bars to work with and trading is
// allowed for expert advisors
if(Bars<100 || IsTradeAllowed()==false) return;
// If we have no open position check for opening one
if(CalculateCurrentOrders()==0) CheckForOpen();
} - Optimized for EURUSD
- Using daily charts and weekly charts
- Only 1 Position opened at the same time
- Fixed TakeProfit and StopLoss
I have backtested it for the year 2008 and 2007 on the EURUSD chart.
But it's also possible to use it with other charts - in that case, you should change the TakeProfit and StopLoss variables.
The idea is quite simple. The following two indicators are used:
- MACD (12,26,9)
- RSI (14)
The following algorithm (oh, you call it trading system, right?
) is used:
- Check if we have already a position opened -> yes? quit!
- Check if we are at the beginning of a new bar (daily chart) -> no? quit!
- Check wether we are in a bullish or a bearish trend
- a bullish trend is indicated that way:
- the RSI signal of the current week's bar must be greater than 50, and
- the previous MACD daily value must be smaller than the current MACD daily value, and
- the current MACD daily value must be greater than the current MACD daily signal
- a bearish trend is indicated the opposite direction:
- RSI signal of the current week must be smaller than 50, and
- the precvious MACD daily value must be greater than the current MACD daily value, and
- the current MACD daily value must be smaller than the current MACD daily signal
- If we are in a bullish trend, buy with TakeProfit 80 and StopLoss 60, in a bearish trend sell with same TakeProfit and StopLoss
First attached picture is a backtest of the EURUSD from 1st of january 2008 until today with a starting budget of $10.000 and a lot size of 1.0, TP 80 and SL 60.
Second pictures demonstrates a buying and a selling signal. But this is not 100% correct, because the RSI shown is on a daily basis, while this EA checks the weekly values.
Edit: I am using a fixed lot size and no money or risk management at the moment.
Any comments welcome!