Orders counting system!

 

Hii people, im trying to code a system that ill can insert a BUY & SELL rules and the rest will be managed by the "system",

i would love to get some help with this, so please just aboard if you interested :). 

 

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

# 1: 

-  We set the rules of the strategy as "Strategy_BuyRule" / "Strategy_SellRule",

   then we set the execution price as "OrderBuyAtPrice" / "OrderSellAtPrice", 

   then we can set the amount of that same order as "OrderAmount".

 

# 2:

-  We set the rules of the closure as "OrderBuyClosingRule" / "OrderSellClosingRule", then if "OrderCloseOnSettedRule" is true it will close by the setted closure rule.

-  If we set "OrderCloseOnReverse" to true it will close and reverse to other direction.

-  If we set "OrderCloseOnReversedCandle" to true it will close at the reversed candle.

 

# 3 (The way it all should work):

-  So.., we set the rules for the execution and for the closure,

   now.., we got to keep in mind that if we will want to use Stop/Limit orders it will will decrees from "OrdersBuyStop" / "OrdersBuyLimit" and add to "OrdersBuy", cause when the pending order becomes a market order it turns to OP_BUY..

   and lets say we will use StopLoss / TakeProfit, so we will need to make sure that if the StopLoss / TakeProfit hit we will decrees from "OrdersBuy" / "OrdersSell", cause the order is closed by StopLoss / TakeProfit..

 

-  This is where i left off cause of my low MQL skills..., some things are working but some of them don't..,

   i hope someone wanting something like this cause this will really ease things.., so lets gather together and make this thing! :)

   Thank you for your time and effort,

   

   *More ideas will be great!, im trying to make the ultimate EA Template that will really ease things up when you wanna automate your stuff or just check something on history by automation..

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 


 

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
extern int    Strategy_Type=1;                     //Strategy Type.
extern int    MagicNumber=777;                     //Unique MagicNumber.
extern int    Slippage=1.0;                        //Slippage. (in pips)

extern double Lot=0.01;                            //LotSize.

extern double StopLoss=0;                          //StopLoss. (in pips)
extern double TakeProfit=0;                        //TakeProfit. (in pips)

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
bool   OrderCloseOnReverse=false;                  //Close order on reversal strategy rule.
bool   OrderOncePerCandle=false;                   //Order once per candle.
bool   OrderOncePerVolume=false;                   //Order once per candle (Based on volume). - *Dont use on renko*
bool   OrderOnEachSameDirectionCandle=false;       //Order on each candle with the same direction of the rule.
bool   OrderCloseOnReversedCandle=false;           //Order close on reversed candle.
bool   OrderCloseOnSettedRule=false;               //Order close on setted rule.
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
string OrderExecute;
double OrderBuyAtPrice;
double OrderSellAtPrice;
double OrderBuyClosingRule;
double OrderSellClosingRule;
int    OrderAmount;

double Strategy_BuyRule;
double Strategy_SellRule;

bool   Strategy_BuyAction=false;
bool   Strategy_SellAction=false;

bool   OrdersBuyStop_Boost=false;
bool   OrdersBuyLimit_Boost=false;
bool   OrdersSellStop_Boost=false;
bool   OrdersSellLimit_Boost=false;

/*bool   BuyClosedOnTP=false;
bool   BuyClosedOnSL=false;
bool   SellClosedOnTP=false;
bool   SellClosedOnSL=false;*/


double ppp;
int    CurrentTime=Time[0];

int    OrdersBuy=0;
int    OrdersBuyStop=0;
int    OrdersBuyLimit=0;
int    OrdersSell=0;
int    OrdersSellStop=0;
int    OrdersSellLimit=0;
int    total_orders;
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII








