Problem with Expert Code

 
Hi guys, I don't know if I can write in Italian, however I have a problem with the code of one of my experts, can I post it to you and maybe you tell me where did I go wrong that I've been hitting my head for days?
 
Daniele Palmieri :
Hi guys, I don't know if I can write in Italian, however I have a problem with the code of one of my experts, can I post it to you and maybe you tell me where did I go wrong that I've been hitting my head for days?

Use the forum's built-in translator:

translator

 
Vladimir Karputov #:

Use the forum's built-in translator:


Im sorry.... thank u so much....  
 

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 ...

 double lotCalculating( int direct){

double lot = 0 ;
double lotbuy= lotbuy();
double lotsell = lotsell();


if (direct== 1 ){
lot = (lotbuy* 3 )-lotsell;
Print ( "lot " ,lot, "  lot buy" ,lotbuy, "  lot sell " ,lotsell);
}

if (direct== 2 ){
lot = (lotsell* 3 )-lotbuy;
Print ( "lot " ,lot, "  lot buy" ,lotbuy, "  lot sell " ,lotsell);
}

Print ( " numero lotti calcolato :  " ,lot);

return lot;
}

void PositionStrategy (){

for ( int i= PositionsTotal ()- 1 ; i>= PositionsTotal ()- 2 ; i--){

ulong positionticket = PositionGetTicket (i- 1 );
long positiondirect = PositionGetInteger ( POSITION_TYPE );

double openprice = PositionGetDouble ( POSITION_PRICE_OPEN );

if ( PositionSelectByTicket (positionticket)) {
if (positiondirect == POSITION_TYPE_BUY ){
Print ( "Buy Stop" );
trade.BuyStop(lotCalculating( 2 ),(openprice+Pos_Dis* _Digits ), _Symbol , 0 , 0 , ORDER_TIME_GTC , 0 );
return ;
}
if (positiondirect== POSITION_TYPE_SELL ){
Print ( "sell stop" );
trade.SellStop(lotCalculating( 1 ),(openprice-Pos_Dis* _Digits ), _Symbol , 0 , 0 , ORDER_TIME_GTC , 0 );
return ;
      }
   }
} //end for 
}

double lotbuy (){

double lot= 0 ;
for ( int i= PositionsTotal ()- 1 ; i>= 0 ; i--){
string pos_symbol = PositionGetSymbol (i);
long pos_direct = PositionGetInteger ( POSITION_TYPE );

if ( _Symbol ==pos_symbol)
if (pos_direct== POSITION_TYPE_BUY )
lot = PositionGetDouble ( POSITION_VOLUME )+lot;
}
return (lot);
}

double lotsell(){
double lot= 0 ;
for ( int i= PositionsTotal ()- 1 ; i>= 0 ; i--){
string pos_symbol = PositionGetSymbol (i);
long pos_direct = PositionGetInteger ( POSITION_TYPE );

if ( _Symbol ==pos_symbol)
if (pos_direct== POSITION_TYPE_SELL )
lot = PositionGetDouble ( POSITION_VOLUME )+lot;

}
return (lot);

}
 
the problem is that, when it calculates the lots of orders to be placed, it returns 0.0 as the value
 

What is it???

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PositionStrategy()
  {
   for(int i=PositionsTotal()- 1 ; i>=PositionsTotal()- 2 ; i--)
     {
      ulong positionticket=PositionGetTicket(i- 1);
 

in practice I have to take the penultimate open position, and place the order at the same price as the opening of the position. 


I've already jotted down a new version, using an array, but it still doesn't open.


void PositionStrategy (){

int a =1;
ulong positions [50];


for (int i = PositionsTotal()-1; i>=0; i--){
string simbol=PositionGetSymbol(i);

      if(simbol==_Symbol){

      ulong ticket = PositionGetTicket(i);
      Print("indice array  ",a);
      positions[a]= ticket;
      
      a++;
      }
}// end for
for (int i =0; i<=50; i++){
if (PositionSelectByTicket(positions[a+1])){
   Print(" ticket  ", positions[a+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);
      return;
      }// 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);
      return;
      }// end sell stop

}// end ticket
}// end 2 for
}
 
Daniele Palmieri # :

in practice I have to take the penultimate open position, and place the order at the same price as the opening of the position. 


I've already jotted down a new version, using an array, but it still doesn't open.


What is it???

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

How can you loop through 'i' and take the value 'i + 1' ?????????

 

In practice, the a is the index of the array in which I store the ticks of the open positions only on a given symbol, 


i didn't set the array as series, so i thought to start the array from 0 with the last open position and take the index +1 for the penultimate open position,

 
Daniele Palmieri # :

In practice, the a is the index of the array in which I store the ticks of the open positions only on a given symbol, 


i didn't set the array as series, so i thought to start the array from 0 with the last open position and take the index +1 for the penultimate open position,

You have taken a loop on 'i' - so why then do you refer not to 'i', but to 'a' ??? This is the third time I’m telling you - this is very stupid: correct your mistake !!!

 

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

void PositionStrategy (){

int a =1;
ulong positions [50];
int size = ArraySize(positions);


for (int i = PositionsTotal()-1; i>=0; i--){
string simbol=PositionGetSymbol(i);

      if(simbol==_Symbol){

      ulong ticket = PositionGetTicket(i);
      Print("indice array  ",a);
      positions[a]= ticket;
      a++;
      }
      
}// end for
for (int i =0; i<=size; i++){
if (PositionSelectByTicket(positions[i+1])){
   Print(" ticket  ", positions[i+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);
      return;
      }// 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);
      return;
      }// end sell stop

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