Open Forex4you Account

Server รัน EA 1000 / ปี

สั่งชื้อคู่มือการโปรแรกม MQL4

Author Topic: ชี้แนะการปิดออร์เดอร์ที่กำไร 5 ออร์เดอร์และออร์เดอร์ที่ขาดทุนเยอะสุดให้ด้วยครับ  (Read 13609 times)

gunlanutt

  • Newbie
  • *
  • Posts: 46
รบกวนทุกท่านด้วยครับ ผมหาวิธีการคัตออร์เดอร์ที่เป็นบวกจำนวน 5 ออร์เดอร์และที่ติดลบไม้ตรงกันข้ามเยอะสุด 1 ออร์เดอร์ครับ แต่ไม่รู้จะใช้วิธีไหนดีครับ

หลักการของอีเอตัวนี้ก็คือเปิดออร์เดอร์พร้อมกันทั้ง บายและเซล เมื่อราคาวิ่งขึ้นไปตามระยะที่กำหนดก็เปิดเพิ่มเป็นคู่ไปเรื่อย ๆ ครับ ไม่มี sl tp
ที่ต้องการก็คืออย่างเช่น เมื่อราคาวิ่งขึ้นไป ทำให้ ออร์เดอร์บาย 5 ออร์เดอร์บวก จึงต้องการปิดกำไร และตัดออร์เดอร์เซลที่ติดลบเยอะที่สุดออกหนึ่งออร์เดอร์ครับ
ผมต้องเขียนโค๊ดอย่างไรครับ ผมแนบไฟล์ที่ผมลองงมเขียนในการเปิดออร์เดอร์มาด้วยครับและแนบรูป



//+------------------------------------------------------------------+
//|                                                                                       |
//|                        Copyright  2018 MetaQuotes Software Corp. |
//|                                                     xxxxxxxxx@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright ฉ 2017, MetaQuotes Software Corp."
#property link      "www.urbanforex.com"

extern string  EA_Name        = "";
extern int     MaxOrder       = 5;
extern int     Pip_pendig     = 100;
extern double  Lots           = 0.1;
extern int     Pips           = 100;

extern int     MagicNumber    = 111;

int         slippage = 0;
int         OpenOrders=0, cnt=0;
int         mode=0, myOrderType=0;
double      sl=0, tp=0;
double      BuyPrice=0, SellPrice=0;
double      mylotsi=0;
double      TP_Buy, TP_Sell;
double      Sum_Profit_Sell, Sum_Profit_Buy;

int      OrderBuy, OrderSell;
int      OrderBuyStop, OrderBuyLimit;
int      OrderSellStop, OrderSellLimit;
double   Order_Profit,DD;
double   LastPriceBuy=0, LastPriceSell=0;
double   LastPriceBuy2=0, LastPriceSell2=0;
double   LastPriceBuyF=0, LastPriceSellF=0;
int      result, ticket;
bool     res, tic;
int      order_total;
int      cs = 0;
int      cbl = 0, cbs = 0;
int      csl = 0, css = 0;
double   op_sl, op_ss, op_bl, op_bs;
int      ticket_sl, ticket_ss;
int      ticket_bl, ticket_bs;
int      ticket2;
double   TicketBuy;
double   TicketSell;
double   Sicket, Sprofit;
double   Bicket, Bprofit;
bool     Tic;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
//
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   //DeleteObject();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
ShowComment();
//------------------------------------------------------------------------------------------------------------------------------
   OrderSell=0;
   OrderBuy=0;
   OrderBuyStop = 0;
   OrderBuyLimit = 0;
   OrderSellStop = 0;
   OrderSellLimit = 0;
   Order_Profit = 0;
   OpenOrders=0;
   LastPriceBuy = 0;
   LastPriceSell= 0;
   LastPriceBuy2 = 999;
   LastPriceSell2= -999;
   LastPriceBuyF = -999;
   LastPriceSellF= 999;
   Sum_Profit_Sell = 0;
   Sum_Profit_Buy = 0;
for(cnt=0;cnt<OrdersTotal();cnt++)   
   {
     int result1 = OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
     if (OrderSymbol()==Symbol())
     {            
          OpenOrders++;
          Order_Profit   = Order_Profit+OrderProfit();
     }
     if (OrderSymbol()==Symbol() && OrderType()==OP_SELL)
     {            
          OrderSell++;
        Sum_Profit_Sell  = Sum_Profit_Sell+OrderProfit()+OrderSwap()+OrderCommission();
        LastPriceSell = OrderOpenPrice();
          //Print("Ticket Sell #"+OrderSell+" : "+TicketSell[OrderSell]);
          if(OrderOpenPrice()>LastPriceSell2)
              {
                 LastPriceSell2 = OrderOpenPrice();
                 TicketSell     = OrderTicket();
              }
          if(OrderOpenPrice()<LastPriceSellF)
              {
                 LastPriceSellF = OrderOpenPrice();
                 TicketSell     = OrderTicket();
              }
     }
    
    
     if (OrderSymbol()==Symbol() && OrderType()==OP_BUY)
     {            
          OrderBuy++;
        Sum_Profit_Buy  = Sum_Profit_Buy+OrderProfit()+OrderSwap()+OrderCommission();
        LastPriceBuy = OrderOpenPrice();
          //Print("Ticket Buy #"+OrderBuy+" : "+TicketBuy[OrderBuy]);
         
          if(OrderOpenPrice()<LastPriceBuy2)
              {
                 LastPriceBuy2 = OrderOpenPrice();
                 TicketBuy     = OrderTicket();
              }
          if(OrderOpenPrice()>LastPriceBuyF)
              {
                 LastPriceBuyF = OrderOpenPrice();
                 TicketBuy     = OrderTicket();
              }
    
     }
   }     