//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
int init()
{
   if(Point==0.00001) {ppp=0.0001;Slippage*=10;} //5-6 digits Points fix
   else if(Point==0.001) ppp=0.01; //3 digits Points fix(for Yen based pairs)
   else ppp=Point; //Normal
   //
   return(0);
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
int start()
{
   StrategyRefreshData(); Strategy();
   //
   int OrdersAll=
   OrdersBuy+OrdersBuyStop+OrdersBuyLimit+
   OrdersSell+OrdersSellStop+OrdersSellLimit;
   Comment(
      "\n\nTOTAL: "+OrdersAll
      +"\n\nOrdersBuy: "+OrdersBuy
      +"\nOrdersBuyStop: "+OrdersBuyStop
      +"\nOrdersBuyLimit: "+OrdersBuyLimit
      +"\nOrdersSell: "+OrdersSell
      +"\nOrdersSellStop: "+OrdersSellStop
      +"\nOrdersSellLimit: "+OrdersSellLimit
      +"\n\nStrategy_BuyAction: "+Strategy_BuyAction
      +"\nStrategy_SellAction: "+Strategy_SellAction
      +"\n\nOrdersBuyStop_Boost: "+OrdersBuyStop_Boost
      +"\nOrdersBuyLimit_Boost: "+OrdersBuyLimit_Boost
      +"\nOrdersSellStop_Boost: "+OrdersSellStop_Boost
      +"\nOrdersSellLimit_Boost: "+OrdersSellLimit_Boost
      +"\n\nBuyClosedOnTP: "+BuyClosedOnTP
      +"\nBuyClosedOnSL: "+BuyClosedOnSL
      +"\nSellClosedOnTP: "+SellClosedOnTP
      +"\nSellClosedOnSL: "+SellClosedOnSL
   );
   //
   return(0);
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
int deinit()
{ 
   return(0);
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII









//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
void Strategy()
{
   ////////////////////////////////////////////////////////////////////
   //#Strategys:
   ////////////////////////////////////////////////////////////////////

   if(Strategy_Type==1){
   
      Strategy_BuyRule=(Open[1]<Close[1]);
      Strategy_SellRule=(Open[1]>Close[1]);
      //
      if((Strategy_BuyRule)&&Strategy_BuyAction==false){
         //Order's execution:
         OrderExecute=("Buy"); OrderBuyAtPrice=Ask; OrderAmount=1;
         //Order's closure:
         OrderCloseOnSettedRule=false; //OrderBuyClosingRule=???;
         //
         Strategy_BuyAction=true;
      }
      
      if((Strategy_SellRule)&&Strategy_SellAction==false){
         //Order's execution:
         OrderExecute=("Sell"); OrderSellAtPrice=Bid; OrderAmount=1;
         //Order's closure:
         OrderCloseOnSettedRule=false; //OrderSellClosingRule=???;
         //
         Strategy_SellAction=true;
      }
      
      
      //___________________
      //Order's management:
      StopLoss=0; TakeProfit=0;
      //Order's MM:
      Lot=0.01;
      //Order's closure:
      OrderCloseOnReverse=true; OrderCloseOnReversedCandle=false;
      //_______________________________________________________________
   }
   StrategyAct();
   ////////////////////////////////////////////////////////////////////
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
//II # Act of the strategy after the rules was setted.               II
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
void StrategyAct()
{
   //#Buy Strategy Act:
   if(Strategy_BuyAction||Strategy_SellAction){
   
      //OrderCloseOnReverse:
      if(OrderCloseOnReverse){
         if(OrderExecute=="Buy"){Order_Close(OP_SELL);}
         if(OrderExecute=="BuyStop"){Order_Close(OP_SELLSTOP);}
         if(OrderExecute=="BuyLimit"){Order_Close(OP_SELLLIMIT);}
         if(OrderExecute=="Sell"){Order_Close(OP_BUY);}
         if(OrderExecute=="SellStop"){Order_Close(OP_BUYSTOP);}
         if(OrderExecute=="SellLimit"){Order_Close(OP_BUYLIMIT);}
         
         if(Strategy_BuyAction){Strategy_SellAction=false;}
         if(Strategy_SellAction){Strategy_BuyAction=false;}
      }
      
      //Execute order by type:
      if(OrderExecute=="Buy"){Order(OP_BUY);}
      if(OrderExecute=="BuyStop"){Order(OP_BUYSTOP);}
      if(OrderExecute=="BuyLimit"){Order(OP_BUYLIMIT);}
      if(OrderExecute=="Sell"){Order(OP_SELL);}
      if(OrderExecute=="SellStop"){Order(OP_SELLSTOP);}
      if(OrderExecute=="SellLimit"){Order(OP_SELLLIMIT);}
   }
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
//II # Refreshing data                                               II
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
void StrategyRefreshData()
{
   int count,i,select;
   //
   if(OrderMagicNumber()==MagicNumber&&OrderSymbol()==Symbol()){
   //
      //Market Orders Management:
      if(OrdersTotal()>0){
      //
         for(count=0;count<OrdersTotal();count++){
         select=OrderSelect(count,SELECT_BY_POS);
         //
            if(Strategy_BuyAction||Strategy_SellAction){
            
               //Switching Stop&Limit orders in to market order:
               if(OrdersBuyStop_Boost){OrdersBuyStop_Boost=false;for(i=1;i<=OrderAmount;i++){OrdersBuyStop--;OrdersBuy++;}}
               if(OrdersBuyLimit_Boost){OrdersBuyLimit_Boost=false;for(i=1;i<=OrderAmount;i++){OrdersBuyLimit--;OrdersBuy++;}}
               if(OrdersSellStop_Boost){OrdersSellStop_Boost=false;for(i=1;i<=OrderAmount;i++){OrdersSellStop--;OrdersSell++;}}
               if(OrdersSellLimit_Boost){OrdersSellLimit_Boost=false;for(i=1;i<=OrderAmount;i++){OrdersSellLimit--;OrdersSell++;}}
            
               //Close order on setted rule:
               if(OrderCloseOnSettedRule){
                  if(OrderBuyClosingRule||OrderSellClosingRule){
                     if(OrderExecute=="Buy"){Order_Close(OP_BUY);}
                     if(OrderExecute=="BuyStop"){Order_Close(OP_BUYSTOP);}
                     if(OrderExecute=="BuyLimit"){Order_Close(OP_BUYLIMIT);}
                     if(OrderExecute=="Sell"){Order_Close(OP_BUY);}
                     if(OrderExecute=="SellStop"){Order_Close(OP_SELLSTOP);}
                     if(OrderExecute=="SellLimit"){Order_Close(OP_SELLLIMIT);}
                     
                     if(Strategy_BuyAction){Strategy_BuyAction=false;}
                     if(Strategy_SellAction){Strategy_SellAction=false;}
                  }
               }
               
               //Close order on reversal candle:
               if(OrderCloseOnReversedCandle){
                  if((Open[1]<Close[1])||(Open[1]>Close[1])){
                     if(OrderExecute=="Buy"){Order_Close(OP_BUY);}
                     if(OrderExecute=="BuyStop"){Order_Close(OP_BUYSTOP);}
                     if(OrderExecute=="BuyLimit"){Order_Close(OP_BUYLIMIT);}
                     if(OrderExecute=="Sell"){Order_Close(OP_BUY);}
                     if(OrderExecute=="SellStop"){Order_Close(OP_SELLSTOP);}
                     if(OrderExecute=="SellLimit"){Order_Close(OP_SELLLIMIT);}
                     
                     if(Strategy_BuyAction){Strategy_BuyAction=false;}
                     if(Strategy_SellAction){Strategy_SellAction=false;}
                  }
               }
            }
         }
      }
   }
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII








//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
//II # Order                                                         II
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
void Order(int type_of_order)
{
   int Ticket,i;
   double SL,TP;
   //--
   
   if(type_of_order==OP_BUY||type_of_order==OP_BUYSTOP||type_of_order==OP_BUYLIMIT){
      for(i=1;i<=OrderAmount;i++){
      //#Order Buy:
      SL=(OrderBuyAtPrice)-StopLoss*ppp; TP=(OrderBuyAtPrice)+TakeProfit*ppp;
      Ticket=OrderSend(Symbol(),type_of_order,Lot,OrderBuyAtPrice,Slippage,SL,TP," ",MagicNumber,0,DodgerBlue);
      //
      if(type_of_order==OP_BUY){OrdersBuy++;}
      if(type_of_order==OP_BUYSTOP){OrdersBuyStop++;OrdersBuyStop_Boost=true;}
      if(type_of_order==OP_BUYLIMIT){OrdersBuyLimit++;OrdersBuyLimit_Boost=true;}
      }
   }
   
   //#Order Sell:
   if(type_of_order==OP_SELL||type_of_order==OP_SELLSTOP||type_of_order==OP_SELLLIMIT){
      for(i=1;i<=OrderAmount;i++){
      //
      SL=(OrderSellAtPrice)+StopLoss*ppp; TP=(OrderSellAtPrice)-TakeProfit*ppp;
      Ticket=OrderSend(Symbol(),type_of_order,Lot,OrderSellAtPrice,Slippage,SL,TP," ",MagicNumber,0,Red);
      //
      if(type_of_order==OP_SELL){OrdersSell++;}
      if(type_of_order==OP_SELLSTOP){OrdersSellStop++;OrdersSellStop_Boost=true;}
      if(type_of_order==OP_SELLLIMIT){OrdersSellLimit++;OrdersSellLimit_Boost=true;}
      }
   }
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
//II # Order_Close                                                   II
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
int Order_Close(int type_of_order)
{
   int total=OrdersTotal();
   int close;
   int del;
   //--
   for(int count=total-1;count>=0;count--){
      int select=OrderSelect(count,SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()==MagicNumber&&OrderSymbol()==Symbol()){
      
         //#Close Order Buy:
         if(type_of_order==OP_BUY){
            close=OrderClose(OrderTicket(),OrderLots(),Bid,100);
            if(!close){Print("#ERROR!: Order ("+OrderType()+") - Close<0.");
            OrdersBuy--;
         }
         //#Close Order BuyStop:
         if(type_of_order==OP_BUYSTOP){
            del=OrderDelete(OrderTicket());
            if(!del){Print("#ERROR!: Order ("+OrderType()+") - Delete<0.");
            OrdersBuyStop--;
         }
         //#Close Order BuyLimit:
         if(type_of_order==OP_BUYLIMIT){
            del=OrderDelete(OrderTicket());
            if(!del){Print("#ERROR!: Order ("+OrderType()+") - Delete<0.");}
            OrdersBuyLimit--;
         }
         
         
         //#Close Order Sell:
         if(type_of_order==OP_SELL){
            close=OrderClose(OrderTicket(),OrderLots(),Bid,100);
            if(!close){Print("#ERROR!: Order ("+OrderType()+") - Close<0.");}
            OrdersSell--;
         }
         //#Close Order SellStop:
         if(type_of_order==OP_SELLSTOP){
            del=OrderDelete(OrderTicket());
            if(!del){Print("#ERROR!: Order ("+OrderType()+") - Delete<0.");}
            OrdersSellStop--;
         }
         //#Close Order SellLimit:
         if(type_of_order==OP_SELLLIMIT){
            del=OrderDelete(OrderTicket());
            if(!del){Print("#ERROR!: Order ("+OrderType()+") - Delete<0.");}
            OrdersSellLimit--;
         } 
      }
   }
   return(0);
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
 
Karish:

Hii there, please help me with this i got flooded many times when it happen, i need some kind of filter that will help me to do each function just 1 time only... without any FLAGs please just by Time is this possable..?, thanks/

 

<link removed by moderator>

Please edit your post, and use the SRC button to publish your code.
 
updated, sorry.
Reason: