Download MetaTrader 5

EA is not taking trades after first trade

To add comments, please log in or register
Mimi
501
Mimi 2014.03.08 08:25 

Dear programmer,

I have searched in the forum for similar thread, i got one. I tried getting idea from it & tried different coding. But nothing work. If possible please help.

Strategy works fine in EA. Just after 1st trade its not taking any more.

What i required to add in it to make it work?

I want my trade signal getting generated with each bars or daily. Say i put EA on daily chart, so for each daily bar it will create signal & trade.

Any guidance.

Thank you

Regards.

Files:
kk_2.mq4 4 kb
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2014.03.08 08:54  
cashcube:

Dear programmer,

I have searched in the forum for similar thread, i got one. I tried getting idea from it & tried different coding. But nothing work. If possible please help.

Strategy works fine in EA. Just after 1st trade its not taking any more.

What i required to add in it to make it work?

You may want to print out S and R1 to see if the values are as you expect . . .

S1 = highest - 40;

R1 = lowest + 40;

. . . . what kind of value do you expect ?

Mimi
501
Mimi 2014.03.09 03:50  

Sorry my mistake I re uploaded again with correct code here. Forgot to add this.

S1 = highest - (40 * Point);

But after adding its still not working. Only showing for one day.

Keith Watford
Moderator
9640
Keith Watford 2014.03.09 05:22  

You have 2 Global scope variables that hold the OrderTicket numbers

int BuyTicket;
int SellTicket;

So when the first order is placed, one of them will have a value. The code that will reset them to zero is conditional that when added together they will total less than 2

int Ticket = BuyTicket + SellTicket;
double CP = Bid;
lowest = Low[iLowest(NULL,PERIOD_D1,MODE_LOW,1,1)];
highest = High[iHighest(NULL,PERIOD_D1,MODE_HIGH,1,1)];

if( (highest - CP) > (20 * Point))
{
if(Ticket < 2)             // BuyTicket or SellTicket will be more than 2 once the first trade has been sent.
{

// if(Ticket < 2) is not true none of this code will be executed

}
}
else if( (CP - lowest)> (20 * Point))
{
if(Ticket < 2 )
{

//  Same as above, not executed

}
}
return(0);
}
Mimi
501
Mimi 2014.03.09 13:10  
GumRai:

You have 2 Global scope variables that hold the OrderTicket numbers

So when the first order is placed, one of them will have a value. The code that will reset them to zero is conditional that when added together they will total less than 2


I added that condition, because at first it was giving so many orders one at time. So i realized, that every time current price stays above 20 pips from the low (according to the strategy), market placing orders constantly on R1.

So i thought, adding limit. When the condition met, only 1-2 trades will be taken for the day.

You are right, when i added Ticket < 10, it was taking more than one trades on other days too. But it, its random, say i choose date from 21st jan,2014 to 28th feb. 1st trade made on 21st jan, next are on 7th feb & then no more.

Another thing, its showing ordersend error 130. I felt, orders are placing too close to the ask price, so there may be problem with stoploss. I added a tweak here. When one ask price is lower than S1 & difference between them is 20 pips, then place sell orders.

// Sell Order
ES = MathAbs((R1 - Ask));

