Download MetaTrader 5
To add comments, please log in or register
Did you like the article? Share it with your friends!
boh113
27
boh113 2016.10.18 05:27 

//+---------------------------------------------------------------------------------------------------+
//|                                                                            HLHB Trend-Catcher.mq4 |
//|                                                                            Copyright 2016, Boh113 |
//|                  http://www.babypips.com/blogs/loonie-adventures/forex-hlhb-system-explained.html |
//+---------------------------------------------------------------------------------------------------+
#property copyright "Copyright 2016, Boh113"
#property link      "http://www.babypips.com/blogs/loonie-adventures/forex-hlhb-system-explained.html"
#property version   "2.01"
#property strict
//+---------------------------------------------------------------------------------------------------+
input int      TakeProfit=2000;
input int      StopLoss=500;
input int      FastMA=5;
input int      SlowMA=10;
input int      RSI=10;
input double   LotSize=0.02;
input bool     TrailingStop=true;
//+---------------------------------------------------------------------------------------------------+
int start()
{
int ord,i;
     
double PreviousSlowMA=iMA(NULL,0,SlowMA,0,MODE_EMA,PRICE_CLOSE,2);
double CurrentSlowMA=iMA(NULL,0,SlowMA,0,MODE_EMA,PRICE_CLOSE,1);
double PreviousFastMA=iMA(NULL,0,FastMA,0,MODE_EMA,PRICE_CLOSE,2); 
double CurrentFastMA=iMA(NULL,0,FastMA,0,MODE_EMA,PRICE_CLOSE,1);
double PreviousRSI=iRSI(NULL,0,RSI,PRICE_MEDIAN,2);
double CurrentRSI=iRSI(NULL,0,RSI,PRICE_MEDIAN,1);

TSL();
   
for(i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderSymbol()==Symbol())ord++;
}
if(ord>0)return(0);
              
if(PreviousFastMA<PreviousSlowMA&&CurrentFastMA>CurrentSlowMA)
{
if(PreviousRSI<50.00&&CurrentRSI>50.00)
Buy_Function();
}

if(PreviousFastMA>PreviousSlowMA&&CurrentFastMA<CurrentSlowMA)
{
if(PreviousRSI>50.00&&CurrentRSI<50.00)
Sell_Function();
}
return(0);
}
//+---------------------------------------------------------------------------------------------------+
int Buy_Function()
{
{
OrderSend(Symbol(),OP_BUY,LotSize,Ask,5,Ask-(StopLoss*Point),Ask+(TakeProfit*Point),Blue);
}
return(0);
}
//+---------------------------------------------------------------------------------------------------+
int Sell_Function()
{
{
OrderSend(Symbol(),OP_SELL,LotSize,Bid,5,Bid+(StopLoss*Point),Bid-(TakeProfit*Point),Red);
}
return(0);
}
//+---------------------------------------------------------------------------------------------------+
int TSL()
{ 
for(int i=0;i<OrdersTotal();i++)

if(TrailingStop==True)
{
OrderSelect(i,SELECT_BY_POS);
if (OrderType()==OP_BUY)
{
if (OrderOpenPrice()<Bid)
{
if (OrderStopLoss()<(Bid-(StopLoss*Point)))
{
bool res=OrderModify(OrderTicket(),OrderOpenPrice(),(Bid-(StopLoss*Point)),OrderTakeProfit(),0,Blue);
}
}
}
else if (OrderType() == OP_SELL)
{
if (OrderOpenPrice()>Ask)
{
if (OrderStopLoss()>(Ask+(StopLoss*Point)))
{
bool res=OrderModify(OrderTicket(),OrderOpenPrice(),(Ask+(StopLoss*Point)),OrderTakeProfit(),0,Red);
}
}
}
}
return(0);
}
//+---------------------------------------------------------------------------------------------------+
Hi guys,

I am new to coding and have tried following robopip's guide to a very basic EA of the HLHB system http://www.babypips.com/blogs/art-of-automation/forex-ea-20150529.html

Now I think I have corrected the multiple entry issue and have managed to add a trailing stop loss of sorts. 

I could use advise/help on ways to clean up the code or make it more efficient, as well as being able to add an exit condition of when the EMA crosses back.

Please find attached my code so far.

 

**Edited as asked** 

whroeder1
13656
whroeder1 2016.10.18 12:43  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Please use the link button (control-K.) Use the link button See the difference: http://www.babypips.com/blogs/art-of-automation/forex-ea-20150529.html.
  3. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  4. Ask+(StopLoss*Point)
    You don't adjust for 4/5 digit brokers. (SL/TP and slippage)
    #define POINT int ///< `CHANGE / _Point`.
    #define PIP double ///< `POINT / PipsPerPOINT`.
    CHANGE points_to_change(POINT n){ return n * _Point; }
    POINT change_to_points(CHANGE c){ return POINT(c / _Point + 0.5); }
    CHANGE pips_to_change(PIP n){ return points_to_change(pips_to_points(n));}
    PIP change_to_pips(CHANGE c){ return points_to_pips(change_to_points(c));}
    // Digits DE30=1/JPY=3/EURUSD=5 10 pt/PIP. forum.mql4.com/43064#515262
    // tick 0.5 tick = point = 0.001
    POINT pips_to_points(PIP n){ if( (_Digits&1) == 1) n *= 10.0;
    return POINT(n); }

boh113
27
boh113 2016.10.19 08:48  
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Please use the link button (control-K.)See the difference: http://www.babypips.com/blogs/art-of-automation/forex-ea-20150529.html.
  3. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  4. You don't adjust for 4/5 digit brokers. (SL/TP and slippage)

Hi WHRoeder,

 

Thanks for replying, will have a look at the return code and error articles when I get home. Will also try and implement the 4/5 digit adjustment.

 

Will reply back once I've had a chance to go through it all.

 

Cheers for the help 

/
To add comments, please log in or register