Problem with break

Back to topics list To post a new topic, please log in or register
avatar
32
m_shafiei2006 2016.07.30 09:27
 

Hi

I have got a problem in my code, so I cut and change part of it for my question;

I need to get the last index of open buy or sell orders that already numbered in its comment. but when the same orders become more than 1, it doesn't return (return -1) its index, it seems break doesn't work and "for" continues.

thanks



#include <stdlib.mqh>
#property strict

extern double     Lot = 0.5,
                  Distance = 200.0;



double FirstBuyPrice = -1, FirstSellPrice = -1;
                  
                  
                  
                  
int    OpenLongTicket()
       {
          int order = OrderSend(Symbol(),OP_BUY,Lot,Ask,3,0,0,IntegerToString(LastBuyLevel()+1),0,0,clrBlue);
          return(order);
          if (order < 0)
          Alert("Open Long Error = ",ErrorDescription(GetLastError()));
       } 
 
int    OpenShortTicket()
       {
          int order = OrderSend(Symbol(),OP_SELL,Lot,Bid,3,0,0,IntegerToString(LastSellLevel()+1),0,0,clrRed);
          return(order);
          if (order < 0)
          Alert("Open Short Error = ",ErrorDescription(GetLastError()));
           
       } 
          

int LastBuyLevel ()
{
   int Level = -1;
   for (int i = OrdersTotal()-1; i >= 0 ;i--)
   {
      if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))
      {
         if (OrderType() == OP_BUY)
         Level++;
      }
   }
   
   return (Level);
}


int LastSellLevel ()
{
   int Level = -1;
   for (int i = OrdersTotal()-1; i >= 0 ;i--)
   {
      if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))
      {
         if (OrderType() == OP_SELL)
         Level++;
      }
   }
   return (Level);
}

double MNextDistance (int Type)
{
   double  Current = 0;
   if ((Type == OP_BUY && LastBuyLevel() == 0) || (Type == OP_SELL && LastSellLevel() == 0))
   Current = Distance;
   if (Type == OP_BUY && LastBuyLevel() > 0)
   Current = ((LastBuyLevel()+1)*Distance);
   if (Type == OP_SELL && LastSellLevel() > 0)
   Current = ((LastSellLevel()+1)*Distance);
   return (Current);
}

int     LastLevelIndex(int Type)
        {
           int i = -1;
           for(i=OrdersTotal()-1;i>=0;i--)
           if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
              if(Type == OP_BUY && OrderType() == OP_BUY && LastBuyLevel()==StrToInteger(OrderComment()))
              break;
              else
              if(Type == OP_SELL && OrderType() == OP_SELL && LastSellLevel()==StrToInteger(OrderComment()))
              break;
           }
           return(i);
        }
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if (OrdersTotal() == 0)
   {
      OpenLongTicket();
      FirstBuyPrice = Ask;
      OpenShortTicket();
      FirstSellPrice = Bid;
   }

   if (Ask <= (FirstBuyPrice - (MNextDistance (OP_BUY)*Point)))
   OpenLongTicket();
   if (Bid >= (FirstSellPrice + (MNextDistance (OP_SELL)*Point)))
   OpenShortTicket();

  
Comment (
         "\n","LastLevelIndex(OP_BUY) : ",LastLevelIndex(OP_BUY),
         "\n","LastLevelIndex(OP_SELL) : ",LastLevelIndex(OP_SELL)
         );  
  }
//+------------------------------------------------------------------+
avatar
1109
Ovo 2016.07.30 09:45 #
 

 You are using functions which change the selected deal:

LastSellLevel() and  LastBuyLevel()

 

avatar
32
m_shafiei2006 2016.07.30 10:57 #
 
Ovo:

 You are using functions which change the selected deal:

LastSellLevel() and  LastBuyLevel()

 

Sure !

If I have 1 buy position, OrderComment() of this position, is equal to 0 and LastBuyLevel() == 0 so the index of this position is 0, it is OK

If I have 2 buy positions, OrderComment() of the last buy position, is equal to 1 and LastBuyLevel() == 1 so the index of this position is 1, but LastLevelIndex(OP_BUY) returns -1

If I have 3 buy positions, OrderComment() of the last buy position, is equal to 2 and LastBuyLevel() == 2 so the index of this position is 2, but LastLevelIndex(OP_BUY) returns -1

...

so LastBuyLevel() is always equal to OrderComment() of the last buy position. I need Index of the last buy position.

can you explain it how does it change ?

avatar
12992
WHRoeder 2016.07.30 14:36 #
 
Ovo:

You are using functions which change the selected deal:

LastSellLevel() and  LastBuyLevel()

  1. Get those values, before you enter your LastLevelIndex select loop.
  2. brokers can change comments, including complete replacement.
avatar
32
m_shafiei2006 2016.07.30 15:09 #
 
WHRoeder:
  1. Get those values, before you enter your LastLevelIndex select loop.
  2. brokers can change comments, including complete replacement.

thanks

that worked, and thanks again for the suggestion.

Back to topics list  

To add comments, please log in or register