Little help with my code please.

 

Hi guys

Please help me with this code. The expert opens a buy position and then SellStop.
But if there is manually
open Positions (market or pending) . the Expert stops working even though I did selected the orders with magic number in the Loop

What's wrong in my code ???
I do not expert to deal with manually opened positions. Only deals with  order had MAGIC NUMBER .
Please look at the following images for clarification on the chart.

//+------------------------------------------------------------------+
//|                                                        count.mq4 |
//|                   Copyright 2018, Engr.Mohamed Abdelwaged.       |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Engr.M.AbdelWagid"
#property link      "Engr.M.AbdelWagid"
#property version   "1.00"
#property strict

extern int distance = 30;
extern int TakeProfit = 35;
extern int StopLoss   = 60;
//-----
extern double lot1   =  0.01;
extern double lot2   =  0.02;
//-----
double pt;
int MagicNumber=1234;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(Digits==4 || Digits<=2) pt=Point;
   if(Digits==5 || Digits==3) pt=Point*10;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int total=OrdersTotal();
   double OpenLongOrders=0,OpenShortOrders=0,PendLongs=0,PendShorts=0;
//---
   if(total==0 && OpenLongOrders==0 && OpenShortOrders==0 && PendLongs==0 && PendShorts==0)
     {
      openbuy();
     }
//---     
   int i;
   for(i=0;i<total;i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) //selected orders with magic number only
        {
         int type=OrderType();

         if(type == OP_BUY )       {OpenLongOrders=OpenLongOrders+1;}
         if(type == OP_SELL )      {OpenShortOrders=OpenShortOrders+1;}
         if(type == OP_BUYSTOP )   {PendLongs=PendLongs+1;}
         if(type == OP_SELLSTOP )  {PendShorts=PendShorts+1;}

         if(total==1 && OpenLongOrders==1 && OpenShortOrders==0 && PendLongs==0 && PendShorts==0)
           {
            sellstop1();  // if there is a manually opened positions the selltop1 didn't executed. why ??? 
           }
         
        }
     }
  }
  
//+------------------------------------------------------------------+
//| openbuy                                                          |
//+------------------------------------------------------------------+
void openbuy()
  {
   if(OrderSend(Symbol(),OP_BUY,lot1,Ask,0,Bid-StopLoss*pt,Bid+TakeProfit*pt,NULL,MagicNumber,0,clrBlue)){printf("01");}
  }  
//+------------------------------------------------------------------+
//| sellstop1                                                        |
//+------------------------------------------------------------------+
void sellstop1()
  {
   if(OrderSend(Symbol(),OP_SELLSTOP,lot2,Bid-distance*pt,0,(Ask-distance*pt)+StopLoss*pt,(Ask-distance*pt)-TakeProfit*pt,NULL,MagicNumber,0,clrBlue)){printf("02");}
  }
//+------------------------------------------------------------------+

01

02

 

Because it will only work when:

total==1
So if there are more then one it won't open a new order.
 
Mohamed Abdelwaged:


Hi guys

Please help me with this code. The expert opens a buy position and then SellStop.
But if there is manually
open Positions (market or pending) . the Expert stops working even though I did selected the orders with magic number in the Loop

What's wrong in my code ???
I do not expert to deal with manually opened positions. Only deals with  order had MAGIC NUMBER .
Please look at the following images for clarification on the chart.


Because if you open an order manually or with another EA the amount of of Orders aren't zero.

int total=OrdersTotal();

Your condition isn't met

if(total==0 && ...

You have to use a loop and find out all opened orders and filter them by magic number.

 
Petr Nosek:

Because if you open an order manually or with another EA the amount of of Orders aren't zero.

Your condition isn't met

You have to use a loop and find out all opened orders and filter them by magic number.

how can I do this ???!!

is there an Idea ?

can you edit the code for me ?

 
Marco vd Heijden:

Because it will only work when:

So if there are more then one it won't open a new order.
is there anyway to make expert calculate it's orders only with magic number

without using orderstotal();

????!!!

can you edit the code ?

 
If you understood correctly. That's right.
void OnTick()
  {
//---
   int total=0;
   double OpenLongOrders=0,OpenShortOrders=0,PendLongs=0,PendShorts=0;

   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if(OrderSymbol()==Symbol())
      if(OrderMagicNumber()==MagicNumber) //selected orders with magic number only
        {
         total++;
         switch( OrderType() )
         {
           case OP_BUY:
               OpenLongOrders++;
               break;
           case OP_SELL:
               OpenShortOrders++;
               break;
           case OP_BUYSTOP:
               PendLongs++;
               break;
           case OP_SELLSTOP:
               PendShorts++;
               break;
         }
        }
     }//---
     if(total==0 && OpenLongOrders==0 && OpenShortOrders==0 && PendLongs==0 && PendShorts==0) openbuy();
     if(total==1 && OpenLongOrders==1 && OpenShortOrders==0 && PendLongs==0 && PendShorts==0) sellstop1(); 
  }
 
Mohamed Abdelwaged:

how can I do this ???!!

is there an Idea ?

can you edit the code for me ?

If I understand you well you want to open a BUY (if there isn't another order from this EA) and then immediately open SELL STOP. Than you can try this (I haven't tested it).

void OnTick()
  {
   if(!ExistOrder(MagicNumber))
      if(openbuy()) sellstop1();
  }
  
bool ExistOrder(const int mn)
  {
   int total=OrdersTotal()-1;
   bool found=false;
   for(int i=total;i>-1 && !found;i--)
      found=OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber()==mn;
   return(found);
  }

bool openbuy()
  {
   if(OrderSend(Symbol(),OP_BUY,lot1,Ask,0,Bid-StopLoss*pt,Bid+TakeProfit*pt,NULL,MagicNumber,0,clrBlue)){printf("01");return(true);}
   return(false);
  }

void sellstop1()
  {
   if(OrderSend(Symbol(),OP_SELLSTOP,lot2,Bid-distance*pt,0,(Ask-distance*pt)+StopLoss*pt,(Ask-distance*pt)-TakeProfit*pt,NULL,MagicNumber,0,clrBlue)){printf("02");}
  }
 
Konstantin Nikitin:
If you understood correctly. That's right.
I tested it but didn't open any position .
 
Mohamed Abdelwaged:

Hi guys

Please help me with this code. The expert opens a buy position and then SellStop.
But if there is manually
open Positions (market or pending) . the Expert stops working even though I did selected the orders with magic number in the Loop

What's wrong in my code ???
I do not expert to deal with manually opened positions. Only deals with  order had MAGIC NUMBER .
Please look at the following images for clarification on the chart.


  I'm trying to edit it. image and edited code. Greetings

//+------------------------------------------------------------------+
//|                                                        count.mq4 |
//|                   Copyright 2018, Engr.Mohamed Abdelwaged.       |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Engr.M.AbdelWagid"
#property link      "Engr.M.AbdelWagid"
#property version   "1.00"
#property strict

extern int distance = 30;
extern int TakeProfit = 35;
extern int StopLoss   = 60;
//-----
extern double lot1   =  0.01;
extern double lot2   =  0.02;
//-----
double pt;
int ticket;
int MagicNumber=1234;
int OpenOrderTotal=0;
extern string ExpertName="Abdelwaged";
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(Digits==4 || Digits<=2) pt=Point;
   if(Digits==5 || Digits==3) pt=Point*10;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
void OnTickTotalOrders()
  {
//---
   int total=OrdersTotal();
   OpenOrderTotal=0;
   double OpenLongOrders=0,OpenShortOrders=0,PendLongs=0,PendShorts=0;
//---
   
//---     
   int i;
   for(i=0;i<total;i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) //selected orders with magic number only
        {
         int type=OrderType();

         if(type == OP_BUY  || type == OP_SELL || type == OP_BUYSTOP || type == OP_SELLSTOP )   OpenOrderTotal= OpenOrderTotal+1;

         
         
        }
     }
  }  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   OpenOrderTotal=0;
   int total=OrdersTotal();
   OnTickTotalOrders();
  
   
   double OpenLongOrders=0,OpenShortOrders=0,PendLongs=0,PendShorts=0;
//---
   if(OpenOrderTotal==0 && OpenLongOrders==0 && OpenShortOrders==0 && PendLongs==0 && PendShorts==0)
     {
      openbuy();
     }
     OnTickTotalOrders();
//---     
   int i;
   for(i=0;i<total;i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) //selected orders with magic number only
        {
         int type=OrderType();

         if(type == OP_BUY )       {OpenLongOrders=OpenLongOrders+1;}
         if(type == OP_SELL )      {OpenShortOrders=OpenShortOrders+1;}
         if(type == OP_BUYSTOP )   {PendLongs=PendLongs+1;}
         if(type == OP_SELLSTOP )  {PendShorts=PendShorts+1;}

         
         
        }
        
        if(OpenOrderTotal==1 && OpenLongOrders==1 && OpenShortOrders==0 && PendLongs==0 && PendShorts==0)
           {
            sellstop1();  // if there is a manually opened positions the selltop1 didn't executed. why ??? 
           }
     }
  }
  
//+------------------------------------------------------------------+
//| openbuy                                                          |
//+------------------------------------------------------------------+
void openbuy()
  {
    ticket=(OrderSend(Symbol(),OP_BUY,lot1,Ask,0,Bid-StopLoss*pt,Bid+TakeProfit*pt,ExpertName+" "+(string)MagicNumber,MagicNumber,0,clrBlue));
    if (GetLastError()!=0) Print(" BUY Order Error "+(string)ticket);
    {printf("01");}
  }  
//+------------------------------------------------------------------+
//| sellstop1                                                        |
//+------------------------------------------------------------------+
void sellstop1()
  {
    ticket=(OrderSend(Symbol(),OP_SELLSTOP,lot2,Bid-distance*pt,0,(Ask-distance*pt)+StopLoss*pt,(Ask-distance*pt)-TakeProfit*pt,ExpertName+" "+(string)MagicNumber,MagicNumber,0,clrBlue));
    if (GetLastError()!=0) Print(" SELL Order Error "+(string)ticket);   
   // {printf("02");}
   
  }
//+------------------------------------------------------------------+
Files:
Reason: