Open Forex4you Account

Server รัน EA 1000 / ปี

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

Author Topic: สอบถามเรื่องการเว้นระยะห่างPendingOrderครับ  (Read 13044 times)

akalak

  • Newbie
  • *
  • Posts: 12


ติดตรงจุดนี้ครับ แล้วในกรณีที่มันห่างไม่ว่าAsk จะบวกลบลงไปต่ำกว่า ระยะที่เราต้องการ ต้องเพิ่มคำสั่ง else เข้าไปด้วยไหมครับ

void OpenBuy(){

double sl = Ask-SL*Point;
double tp = Ask+TP*Point;

   OrderSend(Symbol(),OP_BUYSTOP, Lotsize, Ask+ Distance*Point, 3, Ask+ Distance*Point-SL*Point,Ask+Distance*Point+TP*Point, "", MagicNumber, 0, Blue);
        if(OrdersTotal()>0){
        double LastOpenPrice;
            OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);
               LastOpenPrice = OrderOpenPrice();
                  if( LastOpenPrice-Ask>Point*OrderDistand){
                     OrderSend(Symbol(),OP_BUYSTOP, Lotsize, Ask+ Distance*Point, 3, Ask+ Distance*Point-SL*Point,Ask+Distance*Point+TP*Point, "", MagicNumber, 0, Blue);
               
                  }     
        }
        return(0);
}

akalak

  • Newbie
  • *
  • Posts: 12
หรือถ้าผมใช้ void ไม่ต้องใส่ return ครับ เพราะไม่ใส่คำว่า return แล้วมันผ่าน

akalak

  • Newbie
  • *
  • Posts: 12
ผ่านแล้วครับ
แต่มันไม่เว้นระยะ pending order ให้ครับ
ต้องแก้ยังไงครับ
//------------------------------------------------------------------------------------------
void OpenBuy(){
double   sum;
int      count;
   for(int i=0;i<OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol()){
            sum=sum+OrderOpenPrice();
            count++;
         }
}
double sl = Ask-SL*Point;
double tp = Ask+TP*Point;
 
   OrderSend(Symbol(),OP_BUYSTOP, Lotsize, Ask+ Distance*Point, 3, Ask+ Distance*Point-SL*Point,Ask+Distance*Point+TP*Point, "", MagicNumber, 0, Blue);
        if(OrdersTotal()>0){
        double LastOpenPrice;
            OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);
               LastOpenPrice = OrderOpenPrice();
                  if( LastOpenPrice-Ask>Point*OrderDistand && LastOpenPrice-Ask<Point*OrderDistand ){
                     OrderSend(Symbol(),OP_BUYSTOP, Lotsize, Ask+ Distance*Point, 3, Ask+ Distance*Point-SL*Point,Ask+Distance*Point+TP*Point, "", MagicNumber, 0, Blue);
               
                  }     
        }
       
}
void OpenSell(){
double   sum;
int      count;
   for(int i=0;i<OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol()){
            sum=sum+OrderOpenPrice();
            count++;
         }
}
double sl = Bid+SL*Point;
double tp = Bid-TP*Point;
   OrderSend(Symbol(), OP_SELLSTOP, Lotsize, Bid-Distance*Point, 3, Bid- Distance*Point+SL*Point,Bid-Distance*Point-TP*Point, "", MagicNumber, 0, Red);
        if(OrdersTotal()>0){
        double LastOpenPrice;
            OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);
               LastOpenPrice =  OrderOpenPrice();
                  if( LastOpenPrice-Bid>Point*OrderDistand && LastOpenPrice-Bid<Point*OrderDistand ){
                     OrderSend(Symbol(),OP_SELLSTOP, Lotsize, Ask+ Distance*Point, 3, Ask+ Distance*Point-SL*Point,Ask+Distance*Point+TP*Point, "", MagicNumber, 0, Blue);
                     
                  } 
        }
 
}
 //----------------------------------------------------------------------------------------------------------
« Last Edit: กันยายน 18, 2016, 12:44:08 am by akalak »

admin

  • Administrator
  • Hero Member
  • *****
  • Posts: 2386
จะให้มันเว้นระยะยังไงครับไม่เห็นให้รายละเอียด ไวเลยครับ

akalak

  • Newbie
  • *
  • Posts: 12
อยากกำหนดให้เพ็นด้งออเดอร์มันเปิดห่างกันที่ละ50 แบบนี้ครับ

admin

  • Administrator
  • Hero Member
  • *****
  • Posts: 2386
โทษทีที่เข้ามาอ่ายช้า ตอนนี้ทำได้หรือยังครับ

akalak

  • Newbie
  • *
  • Posts: 12
ยังเลยครับพี่ พยายามแก้อยู่ แล้วก้คิดไว้อีกวิธีคิดให้โมดิไฟ ตัวเพ็นดิ้งตามราคาไปเลยครับเมื่อตัวเพ็นดิ้งมันห่างจากราคาปัจจุบัน ให้คล้ายเทลลิ้งสต็อบ

akalak

  • Newbie
  • *
  • Posts: 12
ตอนนี้ได้ประมาณนี้ครับ คิดไม่ออกจะใช้คำสั่งแบบไหน


//+------------------------------------------------------------------+
//|                                         O_BuyStop&O_SellStop.mq4 |
//|                                                           Akalak |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Akalak"
#property link      "https://www.mql5.com"
#property version   "3.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
extern int           MagicNumber = 1234;
extern double        Lotsize  = 0.01;
extern int           SL       = 200;
extern int           TP       = 1000;
extern bool          UseMoveToBreakevent  = True;
extern int           WhenTomoveToBe       = 110;
extern int           PipsToLockIn         = 30;
extern bool          UseTraillingStop     = True;
extern int           WhenToTrail          = 110;
extern int           TrailAmount          = 110;
extern int           MaxOrder             = 0;
extern int           Distance             = 110;
extern int           OrderDistand         = 110;

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


double Fma,Mma,Sma,FmaSh,MmaSh,SmaSh;
void GetMA(){

      Fma = iMA(Symbol(),PERIOD_H4,5,0,MODE_EMA,PRICE_WEIGHTED,0);
//    Mma = iMA(Symbol(),0,20,0,MODE_EMA,PRICE_WEIGHTED,0);
      Sma = iMA(Symbol(),PERIOD_H4,20,0,MODE_EMA,PRICE_WEIGHTED,0);
//    FmaSh = iMA(Symbol(),0,5,0,MODE_EMA,PRICE_WEIGHTED,1);
//    MmaSh = iMA(Symbol(),0,20,0,MODE_EMA,PRICE_WEIGHTED,1);
//    SmaSh = iMA(Symbol(),0,50,0,MODE_EMA,PRICE_WEIGHTED,1);
   
}
//-------------------------------------------------------------------------
void OpenBuy(){
   
       OrderSend(Symbol(), OP_BUYSTOP,Lotsize , Ask+Distance*Point, 3, Ask+Distance*Point-SL*Point,Ask+Distance*Point+TP*Point, "", MagicNumber, 0, Blue);
       
}
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
void OpenSell(){
   
     OrderSend(Symbol(), OP_SELLSTOP, Lotsize, Bid-Distance*Point, 3, Bid- Distance*Point+SL*Point,Bid-Distance*Point-TP*Point, "", MagicNumber, 0, Red);
     
}   
//-----------------------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------------------
void DeleteOrder(int type){
   for(int i= OrdersTotal()-1;i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS)==True){
         if(OrderMagicNumber() == MagicNumber && OrderSymbol()== Symbol()){
            OrderDelete(OrderTicket());
       
     
         }
      }   
   }
}
//----------------------------------------------------------------------------------------------------------
void CloseOrderAll(int type){
   for(int i= OrdersTotal()-1;i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS)==True){
         if(OrderMagicNumber() == MagicNumber && OrderSymbol()== Symbol()){
            switch(type){
            case OP_BUY:
                  OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0,0);
           
                     break;
           
            case OP_SELL:
                  OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),0,0);
           
                     break;
           
         
            }
         }
      }   
   }
}
//----------------------------------------------------------------------------------------------------------
void CloseOrderBuy(int type){
   for(int i= OrdersTotal()-1;i>=0;i--){
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderType() ==OP_BUY && OrderSymbol()== Symbol()){
      OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0);
      }
   }
}
//----------------------------------------------------------------------------------------------------------
void CloseOrderSell(int type){
   for(int i= OrdersTotal()-1;i>=0;i--){
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderType() ==OP_SELL && OrderSymbol()== Symbol()){
      OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),0);
      }
   }
}             
   
