Problem with buy and sell orders?

 

Hello,

I should start off and say I am new to mql4. I am attempting to make a simple grid trader to start off with. Piecing it together slowly.

I am currently struggling with getting my EA to trade both buy and sell orders. Currently it seems to only want to trade sell orders. I currently am using:

 

int checktrade()
{
double   trend = iAO(Symbol(),PERIOD_D1,1);
double   strenght = iRSI(Symbol(),PERIOD_D1,rsiperiod,PRICE_CLOSE,1);
double   rvi = iRVI(Symbol(),PERIOD_D1,rviperiod,MODE_MAIN,1);
double   rvis = iRVI(Symbol(),PERIOD_D1,rviperiod,MODE_SIGNAL,1);
if((rvis>rvi) && trend>0) isbuy=OP_BUY; //return(OP_BUY);
if((rvis<rvi) && trend<0) isbuy=OP_SELL; //return(OP_SELL);
Print(isbuy);
}

 To check if my trades are valid. I have attempted to use return(OP_BUY/OP_SELL); vs isbuy=OP_BUY/OP_SELL; with no success. Also, please ignore the unused indicators, I have just been playing with calling in indicators.

Find below my open trade logic.

 

if(AccountEquity()<=(AccountBalance()-(AccountBalance()*0.03)))
   {
      Print("Equity too low. Current level in DD:", ((AccountBalance()-AccountEquity())/AccountBalance())*100,"% ");
      return(0);
   }  
      else
   {
   checktrade();
      if(OrdersTotal()>=MaxCount)
      {
         return(0);
      }
         else
      {
      //No Trade
         if(isbuy==0)
         {
            return(0);
         }
      //Buy
         if(isbuy==OP_BUY)
         {
            //checkprice();
            ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,5,0,0,"ORDER CHECK", MagicNumber,0,Green);
               if(ticket<0)
               {
               Print("OrderSend failed with error ",GetLastError());
               return(0);
               } 
                  else
               {
               orderid = orderid+1;
               OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
               OrderModify(ticket,OrderOpenPrice(),0,OrderOpenPrice()+(tp*Point),0,Green);
               return(0);
               }
            return(0);
         }
      //Sell
         if(isbuy==OP_SELL)
         {
            //checkprice();
            ticket=OrderSend(Symbol(),OP_SELL,lot,Bid,5,0,0,"ORDER CHECK", MagicNumber,0,Red);
               if(ticket<0)
               {
               Print("OrderSend failed with error ",GetLastError());
               return(0);
               } 
                  else
               {
               orderid = orderid+1;
               OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
               OrderModify(ticket,OrderOpenPrice(),0,OrderOpenPrice()-(tp*Point),0,Red);
               return(0);
               }
            return(0);
         }    
      }
   }

 It is probably only a basic problem, but it seems as though once it gets the signal to sell, it doesn't want to change back.

 

Thanks for all the help 

 
Tempestshade:

Hello,

I should start off and say I am new to mql4. I am attempting to make a simple grid trader to start off with. Piecing it together slowly.

I am currently struggling with getting my EA to trade both buy and sell orders. Currently it seems to only want to trade sell orders. I currently am using:

You need to read the documentation on the code you are using . . .  it is clear you don't understand it,  how do you expect to make progress without expanding your knowledge and understanding as you progress ?

You have this code . . .

      //No Trade
         if(isbuy==0)
         {
            return(0);
         }
      //Buy
         if(isbuy==OP_BUY)
         {
            //checkprice();

 OP_BUY is a standard constant,  it has an equivalent value of 0,  see here:  OP_BUY

So you check for "No Trade" by checking is isbuy = OP_BUY ,  if it is you return from start,  if it isn't you carry on and check if  isbuy = OP_BUY  ??  well it can't be because if it was you would have already returned using the code above . . .

 
Read the documentation about OrderSend() and its associated standard constants . . .  understand it and make some notes on anything you need to remind yourself about in the future.

 

I told you it was something simple.

I am fully aware that OP_BUY is equivalent to 0. The problem is I have changed the way I check to see if it is a buy signal or not. I use to have the variable isbuy change value to 0(notrade), 1(buy), and 2(sell) in checktrade().

I guess I just forgot to change it.

 

Thank you for your help.

Reason: