Candlestick Expert Advisor

This is my best Expert Advisor so far… It does a wonderful job in 2008, especially if ‘Pyramiding’ is set to be true and ‘Use Filter’ is set to be false. However, it’s performance before 2005 is terrible.. so I’m currently trying to improve its consistency….

//+——————————————————————+
//|                                          candles percent lot.mq4 |
//|                                                       guchialeng |
//|                                         guchialeng.wordpress.com |
//+——————————————————————+
#property copyright “guchialeng”
#property link      “guchialeng.wordpress.com”
//—- input parameters
extern double TakeProfit = 1000;
extern double StopLoss = 100;
extern double TrailingStop = 80;
extern double TradeSizePercent = 15;
extern double TurnSourLimit = 50;
extern string  Order_Comment     = “CANDLESTICKS”;
extern bool UseFilter = true;
extern bool Pyramiding = true;

static double initialOrderBuyPrice = 0;
static double initialOrderSellPrice = 0;

//+——————————————————————+
//| expert initialization function                                   |
//+——————————————————————+
int init()
{
//—-

//—-
return(0);
}
//+——————————————————————+
//| expert deinitialization function                                 |
//+——————————————————————+
int deinit()
{
//—-

//—-
return(0);
}
//+——————————————————————+
//| expert start function                                            |
//+——————————————————————+
datetime Time0 = 0;

int start()
{
//—-
int cnt, ticket, total;
datetime orderclosetime;
string   rightnow;
int      rightnow2;
int      historyTotal;
int      difference;
double   PBullStop;
double   PBearStop;
bool     BullCondition;
bool     BearCondition;

if (Time0 != Time[0])
{

int n, i;
double zag, zig;
i=0;
while(n<2)
{
if(zig>0) zag=zig;
zig=iCustom(NULL, 0, “ZigZag”, 0, i);
if(zig>0) n+=1;
i++;

//Print(“Zig = “, zig, ” Zag = “, zag);
}

//Bullish Engulfing

if (UseFilter)
BullCondition = ((BullishEngulfingExists()  && zig – Close[1]>= 25*Point) || (BullishHaramiExists()  && zig – Close[1]>= 50*Point)) && Close[1] > iMA(NULL,0,200,0,MODE_SMA,PRICE_CLOSE,1);
else
BullCondition = ((BullishEngulfingExists() && zig – Close[1]>= 25*Point)  || (BullishHaramiExists()  && zig – Close[1]>= 50*Point));

if (BullCondition)
{
if (zag < zig )
{

if (DojiAtBottomExists())
Order_Comment     = “DojiAtBottom”;
else if (BullishEngulfingExists())
Order_Comment     = “BullishEngulfing”;
else
Order_Comment     = “BullishHarami”;

ticket = OrderSend(Symbol(), OP_BUY, LotsOptimized(), Ask, 3, Ask – StopLoss*Point, Ask + TakeProfit*Point, Order_Comment, 12345, 0, Green);
Print(“Number of Open Orders = “, OrdersTotal());
OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);
initialOrderBuyPrice = OrderOpenPrice();
}
}

if (UseFilter)
BearCondition = ((SpinningTopExists() && High[1] == zag) || LongUpCandleExists()) && Close[1] < iMA(NULL,0,200,0,MODE_SMA,PRICE_CLOSE,1);
else
BearCondition = ((SpinningTopExists() && High[1] == zag) || LongUpCandleExists());

//if((LongDownCandleExists() && High[1] == zag)|| LongUpCandleExists())
//if (DojiAtTopExists() || BearishEngulfingExists())
if(BearCondition)
//if(LongUpCandleExists())
{

if (zag > zig && zag – zig >= 25*Point)
{

if (SpinningTopExists())
Order_Comment     = “Spinning Top”;
else
Order_Comment     = “Long Up Candle”;

ticket = OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, 3, Bid + StopLoss*Point, Bid – TakeProfit*Point, Order_Comment, 12345, 0, Red);
OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);
initialOrderSellPrice = OrderOpenPrice();
Print(“Initial Order Sell Price = “, initialOrderSellPrice);
}
}

Time0 = Time[0];
}
//—-
total  = OrdersTotal();
/*historyTotal=OrdersHistoryTotal();

for(cnt=0;cnt<historyTotal;cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY)==true)
{
if (OrderSymbol()==Symbol())       // hdb – only symbol and magic
{
orderclosetime=OrderCloseTime();
rightnow=Year()+”-“+Month()+”-“+Day()+” “+Hour()+”:”+Minute()+”:”+Seconds();
rightnow2=StrToTime(rightnow);
difference=rightnow2-orderclosetime;
if(difference < 5*60 && OrderProfit() >=100*Point && OrdersTotal()==0)
if (OrderType() == OP_SELL)

ticket = OrderSend(Symbol(), OP_BUY, LotsOptimized(), Ask, 3, Ask – StopLoss*Point, Ask + TakeProfit*Point, “REVERSE CLOSED SELL ORDERS”, 12345, 0, Green);
else
ticket = OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, 3, Bid + StopLoss*Point, Bid – TakeProfit*Point, “REVERSE CLOSED BUY ORDERS”, 12345, 0, Red);

}
}
}*/

for(cnt = 0; cnt < total; cnt++)
{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())
{

//Pyramiding and Trailing Stop for Long Positions
if(OrderType() == OP_BUY)
{

if (Pyramiding)
{
PBullStop = MathMax(Ask – StopLoss*Point, initialOrderBuyPrice);
if((Bid – OrderOpenPrice() > Point*25 && Bid – OrderOpenPrice() < Point*30 && OrdersTotal() == 1)|| (Bid – OrderOpenPrice() > Point*50 && Bid – OrderOpenPrice() < Point*55 && OrdersTotal() == 2)|| (Bid – OrderOpenPrice() > Point*75 && Bid – OrderOpenPrice() < Point*80 && OrdersTotal() == 3)|| (Bid – OrderOpenPrice() > Point*100 && Bid – OrderOpenPrice() < Point*105 && OrdersTotal() == 4))
//if((Bid – OrderOpenPrice() >= Point*25 && Bid – OrderOpenPrice() < Point*26 && OrdersTotal() == 1)|| (Bid – OrderOpenPrice() >= Point*50 && Bid – OrderOpenPrice() < Point*51 && OrdersTotal() == 2)|| (Bid – OrderOpenPrice() >= Point*75 && Bid – OrderOpenPrice() < Point*76 && OrdersTotal() == 3)|| (Bid – OrderOpenPrice() >= Point*100 && Bid – OrderOpenPrice() < Point*101 && OrdersTotal() == 4))
//if((Bid – OrderOpenPrice() >= Point*25 && Bid – OrderOpenPrice() < Point*26 && OrdersTotal() == 1)|| (Bid – OrderOpenPrice() >= Point*50 && Bid – OrderOpenPrice() < Point*51 && OrdersTotal() == 2))
{
ticket = OrderSend(Symbol(), OP_BUY, LotsOptimized(), Ask, 3, PBullStop, Ask + TakeProfit*Point, “Long Pyramid”, 12345, 0, Green);

}
}

// check for trailing stop
if(TrailingStop > 0)
{

if(Bid – OrderOpenPrice() > Point*TurnSourLimit && Bid – OrderOpenPrice() < Point*TrailingStop)
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, Green);

if(Bid – OrderOpenPrice() > Point*TrailingStop)
{
if(OrderStopLoss() < Bid – Point*TrailingStop)
{

OrderModify(OrderTicket(), OrderOpenPrice(), Bid – Point*TrailingStop, OrderTakeProfit(), 0, Green);
return(0);
}
}
}
}//End of Pyramiding and Trailing Stop for Long

if(OrderType() == OP_SELL)
{

if (Pyramiding)
{
PBearStop = MathMin(Bid + StopLoss*Point, initialOrderSellPrice);

//Pyramiding and Trailing Stop for Short Positions
if((OrderOpenPrice()-Ask > Point*25 && OrderOpenPrice()-Ask < Point*30 && OrdersTotal() == 1)|| (OrderOpenPrice()-Ask > Point*50 && OrderOpenPrice()-Ask < Point*55 && OrdersTotal() == 2)|| (OrderOpenPrice()-Ask > Point*75 && OrderOpenPrice()-Ask < Point*80 && OrdersTotal() == 3)|| (OrderOpenPrice()-Ask > Point*100 && OrderOpenPrice()-Ask < Point*105 && OrdersTotal() == 4))
//if((OrderOpenPrice()-Ask >= Point*25 && OrderOpenPrice()-Ask < Point*26 && OrdersTotal() == 1)|| (OrderOpenPrice()-Ask >= Point*50 && OrderOpenPrice()-Ask < Point*51 && OrdersTotal() == 2)|| (OrderOpenPrice()-Ask >= Point*75 && OrderOpenPrice()-Ask < Point*76 && OrdersTotal() == 3)|| (OrderOpenPrice()-Ask >= Point*100 && OrderOpenPrice()-Ask < Point*101 && OrdersTotal() == 4))
//if((OrderOpenPrice()-Ask >= Point*25 && OrderOpenPrice()-Ask < Point*26 && OrdersTotal() == 1)|| (OrderOpenPrice()-Ask >= Point*50 && OrderOpenPrice()-Ask < Point*51 && OrdersTotal() == 2))
{
Print(“Pyramiding here with PStop = “, PBearStop, ” and Initial Order Sell Price = “, initialOrderSellPrice);
OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, 3, PBearStop, Bid – TakeProfit*Point, “PYRAMIDING SELL”, 12345, 0, Red);
}
}

