Help with HLHB system code

Hi guys,

I am new to coding and have tried following robopip’s (unable to insert links) guide to a very basic EA of the HLHB system.

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, sorry couldnt upload the source file

//±--------------------------------------------------------------------------------------------------+
//| HLHB Trend-Catcher.mq4 |
//| Copyright 2016, Boh113 |
//| (unable to attach links|
//±--------------------------------------------------------------------------------------------------+
#property copyright “Copyright 2016, Boh113”
#property link “unable to attach links”
#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-(StopLossPoint),Ask+(TakeProfitPoint),Blue);
}
return(0);
}
//±--------------------------------------------------------------------------------------------------+
int Sell_Function()
{
{
OrderSend(Symbol(),OP_SELL,LotSize,Bid,5,Bid+(StopLossPoint),Bid-(TakeProfitPoint),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-(StopLossPoint)))
{
bool res=OrderModify(OrderTicket(),OrderOpenPrice(),(Bid-(StopLoss
Point)),OrderTakeProfit(),0,Blue);
}
}
}
else if (OrderType() == OP_SELL)
{
if (OrderOpenPrice()>Ask)
{
if (OrderStopLoss()>(Ask+(StopLossPoint)))
{
bool res=OrderModify(OrderTicket(),OrderOpenPrice(),(Ask+(StopLoss
Point)),OrderTakeProfit(),0,Red);
}
}
}
}
return(0);
}
//±--------------------------------------------------------------------------------------------------+

1 Like

hi,
I changed the open conditions to use a “Signal”. gives a bit more flexibility from just using an open function.
Added a close function for the trades too. So you could now add another exit signal.

I think i fixed the trailing stop. Great you gave that a go! Its a bit of a tricky code.

hope it gives you some ideas…

//+---------------------------------------------------------------------------------------------------+
//| HLHB Trend-Catcher.mq4 |
//| Copyright 2016, Boh113 |
//| (unable to attach links|
//+---------------------------------------------------------------------------------------------------+
#property copyright "Copyright 2016, Boh113"
#property link "unable to attach links"
#property version "2.01"
#property strict
//+---------------------------------------------------------------------------------------------------+
extern int magic=12345;
input int TakeProfit=2000;
input int StopLoss=500;
input int FastMA=5;
input int FastMaMethod=1; //0-3
input int FastMaPrice=0;  //0-6
input int FastMaShift=1;
input int SlowMA=10;
input int SlowMaMethod=1; //0-3
input int SlowMaPrice=0;  //0-6
input int SlowMaShift=1;
input int RSI=10;
input int PreviousRSIShift=1; 
input int CurrentRSIShift=1;
input double LotSize=0.02;
input bool TrailingStop=true;
extern double trailingstart=10;
extern double trailingstep=10;
extern double trailingstop=10;
extern bool reversesignal=false;
//+---------------------------------------------------------------------------------------------------+
int i;
double dg=Digits;
int signal;
int start()
{
int ord;

double PreviousSlowMA=iMA(NULL,0,SlowMA,0,SlowMaMethod,SlowMaPrice,2);
double CurrentSlowMA=iMA(NULL,0,SlowMA,0,SlowMaMethod,SlowMaPrice,1);
double PreviousFastMA=iMA(NULL,0,FastMA,0,FastMaMethod,FastMaPrice,2);
double CurrentFastMA=iMA(NULL,0,FastMA,0,FastMaMethod,FastMaPrice,1);
double PreviousRSI=iRSI(NULL,0,RSI,PRICE_MEDIAN,PreviousRSIShift);
double CurrentRSI=iRSI(NULL,0,RSI,PRICE_MEDIAN,CurrentRSIShift);

//TSL();
if(TrailingStop)movetrailingstop(trailingstart,trailingstop);

for(i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderSymbol()==Symbol())ord++;
}
if(ord>0)return(0);

signal=0;

if(PreviousFastMA<PreviousSlowMA&&CurrentFastMA>CurrentSlowMA)
{
if(PreviousRSI<50.00&&CurrentRSI>50.00)
signal=1;if(reversesignal)signal=2;

}

