InvestorsHub Logo
Post# of 102
Next 10
Followers 38
Posts 9736
Boards Moderated 2
Alias Born 06/03/2007

Re: None

Tuesday, 05/12/2009 3:08:30 AM

Tuesday, May 12, 2009 3:08:30 AM

Post# of 102
OK gang, I have coded probably 5 EAs since starting with MT4 and I keep thinking about HA candles, specifically HA candles with no tails since on an HA chart these signify a strong trend. My idea is to find a candle with no tail and a long body. Here is my code so far, but it doesn't work like I expected it would, some timeframes work great, some pairs work great, but not all. In theory, it should be a perfect trend follower, trading the HA trend similar to a Renko chart...here is the code so far.

I think there is definitely merit to this, but gotta step through the logic first...

//+------------------------------------------------------------------+
//| HA_EA.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""

extern double SL=40;//Stop Loss
extern double TP=200;//Take Profit
extern double lots=0.10;
extern int slippage=3;
extern int magicnumber=913;
double scalp=0.75;
double entry;
bool up=False;
bool down=False;


//***************** START **********************
int start()
{
int D=(Digits);//# of digits after decimal of current pair in the window

double SNSL=NormalizeDouble(Bid+SL*Point,D);//Short position stop loss
double SNTP=NormalizeDouble(Bid-TP*Point,D);//Short position take profit
double LNSL=NormalizeDouble(Ask-SL*Point,D);//Long position stop loss
double LNTP=NormalizeDouble(Ask+TP*Point,D);//Long position take profit
int OT=OrdersTotal();

//first determine previous HA candle
double haC=(Open[1]+High[1]+Low[1]+Close[1])/4;
double haO=(Open[1]+Close[1])/2;
double haH=MathMax(High[1],MathMax(haO,haC));
double haL=MathMin(Low[1],MathMin(haO,haC));

//find a candle where there is no upper/lower tail and a body longer than 30
//percent of the candle

if(haL>=haO && MathAbs(haC-haO)>(0.3*MathAbs(haH-haL))) up=True; else up=False;
if(haH<=haO && MathAbs(haC-haO)>(0.3*MathAbs(haH-haL))) down=True; else down=False;
//if(OT==1) Print("CSum = ",CSum, " | PCSum = ",PCSum," || Orders Open = ",OT," Entry Price - ",entry, " Target Price - ",entry+scalp);


if(OT<1) //Are any orders open already?
{
//SHORT ENTRY
if(down==True && IsTradeAllowed()==True)
{
int shortticket=OrderSend(Symbol(),OP_SELL,lots,Bid,slippage,SNSL,SNTP,"Short Order Being Placed ",magicnumber,0,Red);
if(shortticket<0)
{
Print("Short Order Send failed - error #",GetLastError());
}
if(shortticket>0)
{
if(OrderSelect(shortticket,SELECT_BY_TICKET,MODE_TRADES))
entry=OrderOpenPrice();
Print("SELL order opened : ",OrderOpenPrice(),"|| Target Price : ",entry-scalp);
}
return(0);
}

//LONG ENTRY
if(up==True && IsTradeAllowed()==True)
{
int longticket=OrderSend(Symbol(),OP_BUY,lots,Ask,slippage,LNSL,LNTP,"Long Order Being Placed ",magicnumber,0,Green);
if(longticket<0)
{
Print("Long Order Send failed - error #",GetLastError());
}
if(longticket>0)
{
if(OrderSelect(longticket,SELECT_BY_TICKET,MODE_TRADES))
entry=OrderOpenPrice();
Print("BUY order opened : ",OrderOpenPrice(),"|| Target Price : ",entry+scalp);
}
return(0);
}
return(0);
}
//************** Close Orders ***********************
for(int cnt=0;cnt<OT;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==magicnumber)
{
if(OrderType()==OP_BUY) // long position is open
{
if(haL<haO)
{
OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Blue);
return(0);
}
}
if(OrderType()==OP_SELL) // short position is open
{
if(haH>haO)
{
OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Blue);
return(0);
}
}
}
}
return(0);
}



//+-----------------------------------------

"remember the mayonaise jar...keep cool but don't freeze"

Join InvestorsHub

Join the InvestorsHub Community

Register for free to join our community of investors and share your ideas. You will also get access to streaming quotes, interactive charts, trades, portfolio, live options flow and more tools.