Working on DCA Manager

This commit is contained in:
Nkondog Anselme
2022-03-15 23:11:18 +01:00
parent 1f301a2f9a
commit 2fcda979d5
4 changed files with 26 additions and 107 deletions
+10 -16
View File
@@ -14,22 +14,20 @@
#include <Nkanven\CandleCount\ScanPositions.mqh> //Trading conditions checks #include <Nkanven\CandleCount\ScanPositions.mqh> //Trading conditions checks
#include <Nkanven\CandleCount\LotSizeCal.mqh> //Lot size calculator #include <Nkanven\CandleCount\LotSizeCal.mqh> //Lot size calculator
#include <Nkanven\CandleCount\EntriesManager.mqh> //Lot size calculator #include <Nkanven\CandleCount\EntriesManager.mqh> //Lot size calculator
#include <Nkanven\CandleCount\CloseTransactions.mqh> //Emergency close of transaction
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Expert initialization function | //| Expert initialization function |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#include <Indicators/Trend.mqh>
#include <Indicators/Oscilators.mqh> CiMA* sma;
CiATR* atr;
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnInit() int OnInit()
{ {
//--- //---
atr = new CiATR(); sma = new CiMA();
atr.Create(gSymbol, InpTimeFrame, InpAtrPeriod); sma.Create(gSymbol, InpTimeFrame, InpPeriods, InpAppliedPrice, InpMethod, PRICE_CLOSE);
//--- //---
return(INIT_SUCCEEDED); return(INIT_SUCCEEDED);
} }
@@ -49,24 +47,20 @@ void OnTick()
//--- //---
TimeCurrent(dt); TimeCurrent(dt);
SymbolInfoTick(_Symbol,last_tick); SymbolInfoTick(InpInstrument1,last_tick);
SymbolInfoTick(InpInstrument2, blast_tick);
sma.Refresh(-1);
gSma = sma.Main(1);
CheckOperationHours(); CheckOperationHours();
CheckPreChecks(); CheckPreChecks();
ScanPositions(); ScanPositions();
//Get ATR values
atr.Refresh(-1);
gAtr = atr.Main(1);
if(!gIsPreChecksOk) if(!gIsPreChecksOk)
return; return;
if(InpActivateRiskWatcher) Print("Good for trading...");
{
drawdownWatcher();
CloseTransactions();
}
ExecuteEntry(); ExecuteEntry();
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
@@ -1,74 +0,0 @@
//+------------------------------------------------------------------+
//| CloseTransactions.mqh |
//| Copyright 2021, Nkondog Anselme Venceslas |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
#property link "https://www.mql5.com"
CTrade trade;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseTransactions()
{
bool result = true;
int cts = PositionsTotal();
ulong theTicket;
double positionProfit = 0.0;
if(cts > 0)
{
for(int i = cts-1; i>=0; i--)
{
ulong ticket = PositionGetTicket(i);
if(PositionSelectByTicket(ticket))
{
if(PositionGetInteger(POSITION_MAGIC)==InpMagicNumber)
{
if(positionProfit > PositionGetDouble(POSITION_PROFIT))
{
positionProfit = PositionGetDouble(POSITION_PROFIT);
theTicket = ticket;
}
}
}
else
{
Print("Error (", GetLastError(), ") while selecting position by ticket");
}
}
if(gEmergencyClose)
{
if(!trade.PositionClose(theTicket))
Print("Error (", GetLastError(), ") while deleting all buy positions");
}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void drawdownWatcher()
{
double amountDiff, equity, balance, percentDiff;
balance=AccountInfoDouble(ACCOUNT_BALANCE);
equity=AccountInfoDouble(ACCOUNT_EQUITY);
gEmergencyClose = false;
if(equity < balance)
{
amountDiff = balance - equity;
percentDiff = (amountDiff*100)/balance;
Print("amountDiff ", amountDiff, " percentDiff ", percentDiff, " InpMaxDrawdown ", InpMaxDrawdown);
if(InpMaxDrawdown < percentDiff)
{
gEmergencyClose = true;
}
}
}
//+------------------------------------------------------------------+
Binary file not shown.
+16 -17
View File
@@ -73,26 +73,17 @@ input double InpMinLotSize=0.01; //Minimu
input double InpMaxLotSize=100; //Maximum Position Size Allowed input double InpMaxLotSize=100; //Maximum Position Size Allowed
input int InpMaxSpread=10; //Maximum Spread Allowed input int InpMaxSpread=10; //Maximum Spread Allowed
input int InpSlippage=1; //Maximum Slippage Allowed in points input int InpSlippage=1; //Maximum Slippage Allowed in points
input bool InpActivateRiskWatcher=false; //Active risk watcher input string Comment_01="----------------------"; //Stop loss settings
input string Comment_00="----------------------"; //Stop loss settings
input ENUM_MODE_SL InpStopLossMode=SL_FIXED; //Stop Loss Mode
input int InpDefaultStopLoss=200; //Default Stop Loss In Points (0=No Stop Loss) input int InpDefaultStopLoss=200; //Default Stop Loss In Points (0=No Stop Loss)
input int InpMinStopLoss=0; //Minimum Allowed Stop Loss In Points input int InpMinStopLoss=0; //Minimum Allowed Stop Loss In Points
input int InpMaxStopLoss=5000; //Maximum Allowed Stop Loss In Points input int InpMaxStopLoss=5000; //Maximum Allowed Stop Loss In Points
input double InpMaxDrawdown=2.5; //Max DD level input string Comment_02="----------------------"; //Take profit settings
input string Comment_01="----------------------"; //Take profit settings
input ENUM_MODE_TP InpTakeProfitMode=TP_FIXED; //Take Profit Mode
input int InpDefaultTakeProfit=60; //Default Take Profit In Points (0=No Take Profit) input int InpDefaultTakeProfit=60; //Default Take Profit In Points (0=No Take Profit)
input int InpMinTakeProfit=0; //Minimum Allowed Take Profit In Points input int InpMinTakeProfit=0; //Minimum Allowed Take Profit In Points
input int InpMaxTakeProfit=5000; //Maximum Allowed Take Profit In Points input int InpMaxTakeProfit=5000; //Maximum Allowed Take Profit In Points
input double InpTakeProfitPercent=1.0; //Take Profit percent on risk base input double InpTakeProfitPercent=1.0; //Take Profit percent on risk base
input string Comment_1="========="; //ATR settings input string Comment_03="----------------------"; //Trading Hours Settings
// Average True Range
input int InpAtrPeriod = 14; //ATR period
input int InpAtrMultiplier = 3; //ATR multiplier
input string Comment_2="=========="; //Trading Hours Settings
input bool InpUseTradingHours=false; //Limit Trading Hours input bool InpUseTradingHours=false; //Limit Trading Hours
input ENUM_MODE_TRADING_TIME InpTradingPeriods=ALL_DAY_TRADING; //Select trading periods input ENUM_MODE_TRADING_TIME InpTradingPeriods=ALL_DAY_TRADING; //Select trading periods
input int InpDayTradingHourStart=7; //Day Trading Start Hour (Broker Server Hour) input int InpDayTradingHourStart=7; //Day Trading Start Hour (Broker Server Hour)
@@ -100,18 +91,26 @@ input int InpDayTradingHourEnd=21; //Day Tr
input int InpNightTradingHourStart=1; //Night Trading Start Hour (Broker Server Hour) input int InpNightTradingHourStart=1; //Night Trading Start Hour (Broker Server Hour)
input int InpNightTradingHourEnd=5; //Night Trading End Hour (Broker Server Hour) input int InpNightTradingHourEnd=5; //Night Trading End Hour (Broker Server Hour)
input string Comment_02="----------------------"; //Stop loss settings input string Comment_04="----------------------"; //DCA settings
input bool InpActivateDCAHedging=false; //Active DCA Hedging
input string InpInstrument1="EURUSD.i"; //Instrument 1
input string InpInstrument2="USDCHF.i"; //Instrument 2
input string Comment_05="----------------------"; //Stop loss settings
// Fast moving average
input int InpPeriods = 21; // Fast periods
input ENUM_MA_METHOD InpMethod = MODE_SMA; // Fast method
input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Fast price
input string InpComment = __FILE__; //Default trade comment input string InpComment = __FILE__; //Default trade comment
input int InpMagicNumber = 198901; //Magic Number input int InpMagicNumber = 198901; //Magic Number
input ENUM_TIMEFRAMES InpTimeFrame = PERIOD_CURRENT; input ENUM_TIMEFRAMES InpTimeFrame = PERIOD_CURRENT;
input double InpMinCandleLenght = 25.0; //Min candle length input int InpSameCandleCount= 2; //Same Candle in a row
input int InpCandleWickPercent = 75; //Candle wick percent
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
string gSymbol = Symbol(); string gSymbol = Symbol();
double gAtr; double gSma;
int gTotalSellPositions, gTotalBuyPositions, gTotalPositions; int gTotalSellPositions, gTotalBuyPositions, gTotalPositions;
bool gIsOperatingHours=false; bool gIsOperatingHours=false;
@@ -128,6 +127,6 @@ long Spread = SymbolInfoInteger(gSymbol,SYMBOL_SPREAD) / 100; //Check t
int gOrderOpRetry = 10; int gOrderOpRetry = 10;
MqlTick last_tick; MqlTick last_tick, blast_tick;
MqlDateTime dt; MqlDateTime dt;
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+