MT5入门到精通之九(交易类)
一1.订单号理解
二、注意以下区别
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());
}
//+------------------------------------------------------------------+
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。