Coverage Hedge Grid

 

Hi all,

I am a newbie, so please forgive me if my questions are very elementary.

I am writing a function that will scan all open orders and if conditions are met, for each of them opens an hedge grid every "x" pips (i.e. if I have a buy order, it creates SellStop orders every "x" pips).

The grid is opened correctly, BUT, everytime, altready processed orders are taken in account, so it creates again an hedge grid.

I  put a LastCoverageLong variable that stores the OpenOrderTime of last processed orders, and I check it when selection the orders to cover, but it get reset at the begin of the function... I can't understand why.

can you please help?

thanks a lot

eros


void ApriReteCopertura(double argSizeHedgePerc,int argAmpiezzaGriglia)

  {
   double UsePoint=PipPoint(Symbol());
   datetime LastCoperturaLong;
   for(int b=OrdersTotal()-1;b>=0;b--)

     {
     
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_BUY)
               if(OrderOpenPrice()-OrderStopLoss()>(argAmpiezzaGriglia*UsePoint)) 
                  if(OrderOpenTime()>LastCoperturaLong ) //HERE I CHECK THE ORDER OPEN TIME WITH THE LAST TIME OF COVERAGE, LastCoperturaLong is set to initial value


                    {
                     OrderPrint();
                     Alert("In entrata OPT ",OrderOpenTime()," Last ",LastCoperturaLong);
                     double SizeHedge=NormalizeDouble(((OrderLots()*argSizeHedgePerc)/100),2); //to do: sistemare i digit dei lotti
                     int quanti=NormalizeDouble(((OrderOpenPrice()-OrderStopLoss())/UsePoint)/argAmpiezzaGriglia,0);                   
                     for(int d=1;d<=quanti;d++)
                       {
                        double OpPr = OrderOpenPrice()-d*argAmpiezzaGriglia*UsePoint;
                        double SL   = OpPr+argAmpiezzaGriglia*UsePoint;
                        double TP   = OpPr-argAmpiezzaGriglia*UsePoint;
                        string Comm=StringConcatenate("Rete per ",OrderTicket());
                        OrderSend(Symbol(),OP_SELLSTOP,SizeHedge,OpPr,NULL,SL,TP,Comm,NULL,NULL,NULL);
                        //Alert("range: ", zNormalize(OrderOpenPrice()-OrderStopLoss())," quanti ", quanti, " SizeHedge ", +SizeHedge," Apertura: ", OpPr, " SL: ",             SL, " TP: ", TP);
                        
                       } //fine del FOR
                     
                     LastCoperturaLong=OrderOpenTime(); //HERE I UPDATE THE LastCoperturaLong with the OpenTime of last processed order...

                     Alert("in uscita ","OPT ",OrderOpenTime()," Last ",LastCoperturaLong);
                     
                    }
     }

  } //fine funzione ApriReteCopertura
 
eros eros:

Hi all,

I am a newbie, so please forgive me if my questions are very elementary.

I am writing a function that will scan all open orders and if conditions are met, for each of them opens an hedge grid every "x" pips (i.e. if I have a buy order, it creates SellStop orders every "x" pips).

The grid is opened correctly, BUT, everytime, altready processed orders are taken in account, so it creates again an hedge grid.

I  put a LastCoverageLong variable that stores the OpenOrderTime of last processed orders, and I check it when selection the orders to cover, but it get reset at the begin of the function... I can't understand why.

can you please help?

thanks a lot

eros


anyone can help? I think it is only related to where the variable is defined, but I tried different situations, with no success.
 
eros eros:

I  put a LastCoverageLong variable that stores the OpenOrderTime of last processed orders, and I check it when selection the orders to cover, but it get reset at the begin of the function... I can't understand why.

local variable is reset at the end on function loop.

 
Mohamad Zulhairi Baba:

local variable is reset at the end on function loop.

yes, that is exactly what is happening. so what should I do to prevent this? how to define and manage it as global variable?

If I define as global variable, should I pass it to the function when call it? and should the function return back a value?

 
eros eros:

yes, that is exactly what is happening. so what should I do to prevent this? how to define and manage it as global variable?

If I define as global variable, should I pass it to the function when call it? and should the function return back a value?

just to let you know, I solved using globlal variable, passing it to the function and expecting back a datetime result that is stored in the global variable.

this is working now, thanks

Reason: