can anyone help check this code and tell me why it doesnt seem to work?

 

Hello everyone, please i need help with this code I wrote for an EA.it compiled perfectly but doesn't seem to do anything when I tried to use it to trade.

The EA is supposed to open a buy position when ema 5 is above ema 10 and RSi value is above 50 and the diference between the two emas is >= to 20.It should open a sell postion when ema 10 is aabove ema 5, Rsi value is below 50 and the differnce in ema is >= 20.it should close buy position when rsi is goes below 50 and close sell position hwen rsi goes above 50.

Overlook the stochastic part .I am not included it for now.intend using it later on when I am on the next stage. Also overlok the take profi and stop loss value i assigned.They are at that value bcos I really do not want the trade to be closed at stop loss or take profit rather at the cross of Rsi value below or above 50(depending on if it is is a buy or sell position that is to be closed").

//+------------------------------------------------------------------+
//| Balanced System.mq4 |

//+------------------------------------------------------------------+
#property copyright "MALACHY"

//+------------------------------------------------------------------+
extern double Lots=0.1; // these values can be used for both buy and sell orders
extern double TakeProfit=1000; // hence they are declared globally
//extern double StopLoss=500;


//---------------------------------------------------------------------
int start()
{




// difference between the two ema's
double Rsi_value=iRSI(NULL,0,14,0,0); // Rsi value assigned to a variable
double Stoch_value=iStochastic(NULL,0,14,3,3,1,1,1,0); //Stochastic value assigned to a variable


double Ema_5_value=iMA(NULL,0,5,0,0,Bid,0); //value of ema assigned to a variable
double Ema_10_value=iMA(NULL,0,10,0,0,Bid,0); // value of ema asigned to a variable

double Ema_diff_buy=(Ema_5_value) - (Ema_10_value); // differeince between the two Ema's for buy critria
double Ema_diff_sell=(Ema_10_value) - (Ema_5_value); // difference between the two ema's for sell criteria
int total=OrdersTotal();
int cnt;

//-----------------------------------------------------------------------------------------------

if(Ema_5_value > Ema_10_value && Rsi_value > 50 && Ema_diff_buy>=20 && total<1)


{
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,500,Ask+TakeProfit*Point,NULL,0,0,Red);
}


if(Ema_5_value < Ema_10_value && Rsi_value < 50 && Ema_diff_sell >=20 && total<1)

{
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,500,Bid-TakeProfit*Point, NULL,0,0,Green);
}

for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES);

if(OrderType()==OP_BUY && Rsi_value < 50 ) //long position exists. we also check for criteria for closing buy position




OrderClose(OrderTicket(),OrderLots(), Bid,3,Yellow);
}


{

if(OrderType()==OP_SELL && Rsi_value > 50) //does short position exist?
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position

return(0); // exit
}

//------------------------------------------------------------------------------------------------
return(0);
}
}

I would be most grateful if sosmeone can help look at this code.Thanks in advance

cheers,

Malik

 

NF

Your lines

double Ema_5_value=iMA(NULL,0,5,0,0,Bid,0); //value of ema assigned to a variable
double Ema_10_value=iMA(NULL,0,10,0,0,Bid,0); // value of ema asigned to a variable

are wrong - should be

double Ema_5_value=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0); //value of ema assigned to a variable
double Ema_10_value=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0); // value of ema asigned to a variable
as written - you were calling the SMA - see https://docs.mql4.com/constants/movings

and I dont know what Bid was doing in there!

See https://docs.mql4.com/constants/prices

IMHO you would need a greater differential in EMA 'speed' to get a half-useful signal - consider EMA 5/20, 4/20, 2/18 etc

Also wrong is the OrderSend - StopLoss should be a price not a pip-distance - see https://docs.mql4.com/trading/OrderSend

Keep trying tho :)

Good Luck

-BB-

 
BarrowBoy wrote >>

NF

Your lines

are wrong - should be

as written - you were calling the SMA - see https://docs.mql4.com/constants/movings

and I dont know what Bid was doing in there!

See https://docs.mql4.com/constants/prices

IMHO you would need a greater differential in EMA 'speed' to get a half-useful signal - consider EMA 5/20, 4/20, 2/18 etc

Also wrong is the OrderSend - StopLoss should be a price not a pip-distance - see https://docs.mql4.com/trading/OrderSend

Keep trying tho :)

Good Luck

-BB-

Thanks BB for looking at my codes.I would take your advice.