天天看點

Ava Trader MT4 Terminal EA交易(4)注冊模拟賬号,寫第一個政策

注冊模拟号

https://myvip.avatrade.cn/
           

政策要求

監控一分鐘k線,如果連續兩根陽線,就 做多 設定 10 個點內插補點的 止盈跟 止損
           

建立ea模闆 first1

代碼如下

//+------------------------------------------------------------------+
//|                                                       first1.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, jiegemena."
#property link      "https://blog.csdn.net/jiegemena"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+


//--- input parameters
input int RedKNums = 2; // 連續幾根陽線
input double ClearLoss = 10; // 止損點位
input double ClearProfit = 10; // 止盈點位

int OId = 0;
string sign1 = "order1";

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- 每個交易資料回調
   DealWork();
  }
//+------------------------------------------------------------------+


// 檢測
void DealWork()
  {
   if(!CheckPosition1(OId))
     {
      Print("沒有持倉");
      int max0 = 0;
      for(int i=0; i<RedKNums; i++)
        {
         if(GetKGreen(i + 1))
           {
            max0++;
           }
        }

      if(max0 == RedKNums)
        {
         Print("符合條件買入");
         Buy();
         Sleep(60 * 1000);
        }

     }
   else
     {
      Print("有持倉");
     }
  }


// 檢查 前 i 分鐘 是否陽線
bool GetKGreen(int i)
  {
   double k = GetKPoint(i);
   if(k > 0)
     {
      Print("k 大",k,":", Time[i]);
      return true;
     }
   Print("k 小",k,":", Time[i]);
   return false;
  }

// 前 i 分鐘的 k線 內插補點
double GetKPoint(int i)
  {
   return Close[i]  - Open[i];
  }

// 做多
void Buy()
  {
   double BuyPri = Ask;

   double stoploss = BuyPri - ClearLoss*Point;
   double takeprofit = BuyPri + ClearProfit*Point;
   Print("購買");
   Print("目前:", BuyPri);
   Print("stoploss:", stoploss);
   Print("takeprofit:", takeprofit);
   OId = OrderSend(Symbol(),OP_BUY,1,BuyPri,3,stoploss,takeprofit,sign1,1314168,0,clrGreen);
   if(OId<0)
     {
      Print(" 交易失敗 OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully:", OId);
  }

// 做空
void Sell1()
  {
   double SellPri = Bid;

   double stoploss = SellPri + ClearProfit*Point;
   double takeprofit = SellPri - ClearLoss*Point;
   Print("購買");
   Print("目前:", SellPri);
   Print("stoploss:", stoploss);
   Print("takeprofit:", takeprofit);
   OId = OrderSend(Symbol(),OP_SELL,1,SellPri,3,stoploss,takeprofit,sign1,1314168,0,clrGreen);
   if(OId<0)
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully:", OId);
  }


// 笨方法檢測持倉
bool CheckPosition1(int oid)
  {
   string comment;
// 單号是否存在
   if(OrderSelect(oid,SELECT_BY_TICKET)==false)
     {
      Print("OrderSelect failed error code is",GetLastError());
      return false;
     }
   comment = OrderComment();
   if(comment == sign1)
     {
      return true;
     }
   return false;
  }
//+------------------------------------------------------------------+

           

點選編寫

Ava Trader MT4 Terminal EA交易(4)注冊模拟賬号,寫第一個政策

回到交易軟體,把first1 拖到行情視窗,設定一分鐘圖

Ava Trader MT4 Terminal EA交易(4)注冊模拟賬号,寫第一個政策

觀察圖示是否正常

Ava Trader MT4 Terminal EA交易(4)注冊模拟賬号,寫第一個政策

隻要有價格跳動,觀察日志,已經在正常運作

Ava Trader MT4 Terminal EA交易(4)注冊模拟賬号,寫第一個政策

程式符合條件

Ava Trader MT4 Terminal EA交易(4)注冊模拟賬号,寫第一個政策

正常設定了 10 個點的止盈止損

Ava Trader MT4 Terminal EA交易(4)注冊模拟賬号,寫第一個政策

繼續閱讀