Why does this code not produce sells?

 

Really need some help here.Ive attached the EA as well as a Screenshot to make it easier to test out if you dont want to copy paste.

Objective: I only need this to produce one buy after there is already one sell already and/or one sell after there is one buy already.

Problem: The following piece of code does not produce the second sell after the first buy, yet it does produce many second buys after the first sell. As you can see, the prerequisite to produce a second buy is that there is already a first sell, and the prerequisite to produce a second sell is that there is already a first buy.

Notice it does produce buys after the initial first sell, which is the prerequisite to place the second buy. Ive tried changing the order and it does affect it however it only works right for the first one that requires the prerequisite of an order placed.

Ive used a counter to count both buys and sells. For some reason its not producing the second sell . Please help me find out why.


//+------------------------------------------------------------------+
//|                                                          whynosell.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
if (sellcount_order()==0) 
   {
   OrderSend(Symbol(),OP_SELL,.01,Bid,0,0,0,1, 1 ,0,clrNONE); 
   }
if (buycount_order()==0)
   {
   OrderSend(Symbol(),OP_BUY,.01,Ask,0,0,0,1, 1 ,0,clrNONE); 
   }
if (sellcount_order()==1) 
   {
   OrderSend(Symbol(),OP_BUY,.01,Ask,0,0,0,2, 2 ,0,clrNONE); 
   }
if (buycount_order()==1)
   {
   OrderSend(Symbol(),OP_SELL,.01,Bid,0,0,0,2, 2 ,0,clrNONE); 
   }
   }
   

//+------------------------------------------------------------------+
//create new order counting function
int sellcount_order()
  {
   int sellcount=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS)==true)
        {
        //CREATE ANOTHER ORDER
         if(OrderSymbol()==Symbol()&& OrderType()==OP_SELL  )
           {
            sellcount=sellcount+1;
            //count++;
           }
        }
     }
   return(sellcount);
  }
  
  int buycount_order()
  {
   int buycount=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS)==true)
        {
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY   )
           {
            buycount=buycount+1;
            //count++;
           }
        }
     }
   return(buycount);
  }
  



1. How do I get it to also send sells?

2. How do I get it to only send ONE buy when

(sellcount_order()==1) 

or ONE sell when

(buycount_order()==1) 

instead of many as it does?

Thanks a ton in advanced.

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
Files:
 
  1. Did you check the log for errors?
  2. Please correct the call of OrderSend(). Put the cursor on OrderSend(..) and press F1. Then you can see that your "NULLs " are misleading!! Start from using the example in the reference (F1).
 
Carl Schreiber:
  1. Did you check the log for errors?
  2. Please correct the call of OrderSend(). Put the cursor on OrderSend(..) and press F1. Then you can see that your "NULLs " are misleading!! Start from using the example in the reference (F1).

1. Yes. No errors.

2. Updated the message and reuploaded EA .

Any idea how to solve the issue? Trying static variable to stop it out with no success.. Gotta be a way.

 
Have you gone through your code with the debugger?
 
Check the return value of OrderSend()
 

Think about your logic...

if (sellcount_order()==0) 
   {
   OrderSend(Symbol(),OP_SELL,.01,NULL,NULL,NULL,NULL,1,1,NULL,NULL);
   }
if (buycount_order()==0)
   {
   OrderSend(Symbol(),OP_BUY,.01,NULL,NULL,NULL,NULL,1,1,NULL,NULL);
   }
if (sellcount_order()==1) 
   {
   OrderSend(Symbol(),OP_BUY,.01,NULL,NULL,NULL,NULL,2,2,NULL,NULL);
   }
if (buycount_order()==1)
   {
   OrderSend(Symbol(),OP_SELL,.01,NULL,NULL,NULL,NULL,2,2,NULL,NULL);
   }

You start off with no orders.

You open a sell (first logic), then you open a buy (second logic),

When the third logic is reached, the sell count == 1 so you open another buy.

The fourth logic never triggers,  because buycount == 2 (after the third logic).

At the end of this first run, you have 1 Sell and 2 Buy orders.

On every call thereafter, the third logic opens another buy order. Nothing else triggers.

 
nadiawicket:

I got it down.

Did the trick

Thanks a ton


There are latent problems with that code.

OrderMagicNumber() requires an OrderSelect().

It will be using the last orderselect from your sellcount_order() or buycount_order() call. That could be anything.

 
honest_knave:


There are latent problems with that code.

OrderMagicNumber() requires an OrderSelect().

It will be using the last orderselect from your sellcount_order() or buycount_order() call. That could be anything.

Thanks for pointing that out
 

I did learn something new from this thread.

If you pass NULL or 0 for price in an OrderSend call, it works.

No need to bother with Bid / Ask, particularly useful when opening orders on other symbols.


 

Thought I had it, I didnt. Any more thoughts are greatly appreciated.


Tried to fix logic to no avail.

 
nadiawicket:

Thought I had it, I didnt. Any more thoughts are greatly appreciated.


Tried to fix logic to no avail.


It might help if we understood your goal more clearly.

nadiawicket:

Objective: I only need this to produce one buy after there is already one sell already and/or one sell after there is one buy already. 

To achieve this goal, you simply open a buy and a sell at the same time.