Hey there!
First, a quick word of warning - I am quite new to FX trading and programming with the MQL4 since yesterday(I got some programming background), so what I write may be very stupid:D
Yesterday I decided to learn to program on the MQL language and figured that this system is pretty simple to be my first Expert Advisor project. Here’s the code for it with some comments:
#property copyright "yasen"
#property link "hh"
double currTradeTrailing;
bool isInTrade=false; //false if there is a trade open, aimed at keeping the number of open trades to 1
extern double riskPercentage = 0.01;
int start()
{
double prevSAR;
double TrailingStopPips;
double positionSize;
prevSAR=iSAR(Symbol(), 0, 0.02, 0.2, 1); //getting previous period SAR
TrailingStopPips=MathAbs(Open[0]-prevSAR); //calculates stop-loss amount
positionSize=(AccountBalance()*riskPercentage)/TrailingStopPips/10000; //calculates position size
if (positionSize<1) { positionSize=1; } //ensuring that if the calculated position size in never under 1 (smallest lot)
if( OrderSelect(0,SELECT_BY_POS) && OrderCloseTime()==0)
{
if(currTradeTrailing>0 && Bid-OrderOpenPrice()>currTradeTrailing && OrderStopLoss()<Bid-currTradeTrailing) // whether to trail the stop-loss
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-currTradeTrailing,OrderTakeProfit(),0);
}
} else {
isInTrade=false; //if open trades = 0 ---> set false
}
//the Volume[0] declarations are avoiding volume mismatches (don't know if that's the right way to do it)
//if the open price of the new bar is below the previous SAR value and the previous bar's close price is below the previous SAR
//checking if our trailing stop is not smaller than the smallest allowed stop-loss
//executes only if isInTrade == false, i.e. when there are no other trades open
if (Volume[0]>=1 && Volume[0]<7 && Open[0]<prevSAR && Close[1]<prevSAR && MarketInfo(Symbol(),MODE_STOPLEVEL)/10000<TrailingStopPips && isInTrade==false)
{
if(iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_MAIN,0)>iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_MAIN,1) && iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_MAIN,1)>iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_MAIN,2) && iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_MAIN,2)>iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_MAIN,3) && iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_MAIN,3)>iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_MAIN,4) && iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_MAIN,4)>iStochastic(Symbol(),0,5,3,3,MODE_SMA,0,MODE_MAIN,5)) //checks if there is an upward trend in the Stochastic
{
isInTrade=true;
currTradeTrailing=TrailingStopPips;
OrderSend(Symbol(),OP_BUY,positionSize,Ask,3,Bid-TrailingStopPips,0,"comment",0,0,Green);
}
}
return;
}
I tested it on EURUSD 30M and for a period of around 1.5 months it generated a total loss of -$53 out of 31 trades(starting balance $3000, risk 1%). Go try it yourself and
I am pretty sure I’m not doing something right, I’d greatly appreciate some comments on how to improve the code, as you can see I am a total NEWBIE in this Thanks a lot!
Yasen