Download MetaTrader 5

EA opens SELL orders but no BUY orders

To add comments, please log in or register
onefai
9
onefai  

Hello everybody,

I've been trying to make my very first self-made EA, but I've been failing to get it to open BUY orders. It seems to open SELL orders just without a problem, and since the code for both OrderSend() are identical, I can't grasp what is going wrong.

This is the relevant part of my code:

      total=OrdersTotal();
      
      
      if(total<1)
        {
        //--- no opened orders identified
        
        //--- check for long position (BUY) possibility
         if(Ask>senkouspan_a_one && Ask>senkouspan_b_one && Ask>senkouspan_a_two && Ask>senkouspan_b_two && Ask>PSAR_five && Ask>PSAR_fifteen && stochmain_five>stochsignal_five)
            {
            if((Time[0]-ordertime)>somelimit*60)  
               {
                 //execute my OrderSend function
                 ticket=OrderSend(Symbol(),OP_BUY,Lots,Bid,3,0,0," ",0,0,Green);
                  if(ticket>0)
                    {
                    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                     Print("BUY order opened : ",OrderOpenPrice());
                    }
                  else
                     Print("Error opening BUY order : ",GetLastError());
               ordertime = Time[0];
               }
            }
        //--- check for short position (SELL) possibility
         if(Ask<senkouspan_a_one && Ask<senkouspan_b_one && Ask<senkouspan_a_two && Ask<senkouspan_b_two && Ask<PSAR_five && Ask<PSAR_fifteen && stochmain_five<stochsignal_five)
           {
            if((Time[0]-ordertime)>somelimit*60)  
               {
                 //execute my OrderSend function
                 ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0," ",0,0,Red);
                  if(ticket>0)
                    {
                    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                     Print("SELL order opened : ",OrderOpenPrice());
                    }
                  else
                     Print("Error opening SELL order : ",GetLastError());
               ordertime = Time[0];
               }
           }
         //--- exit from block
         return;
        
        }

 

You can see that both if(...) have the same content, but switched relational operators... What am I doing wrong?

 

Thank you for taking your time to read all of this.

Regards,
onefai 

Keith Watford
Moderator
9641
Keith Watford  

You don't tell us whether the EA tries to place orders and fails or whether no attempts to buy are made.

What does getlasterror() return?

A buy order is opened at Ask, not bid.

onefai
9
onefai  
GumRai:

You don't tell us whether the EA tries to place orders and fails or whether no attempts to buy are made.

What does getlasterror() return?

A buy order is opened at Ask, not bid.

Oh, sorry, I really didn't know about that haha. My code is basically snippets of other EAs and indicators.

getlasterror() returns:

2016.05.29 03:53:24.731 2016.05.27 22:50  IchiCloudScalp DAX30,M1: Error opening BUY order : 138

2016.05.29 03:53:24.731 2016.05.27 22:50  IchiCloudScalp DAX30,M1: OrderSend error 138

 

Thanks for the hint, I switched out those Asks and Bids simply for iOpen(NULL,0,0) now.


onefai
9
onefai  

Oh my goodness.. nevermind, now I understand what you meant. I just switched out the Bid in OrderSend with Ask, and it opened a BUY position. It works fine now.

Thank you! 

To add comments, please log in or register