Open Forex4you Account

Server รัน EA 1000 / ปี

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

Author Topic: อยากให้ EA 100pips_V3[1].1 เทรดแค่ครั้งละ10 ออเดอร์  (Read 7841 times)

joetan

  • Newbie
  • *
  • Posts: 9
อยากให้EAตามโค้ดข้างล่างนี้เทรดแค่ครั้งละ10ออเดอร์น่ะครับ  คืออีเอตัวนี้มันเปิดออเดอร์เร็วมาก(ผมใช้กับFxOpen) และมันจะเปิดแบบไม่หยุด บางครั้งเปิดทั้งบายและเซล บางครั้งเปิดจนมาร์จิ้นเกือยหมดถ้าช่วงไม่มีข่าวก้อพอทำกำไรแหละครับ
แต่อยากให้มันเปิดแค่ 10 ออเดอร์ก้อพอ ผมลองแกะโค้ดและลองใช้โค้ดข้างล่างนี้ใส่เข้าไปแต่ก้อไม่เป็นผล
 
int total;
   total=OrdersTotal();
   if(total<10)

ไม่ทราบว่าผมทำตรงไหนผิด

EA 100pips V3[1].1 (สำหรับFxOpen)
โค้ด

extern int     timeframe      = 5;
extern double  stopLoss       = 100;
extern double  lTakeProfit    = 20;
extern double  sTakeProfit    = 20;
extern double  lTrailingStop  = 15;
extern double  sTrailingStop  = 15;
extern color   clOpenBuy      = Blue;
extern color   clCloseBuy     = Aqua;
extern color   clOpenSell     = Red;
extern color   clCloseSell    = Violet;
extern color   clModiBuy      = Blue;
extern color   clModiSell     = Red;
extern string  Name_Expert    = "100 pips";
extern int     Slippage       = 2;
extern bool    UseSound       = true;
extern string  NameFileSound  = "shotgun.wav";
extern double  Lots           = 0.1;

int init(){return(0);}

int deinit(){return(0);}

int start(){
   
   
   if(Bars<100)   {Print("bars less than 100");return(0);}
   if(lTakeProfit<10){Print("TakeProfit less than 10");return(0);}
   if(sTakeProfit<10){Print("TakeProfit less than 10");return(0);}
   
   if(timeframe==0) {timeframe=Period();}
   double diClose0=iClose(Symbol(),timeframe,0);
   double diMA1=iMA(Symbol(),timeframe,7,0,MODE_SMA,PRICE_OPEN,0);
   double diClose2=iClose(Symbol(),timeframe,0);
   double diMA3=iMA(Symbol(),timeframe,6,0,MODE_SMA,PRICE_OPEN,0);

   if(AccountFreeMargin()<(1000*Lots)){
      Print("We have no money. Free Margin = ", AccountFreeMargin());
      return(0);  }
     
   if(!ExistPositions())  {
      if((diClose0<diMA1)) {
         OpenBuy();
         return(0);  }
      if ((diClose2>diMA3))   {
         OpenSell();
         return(0);  }  }
   
   TrailingPositionsBuy(lTrailingStop);
   TrailingPositionsSell(sTrailingStop);

return (0); }//end start

// - - - - - - FUNCTIONS - - - - - - -
 
bool ExistPositions()   {
   for(int i=0;i<OrdersTotal(); i++)  {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderComment()==Name_Expert) return(True);
         else return(false); }   }

void TrailingPositionsBuy(int trailingStop) {
   for(int i=0;i<OrdersTotal();i++) {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderComment()==Name_Expert) {
            if(OrderType()==OP_BUY) {
               if(Bid-OrderOpenPrice()>trailingStop*Point) {
                  if(OrderStopLoss()<Bid-trailingStop*Point || OrderStopLoss()==0)   {
                     ModifyStopLoss(Bid-trailingStop*Point); }}}}}}

