Friday, December 16, 2022

forexpark average order

//+------------------------------------------------------------------+

//|                                                       ForexPerk-Copy.mq4 |

//|                        Copyright 2022, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

extern int MagicNumber=10001;

extern double Lots =0.01;

extern int Takeprofit=100;

extern int gap = 100;

extern int Slippage=3;

extern double Maxbuyposition=10;

extern double Maxsellposition=10;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+


int OpenBuyOrders;


int OpenSellOrders;


int total;

 

void OnTick()

 

int speared = SymbolInfoInteger(OrderSymbol(), SYMBOL_SPREAD);

double profit = AccountProfit();  

  OpenBuyOrders=0;


  OpenSellOrders=0;


  total=0;


//Count Pending Stop Orders


  for(int i=0; i<OrdersTotal(); i++ )


   {


    if(OrderSelect(i, SELECT_BY_POS)==true)


     {


      if (OrderType()==OP_BUY && OrderSymbol() == Symbol())


        OpenBuyOrders++;


      if (OrderType()==OP_SELL && OrderSymbol() == Symbol())


        OpenSellOrders++;


     }


    total=OpenBuyOrders + OpenSellOrders;


   }

//last close

datetime ctime = Time[0];

datetime lastclose = 0;

int c = OrdersHistoryTotal(); 


  int a = OrdersHistoryTotal() - 10;


  int b = OrdersHistoryTotal();


  for (c = a;c < b ;c++)

     {

           if (OrderSelect(c,SELECT_BY_POS, MODE_HISTORY) == true  && OrderMagicNumber()== MagicNumber)

              {      

               //  Alert("orders found"+ OrderTicket() + " " + OrderCloseTime());

             lastclose = OrderCloseTime();

              }

     }

 //lastprice

 double buyticket = 0;


double lastbuy = 0;


double buytarget = 0;


double buyprofit = 0;


datetime buydate = 0;


double sellticket = 0;


double lastsell = 0;


double selltarget = 0;


double sellprofit = 0;


