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-(StopLossPoint)),OrderTakeProfit(),0,Blue);
}
}
}
else if (OrderType() == OP_SELL)
{
if (OrderOpenPrice()>Ask)
{
if (OrderStopLoss()>(Ask+(StopLossPoint)))
{
bool res=OrderModify(OrderTicket(),OrderOpenPrice(),(Ask+(StopLossPoint)),OrderTakeProfit(),0,Red);
}
}
}
}
return(0);
}
//±--------------------------------------------------------------------------------------------------+