//check for trailing stop
if(TrailingStop > 0)
{
if((OrderOpenPrice() – Ask) > (Point*TrailingStop))
{
if(OrderStopLoss() > (Ask + Point*TrailingStop))
{
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point*TrailingStop, OrderTakeProfit(), 0, Red);
return(0);

}
}
}
}
}
}
//—-
return(0);
}
bool BullishEngulfingExists()
{
if (Open[1] <= Close[2] && Close[1] >= Open[2] && Open[2] – Close[2] >= 10*Point && Close[1] – Open[1] >= 10*Point)
return (true);
return (false);
}

bool BullishHaramiExists()
{
if (Close[2] < Open[2] && Open[1] < Close[1] && Open[2] – Close[2] > iATR(NULL, 0, 14, 2) && Open[2] – Close[2] > 4*(Close[1] – Open[1]))
return (true);
return (false);
}

bool DojiAtBottomExists()
{
if (Open[3] – Close[3] >= 8*Point && MathAbs(Close[2] – Open[2]) <= 1*Point && Close[1] – Open[1] >= 8*Point)
return (true);
return (false);
}

bool DojiAtTopExists()
{

if (Close[3] – Open[3] >= 8*Point && MathAbs(Close[2] – Open[2]) <= 1*Point && Open[1] – Close[1] >= 8*Point)
return (true);
return (false);
}

bool BearishHaramiExists()
{
if (Close[2] > Close[1] && Open[2] < Open[1] && Close[2] > Open[2] && Open[1] > Close[1] && Close[2] – Open[2] > iATR(NULL, 0, 14, 2) && Close[2] – Open[2] > 4*(Open[1] – Close[1]))
return (true);
return (false);
}

bool LongUpCandleExists()
{
if (Open[2] < Close[2] && High[2] – Low[2] >= 40*Point && High[2] – Low[2] > 2.5*iATR(NULL, 0, 14, 2) && Close[1] < Open[1] && Open[1] – Close[1] > 10*Point)
return (true);
return (false);
}

bool LongDownCandleExists()
{
if (Open[1] > Close[1] && High[1] – Low[1] >= 40*Point && High[1] – Low[1] > 2.5*iATR(NULL, 0, 14, 1))
return (true);
return (false);
}

bool BearishEngulfingExists()
{
if (Open[1] >= Close[2] && Close[1] <= Open[2] && Open[2] – Close[2] >= 10*Point && Close[1] – Open[1] >= 10*Point)
return (true);
return (false);

}

bool SpinningTopExists()
{
if (High[1] – Low[1] > 1.5*iATR(NULL, 0, 14, 1))
Print(“ST Condition 1 Met”);

if (MathAbs(Open[1] – Close[1])*5 < High[1] – Low[1])
Print(“ST Condition 2 Met”);

if (High[1] – Low[1] > 1.5*iATR(NULL, 0, 14, 1) && (High[1] – Low[1] > 30*Point) && MathAbs(Open[1] – Close[1])*5 < High[1] – Low[1])
return (true);
return (false);
}
double LotsOptimized()
{
double lot;
//int magnitude = 0;
int Capital = 3000;
//—- select lot size

if (AccountBalance() < Capital)
lot = 0.1;

/*else
{
if (AccountBalance() >= Capital && AccountBalance() < 1.3*Capital)
magnitude = 1;
else if (AccountBalance() >= 1.3*Capital && AccountBalance() < 1.5*Capital)
magnitude = 2;
else if (AccountBalance() >= 1.5*Capital)
magnitude = 3;

lot=NormalizeDouble(MathFloor(AccountFreeMargin()*TradeSizePercent/10000)/10,1);
if (lot < 0.1)
lot=0.1;

lot = lot*magnitude;
}*/

//—-
else
{
lot=NormalizeDouble(MathFloor(AccountFreeMargin()*TradeSizePercent/10000)/10,1);
if (lot < 0.1)
lot=0.1;
}
return(lot);
}

//+————————————————————+

