Open Forex4you Account

Server รัน EA 1000 / ปี

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

Author Topic: อยากได้โค๊ด เงื่อนไขทุก 15 นาทีครับ  (Read 9998 times)

maticont

  • Newbie
  • *
  • Posts: 16
คือผมจะตั้ง pending limite ไว้  5 นาทียังไม่ชน ก็ให้มาตั้ง pending ใหม่ เลยอยากเอาเงื่อนไข อะไรก็ได้ที่ทุก 5 นาทีจะเกิดครั้ง เอามาลบ pending เดิม แล้วตั้งใหม่ครับ

admin

  • Administrator
  • Hero Member
  • *****
  • Posts: 2386
ตัวอย่างการใช้งาน  OnTimer()
int OnInit()
{
        EventSetTimer(1); // ใฃ้ประกาศค่าเวลาที่จะให้ EA ทำงานทุกๆกี่วินาที ในตัวอย่างคือ 1 วินาทีก็ต้องคูณให้ได้ 15 นาที
        return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
        EventKillTimer();
}
void OnTimer()
{
        // เขียนคำสั่ง Delete Pening  แล้วว่าง Pending ใหม่
}

maticont

  • Newbie
  • *
  • Posts: 16
ขอบคุณครับ แอดมิน

maticont

  • Newbie
  • *
  • Posts: 16
ผมลองทำแล้ว แต่ไม่รู้ทำใมมันไม่ตั้ง Pending ครับ แล้วก็ถ้ามันทำ trailingStop มันจะทำงานให้ทุกออเดอร์ รึป่าวครับ
//+------------------------------------------------------------------+
//|                                                         haha.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
extern double Lot=0.01;
extern int trailingStop =30;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer

        EventSetTimer(1);
        return(INIT_SUCCEEDED);
 
}


     
//---
 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
  EventKillTimer();
     
  }
 

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
 if(OrderType() == OP_BUY)
             {
               if(trailingStop > 0)
                 {
                   if(Bid - OrderOpenPrice() > trailingStop*Point)
                     {
                       if(OrderStopLoss()==0 || (Bid - OrderStopLoss()> trailingStop*Point))
                           OrderModify(OrderTicket(), OrderOpenPrice(),
                                       Bid - trailingStop*Point, OrderTakeProfit(), 0, Blue);
                     }
                 }
             }
           else
             {
               if(trailingStop > 0)
                 {
                   if(OrderOpenPrice() - Ask > trailingStop*Point)
                     {
                       if(OrderStopLoss() == 0 || OrderStopLoss()-Ask >  trailingStop*Point)
                           OrderModify(OrderTicket(), OrderOpenPrice(),
                                       Ask + trailingStop*Point, OrderTakeProfit(), 0, Red);
                     }           
                 }
             }
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
    int result = OrderDelete( OrderTicket() );
  int penbuy=OrderSend(Symbol(),OP_BUYLIMIT,Lot,Bid-Point*30,3,0,0,NULL,1,0,Green);
   int pensell=OrderSend(Symbol(),OP_SELLLIMIT,Lot,Ask+Point*30,3,0,0,NULL,1,0,Green);
  }
//+------------------------------------------------------------------+

admin

  • Administrator
  • Hero Member
  • *****
  • Posts: 2386
เอาฟังก์ชั่น OnTick()  ออกไปเลยครับถ้าจะใช้ระบบตั้งเวลาทุกอย่างต้องเขียนใน OnTimer()  แล้วตรง EventSetTimer(1);  ต้องแก้เป็น EventSetTimer(900);