if(PreviousFastMA>PreviousSlowMA&&CurrentFastMA<CurrentSlowMA)
{
if(PreviousRSI>50.00&&CurrentRSI<50.00)
signal=2;if(reversesignal)signal=1;

}

if (signal==1){
   Buy_Function();
   close(OP_SELL,"",Symbol());
   }
   
if (signal==2){
   Sell_Function();
   close(OP_BUY,"",Symbol());   
   }
return(0);
}
//+---------------------------------------------------------------------------------------------------+
int Buy_Function()
{
{
OrderSend(Symbol(),OP_BUY,LotSize,Ask,5,Ask-(StopLoss*Point),Ask+(TakeProfit*Point),"",magic,0,Blue);
}
return(0);
}
//+---------------------------------------------------------------------------------------------------+
int Sell_Function()
{
{
OrderSend(Symbol(),OP_SELL,LotSize,Bid,5,Bid+(StopLoss*Point),Bid-(TakeProfit*Point),"",magic,0,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()+trailingstart)*Point<Bid)
//{
//if (OrderStopLoss()<((Bid-StopLoss-trailingstart)*Point))
//{
//bool res=OrderModify(OrderTicket(),OrderOpenPrice(),(Bid-(StopLoss*Point)),OrderTakeProfit(),0,Blue);
//}
//}
//}
//else if (OrderType() == OP_SELL)
//{
//if ((OrderOpenPrice()-trailingstart)*Point>Ask)
//{
//if (OrderStopLoss()>(Ask+(StopLoss+trailingstart)*Point))
//{
//bool res=OrderModify(OrderTicket(),OrderOpenPrice(),(Ask+(StopLoss*Point)),OrderTakeProfit(),0,Red);
//}
//}
//}
//}
//return(0);
//}
//+---------------------------------------------------------------------------------------------------+

//close(OP_BUY,"",Symbol());
void close(int type,string comment,string symbol){
   if(OrdersTotal()>0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==symbol && OrderMagicNumber()==magic && OrderComment()==comment  ){
            if(type==OP_BUY && OrderType()==OP_BUY){
               RefreshRates();OrderClose(OrderTicket(),OrderLots(),nd(Bid,dg),0);
            }
            if(type==OP_SELL && OrderType()==OP_SELL){
               RefreshRates();OrderClose(OrderTicket(),OrderLots(),nd(Ask,dg),0);
            }
         }
      }
   }
}


//nd(val,dg);
double nd(double value,double digits){
   double val;
   val=NormalizeDouble(value,digits);
   return(val);
}

void movetrailingstop(double trailingstart,double trailingstop){
   RefreshRates();
   if(OrdersTotal()>0){
      for(i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==magic){
               if(OrderType()==OP_BUY){
               Comment("1st ",nd(Ask,dg), "   Orderopenprice ",nd(OrderOpenPrice()+trailingstart*Point,dg));
                  if(nd(Ask,dg)>nd(OrderOpenPrice()+trailingstart*Point,dg) && nd(OrderStopLoss(),dg)<nd(Bid-(trailingstop+trailingstep)*Point,dg)){
                     
                     OrderModify(OrderTicket(),OrderOpenPrice(),nd(Bid-trailingstop*Point,dg),OrderTakeProfit(),0,Blue);
                     
                   }
               }
               else{
                  if(nd(Bid,dg)<nd(OrderOpenPrice()-trailingstart*Point,dg) && (nd(OrderStopLoss(),dg)>(nd(Ask+(trailingstop+trailingstep)*Point,dg))) || (OrderStopLoss()==0)){                 
                     OrderModify(OrderTicket(),OrderOpenPrice(),nd(Ask+trailingstop*Point,dg),OrderTakeProfit(),0,Red);
                   }
               }
            }
        }
   }
}         


Just from a quick optimisation…

Hi Comstrader,

Thanks for the reply, will give it all a go when I get home.

Will give you a shout and let you know how I go.

Cheers

1 Like

Any updates on this? Were you able to test? How were the results?

1 Like

i had run a test using this code and seems to be just fine some slight errors but nothing difficult to correct, i ran optimization and yielded pretty nice results.

thanks for sharing this

Do you mind sharing the EA file with any changes you made? Still battling to understand the variables and whatnot.