MT5入門到精通之九(交易類)
一1.訂單號理解
二、注意以下區(qū)別
1.開倉不需要特殊操作
2.平倉和修改持倉單止盈止損
request.position=PositionGetTicket(i);
3.刪除掛單
request.order=OrderGetTicket(i);
三交易類
1.實現(xiàn)
//+------------------------------------------------------------------+
//| 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止損點(diǎn)斑举,tpPoint = take profit Point止盈點(diǎn)
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();
//滑點(diǎn)
request.deviation=100;
//多單
request.type=ORDER_TYPE_BUY;
request.comment=comment;
//--- 發(fā)送請求
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // 如果不能發(fā)送請求搅轿,輸出錯誤代碼
//--- 操作信息
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();
//滑點(diǎn)
request.deviation=100;
//空單
request.type=ORDER_TYPE_SELL;
request.comment=comment;
//--- 發(fā)送請求
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // 如果不能發(fā)送請求,輸出錯誤代碼
//--- 操作信息
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;//滑點(diǎn)
request.position=PositionGetTicket(i);//平倉最好都寫下富玷,不然和sell一樣了璧坟。
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // 如果不能發(fā)送請求没宾,輸出錯誤
}
}
}
}
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; //滑點(diǎn)
request.position =PositionGetTicket(i);
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // 如果不能發(fā)送請求,輸出錯誤
}
}
}
}
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; //滑點(diǎn)
request.position =PositionGetTicket(i);
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // 如果不能發(fā)送請求沸柔,輸出錯誤
}
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; //滑點(diǎn)
request.position =PositionGetTicket(i);
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // 如果不能發(fā)送請求循衰,輸出錯誤
}
}
}
}
//修改止盈止損
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"
//顯示確認(rèn)框
#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());
}
//+------------------------------------------------------------------+
如果您發(fā)現(xiàn)本文對你有所幫助,如果您認(rèn)為其他人也可能受益褐澎,請把它分享出去会钝。