datetime selldate = 0;

 for (int cntl = 0; cntl < OrdersTotal(); cntl++)


      {


if(OrderSelect(cntl,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber()== MagicNumber){


//buy details

if(OrderMagicNumber()== MagicNumber && OrderType() == OP_BUY && OrderSymbol() == Symbol()){


buyticket = OrderTicket();


lastbuy = OrderOpenPrice();


buydate = OrderOpenTime();


buyprofit = OrderProfit();

}


//sell details

if(OrderMagicNumber()==  MagicNumber && OrderType() == OP_SELL && OrderSymbol() == Symbol()){


sellticket = OrderTicket();


lastsell = OrderOpenPrice();


selldate = OrderOpenTime();


sellprofit = OrderProfit();

}

}}    

double buygap = lastbuy-gap*Point;

double sellgap = lastsell+gap*Point;

   //signal

string signal="";   

if ((OpenBuyOrders < Maxbuyposition) && (ctime > lastclose) && (ctime > OrderOpenTime()) && (Close[0] > High[1]))

{

//set the signal variable to buy

signal="buy";

}

//buy 2nd

if ((OpenBuyOrders < Maxbuyposition) && (ctime > lastclose) && (ctime > OrderOpenTime())&& (buygap > Ask) && (OrderOpenPrice() > Ask) && (Close[0] > High[1]))

{

//set the signal variable to buy

signal="buy";

}


//if the moving average above the current price

if ((OpenSellOrders < Maxsellposition) && (ctime > lastclose) && (ctime > OrderOpenTime()) &&  (Close[0] < Low[1]))

{

//set the signal variable to sell

signal="sell";

}  

//sell 2nd

if ((OpenSellOrders < Maxsellposition) && (ctime > lastclose) && (ctime > OrderOpenTime()) && (sellgap < Bid) && (OrderOpenPrice() < Bid)  &&  (Close[0] < Low[1]))

{

//set the signal variable to sell

signal="sell";

}   

   //

//average buy

double Lotsb   = 0.0;

   double OrderPriceb = 0.0;

   double LotsPriceb = 0.0;

   double averagebuy = 0.0;

   double buytar = 0.0;

   for (int ba = OrdersTotal() - 1; ba >= 0; ba--)

   {

      OrderSelect(ba, SELECT_BY_POS, MODE_TRADES);

      if (OrderSymbol() == Symbol() && OrderType() == OP_BUY)

      {

         Lotsb = Lotsb + OrderLots();

         LotsPriceb = LotsPriceb + OrderLots()*OrderOpenPrice();


         //Print("Lotsb : ", OrderLots(), "   OrderOpenPrice : ", OrderOpenPrice());

buytar = LotsPriceb/Lotsb+Takeprofit*Point;  

      }

    

   }   

//average sell

double Lotss   = 0.0;

   double OrderPrices = 0.0;

   double LotsPrices = 0.0;

   double averagesell = 0.0;

   double selltar = 0.0;

   for (int sa = OrdersTotal() - 1; sa >= 0; sa--)

   {

      OrderSelect(sa, SELECT_BY_POS, MODE_TRADES);

      if (OrderSymbol() == Symbol() && OrderType() == OP_SELL)

      {

         Lotss = Lotss + OrderLots();

         LotsPrices = LotsPrices + OrderLots()*OrderOpenPrice();


        // Print("Lotss : ", OrderLots(), "   OrderOpenPrice : ", OrderOpenPrice());

selltar = LotsPrices/Lotss-Takeprofit*Point;

      }

      

   }   

//

double buytp = Ask+Takeprofit*Point;

double selltp = Bid-Takeprofit*Point;

//double target = 1.00+(speared*0.01);

   Comment ("Speared",speared , " Equity ",profit," Ask ",Ask, " Bid ",Bid, " buygap ",buygap," sellgap ",sellgap,

   "\n Total buy ",OpenBuyOrders," Total sell ",OpenSellOrders," total ",total," order close ",OrderClosePrice()," order open ",OrderOpenPrice(),

   "\n current time " ,TimeCurrent(), " current open time ",ctime," last closed order ",lastclose, " buytp ",buytp, " selltp ",selltp,

   "\n last buy ",lastbuy, "average buy",averagebuy, " buy tar ",buytar," last sell ",lastsell, "average sell",averagesell," selltar ",selltar,

   "\n The current signal is : ",signal);

 int result=0;

double lots = Lots;//0;

//if(OrdersTotal() == 0){lots = Lots;}else{lots = Lots*(OrdersTotal()+1);}

if ((signal == "buy" && OpenBuyOrders < Maxbuyposition))//Here is your open Buy rule

     {Alert("open buy");

result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"100pip EA https://forexwithcandlestick.blogspot.com",MagicNumber,0,Green);

  if(result>0)

        {

         OrderSelect(result,SELECT_BY_TICKET);

         OrderModify(OrderTicket(),OrderOpenPrice(),0,0,0,Red);

        }      

     }

if ((signal == "sell" && OpenSellOrders < Maxsellposition)) //Here is your open Sell rule

     { Alert("open sell");

 result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"100pip EA https://forexwithcandlestick.blogspot.com ",MagicNumber,0,Red);

        if(result>0)

        {

         OrderSelect(result,SELECT_BY_TICKET);

         OrderModify(OrderTicket(),OrderOpenPrice(),0,selltp,0,Red);

        }

       

        

     } 

if(Ask < selltar && OrderClosePrice() < selltar)

{

for (int ss = OrdersTotal()-1; ss >= 0; ss--)

{    

if(OrderSelect(ss,SELECT_BY_POS) && OrderType() == OP_SELL && OrderSymbol() == Symbol()){

   ulong orderticket = OrderTicket();

   Print(orderticket);

   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,Red);

  }



}//order closed end


//

if(Bid > buytar && OrderClosePrice() > buytar)

{

for (int bb = OrdersTotal()-1; bb >= 0; bb--)

{    

if(OrderSelect(bb,SELECT_BY_POS) && OrderType() == OP_BUY && OrderSymbol() == Symbol()){

   ulong orderticketb = OrderTicket();

   Print(orderticketb);

  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,Red);

  }


}

}//buy closed  

 

 }