Problem with Expert Code - page 2

 
Daniele Palmieri #:

Anyway, back to us I think the problem is in this part of the code ... In practice, once you enter with two different positions (a buy and a sell) the program should automatically place, buy stop and sell stop based on the last purchased position, but does not ...

Try this code


#include <Trade\PositionInfo.mqh>

CPositionInfo        eaposition;


double LastBuyPrice()
  {
   double price = 0;
   for(int i = PositionsTotal(); i >= 0; i--)
     {
      if(eaposition.SelectByIndex(i))
        {
         string PosSymbol        = eaposition.Symbol();
         string comment          = eaposition.Comment();
         ENUM_POSITION_TYPE type = eaposition.PositionType();
         if(PosSymbol == _Symbol && type == POSITION_TYPE_BUY)
           {
            price = eaposition.PriceOpen();
            break;
           }
        }
     }
   return price;
  }
 
Daniele Palmieri # :

Sorry Vladimir, but I am writing to you while I am at work or as I get home after 12 hours of work, yes you are right it was a very stupid mistake, however I corrected it and it seems now I can open more symbols without messing with the positions.



Anyway if I may ask, would you leave the array static or turn it into dynamic?


I am attaching the current code

You don't listen to anything and keep making a silly mistake:

for (int i =0; i<=size; i++)
   {
    if (PositionSelectByTicket(positions[i+1]))
       {
 
Vladimir Karputov #:

You don't listen to anything and keep making a silly mistake:

Excuse me Vladimir but I can't understand, the loop you are talking about is the one that occurs when "i" becomes equal to "size" and I take "i + 1" which is outside the array?

 
Mr Anucha Maneeyotin #:

Try this code


Thanks Anucha for the code, however I shouldn't take the last open position on the symbol but the penultimate ... reading the code I think it returns the last position right?

 
Daniele Palmieri # :

Thanks Anucha for the code, however I shouldn't take the last open position on the symbol but the penultimate ... reading the code I think it returns the last position right?

NO!!! If you enter the list you MUST be on this list !!!

Forum on trading, automated trading systems and testing trading strategies

Problem with Expert Code

Vladimir Karputov, 2021.09.24 05:45

You don't listen to anything and keep making a silly mistake:

for (int i =0; i<=size; i++)
   {
    if (PositionSelectByTicket(positions[i+1]))
       {

 
Vladimir Karputov #:

NO!!! If you enter the list you MUST be on this list !!!


Sorry Vlad, i m not a programmer.... however i had write this code now.... It working very well... So i must to close this post ?

for (int i =PositionsTotal()-1; i>=0; i--){
if (pos_info.SelectByIndex(i)){
   ulong ticket = PositionGetTicket(i);
if (ticket==positions[1]){
   Print(" ticket  ", positions[1]);
   long positiondirect = PositionGetInteger(POSITION_TYPE);
   double openprice = PositionGetDouble(POSITION_PRICE_OPEN);
   
      if (positiondirect == POSITION_TYPE_BUY){
      Print("Buy Stop");
      trade.BuyStop(lotCalculating(2),(openprice+Pos_Dis*_Digits),_Symbol,0,0,ORDER_TIME_GTC,0);
      break;
      }// end buy stop
      if (positiondirect==POSITION_TYPE_SELL){
      Print("sell stop");
      trade.SellStop(lotCalculating(1),(openprice-Pos_Dis*_Digits),_Symbol,0,0,ORDER_TIME_GTC,0);
      break;
      }// end sell stop

}// end ticket
}// end if idenx
}// end 2 for
}
Reason: