Have difficulty with advanced order management.

 

Hi all,

i have  difficulty to code my exigency.

Let assume that i have multiple buy position opened. 

If the ea want open  a new buy position i want that before it verify that some of the all other open buy position haven't the same or + o - the same open price level respect the actual ask;

Hope to be clarify,

Thank you 

 
So go through an orderSelect loop and compare each open price with the new one.
 

I already had codes which does something like what you're looking for. So here you go. Simi-Tested on 4-digit Broker.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    int Magic=7; string Symb=Symbol();
    Buy_Order_Origination(Magic,Symb);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void Buy_Order_Origination(int Magic, string Symb){
    double Aski=MarketInfo(Symb,MODE_ASK);
    if(isOnp_Exist(Magic,Symb,OP_BUY,Aski,0)){return;}
    double BrkMinLot=MarketInfo(Symb,MODE_MINLOT);
    Order_Send(Magic,Symb,OP_BUY,Aski,BrkMinLot);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool isOnp_Exist(int Mg, string Sy, int Ty, double P, int Dif){
    for(int i=OrdersTotal()-1; i>=0; i--){
        if(!OrderSelect(i,SELECT_BY_POS)){continue;}
        if(OrderMagicNumber()!=Mg){continue;} 
        if(OrderSymbol()!=Sy){continue;}
        if(OrderType()!=Ty){continue;}
        int iP=p2i(Sy,P);
        int Onp=p2i(Sy,OrderOpenPrice());
        int Difference=MathAbs(Onp-iP);
        if(Difference<=Dif){return(true);}
}   }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int p2i(string Symb, double X){
    /*Converts Price_2_Integer For Comparing With Equal Sign*/
    if(Digits%2==0){
        if(StringFind(Symb,"JPY")>-1){int Y=100;}else{Y=10000;}
    }else{
        if(StringFind(Symb,"JPY")>-1){Y=1000;}else{Y=100000;}
    }return(X*Y);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int Order_Send(int Magic, string Symb,
    int OType, double Price, double Lots){
    int Order_Slip=0; color OrColor;
    if(OType==OP_BUY){OrColor=Blue;}
    if(OType==OP_SELL){OrColor=Red;}
    int OrTicket=OrderSend(
        Symb,OType,Lots,Price,Order_Slip,
        0,0,"Magic="+Magic,Magic,0,OrColor);
return(OrTicket);    }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Thank you all.

And if i need to have the magic number of the order that have the highest openprice ??

How can i have this return?

Thank you 

 

These are the easier stuff to learn. I recommend trying instead of asking for every little function.

Print("Magic_Of_Highest_OpenPrice="+Magic_HiOnp());

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int Magic_HiOnp(){//If you need to filter by Symb/Type then Add em.
    double HiOnp; int ReturnMg;
    for(int i=OrdersTotal()-1; i>=0; i--){
        if(!OrderSelect(i,SELECT_BY_POS)){continue;}
        //if(OrderMagicNumber()!=Mg){continue;} 
        //if(OrderSymbol()!=Sy){continue;}
        //if(OrderType()!=Ty){continue;}
        if(OrderOpenPrice()<HiOnp){continue;}
        HiOnp=OrderOpenPrice();
        ReturnMg=OrderMagicNumber();
    }return(ReturnMg);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reason: