Greetings!
I guess I'm following the usual story here. New to MQL4, some coding background (BASIC and HTML), blah,blah,blah, now I'm trying my hands at writing my own EA.
It is nothing special. I'm working on an EMA breakout EA. As it is one of my first I thought that I'd keep it simple. So the EA works based on a bar opening below the 20 EMA and then closing above the line. The next bar it opens an order a 3 pips above the previous bar high. Since it is a basic EA the order is closed upon hitting the TP or SL point, it has no other coding for closing an order.
Here's the problem. The first version I have opens an order after every tick. Nice when you have a profitable trade, sucks when you don't. So in version 2 I tried adding in a counter code from another EA and found that now it does not open any trades.
Here's the version 1 code (sorry I don't know how to put it in a neat little box):
int start()
{
//----
int cnt, total, ticket;
double emaT = iMA(NULL,0,20,0,MODE_EMA, PRICE_CLOSE,0);
double MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
total=OrdersTotal();
if(total<1)
bool buy = false;
bool sell = false;
if((Ask==High[1] > 3*Point) && (Open[1] < emaT) && (Close[1] > emaT) && (MacdCurrent > 0)) buy = true;
if((Low[1]==Bid > 3*Point) && (emaT > Close[1]) && (Open[1] > emaT) && (MacdCurrent < 0)) sell = true;
{
if(buy)
ticket=OrderSend(Symbol(),OP_BUY,Lots, Ask,0,Ask-StopLoss*Point,Ask+TakeProfit*Point, "Momo Trade",magicnumber,0, Green);
if(ticket>0)
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
return(0);
}
{
if(sell)
ticket=OrderSend(Symbol(),OP_SELL,Lots, Bid,0,Bid+StopLoss*Point, Bid-TakeProfit*Point, "Momo Trade",magicnumber,0, Red);
if(ticket>0)
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
return(0);
}
return(0);
}
//----
Version 2 is the same, but with this code added for a counter:
orders=OrdersTotal();
for(cnt=0;cnt<OrdersTotal();cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS))
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
Also, while I have everyone's attention is my code correct for placing an order based on the previous high/low with the (Ask==High[1] > 3*Point) code?
With Thanks!
I guess I'm following the usual story here. New to MQL4, some coding background (BASIC and HTML), blah,blah,blah, now I'm trying my hands at writing my own EA.
It is nothing special. I'm working on an EMA breakout EA. As it is one of my first I thought that I'd keep it simple. So the EA works based on a bar opening below the 20 EMA and then closing above the line. The next bar it opens an order a 3 pips above the previous bar high. Since it is a basic EA the order is closed upon hitting the TP or SL point, it has no other coding for closing an order.
Here's the problem. The first version I have opens an order after every tick. Nice when you have a profitable trade, sucks when you don't. So in version 2 I tried adding in a counter code from another EA and found that now it does not open any trades.
Here's the version 1 code (sorry I don't know how to put it in a neat little box):
int start()
{
//----
int cnt, total, ticket;
double emaT = iMA(NULL,0,20,0,MODE_EMA, PRICE_CLOSE,0);
double MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
total=OrdersTotal();
if(total<1)
bool buy = false;
bool sell = false;
if((Ask==High[1] > 3*Point) && (Open[1] < emaT) && (Close[1] > emaT) && (MacdCurrent > 0)) buy = true;
if((Low[1]==Bid > 3*Point) && (emaT > Close[1]) && (Open[1] > emaT) && (MacdCurrent < 0)) sell = true;
{
if(buy)
ticket=OrderSend(Symbol(),OP_BUY,Lots, Ask,0,Ask-StopLoss*Point,Ask+TakeProfit*Point, "Momo Trade",magicnumber,0, Green);
if(ticket>0)
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
return(0);
}
{
if(sell)
ticket=OrderSend(Symbol(),OP_SELL,Lots, Bid,0,Bid+StopLoss*Point, Bid-TakeProfit*Point, "Momo Trade",magicnumber,0, Red);
if(ticket>0)
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
return(0);
}
return(0);
}
//----
Version 2 is the same, but with this code added for a counter:
orders=OrdersTotal();
for(cnt=0;cnt<OrdersTotal();cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS))
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
Also, while I have everyone's attention is my code correct for placing an order based on the previous high/low with the (Ask==High[1] > 3*Point) code?
With Thanks!