//-------------------------------------------------------------------- 
         
//--------------------------------------------------------------------Setting SellLimit Pending-----------------------------------------------
   order_total = OrdersTotal();
   cs = false;
   csl = false;
   css = false;
   cbl = false;
   cbs = false;
   for(int i = order_total; i >= 0; i--)
   {
      if(OrderSelect(i,SELECT_BY_POS) == true && OrderSymbol() == Symbol())
      {
         if(OrderType() == OP_SELL)
         {
            cs = true;
         }
         if(OrderType() == OP_SELLLIMIT)
         {
            op_sl = NormalizeDouble(OrderOpenPrice(), Digits);
            ticket_sl = OrderTicket();
            csl = true;
         }
         if(OrderType() == OP_SELLSTOP)
         {
            op_ss = NormalizeDouble(OrderOpenPrice(), Digits);
            ticket_ss = OrderTicket();
            css = true;
         }
         if(OrderType() == OP_BUYLIMIT)
         {
            op_bl = NormalizeDouble(OrderOpenPrice(), Digits);
            ticket_bl = OrderTicket();
            cbl = true;
         }
         if(OrderType() == OP_BUYSTOP)
         {
            op_bs = NormalizeDouble(OrderOpenPrice(), Digits);
            ticket_bs = OrderTicket();
            cbs = true;
         }
      }
   }
     
//-------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------- 
//-------------------------------------------------------------------------------------------------------
   if(cs==false)
   {
      ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,2,0,0,"",0,0,clrRed);
      ticket = OrderSend(Symbol(),OP_BUY,Lots,Bid,2,0,0,"",0,0,clrBlue);
      return(0);
   }

//+-----------------------------------------Follow-------------------------+
        if (Ask-LastPriceBuy>=Pips*Point && Ask>=LastPriceBuyF+Pips*Point)
        {      
          BuyPrice=Ask;
          mylotsi=Lots;
          ticket2 = OrderSend(Symbol(),OP_BUY,mylotsi,BuyPrice,slippage,0,0,EA_Name,MagicNumber,0,clrBlue);
          return(0);
        }   


        if (LastPriceSell-Bid>=Pips*Point && Bid<=LastPriceSellF-Pips*Point)
        {      
          SellPrice=Bid;      
          mylotsi=Lots;
          ticket2 = OrderSend(Symbol(),OP_SELL,mylotsi,SellPrice,slippage,0,0,EA_Name,MagicNumber,0,clrRed);
          return(0);
        }   

//------------------------------------Martingale--------------------------
        if (LastPriceBuy-Ask>=Pips*Point && Ask<=LastPriceBuy2-Pips*Point)
        {      
          BuyPrice=Ask;
          mylotsi=Lots;
          ticket2 = OrderSend(Symbol(),OP_BUY,mylotsi,BuyPrice,slippage,0,0,EA_Name,MagicNumber,0,clrNavy);
          return(0);
        }   


        if (Bid-LastPriceSell>=Pips*Point && Bid>=LastPriceSell2+Pips*Point)
        {      
          SellPrice=Bid;   
          mylotsi=Lots;
          ticket2 = OrderSend(Symbol(),OP_SELL,mylotsi,SellPrice,slippage,0,0,EA_Name,MagicNumber,0,clrOrangeRed);
          return(0);
        }   

//-----------------------------------------------------------------------------------------------------

if(OrderSell>0)
  {
   Sicket=-1;
   Sprofit=-9999999;
   Bprofit= 9999999;
   for(cnt=0;cnt<OrdersTotal();cnt++)   
      {
        int result2 = OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
        if (OrderSymbol()==Symbol() && OrderType()==OP_BUY)
        {            
               if (OrderProfit()<Bprofit)
                  {
                     Bicket = OrderTicket();
                     Bprofit = OrderProfit();
                  }
        }
        if (OrderSymbol()==Symbol() && OrderType()==OP_SELL)
        {            
               if (OrderProfit()>Sprofit)
                  {
                     Sicket = OrderTicket();
                     Sprofit = OrderProfit();
                  }
        }
      }
}
       
//------------------------------------------------------------------------------       
if(OrderSell>=MaxOrder)
   {
   for(cnt=MaxOrder;cnt>=0;cnt--)   
      {
                    if(Sicket>0 && Sprofit>0)
                       {
                        if(OrderSelect(Sicket,SELECT_BY_TICKET))
                          {
                           RefreshRates();
                           if(OrderType()==OP_SELL)
                              Tic=OrderClose(OrderTicket(),OrderLots(),Ask,3,clrNONE);
                          }
                       }
      }
   }
                       
                 if(Bicket>0)
                    {
                     if(OrderSelect(Bicket,SELECT_BY_TICKET))
                       {
                        RefreshRates();
                        //if(OrderType()==OP_BUY)
                           //Tic=OrderClose(Bicket,OrderLots(),Bid,3,clrNONE);
                       }
                    }   
     
//----------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------

//----
   return(0);
  }
//+------------------------------------------------------------------+//