Done with some important functions

This commit is contained in:
Nkondog Anselme
2021-11-17 15:41:12 +01:00
parent 3165cd3c21
commit 46a2c49025
6 changed files with 208 additions and 94 deletions
+42 -18
View File
@@ -103,19 +103,31 @@ input int InpSlowPeriods = 20; // Slow periods
input ENUM_MA_METHOD InpSlowMethod = MODE_SMA; // Slow method
input ENUM_APPLIED_PRICE InpSlowAppliedPrice = PRICE_CLOSE; // Slow price
input int InpAtrPeriod = 14; // ATR period
// Bar numbers for comparison
//input int InpBar2 = 2; // Base bar number
//input int InpBar1 = 1; // Crossover bar number
input string Comment_0="=========="; //Risk Management Settings
input ENUM_RISK_DEFAULT_SIZE RiskDefaultSize=RISK_DEFAULT_AUTO; //Position Size Mode
input double DefaultLotSize=1; //Position Size (if fixed or if no stop loss defined)
input ENUM_RISK_BASE RiskBase=RISK_BASE_BALANCE; //Risk Base
input double MaxRiskPerTrade=0.5; //Percentage To Risk Each Trade
input double MinLotSize=0.01; //Minimum Position Size Allowed
input double MaxLotSize=100; //Maximum Position Size Allowed
input double MaxSpread=10.0; //Maximum Spread Allowed
input int Slippage=5; //Maximum Slippage Allowed in points
input ENUM_RISK_DEFAULT_SIZE InpRiskDefaultSize=RISK_DEFAULT_AUTO; //Position Size Mode
input double InpDefaultLotSize=1; //Position Size (if fixed or if no stop loss defined)
input ENUM_RISK_BASE InpRiskBase=RISK_BASE_BALANCE; //Risk Base
input double InpMaxRiskPerTrade=0.5; //Percentage To Risk Each Trade
input double InpMinLotSize=0.01; //Minimum Position Size Allowed
input double InpMaxLotSize=100; //Maximum Position Size Allowed
input double InpMaxSpread=10.0; //Maximum Spread Allowed
input int InpSlippage=5; //Maximum Slippage Allowed in points
input ENUM_MODE_SL InpStopLossMode=SL_AUTO; //Stop Loss Mode
input int InpDefaultStopLoss=0; //Default Stop Loss In Points (0=No Stop Loss)
input int InpMinStopLoss=0; //Minimum Allowed Stop Loss In Points
input int InpMaxStopLoss=5000; //Maximum Allowed Stop Loss In Points
input bool InpAtrStopLoss=false; //Set Stop loss based on ATR
input int InpAtrStopLossFactor=3; //Multiplicator for ATR stop loss
input ENUM_MODE_TP InpTakeProfitMode=TP_AUTO; //Take Profit Mode
input int InpDefaultTakeProfit=0; //Default Take Profit In Points (0=No Take Profit)
input int InpMinTakeProfit=0; //Minimum 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
// Trading time
input int InStartHour = 12; // Trading starting hour
@@ -131,20 +143,32 @@ input double InpVolume = 0.01; // Default order size
input string InpComment = __FILE__; // Default trade comment
input int InpMagicNumber = 198901; // Magic Number
input string Comment_1="=========="; //Trading Hours Settings
input bool InpUseTradingHours=false; //Limit Trading Hours
input string InpTradingHourStart="01"; //Trading Start Hour (Broker Server Hour)
input string InpTradingHourEnd="23"; //Trading End Hour (Broker Server Hour)
input string InpTradingStartMin="30"; //Trading Start minute (Broker Server Hour)
input string InpTradingEndMin="00"; //Trading End minute
bool IsNewCandle=false;
bool IsTradedThisBar=false;
bool IsOperatingHours=false;
bool IsSpreadOK=false;
bool gIsNewCandle=false;
bool gIsTradedThisBar=false;
bool gIsOperatingHours=false;
bool gIsPreChecksOk=false; //Indicates if the pre checks are satisfied
bool gIsSpreadOK=false; //Indicates if the spread is low enough to trade
double LotSize=DefaultLotSize;
int TickValue=0;
double gLotSize=InpDefaultLotSize;
int gTickValue=0;
int TotalOpenBuy=0;
int TotalOpenSell=0;
string mSymbol = Symbol();
int gTotalOpenBuy=0;
int gTotalOpenSell=0;
int gTotalOpenOrders=0;
string gSymbol = Symbol();
datetime gLastBarTraded=NULL;
MqlTick last_tick;
MqlDateTime dt;
ENUM_SIGNAL_ENTRY SignalEntry=SIGNAL_ENTRY_NEUTRAL; //Entry signal variable
ENUM_SIGNAL_EXIT SignalExit=SIGNAL_EXIT_NEUTRAL;
ENUM_SIGNAL_ENTRY gSignalEntry=SIGNAL_ENTRY_NEUTRAL; //Entry signal variable
ENUM_SIGNAL_EXIT gSignalExit=SIGNAL_EXIT_NEUTRAL;
+55 -20
View File
@@ -5,23 +5,58 @@
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
#property link "https://www.mql5.com"
//+------------------------------------------------------------------+
//| defines |
//+------------------------------------------------------------------+
// #define MacrosHello "Hello, world!"
// #define MacrosYear 2010
//+------------------------------------------------------------------+
//| DLL imports |
//+------------------------------------------------------------------+
// #import "user32.dll"
// int SendMessageA(int hWnd,int Msg,int wParam,int lParam);
// #import "my_expert.dll"
// int ExpertRecalculate(int wParam,int lParam);
// #import
//+------------------------------------------------------------------+
//| EX5 imports |
//+------------------------------------------------------------------+
// #import "stdlib.ex5"
// string ErrorDescription(int error_code);
// #import
//+------------------------------------------------------------------+
//Perform integrity checks when the EA is loaded
void CheckPreChecks()
{
gIsPreChecksOk=true;
//Check if Live Trading is enabled in MT4
if(!MQLInfoInteger(MQL_TRADE_ALLOWED))
{
gIsPreChecksOk=false;
Print("Live Trading is not enabled, please enable it in MT4 and chart settings");
return;
}
//Check if the default stop loss you are setting in above the minimum and below the maximum
if(InpDefaultStopLoss<InpMinStopLoss || InpDefaultStopLoss>InpMaxStopLoss)
{
gIsPreChecksOk=false;
Print("Default Stop Loss must be between Minimum and Maximum Stop Loss Allowed");
return;
}
//Check if the default take profit you are setting in above the minimum and below the maximum
if(InpDefaultTakeProfit<InpMinTakeProfit || InpDefaultTakeProfit>InpMaxTakeProfit)
{
gIsPreChecksOk=false;
Print("Default Take Profit must be between Minimum and Maximum Take Profit Allowed");
return;
}
//Check if the Lot Size is between the minimum and maximum
if(InpDefaultLotSize<InpMinLotSize || InpDefaultLotSize>InpMaxLotSize)
{
gIsPreChecksOk=false;
Print("Default Lot Size must be between Minimum and Maximum Lot Size Allowed");
return;
}
//Slippage must be >= 0
if(InpSlippage<0)
{
gIsPreChecksOk=false;
Print("Slippage must be a positive value");
return;
}
//MaxSpread must be >= 0
if(InpMaxSpread<0)
{
gIsPreChecksOk=false;
Print("Maximum Spread must be a positive value");
return;
}
//MaxRiskPerTrade is a % between 0 and 100
if(InpMaxRiskPerTrade<0 || InpMaxRiskPerTrade>100)
{
gIsPreChecksOk=false;
Print("Maximum Risk Per Trade must be a percentage between 0 and 100");
return;
}
}
+42 -20
View File
@@ -5,23 +5,45 @@
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
#property link "https://www.mql5.com"
//+------------------------------------------------------------------+
//| defines |
//+------------------------------------------------------------------+
// #define MacrosHello "Hello, world!"
// #define MacrosYear 2010
//+------------------------------------------------------------------+
//| DLL imports |
//+------------------------------------------------------------------+
// #import "user32.dll"
// int SendMessageA(int hWnd,int Msg,int wParam,int lParam);
// #import "my_expert.dll"
// int ExpertRecalculate(int wParam,int lParam);
// #import
//+------------------------------------------------------------------+
//| EX5 imports |
//+------------------------------------------------------------------+
// #import "stdlib.ex5"
// string ErrorDescription(int error_code);
// #import
//+------------------------------------------------------------------+
//Scan all positions to find the ones submitted by the EA
//NOTE This function is defined as bool because we want to return true if it is successful and false if it fails
bool ScanPositions()
{
//Scan all the orders, retrieving some of the details
gTotalOpenOrders = 0;
gTotalOpenBuy = 0;
gTotalOpenSell = 0;
for(int i=0; i<PositionsTotal(); i++)
{
//If there is a problem reading the order print the error, exit the function and return false
if(PositionGetTicket(i) == 0)
{
int Error=GetLastError();
string ErrorText=GetLastErrorText(Error);
Print("ERROR - Unable to select the order - ",Error," - ",ErrorText);
return false;
}
//If the order is not for the instrument on chart we can ignore it
if(PositionGetSymbol(i)!=gSymbol)
continue;
//If the order has Magic Number different from the Magic Number of the EA then we can ignore it
if(PositionGetInteger(POSITION_MAGIC)!=InpMagicNumber)
continue;
//If it is a buy order then increment the total count of buy orders
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
gTotalOpenBuy++;
//If it is a sell order then increment the total count of sell orders
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
gTotalOpenSell++;
//Increment the total orders count
gTotalOpenOrders++;
//Find what is the open time of the most recent trade and assign it to LastBarTraded
//this is necessary to check if we already traded in the current candle
if((datetime)PositionGetInteger(POSITION_TIME)>gLastBarTraded || gLastBarTraded==NULL)
gLastBarTraded=(datetime)PositionGetInteger(POSITION_TIME);
}
Print("Total positions ", gTotalOpenOrders, " - Total buys ", gTotalOpenBuy, " - Total sells ", gTotalOpenSell);
return true;
}
+37 -19
View File
@@ -5,23 +5,41 @@
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
#property link "https://www.mql5.com"
//+------------------------------------------------------------------+
//| defines |
//+------------------------------------------------------------------+
// #define MacrosHello "Hello, world!"
// #define MacrosYear 2010
//+------------------------------------------------------------------+
//| DLL imports |
//+------------------------------------------------------------------+
// #import "user32.dll"
// int SendMessageA(int hWnd,int Msg,int wParam,int lParam);
// #import "my_expert.dll"
// int ExpertRecalculate(int wParam,int lParam);
// #import
//+------------------------------------------------------------------+
//| EX5 imports |
//+------------------------------------------------------------------+
// #import "stdlib.ex5"
// string ErrorDescription(int error_code);
// #import
//Check and return if it is operation hours or not
void CheckOperationHours()
{
//If we are not using operating hours then IsOperatingHours is true and I skip the other checks
if(!InpUseTradingHours)
{
gIsOperatingHours=true;
return;
}
//Check if the current hour is between the allowed hours of operations, if so IsOperatingHours is set true
Print("1 this is ", (InpTradingHourStart==InpTradingHourEnd && dt.hour==InpTradingHourStart));
if(InpTradingHourStart==InpTradingHourEnd && dt.hour==InpTradingHourStart)
{
gIsOperatingHours=true;
return;
}
if(InpTradingHourStart<InpTradingHourEnd)
{
if(InpTradingHourStart == dt.hour && dt.min >= InpTradingStartMin)
{
gIsOperatingHours=true;
return;
}
if(dt.hour > InpTradingHourStart)
{
gIsOperatingHours=true;
}
}
if(InpTradingHourStart>InpTradingHourEnd && ((dt.hour>=InpTradingHourStart && dt.hour<=23) || (dt.hour<=InpTradingHourEnd && dt.hour>=0)))
{
gIsOperatingHours=true;
}
}
//+------------------------------------------------------------------+