if(Ask > R1 && ES >(20 * Point))
{
SellTicket = OrderSend(Symbol(),OP_SELLSTOP,LotSize,R1,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Stop Order",MagicNumber,D+1,Red);
BuyTicket = 0;
}
else if(Ask < R1 && ES >(20 * Point))
{
SellTicket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,R1,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Limit Order",MagicNumber,D+1,Red);
BuyTicket = 0;
}

After adding it shows no error, But dates are still random.

I have no idea.

Thanks.

Files:
kk_3.mq4 4 kb
whroeder1
14753
whroeder1 2014.03.09 13:19  
S1 = highest - (40 * Point);
4 pips on a 5 digit broker? Adjust
Mimi
501
Mimi 2014.03.09 14:17  
WHRoeder:
4 pips on a 5 digit broker? Adjust


Testing broker is 4 digit broker. But thanks for the link.

I found solution for random dates. There was a mistake on expiration time. I corrected it.

But in delete order still required some attention. Orders are expiring on the day current as i coded according to the strategy.

But i also added another condition.

If Lowest price got changed say Bid < lowest, then those selllimit orders will get deleted. But its not deleting. I checked the tester, but its showing order deleted due expiration. Not for this particular condition. I am not sure.

lowest = Low[iLowest(NULL,PERIOD_D1,MODE_LOW,1,0)];

Is this coding is all right for calculating current day lowest low on 1Hour chart for that day?

Thank you.

Files:
kk_4.mq4 5 kb
whroeder1
14753
whroeder1 2014.03.09 16:36  
cashcube: Testing broker is 4 digit broker. But thanks for the link.
You missed the point, your code is broken on any 5 digit broker. Are you going to find every dependency and fix it after you're long done coding, and/or are you going to put it on a 5 digit broker and wonder why it's loosing.
Keith Watford
Moderator
9640
Keith Watford 2014.03.09 20:00  
cashcube:
lowest = Low[iLowest(NULL,PERIOD_D1,MODE_LOW,1,0)];

Is this coding is all right for calculating current day lowest low on 1Hour chart for that day?

Thank you.

If you strip down your line of code

What does

iLowest(NULL,PERIOD_D1,MODE_LOW,1,0)

return?

Well, it returns the shift of the lowest bar, comparing the range of bar[0] to bar[0] on the daily chart.

It's like saying, "I have 1 bags of apples, which bag has the least amount of apples?"

So we know that this function can only return 0

Therefore

lowest = Low[iLowest(NULL,PERIOD_D1,MODE_LOW,1,0)];

// is exactly the same as

lowest = Low[0];

So your line of code returns the low of the current bar on the current chart

I believe that what you are trying to achieve can be done by

iLow(NULL,PERIOD_D1,0)

which will return the current day's low (so far)

Keith Watford
Moderator
9640
Keith Watford 2014.03.09 22:30  

Please use indentation, you will probably find more people are willing to help if you make your code easier to read

I have tidied it up a bit and added comments in the code, please read them Also additional comments re highlighted

I've also changed the code to get today's high and low

#property copyright "Copyright 2014, Mimi"
#property link      ""
#property version   "1.00"

extern double LotSize = 0.1;
extern double StopLoss = 10;
extern double TakeProfit = 20;
extern int    Slippage = 5;
extern int    MagicNumber = 123;

double currentlow;
double currenthigh;
double S1;
double lowest;
double highest;
double ES;
double EB;
double R1;
int    q;
int    BuyTicket = 0;
int    SellTicket = 0;
double UsePoint;
int    UseSlippage;


// Init function
int init()
{
UsePoint = PipPoint(Symbol());
UseSlippage = GetSlippage(Symbol(),Slippage);
return(UsePoint);
return(UseSlippage);
}
//---------------------------------------------------------------------------------------------------------------
// Start function
int start()
{
datetime now        = TimeCurrent();
          int bod        = now - now % 86400; // Beginning of the day
          int tomorrow   = bod + 86400;
          int expiration = tomorrow - 1;

for( q=0;q<OrdersTotal();q++)
  {
  if (OrderSelect(q, SELECT_BY_POS, MODE_TRADES)==true && OrderSymbol()==Symbol())
    {
    // checking positions for open orders.
    if (OrderType()==OP_BUYSTOP || OrderType() == OP_BUYLIMIT || OrderType () == OP_SELLLIMIT ||OrderType()==OP_SELLSTOP)
       return(0); 
    }  
  } 
  
int Ticket = BuyTicket + SellTicket;
//double CP = Bid;                      // What is the point? Just use Bid
lowest = iLow(NULL,PERIOD_D1,0);        // Today's low 
highest = iHigh(NULL,PERIOD_D1,0);      // Today's high

if( (highest - Bid) > (20 * Point))     //if current price is more than 20 pips below today's high
   {
   if(Ticket < 100)                     //This may work in the strategy tester, it will not in real-time 
      {
      //currenthigh = highest;          //-You don't use this variable
      S1 = highest - (40 * Point);      //---Calculating Support at 40 pips below today's high
      OrderSelect(BuyTicket,SELECT_BY_TICKET);      //1
      if(OrderCloseTime() == 0 && BuyTicket > 0 && OrderType() == OP_BUYLIMIT)  //2
         {
         bool Deleted = OrderDelete(BuyTicket,Green); // You create the variable "Deleted" but only assign a 
         }                                            // value to it, you never reference it.
      if( Bid > highest)                              //How can Bid be higher than today's high ???
         {
         Deleted = OrderDelete(BuyTicket,Green);
         }

      // Calculate stop loss and take profit
      if(StopLoss > 0)   double BuyStopLoss = S1 - (StopLoss * Point);
      if(TakeProfit > 0) double BuyTakeProfit = S1 + (TakeProfit * Point);  
// ----------------------------------------Open buy order
      EB = MathAbs((S1 - Bid));

      if (Bid < S1 && EB > (20 * Point))    //if Bid is at least 20 pips below S1 ie at least 60 pips below today's high
         { 
         BuyTicket = OrderSend(Symbol(),OP_BUYSTOP,LotSize,S1,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Stop Order",MagicNumber,expiration,Green);
         SellTicket = 0;
         }
      else if(Bid > S1 && EB > (20 * Point))
         {
         BuyTicket = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,S1,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Limit Order",MagicNumber,expiration,Green);
         SellTicket = 0;
         }
      }
   }
else if( (Bid - lowest)> (20 * Point))       
   {
   if(Ticket < 100 )
      {
      currentlow = lowest;
      R1 = lowest + (40 * Point);  //---Calculating resistance
      OrderSelect(SellTicket,SELECT_BY_TICKET);
      if(OrderCloseTime() == 0 && SellTicket > 0 && OrderType() == OP_SELLLIMIT)
         {
         Deleted = OrderDelete(SellTicket,Red);
         }
      if( Bid < lowest)
         {
         Deleted = OrderDelete(SellTicket,Red);
         }

      if(StopLoss > 0)   double SellStopLoss = R1 + (StopLoss * Point);
      if(TakeProfit > 0) double SellTakeProfit = R1 -(TakeProfit * Point);

// Sell Order
      ES = MathAbs((R1 - Ask));
      if(Ask > R1 && ES >(20 * Point))
         {
         SellTicket = OrderSend(Symbol(),OP_SELLSTOP,LotSize,R1,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Stop Order",MagicNumber,expiration,Red);
         BuyTicket = 0;
         }
      else if(Ask < R1 && ES >(20 * Point))
         {
         SellTicket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,R1,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Limit Order",MagicNumber,expiration,Red);
         BuyTicket = 0;
         }
      }
   }
return(0);
}

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

// Pip Point Function
double PipPoint(string Currency)
{
int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
return(CalcPoint);
}

// Get Slippage Function
int GetSlippage(string Currency, int SlippagePips)
{
int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 4) double CalcSlippage = SlippagePips;
else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;
return(CalcSlippage);
}


//1 What happens if the OrderSelect fails?

//2 The && BuyTicket > 0 is redundant because if it was not >0 the OrderSelect would have failed.

The && OrderType() == OP_BUYLIMIT is not possible. Earlier in the code, you loop through open orders and return if you find a pending order

So if the && OrderType() == OP_BUYLIMIT this section of code will not be executed

Mimi
501
Mimi 2014.03.10 14:08  
GumRai:

Please use indentation, you will probably find more people are willing to help if you make your code easier to read

I have tidied it up a bit and added comments in the code, please read them Also additional comments re highlighted

I've also changed the code to get today's high and low

//1 What happens if the OrderSelect fails?

//2 The && BuyTicket > 0 is redundant because if it was not >0 the OrderSelect would have failed.

The && OrderType() == OP_BUYLIMIT is not possible. Earlier in the code, you loop through open orders and return if you find a pending order

So if the && OrderType() == OP_BUYLIMIT this section of code will not be executed

Thank you for correcting.

if( Bid > highest)                              //How can Bid be higher than today's high ???
         {
         Deleted = OrderDelete(BuyTicket,Green);
         }

This part of the code added because, if highest high got changed, then calculation of S1 will be different. That's why to added the condition to check, if its true, then delete the pending order. If any better way is possible then please share.

About OrderSelect area, i deleted the orderclosetime part. Now will it be ok? I tested it.

EB = MathAbs((S1 - Bid));

      if (Bid > S1 && EB > (20 * Point))    //if Bid is at least 20 pips below S1 ie at least 60 pips below today's high
         { 
         BuyTicket = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,S1,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Stop Order",MagicNumber,expiration,Green);
         SellTicket = 0;
         }
      else if(Bid < S1 && EB > (20 * Point))
         {
         BuyTicket = OrderSend(Symbol(),OP_BUYSTOP,LotSize,S1,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Limit Order",MagicNumber,expiration,Green);
         SellTicket = 0;
         }

Now about this part (corrected) is say high is 1.3680 bid is at 1.3659 greater than 20, so S1 is 1.3680-40=1.3640. It will place Buylimit. But if bid is below the S1 then it will place buystop, ( I checked the tester, sometimes it was giving ordersend error, so that's why added this condition)

Lastly about the Ticket < 100 condition, i added it because, i want to take only 1-2 trades per day.

Files:
kk_5.mq4 5 kb
12
To add comments, please log in or register