MQL5教程7 交易指令碼開發

很闊愛發表於2020-11-04
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
class jiaoyi
  {

public:
   //com 註釋
   int               buy(string symbol, double lots, int slPoint, int tpPoint, string com, int magic)
     {
      int a;
      MqlTradeRequest request = {0};
      MqlTradeResult result = {0};
      request.action = TRADE_ACTION_DEAL;
      request.symbol = symbol;
      request.type = ORDER_TYPE_BUY;
      request.volume = lots;
      request.deviation = 10;//滑點數
      request.price = SymbolInfoDouble(symbol,SYMBOL_ASK);
      request.sl = SymbolInfoDouble(symbol,SYMBOL_ASK)- slPoint*Point();
      request.tp = SymbolInfoDouble(symbol,SYMBOL_ASK)+tpPoint*Point();
      request.comment = com;
      request.magic = magic;
      //--- 傳送請求
      if(!OrderSend(request,result))
         PrintFormat("OrderSend error %d",GetLastError());     // 如果不能傳送請求,輸出錯誤程式碼
      //--- 操作資訊
      PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
      return result.order;//返回訂單號碼
     }


   //com 註釋
   int               sell(string symbol, double lots, int slPoint, int tpPoint, string com, int magic)
     {
      int a;
      MqlTradeRequest request = {0};
      MqlTradeResult result = {0};
      request.action = TRADE_ACTION_DEAL;
      request.symbol = symbol;
      request.type = ORDER_TYPE_SELL;
      request.volume = lots;
      request.deviation = 10;//滑點數
      request.price = SymbolInfoDouble(symbol,SYMBOL_ASK);
      request.sl = SymbolInfoDouble(symbol,SYMBOL_ASK)+ slPoint*Point();
      request.tp = SymbolInfoDouble(symbol,SYMBOL_ASK)-tpPoint*Point();
      request.comment = com;
      request.magic = magic;
      //--- 傳送請求
      if(!OrderSend(request,result))
         PrintFormat("OrderSend error %d",GetLastError());     // 如果不能傳送請求,輸出錯誤程式碼
      //--- 操作資訊
      PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
      return result.order;//返回訂單號碼
     }

   //平所有多單  市價單
   void              closeAllBuy(string symbol)
     {
      if(symbol == NULL)
        {
         symbol =Symbol();
        }
      int t = PositionsTotal();
      for(int i = t-1; i>=0; i--)
        {
         if(PositionGetTicket(i)>0)
           {
            if(PositionGetSymbol(i) == symbol && PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_BUY)
              {
               MqlTradeRequest request = {0};
               MqlTradeResult result = {0};
               request.action = TRADE_ACTION_DEAL;
               request.position = PositionGetTicket(i);
               request.type =ORDER_TYPE_SELL;
               request.volume = PositionGetDouble(POSITION_VOLUME);
               request.symbol = symbol;
               request.deviation = 10;
               request.price = SymbolInfoDouble(symbol, SYMBOL_BID);
               //--- 傳送請求
               if(!OrderSend(request,result))
                  PrintFormat("OrderSend error %d",GetLastError());     // 如果不能傳送請求,輸出錯誤程式碼
               //--- 操作資訊
               PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);

              }
           }
        }
     }

   //平所有空單  市價單
   void              closeAllSell(string symbol)
     {
      if(symbol == NULL)
        {
         symbol =Symbol();
        }
      int t = PositionsTotal();
      for(int i = t-1; i>=0; i--)
        {
         if(PositionGetTicket(i)>0)
           {
            if(PositionGetSymbol(i) == symbol && PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_SELL)
              {
               MqlTradeRequest request = {0};
               MqlTradeResult result = {0};
               request.action = TRADE_ACTION_DEAL;
               request.position = PositionGetTicket(i);
               request.type =ORDER_TYPE_BUY;
               request.volume = PositionGetDouble(POSITION_VOLUME);
               request.symbol = symbol;
               request.deviation = 10;
               request.price = SymbolInfoDouble(symbol, SYMBOL_ASK);
               //--- 傳送請求
               if(!OrderSend(request,result))
                  PrintFormat("OrderSend error %d",GetLastError());     // 如果不能傳送請求,輸出錯誤程式碼
               //--- 操作資訊
               PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);

              }
           }
        }
     }

   void              closeAll(string symbol)
     {
      this.closeAllBuy(symbol);
      this.closeAllSell(symbol);

     }

   void              mofiysltp(string symbol,ENUM_POSITION_TYPE type, double sl, double tp)
     {
      int t = PositionsTotal();
      for(int i = t-1; i>=0; i--)
        {
         if(PositionGetTicket(i)>0)
           {
            if(type == POSITION_TYPE_BUY)
              {
               //修改多單
               MqlTradeRequest request = {0};
               MqlTradeResult result = {0};
               request.action = TRADE_ACTION_SLTP;
               request.position = PositionGetTicket(i);
               request.type =ORDER_TYPE_BUY;
               request.volume = PositionGetDouble(POSITION_VOLUME);
               request.symbol = symbol;
               if(sl != NULL)
                 {
                  request.sl =NormalizeDouble(sl, Digits());
                 }
               if(tp != NULL)
                 {
                  request.tp = NormalizeDouble(tp, Digits());
                 }
               //--- 傳送請求
               if(!OrderSend(request,result))
                  PrintFormat("OrderSend error %d",GetLastError());     // 如果不能傳送請求,輸出錯誤程式碼
               //--- 操作資訊
               PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
              }
           }
        }


     }

   void              deleteGuadan(string symbol)
     {
      int t = OrdersTotal();
      for(int i = t-1; i>=0; i--)
        {
         if(OrderGetTicket(i)>0)
           {
            if(OrderGetString(ORDER_SYMBOL) == symbol)
              {
               MqlTradeRequest request = {0};
               MqlTradeResult result = {0};
               request.action = TRADE_ACTION_REMOVE;
               request.position = OrderGetTicket(i);
               request.symbol = symbol;

               //--- 傳送請求
               if(!OrderSend(request,result))
                  PrintFormat("OrderSend error %d",GetLastError());     // 如果不能傳送請求,輸出錯誤程式碼
               //--- 操作資訊
               PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
              }
           }
        }
     }
  };
//+------------------------------------------------------------------+

相關文章