technical EXPERT issues

 

hi im having issue i wrote a function isthereactiveorder() if there is a buy dont open a buy if there is sell dont open sell it seems to not working, if any one can provide any guides i well be thankful

 #property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
extern double lotsize;
extern int ma1 =6;
extern int ma2 =18;
extern int ma3 =50;
extern int ma4 =200;

extern double sl=40;
extern double tp=80;
extern double risk=2;



int minbar =ma1;

bool canbuy;
bool cansell;
bool buycross=false;
bool sellcross=false; 

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
   
  }
void OnTick()
  {
   canbuy=false;
   cansell=false;
   isthereactiveorder();
   cheackmacross();
   if(canbuy==true )
     {
      opennew(OP_BUY);
     }
   if(cansell==true )
     {
      opennew(OP_SELL);
     }
     
  }
void isthereactiveorder(){
for( int i = 0 ; i < OrdersTotal() ; i++ ) {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) {
         Print("ERROR - Unable to select the order - ",GetLastError());
         break;
      } 
      if( OrderSymbol()==Symbol() && OrderType() == OP_BUY) canbuy=false;
      if( OrderSymbol()==Symbol() && OrderType() == OP_SELL) cansell=false;
      return;
        
}
}
double calculateLotSize(int stopLoss,int Risk)
{
   
     
    // Fetch some symbol properties
    double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
    double minLot  = MarketInfo(Symbol(), MODE_MINLOT); 
    double maxLot  = MarketInfo(Symbol(), MODE_MAXLOT);
    double tickVal = MarketInfo(Symbol(), MODE_TICKVALUE);
 
    // Calculate the actual lot size
    double lotSize = AccountBalance() * Risk / 100 / (stopLoss * tickVal);
 
    return MathMin(
        maxLot,
        MathMax(
            minLot,
            NormalizeDouble(lotSize / lotStep, 0) * lotStep 
        )
    ); }

void opennew(int ordertype){
    RefreshRates();
    double openprice=0;
    double closeprice=0;
    double stopp=0;
    double takep=0;
    
    if(ordertype==OP_BUY){
            openprice=Ask;
            stopp=openprice-(sl*10*Point);
            takep=openprice+(tp*10*Point);
        }
    if(ordertype==OP_SELL){
        openprice=Bid;
            stopp=openprice+(sl*10*Point);
            takep=openprice-(tp*10*Point);
    }
    OrderSend(Symbol(),ordertype ,calculateLotSize(sl*10,risk),openprice,10,NormalizeDouble(stopp,Digits),NormalizeDouble(takep,Digits),NULL,0,0,clrBlue);
     
    return;
}
void cheackmacross(){
double currMA1=iMA(Symbol(),0,ma1,0,MODE_EMA,PRICE_CLOSE,1);
double prevMA1=iMA(Symbol(),0,ma1,0,MODE_EMA,PRICE_CLOSE,2);
double currMA2=iMA(Symbol(),0,ma2,0,MODE_EMA,PRICE_CLOSE,1);
double prevMA2=iMA(Symbol(),0,ma2,0,MODE_EMA,PRICE_CLOSE,2);
if(prevMA1>prevMA2 && currMA2>currMA1)
  {
   canbuy=true;
  }
if(prevMA1<prevMA2 && currMA2<currMA1)
  {
   cansell=true;
  }

} 
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
Files:
adam_khoo.mq4  3 kb
 
mo798ua:

hi im having issue i wrote a function isthereactiveorder() if there is a buy dont open a buy if there is sell dont open sell it seems to not working, if any one can provide any guides i well be thankful

Hello,

need to call first the isthereactiveorder() function and then the cheackmacross(). First check if there are opened orders and then check signals to open new orders.

Next, need to reset canbuy=false and cansell=false on top of OnTick() function.

I have made some change in your code.
Files:
adam_khoo.mq4  5 kb
 
Nikolaos Pantzos:

Hello,

need to call first the isthereactiveorder() function and then the cheackmacross(). First check if there are opened orders and then check signals to open new orders.

Next, need to reset canbuy=false and cansell=false on top of OnTick() function.

Thanks for your reply , i did what you suggest you can check the code again ,nothing changed yet still ton of orders

 
mo798ua:

Thanks for your reply , i did what you suggest you can check the code again ,nothing changed yet still ton of orders

Corrected code... 

Files:
adam_khoo.mq4  5 kb
 
Nikolaos Pantzos:

Corrected code... 

great thanks , if you may explain how my logic is wrong i will be thankful 

 
mo798ua:

great thanks , if you may explain how my logic is wrong i will be thankful 

You set in 2 function 'canbuy' parameter and 'cansell'. isthereactiveorder and cheackmacross are the two functions.

But the correct way is to count buy and sell orders on 'isthereactiveorder ' function. I deleted 'canbuy/cansell' parameters from 'isthereactiveorder ' function, and I set it to calculate orders with new parameters 'CntBuy/CntSell'.

Now, if canbuy = true and CntBuy = 0, then expert open buy orders, same for the sell orders....

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
For equity securities, the Depth of Market window is available, where you can see the current Buy and Sell orders. Desired direction of a trade operation, required amount and requested price are specified for each order. To obtain...
 
Nikolaos Pantzos:

You set in 2 function 'canbuy' parameter and 'cansell'. isthereactiveorder and cheackmacross are the two functions.

But the correct way is to count buy and sell orders on 'isthereactiveorder ' function. I deleted 'canbuy/cansell' parameters from 'isthereactiveorder ' function, and I set it to calculate orders with new parameters 'CntBuy/CntSell'.

ok i thought since cansell/canbuy is global variable so any function can access it and change it  

 
mo798ua:

ok i thought since cansell/canbuy is global variable so any function can access it and change it  

Yes, but isthereactiveorder  function cansell/canbuy if there are opened orders turn false, but the cheackmacross  function turn they true... cheackmacross function  canceled isthereactiveorder  function.

If you want to check 2 differents facts (orders and signals) is better to use 2 differents parameters.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Nikolaos Pantzos:

Yes, but isthereactiveorder  function cansell/canbuy if there are opened orders turn false, but the cheackmacross  function turn they true... cheackmacross function  canceled isthereactiveorder  function.

every thing is clear now thanks