禁忌石 / mt5 / MT5入門到精通之九(交易類部分)

分享

   

MT5入門到精通之九(交易類部分)

2019-05-02  禁忌石

一1.訂單號理解

Paste_Image.png

二、注意以下區別
1.開倉不需要特殊操作
2.平倉和修改持倉單止盈止損

request.position=PositionGetTicket(i);

3.刪除掛單

request.order=OrderGetTicket(i);

三交易類
1.實現

//+------------------------------------------------------------------+//|                                                        Trade.mqh |//|                        Copyright 2017, MetaQuotes Software Corp. |//|                                             https://www.mql5.com |//+------------------------------------------------------------------+#property copyright "Copyright 2017, MetaQuotes Software Corp."#property link      "https://www.mql5.com"#property version   "1.00"//+------------------------------------------------------------------+//|                                                                  |//+------------------------------------------------------------------+class Trade
  {private:public:   //slPoint = stop loss point止損點,tpPoint = take profit Point止盈點
   int buy(string symbol,double lots,int slPoint,int tpPoint,string comment,int magic) {
      MqlTradeRequest request={0};
      MqlTradeResult  result={0};      //市價單  request.action=TRADE_ACTION_DEAL;
      request.magic=magic;

      request.symbol=symbol;
      request.volume=lots;
      request.price=SymbolInfoDouble(symbol,SYMBOL_ASK);

      request.sl=SymbolInfoDouble(symbol,SYMBOL_ASK)-slPoint*Point();
      request.tp=SymbolInfoDouble(symbol,SYMBOL_ASK)+tpPoint*Point();      //滑點  request.deviation=100;      //多單  request.type=ORDER_TYPE_BUY;

      request.comment=comment;      //--- 發送請求  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);
     }   int sell(string symbol,double lots,int slPoint,int tpPoint,string comment,int magic) {
      MqlTradeRequest request={0};
      MqlTradeResult  result={0};      //市價單  request.action=TRADE_ACTION_DEAL;
      request.magic=magic;

      request.symbol=symbol;
      request.volume=lots;
      request.price=SymbolInfoDouble(symbol,SYMBOL_BID);

      request.sl=SymbolInfoDouble(symbol,SYMBOL_BID)+slPoint*Point();
      request.tp=SymbolInfoDouble(symbol,SYMBOL_BID)-tpPoint*Point();      //滑點  request.deviation=100;      //空單  request.type=ORDER_TYPE_SELL;

      request.comment=comment;      //--- 發送請求  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) {      //持倉單遍歷  int t=PositionsTotal();      for(int i=t-1;i>=0;i--)
        {         //選擇持倉單 if(PositionGetTicket(i)>0)
           {            if(PositionGetString(POSITION_SYMBOL)==symbol && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
              {
               MqlTradeRequest request={0};
               MqlTradeResult  result={0};
               request.action=TRADE_ACTION_DEAL;                     // 交易操作類型   request.symbol=symbol;                              // 交易品種   request.volume=PositionGetDouble(POSITION_VOLUME); //交易量   request.type=ORDER_TYPE_SELL;                        //訂單類型   request.price=SymbolInfoDouble(symbol,SYMBOL_BID); // 持倉價格                                                      request.deviation=100;//滑點   request.position=PositionGetTicket(i);//平倉最好都寫下,不然和sell一樣了。   if(!OrderSend(request,result))
                  PrintFormat("OrderSend error %d",GetLastError());   // 如果不能發送請求,輸出錯誤  }
           }
        }
     }   void closeAllSell(string symbol) {      //持倉單遍歷  int t=PositionsTotal();      for(int i=t-1;i>=0;i--)
        {         //選擇持倉單 if(PositionGetTicket(i)>0)
           {            if(PositionGetString(POSITION_SYMBOL)==symbol && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
              {
               MqlTradeRequest request={0};
               MqlTradeResult  result={0};
               request.action=TRADE_ACTION_DEAL;                     // 交易操作類型   request.symbol=symbol;                              // 交易品種   request.volume=PositionGetDouble(POSITION_VOLUME); // 交易量   request.type=ORDER_TYPE_BUY;                        // 訂單類型   request.price=SymbolInfoDouble(symbol,SYMBOL_ASK); // 持倉價格   request.deviation=100; //滑點   request.position =PositionGetTicket(i);               if(!OrderSend(request,result))
                  PrintFormat("OrderSend error %d",GetLastError());   // 如果不能發送請求,輸出錯誤  }
           }
        }
     }   void closeALLBuyAndSell(string symbol) {      int t=PositionsTotal();      for(int i=t-1;i>=0;i--)
        {         if(PositionGetTicket(i)>0)
           {            if(PositionGetString(POSITION_SYMBOL)==symbol && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
              {
               MqlTradeRequest request={0};
               MqlTradeResult  result={0};
               request.action=TRADE_ACTION_DEAL;                     // 交易操作類型   request.symbol=symbol;                              // 交易品種   request.volume=PositionGetDouble(POSITION_VOLUME); // 交易量   request.type=ORDER_TYPE_SELL;                        // 訂單類型   request.price=SymbolInfoDouble(symbol,SYMBOL_BID); // 持倉價格   request.deviation=100; //滑點   request.position =PositionGetTicket(i);               if(!OrderSend(request,result))
                  PrintFormat("OrderSend error %d",GetLastError());   // 如果不能發送請求,輸出錯誤  }            if(PositionGetString(POSITION_SYMBOL)==symbol && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
              {
               MqlTradeRequest request={0};
               MqlTradeResult  result={0};
               request.action=TRADE_ACTION_DEAL;                     // 交易操作類型   request.symbol=symbol;                              // 交易品種   request.volume=PositionGetDouble(POSITION_VOLUME); // 交易量   request.type=ORDER_TYPE_BUY;                        // 訂單類型   request.price=SymbolInfoDouble(symbol,SYMBOL_ASK); // 持倉價格   request.deviation=100; //滑點   request.position =PositionGetTicket(i);               if(!OrderSend(request,result))
                  PrintFormat("OrderSend error %d",GetLastError());   // 如果不能發送請求,輸出錯誤  }
           }
        }
     }   //修改止盈止損
   void mofiyStoplossAndTakeProfit(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(PositionGetString(POSITION_SYMBOL)==symbol)
              {               if(type==POSITION_TYPE_BUY)
                 {
                  MqlTradeRequest request={0};
                  MqlTradeResult  result={0};                  //Modify Stop Loss and Take Profit values of an opened position  request.action=TRADE_ACTION_SLTP;                  // Position ticket  request.position=PositionGetTicket(i);
                  request.symbol=symbol;                  if(sl!=0)
                    {
                     request.sl=NormalizeDouble(sl,Digits());
                    }                  if(tp!=0)
                    {
                     request.tp=NormalizeDouble(tp,Digits());
                    }                  if(!OrderSend(request,result))
                     PrintFormat("OrderSend error %d",GetLastError());
                 }               if(type==POSITION_TYPE_SELL)
                 {
                  MqlTradeRequest request={0};
                  MqlTradeResult  result={0};
                  request.action=TRADE_ACTION_SLTP;
                  request.position=PositionGetTicket(i);
                  request.symbol=symbol;                  if(sl!=0)
                    {
                     request.sl=NormalizeDouble(sl,Digits());
                    }                  if(tp!=0)
                    {
                     request.tp=NormalizeDouble(tp,Digits());
                    }                  if(!OrderSend(request,result))
                     PrintFormat("OrderSend error %d",GetLastError());
                 }
              }
           }
        }
     }   //刪除所有掛單
   void deleteAllPendingOrders(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};               //Delete the pending order placed previously   request.action=TRADE_ACTION_REMOVE;
               request.order=OrderGetTicket(i);
               request.symbol=symbol;               if(!OrderSend(request,result))
                  PrintFormat("OrderSend error %d",GetLastError());
              }
           }
        }
     }
                     Trade();
                    ~Trade();
  };//+------------------------------------------------------------------+//|                                                                  |//+------------------------------------------------------------------+Trade::Trade()
  {
  }//+------------------------------------------------------------------+//|                                                                  |//+------------------------------------------------------------------+Trade::~Trade()
  {
  }//+------------------------------------------------------------------+

2.使用

//+------------------------------------------------------------------+//|                                              commonUseScript.mq5 |//|                        Copyright 2017, MetaQuotes Software Corp. |//|                                             https://www.mql5.com |//+------------------------------------------------------------------+#property copyright "Copyright 2017, MetaQuotes Software Corp."#property link      "https://www.mql5.com"#property version   "1.00"//顯示確認框#property script_show_confirm#include <wz\Trade.mqh>int magic=170425;
Trade td;//+------------------------------------------------------------------+//|                                                                  |//+------------------------------------------------------------------+void OnStart()
  {// td.buy(Symbol(),0.1,200,200,"buy",magic);//td.sell(Symbol(),0.1,200,200,"sell",magic);//td.closeAllBuy(Symbol());//td.closeAllSell(Symbol());//td.closeALLBuyAndSell(Symbol());//td.mofiyStoplossAndTakeProfit(Symbol(),POSITION_TYPE_BUY,1.00,1.19);
   td.deleteAllPendingOrders(Symbol());
  }//+------------------------------------------------------------------+


    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發現有害或侵權內容,請點擊一鍵舉報。

    來自: 禁忌石 > 《mt5》

    0條評論

    發表

    請遵守用戶 評論公約

    類似文章 更多
    喜歡該文的人也喜歡 更多

    ×
    ×

    .00

    微信或支付寶掃碼支付:

    開通即同意《個圖VIP服務協議》

    全部>>
    欧美观看免费全部完_苍井空免费av片在线观看_乱人伦中文视频在线