Comments
  1. Joseph says:

    Hi, I am very interested in helping you improve your system. I have an idea that might just do that.

    Please contact me at Ucicelestine@yahoo.com

    The minor adaptations to your system, I have in mind will definitely make you a millionaire in 6 months.

    Joe

  2. john says:

    So how does this work?
    Also what does a metatrader do?

  3. guchialeng says:

    This is an expert advisor, similar to the source code of a computer software. You can compile the EA, and load it in Metatrader. Metatrader will then execute open and sell orders based on the instructions in this EA.
    Btw, Metatrader (MT4) is a trading platform… well, you can view charts using MT4 and manually open or close an order. Alternatively, you can automate the process using an EA. For more information, check out this site:
    http://www.metaquotes.net/

  4. gino says:

    When I compile your code I get 2 errors.
    ‘copyright’ – defined value expected C:\Program Files\FXDD – MetaTrader 4\experts\candlesticks.mq4 (6, 1)
    and

    ‘link’ – defined value expected C:\Program Files\FXDD – MetaTrader 4\experts\candlesticks.mq4 (7, 1)

  5. guchialeng says:

    I’m not sure what’s the reason for the error… It may be due to the double quotes (“)… If you copy and paste the codes, you’ll need to replace the double quotes. Try the following:

    1) In Metaeditor, enter Ctrl H
    2) In the “Find What:” field, paste the double quotes from my code
    3) In the “Replace With:” field, enter ” using your keyboard
    4) Click “Replace All”

    Then try recompiling… hope everything’s fine after that….

  6. Jason Baird says:

    i tried to compile ur EA but i got the following warnings.
    “dojiattopexists”
    “bearishharamiexists”
    “longdowncandleexists”
    “bearishengulfingexists”
    is not referenced and will be removed from exp file.

    What should i do?

  7. guchialeng says:

    Hi Jason

    That’s not a problem. It just means that I wrote those functions, but did not use them in my main program. You can proceed to use the EA on your strategy tester.

  8. Murali says:

    When I compiled it in my Meta Editor, the following error says…

    ********************************************

    Compiling ‘CandleStickEA.mq4’…
    ‘copyright’ – defined value expected C:\Program Files\SIGTrader\experts\CandleStickEA.mq4 (6, 1)
    ‘link’ – defined value expected C:\Program Files\SIGTrader\experts\CandleStickEA.mq4 (7, 1)
    2 error(s), 0 warning(s)
    ********************************************

    Can u please help me to solve this..?

    Murali

  9. guchialeng says:

    That’s weird… Did you remove/comment out/made any changes to these two lines…?

    #property copyright “guchialeng”
    #property link “guchialeng.wordpress.com”

    If so, please add them back in…

  10. Murali says:

    No I have not at all made/modify any single letter. I just copied it to my Metaeditor and compiled and got these errors…

    But…

    I got the same Mq4 file at Forex-tsd forum and used it. But it made a Big Loss around 600$..!
    So I removed it .

    Why I dont know.. I attached it to the Eur/Usd and Usd/Jpy 15M chart.

    Please guide me how to use it in a right direction..?

    Thanks for your concern.

    I believe strongly that using candle stick analysis this EA should trade very very wisely with low losses and at higher profits.. Still I believe.. you ea will work fine if u advice its fine tuning…

    Murali

    • guchialeng says:

      I copied and compiled the code… It has to do with the double quotes….

      WordPress and Meta Editor probably represent ” differently. To remedy that, in MetaEditor,
      1) Copy the ” from the mq4 file, and press Ctrl-H
      2) In the field Find What, paste the copied double-quotes in
      3) In the field Replace With, key in the double-quotes using your keyboard.
      4) Then click on “Replace All”

      A word of caution, do not trade this EA with real money… Demo-trade and do extensive back-testing and forward-testing before you use any EA… And, remember, money management is more important then entry signals…

      In addition, candlesticks work better on longer timeframe… Any candlestick formed on 15M charts can’t be trusted…

      • Mohamed says:

        Hello Sir,

        I’m not a programmer for that matter i do not know how to compile your Candlestick EA.

        Can you or someone from this forum please help me to create it for me and send to my email address: programmer822002@yahoo.com

        BTW, Astro Boy…can we discuss more on your Candlestick EA in PM.

        Hope to hear from you soon.

        Cheers

  11. Murali says:

    Now it is compiled smoothly.
    I am using it on a Daily Chart.
    Eager to see the results..
    Thanks for your immediate reply.

    Murali

  12. Loyez says:

    Please,i am new into coding,i just coded an EA but i am having 1 error saying double quotes needed,could you help me to solve this problem,i have really looked into it and it is giving me headache to locate it may be because i dont know much about coding b4,i am a medic.
    please help me to remove this error,let me have your email so that i can send you the ea.
    Thanks
    Loyez

  13. Loyez says:

    Sorry i made mistake with the email address,Please,i am new into coding,i just coded an EA but i am having 1 error saying double quotes needed,could you help me to solve this problem,i have really looked into it and it is giving me headache to locate it may be because i dont know much about coding b4,i am a medic.
    please help me to remove this error,let me have your email so that i can send you the ea.
    Thanks
    Loyez

Leave a comment