132 lines
9 KiB
MQL5
132 lines
9 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| EquityStopOut.mq5 |
|
|
//| Copyright 2018, MetaQuotes Software Corp. |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#define VERSION "1.05"
|
|
#property strict
|
|
#property version VERSION
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
#include "..\Libraries\Common.mqh"
|
|
#include "..\Libraries\Magic.mqh"
|
|
#include "..\Libraries\Runtime.mqh"
|
|
//---
|
|
#ifdef __MQL5__
|
|
#include <Trade\PositionInfo.mqh>
|
|
#include <Trade\Trade.mqh>
|
|
#endif
|
|
#ifdef __MQL4__
|
|
#endif
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
#define TIMER 256
|
|
#define MAGIC MAGIC_EQUITY_STOP_OUT
|
|
//--- input parameters
|
|
input double StopOutBelowEquity=0.0; //低于该净值全部平仓
|
|
//---
|
|
CTrade g_Trade;
|
|
CPositionInfo g_PositionInfo;
|
|
CRuntime g_Runtime;
|
|
//+------------------------------------------------------------------+
|
|
//| Expert initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit()
|
|
{
|
|
LOG(LOG_LEVEL_INFO,"EquityStopOut V"+VERSION);
|
|
g_Runtime.AddCheck(RUNTIME_CHECK_ACCOUNT);
|
|
g_Runtime.AddCheck(RUNTIME_CHECK_CONNECTED);
|
|
g_Runtime.AddCheck(RUNTIME_TRADE_ALLOWED);
|
|
if(!g_Runtime.Check())
|
|
{
|
|
return(INIT_FAILED);
|
|
}
|
|
//---
|
|
g_Trade.LogLevel(LOG_LEVEL_ALL);
|
|
g_Trade.SetAsyncMode(true);
|
|
g_Trade.SetExpertMagicNumber(MAGIC);
|
|
g_Trade.SetMarginMode();
|
|
//--- create timer
|
|
if(!EventSetMillisecondTimer(TIMER))
|
|
{
|
|
const int c_iLastError=GetLastError();
|
|
LOG(LOG_LEVEL_ERROR,
|
|
StringFormat(
|
|
"EventSetMillisecondTimer(%d) FAIL #%d %s",
|
|
TIMER,c_iLastError,ErrorDescription(c_iLastError)));
|
|
return(INIT_FAILED);
|
|
}
|
|
//---
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Expert deinitialization function |
|
|
//+------------------------------------------------------------------+
|
|
void OnDeinit(const int reason)
|
|
{
|
|
//--- destroy timer
|
|
EventKillTimer();
|
|
LOG(LOG_LEVEL_INFO,GetDeinitReasonText(reason));
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Expert tick function |
|
|
//+------------------------------------------------------------------+
|
|
void OnTick()
|
|
{
|
|
//---
|
|
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Timer function |
|
|
//+------------------------------------------------------------------+
|
|
void OnTimer()
|
|
{
|
|
//---
|
|
if(!g_Runtime.Check())
|
|
return;
|
|
//---
|
|
if(PositionsTotal()<1) return;
|
|
//---
|
|
const double c_dAccountEquity=AccountInfoDouble(ACCOUNT_EQUITY);
|
|
if(c_dAccountEquity>=StopOutBelowEquity) return;
|
|
//---
|
|
const string c_szLog=StringFormat(
|
|
"#%d立即清仓:当前净值%.2f低于%.2f",
|
|
AccountInfoInteger(ACCOUNT_LOGIN),
|
|
AccountInfoDouble(ACCOUNT_EQUITY),StopOutBelowEquity);
|
|
LOG(LOG_LEVEL_WARNING,c_szLog);
|
|
CloseAll(g_PositionInfo,g_Trade);
|
|
SendMail(c_szLog,"");
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
#ifdef __MQL5__
|
|
void OnTradeTransaction(const MqlTradeTransaction& trans,
|
|
const MqlTradeRequest& request,
|
|
const MqlTradeResult& result)
|
|
{
|
|
if(trans.type!=TRADE_TRANSACTION_REQUEST)
|
|
{
|
|
return;
|
|
}
|
|
//---
|
|
if(request.action!=TRADE_ACTION_DEAL)
|
|
{
|
|
return;
|
|
}
|
|
//---
|
|
if(request.magic!=MAGIC)
|
|
{
|
|
return;
|
|
}
|
|
//---
|
|
string sRequest,sResult;
|
|
g_Trade.FormatRequest(sRequest,request);
|
|
g_Trade.FormatRequestResult(sResult,request,result);
|
|
LOG(LOG_LEVEL_INFO,sRequest+" "+sResult);
|
|
}
|
|
#endif
|
|
//+------------------------------------------------------------------+
|