Hello,
I am trying to learn Mql4. Since i have no practice in any other programming languages learning mql4 is not so easy. I am practicing some basic stuff and simple codes for now and i have one problem.
I have created simple code with simple buy and sell criteria. When fast EMA crosses slow EMA program must send buy or sell order.
It does that and it ok but whet it gets to TP or SL and orders is closed program opens another order after that.
I want the program work this way:
after opening and closing Buy order program must wait until it meets sell criteria and then open sell. After clossing sell visa versa.
Maby it could be done with "For" loop and OrderSelect() or with bool variables setting on order opening but since my knowledge is poor yet i need some advices.
Any help would be weary thankfull
Thanks in advance!
The code example:
//+------------------------------------------------------------------+
//| Bandymas.mq4 |
//| Copyright 2011, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
extern double Lots = 0.1;
extern double StopLoss = 12;
extern double TakeProofit = 15;
// Trend type (Bull or Bear)
//------------------------------------------------------------------
string Trendas ()
{
double MAtrend;
double MAsignal;
string TrendType;
MAtrend = iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,0);
MAsignal = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);
if (MAsignal>MAtrend)
{ TrendType = "Bull"; return (TrendType);}
if (MAsignal<MAtrend)
{ TrendType = "Bear"; return (TrendType);}
}
string CheckBuy()
{
string Trend = Trendas();
if (Trend=="Bull") return ("Bull");
}
string CheckSell()
{
string Trend = Trendas();
if (Trend=="Bear") return ("Bear");
}
//-----------------------------------------------------------------------
int start()
{
bool AllowTrading;
int Total = OrdersTotal();
if (Total<1)
{
AllowTrading=true;
}
else
{
AllowTrading=false;
}
if (CheckBuy()=="Bull" && AllowTrading==true)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProofit*Point,0);
}
if (CheckSell()=="Bear" && AllowTrading==true)
{
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProofit*Point,0);
}
return(0);
}
I am trying to learn Mql4. Since i have no practice in any other programming languages learning mql4 is not so easy. I am practicing some basic stuff and simple codes for now and i have one problem.
I have created simple code with simple buy and sell criteria. When fast EMA crosses slow EMA program must send buy or sell order.
It does that and it ok but whet it gets to TP or SL and orders is closed program opens another order after that.
I want the program work this way:
after opening and closing Buy order program must wait until it meets sell criteria and then open sell. After clossing sell visa versa.
Maby it could be done with "For" loop and OrderSelect() or with bool variables setting on order opening but since my knowledge is poor yet i need some advices.
Any help would be weary thankfull
Thanks in advance!
The code example:
//+------------------------------------------------------------------+
//| Bandymas.mq4 |
//| Copyright 2011, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
extern double Lots = 0.1;
extern double StopLoss = 12;
extern double TakeProofit = 15;
// Trend type (Bull or Bear)
//------------------------------------------------------------------
string Trendas ()
{
double MAtrend;
double MAsignal;
string TrendType;
MAtrend = iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,0);
MAsignal = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);
if (MAsignal>MAtrend)
{ TrendType = "Bull"; return (TrendType);}
if (MAsignal<MAtrend)
{ TrendType = "Bear"; return (TrendType);}
}
string CheckBuy()
{
string Trend = Trendas();
if (Trend=="Bull") return ("Bull");
}
string CheckSell()
{
string Trend = Trendas();
if (Trend=="Bear") return ("Bear");
}
//-----------------------------------------------------------------------
int start()
{
bool AllowTrading;
int Total = OrdersTotal();
if (Total<1)
{
AllowTrading=true;
}
else
{
AllowTrading=false;
}
if (CheckBuy()=="Bull" && AllowTrading==true)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProofit*Point,0);
}
if (CheckSell()=="Bear" && AllowTrading==true)
{
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProofit*Point,0);
}
return(0);
}
"Even a million miles journey starts with one single step"