void TrailingPositionsSell(int trailingStop) {
   for(int i=0;i<OrdersTotal();i++) {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderComment()==Name_Expert) {
            if(OrderType()==OP_SELL) {
               if(OrderOpenPrice()-Ask>trailingStop*Point) {
                  if(OrderStopLoss()>Ask+trailingStop*Point || OrderStopLoss()==0)  { 
                     ModifyStopLoss(Ask+trailingStop*Point);}}}}}}
 
void ModifyStopLoss(double ldStopLoss) {
   bool fm;
   fm = OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,0);
   if (fm && UseSound) PlaySound(NameFileSound);
}

void OpenBuy() {
   double ldLot, ldStop, ldTake;
   string lsComm;
   ldLot = GetSizeLot();
   ldStop = Ask-Point*stopLoss;
   ldTake = NormalizeDouble(GetTakeProfitBuy(),Digits);
   lsComm = GetCommentForOrder();
   OrderSend(Symbol(),OP_BUY,ldLot,NormalizeDouble(Ask,Digits),Slippage,ldStop,ldTake,lsComm,0,0,clOpenBuy);
   if (UseSound) PlaySound(NameFileSound);   }
 
void OpenSell() {
   double ldLot, ldStop, ldTake;
   string lsComm;
   ldLot = GetSizeLot();
   ldStop = Bid+Point*stopLoss;
   ldTake = NormalizeDouble(GetTakeProfitSell(),Digits);
   lsComm = GetCommentForOrder();
   OrderSend(Symbol(),OP_SELL,ldLot,NormalizeDouble(Bid,Digits),Slippage,ldStop,ldTake,lsComm,0,0,clOpenSell);
   if (UseSound) PlaySound(NameFileSound);   }
 
string GetCommentForOrder() { return(Name_Expert); }
double GetSizeLot() { return(Lots); }
double GetTakeProfitBuy() { return(Ask+lTakeProfit*Point); }
double GetTakeProfitSell() { return(Bid-sTakeProfit*Point); }


อ้อ! อีกอย่างหนึ่งก้อคืออยากให้มันเทรดโดยใช้เปอร์เซนต์เทรดจะได้ไหมครับ เช่นปรับล็อตในการเทรดตามจำนวนมาร์จิ้นที่เพิ่มขึ้น แต่ไม่เกิน10%ของมาร์จิ้นทั้งหมด 

admin

  • Administrator
  • Hero Member
  • *****
  • Posts: 2386
ผมแก้ในส่วน ฟังก์ชั่นชื้อขายนี้ลองดูว่าได้ไหมแต่ผมคิดว่าน่าจะได้


void OpenBuy() {
   double ldLot, ldStop, ldTake;
   string lsComm;
   ldLot = GetSizeLot();
 total=OrdersTotal();
 if(total>=10)
 return (0);
   ldStop = Ask-Point*stopLoss;
   ldTake = NormalizeDouble(GetTakeProfitBuy(),Digits);
   lsComm = GetCommentForOrder();
   OrderSend(Symbol(),OP_BUY,ldLot,NormalizeDouble(Ask,Digits),Slippage,ldStop,ldTake,lsComm,0,0,clOpenBuy);
   if (UseSound) PlaySound(NameFileSound);   }
 
void OpenSell() {
   double ldLot, ldStop, ldTake;
   string lsComm;
   ldLot = GetSizeLot();
 total=OrdersTotal();
if(total>=10)
return (0);
   ldStop = Bid+Point*stopLoss;
   ldTake = NormalizeDouble(GetTakeProfitSell(),Digits);
   lsComm = GetCommentForOrder();
   OrderSend(Symbol(),OP_SELL,ldLot,NormalizeDouble(Bid,Digits),Slippage,ldStop,ldTake,lsComm,0,0,clOpenSell);
   if (UseSound) PlaySound(NameFileSound);   }

joetan

  • Newbie
  • *
  • Posts: 9
ขอบคุณมากครับ เดี๋ยวลองเอาไปเทสก่อนนะครับ  ;D