|
CloseAllPositions.mq5
(5.29 KB, 下载次数: 6)
在手动或自动化交易中,一键平仓功能都是比较常用的,特别是在涉及风控管理的时候,能够快速止损,有效控制风险。这个脚本功能实现起来并不难,但在程序化实现的过程中,会有一些不同的应用场景。
//+------------------------------------------------------------------+
//| |
//| |
//| http://www.popoding.club/ |
//+------------------------------------------------------------------+
#property copyright "Wen Tao Xiong"
#property link "https://www.mql5.com/zh/users/xiongsir/seller"
#property version "1.00"
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
for(int i = 0 ; i < PositionsTotal(); i++)
{
ulong pos_ticket = PositionGetTicket(i);
ulong pos_magic = PositionGetInteger(POSITION_MAGIC);
string pos_symbol = PositionGetString(POSITION_SYMBOL);
double pos_volume = PositionGetDouble(POSITION_VOLUME);
double pos_profit = PositionGetDouble(POSITION_PROFIT);
ENUM_POSITION_TYPE pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
if(pos_magic == 0 && pos_symbol == "EURUSD" && pos_profit < 0)
{
MqlTradeRequest requst = {};
MqlTradeResult result = {};
double ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
double bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
requst.action = TRADE_ACTION_DEAL;
requst.position = pos_ticket;
requst.symbol = pos_symbol;
requst.volume = pos_volume;
requst.deviation = 50;
requst.magic = pos_magic;
int filling_mode = (int)SymbolInfoInteger(Symbol(),SYMBOL_FILLING_MODE);
if(filling_mode == 1)
{
requst.type_filling = ORDER_FILLING_FOK;
}
else
if(filling_mode == 2)
{
requst.type_filling = ORDER_FILLING_IOC;
}
else
if(filling_mode == 3)
{
requst.type_filling = ORDER_FILLING_RETURN;
}
if(pos_type == POSITION_TYPE_BUY)
{
requst.type = ORDER_TYPE_SELL;
requst.price = bid;
}
else
if(pos_type == POSITION_TYPE_SELL)
{
requst.type = ORDER_TYPE_BUY;
requst.price = ask;
}
if(!OrderSend(requst,result))
{
printf("OrderSend Error %d",GetLastError());
}
}
}
}
//+------------------------------------------------------------------+ |
|