//-----------------------------------------------------------------------------------------------------
int CountOrder(int type){
   int CntOrder = 0;
      for(int i=MaxOrder;i<OrdersTotal();i++){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         
         if(OrderType() == type && OrderMagicNumber() == MagicNumber){
            if(OrderSymbol() == Symbol()){
               CntOrder++;
            }
         }   
      }
      return(CntOrder);
}
//--------------------------------------------------------------------------------------
void MoveToBreakevent(){
   for(int i=OrdersTotal()-1;i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderMagicNumber() == MagicNumber){
            if(OrderSymbol() == Symbol()){
               if(OrderType() == OP_BUY){
                  if(Bid-OrderOpenPrice() > WhenTomoveToBe*Point ){
                     if(OrderOpenPrice() > OrderStopLoss()){
                        OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+PipsToLockIn*Point,OrderTakeProfit(),0,Green);
                     }
                  }
               }
               if(OrderType() == OP_SELL){
                  if(OrderOpenPrice()-Ask > WhenTomoveToBe*Point){
                     if(OrderOpenPrice() < OrderStopLoss()){
                        OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-PipsToLockIn*Point,OrderTakeProfit(),0,Red);
                     }
                  }
               }
            }
         }
      }   
   }
}
//-----------------------------------------------------------------------------------------------------------
void MoveToOrderOpenPrice(){
   for(int i=OrdersTotal()-1;i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderMagicNumber() == MagicNumber){
            if(OrderSymbol() == Symbol()){
               if(OrderType() == OP_BUYSTOP){
                  if(Bid-OrderOpenPrice() > OrderDistand*Point ){
                     if(OrderOpenPrice() > OrderStopLoss()){
                        OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+PipsToLockIn*Point,OrderTakeProfit(),0,Green);
                     }
                  }
               }
               if(OrderType() == OP_SELLSTOP){
                  if(OrderOpenPrice()-Ask > OrderDistand*Point){
                     if(OrderOpenPrice() < OrderStopLoss()){
                       OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-PipsToLockIn*Point,OrderTakeProfit(),0,Red);
                     }
                  }
               }
            }
         }
      }   
   }
}
//------------------------------------------------------------------------------------------
void AdjustTrail(){
   for(int i=OrdersTotal()-1;i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderMagicNumber() == MagicNumber){
            if(OrderSymbol() == Symbol()){
               if(OrderType() == OP_BUY){
                  if(Bid-OrderOpenPrice() > WhenToTrail*Point){
                     if(OrderStopLoss()<Bid-TrailAmount*Point){
                        OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailAmount*Point,OrderTakeProfit(),0,0);
                     }
                  }
               }
               if(OrderType() == OP_SELL){
                  if(OrderOpenPrice()- Ask > WhenToTrail*Point){
                     if(OrderStopLoss()>Ask+TrailAmount*Point){
                        OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailAmount*Point,OrderTakeProfit(),0,0);
                     }
                  }
               }
            }
         }
      }   
   }
}


//-----------------------------------------------------------------------------------------------------------
void OnTick(){
   if(UseMoveToBreakevent)MoveToBreakevent();
   if(UseTraillingStop)AdjustTrail();
   GetMA();
   if(CountOrder(OP_BUYSTOP)==0 ){     
      if(Fma > Sma ){
         OpenBuy();
        // MoveToOrderOpenPrice();
        // DeleteOrder(OP_SELLSTOP);
         CloseOrderSell(OP_SELL);
         
          }       
     } 
     
   if(CountOrder(OP_SELLSTOP)==0 ){
      if(Fma < Sma ){
         OpenSell();
      // MoveToOrderOpenPrice();
      //   DeleteOrder(OP_BUYSTOP);
         CloseOrderBuy(OP_BUY);
         
          }
     }         


//+-------------------------------------------------------------------------------------
« Last Edit: กันยายน 30, 2016, 12:33:16 pm by akalak »

akalak

  • Newbie
  • *
  • Posts: 12


แบ็คเทสออกมาดีครับแต่ยังไม่สมบูณ ตัวนี้พยายามจะทำให้มันตัวเพ็นดิ้งเลื่อนตามราคาไปครับ แต่ไม่ได้ แต่ถ้าทำให้เปิดเพ็นดิ้งใหม่ตามระยะที่กำหนดได้ก็ได้ครับ แต่ก็ติดตรงดีลีตออเดอร์ครับถ้าเปิดใช้งานมันลบทันทีเลย

D_Diamond

  • Hero Member
  • *****
  • Posts: 673
    • Expert Forex Trading
จะเลื่อน pending ก็แค่ใช้ ordermodify เทียบกับราคาปัจจุบันครับ

ต.ย.  OpenDistance คือตัวกำหนดระยะห่างจากราคาปัจจุบัน

         case OP_BUYSTOP:
                  if (  Ask + OpenDistance*point < OrderOpenPrice() )
                     OrderModify(OrderTicket(),Ask + OpenDistance*point, (Ask + OpenDistance*point) - StopLoss*point, OrderTakeProfit(), 0, Lime);
                  break;
         case OP_SELLSTOP:
                  if (  Bid - OpenDistance*point > OrderOpenPrice() )
                     OrderModify(OrderTicket(), Bid - OpenDistance*point , (Bid - OpenDistance*point) +StopLoss*point, OrderTakeProfit(), 0, Orange);
                  break;