mirror of
https://github.com/rithsila/MT5-EA-Sniper-Strategy.git
synced 2026-08-26 09:08:22 +00:00
Done with some important functions
This commit is contained in:
Binary file not shown.
@@ -7,6 +7,14 @@
|
|||||||
#property link "https://www.mql5.com"
|
#property link "https://www.mql5.com"
|
||||||
#property version "1.00"
|
#property version "1.00"
|
||||||
|
|
||||||
|
#include <Indicators/Trend.mqh>
|
||||||
|
#include <Indicators/Oscilators.mqh>
|
||||||
|
|
||||||
|
CiMA* fsma;
|
||||||
|
CiMA* ssma;
|
||||||
|
|
||||||
|
CiATR* atr;
|
||||||
|
|
||||||
#include <Nkanven\GDea\Parameters.mqh> // Description of variables
|
#include <Nkanven\GDea\Parameters.mqh> // Description of variables
|
||||||
#include <DL_ErrorHandling.mqh> // Error library
|
#include <DL_ErrorHandling.mqh> // Error library
|
||||||
#include <Nkanven\GDea\PreChecks.mqh> // Prechecks
|
#include <Nkanven\GDea\PreChecks.mqh> // Prechecks
|
||||||
@@ -24,7 +32,14 @@
|
|||||||
int OnInit()
|
int OnInit()
|
||||||
{
|
{
|
||||||
//---
|
//---
|
||||||
|
fsma = new CiMA();
|
||||||
|
ssma = new CiMA();
|
||||||
|
|
||||||
|
fsma.Create(gSymbol, PERIOD_CURRENT, InpFastPeriods, InpFastAppliedPrice, InpFastMethod, PRICE_CLOSE);
|
||||||
|
ssma.Create(gSymbol, PERIOD_CURRENT, InpSlowPeriods, InpFastAppliedPrice, InpSlowMethod, PRICE_CLOSE);
|
||||||
|
|
||||||
|
atr = new CiATR();
|
||||||
|
atr.Create(gSymbol, PERIOD_CURRENT, InpAtrPeriod);
|
||||||
//---
|
//---
|
||||||
return(INIT_SUCCEEDED);
|
return(INIT_SUCCEEDED);
|
||||||
}
|
}
|
||||||
@@ -34,7 +49,7 @@ int OnInit()
|
|||||||
void OnDeinit(const int reason)
|
void OnDeinit(const int reason)
|
||||||
{
|
{
|
||||||
//---
|
//---
|
||||||
|
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Expert tick function |
|
//| Expert tick function |
|
||||||
@@ -42,26 +57,26 @@ void OnDeinit(const int reason)
|
|||||||
void OnTick()
|
void OnTick()
|
||||||
{
|
{
|
||||||
//---
|
//---
|
||||||
|
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|
||||||
//Initialize variables
|
//Initialize variables
|
||||||
void InitializeVariables()
|
void InitializeVariables()
|
||||||
{
|
{
|
||||||
IsNewCandle=false;
|
gIsNewCandle=false;
|
||||||
IsTradedThisBar=false;
|
gIsTradedThisBar=false;
|
||||||
IsOperatingHours=false;
|
gIsOperatingHours=false;
|
||||||
IsSpreadOK=false;
|
gIsSpreadOK=false;
|
||||||
|
|
||||||
LotSize=DefaultLotSize;
|
gLotSize=InpDefaultLotSize;
|
||||||
TickValue=0;
|
gTickValue=0;
|
||||||
|
|
||||||
TotalOpenBuy=0;
|
gTotalOpenBuy=0;
|
||||||
TotalOpenSell=0;
|
gTotalOpenSell=0;
|
||||||
|
|
||||||
SignalEntry=SIGNAL_ENTRY_NEUTRAL;
|
gSignalEntry=SIGNAL_ENTRY_NEUTRAL;
|
||||||
SignalExit=SIGNAL_EXIT_NEUTRAL;
|
gSignalExit=SIGNAL_EXIT_NEUTRAL;
|
||||||
Print("Variables intialized");
|
Print("Variables intialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,15 +84,15 @@ void InitializeVariables()
|
|||||||
void CheckSpread()
|
void CheckSpread()
|
||||||
{
|
{
|
||||||
//Get the current spread in points, the (int) transforms the double coming from MarketInfo into an integer to avoid a warning when compiling
|
//Get the current spread in points, the (int) transforms the double coming from MarketInfo into an integer to avoid a warning when compiling
|
||||||
double SpreadCurr=SymbolInfoInteger(mSymbol, SYMBOL_SPREAD);
|
long SpreadCurr=SymbolInfoInteger(gSymbol, SYMBOL_SPREAD);
|
||||||
Print("Spread ", SpreadCurr);
|
Print("Spread ", SpreadCurr);
|
||||||
if(SpreadCurr<=MaxSpread)
|
if(SpreadCurr<=InpMaxSpread)
|
||||||
{
|
{
|
||||||
IsSpreadOK=true;
|
gIsSpreadOK=true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
IsSpreadOK=false;
|
gIsSpreadOK=false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
@@ -103,19 +103,31 @@ input int InpSlowPeriods = 20; // Slow periods
|
|||||||
input ENUM_MA_METHOD InpSlowMethod = MODE_SMA; // Slow method
|
input ENUM_MA_METHOD InpSlowMethod = MODE_SMA; // Slow method
|
||||||
input ENUM_APPLIED_PRICE InpSlowAppliedPrice = PRICE_CLOSE; // Slow price
|
input ENUM_APPLIED_PRICE InpSlowAppliedPrice = PRICE_CLOSE; // Slow price
|
||||||
|
|
||||||
|
input int InpAtrPeriod = 14; // ATR period
|
||||||
// Bar numbers for comparison
|
// Bar numbers for comparison
|
||||||
//input int InpBar2 = 2; // Base bar number
|
//input int InpBar2 = 2; // Base bar number
|
||||||
//input int InpBar1 = 1; // Crossover bar number
|
//input int InpBar1 = 1; // Crossover bar number
|
||||||
|
|
||||||
input string Comment_0="=========="; //Risk Management Settings
|
input string Comment_0="=========="; //Risk Management Settings
|
||||||
input ENUM_RISK_DEFAULT_SIZE RiskDefaultSize=RISK_DEFAULT_AUTO; //Position Size Mode
|
input ENUM_RISK_DEFAULT_SIZE InpRiskDefaultSize=RISK_DEFAULT_AUTO; //Position Size Mode
|
||||||
input double DefaultLotSize=1; //Position Size (if fixed or if no stop loss defined)
|
input double InpDefaultLotSize=1; //Position Size (if fixed or if no stop loss defined)
|
||||||
input ENUM_RISK_BASE RiskBase=RISK_BASE_BALANCE; //Risk Base
|
input ENUM_RISK_BASE InpRiskBase=RISK_BASE_BALANCE; //Risk Base
|
||||||
input double MaxRiskPerTrade=0.5; //Percentage To Risk Each Trade
|
input double InpMaxRiskPerTrade=0.5; //Percentage To Risk Each Trade
|
||||||
input double MinLotSize=0.01; //Minimum Position Size Allowed
|
input double InpMinLotSize=0.01; //Minimum Position Size Allowed
|
||||||
input double MaxLotSize=100; //Maximum Position Size Allowed
|
input double InpMaxLotSize=100; //Maximum Position Size Allowed
|
||||||
input double MaxSpread=10.0; //Maximum Spread Allowed
|
input double InpMaxSpread=10.0; //Maximum Spread Allowed
|
||||||
input int Slippage=5; //Maximum Slippage Allowed in points
|
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
|
// Trading time
|
||||||
input int InStartHour = 12; // Trading starting hour
|
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 string InpComment = __FILE__; // Default trade comment
|
||||||
input int InpMagicNumber = 198901; // Magic Number
|
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 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;
|
double gLotSize=InpDefaultLotSize;
|
||||||
int TickValue=0;
|
int gTickValue=0;
|
||||||
|
|
||||||
int TotalOpenBuy=0;
|
int gTotalOpenBuy=0;
|
||||||
int TotalOpenSell=0;
|
int gTotalOpenSell=0;
|
||||||
string mSymbol = Symbol();
|
int gTotalOpenOrders=0;
|
||||||
|
string gSymbol = Symbol();
|
||||||
|
|
||||||
|
datetime gLastBarTraded=NULL;
|
||||||
|
|
||||||
MqlTick last_tick;
|
MqlTick last_tick;
|
||||||
|
MqlDateTime dt;
|
||||||
|
|
||||||
ENUM_SIGNAL_ENTRY SignalEntry=SIGNAL_ENTRY_NEUTRAL; //Entry signal variable
|
ENUM_SIGNAL_ENTRY gSignalEntry=SIGNAL_ENTRY_NEUTRAL; //Entry signal variable
|
||||||
ENUM_SIGNAL_EXIT SignalExit=SIGNAL_EXIT_NEUTRAL;
|
ENUM_SIGNAL_EXIT gSignalExit=SIGNAL_EXIT_NEUTRAL;
|
||||||
@@ -5,23 +5,58 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
||||||
#property link "https://www.mql5.com"
|
#property link "https://www.mql5.com"
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| defines |
|
//Perform integrity checks when the EA is loaded
|
||||||
//+------------------------------------------------------------------+
|
void CheckPreChecks()
|
||||||
// #define MacrosHello "Hello, world!"
|
{
|
||||||
// #define MacrosYear 2010
|
gIsPreChecksOk=true;
|
||||||
//+------------------------------------------------------------------+
|
//Check if Live Trading is enabled in MT4
|
||||||
//| DLL imports |
|
if(!MQLInfoInteger(MQL_TRADE_ALLOWED))
|
||||||
//+------------------------------------------------------------------+
|
{
|
||||||
// #import "user32.dll"
|
gIsPreChecksOk=false;
|
||||||
// int SendMessageA(int hWnd,int Msg,int wParam,int lParam);
|
Print("Live Trading is not enabled, please enable it in MT4 and chart settings");
|
||||||
// #import "my_expert.dll"
|
return;
|
||||||
// int ExpertRecalculate(int wParam,int lParam);
|
}
|
||||||
// #import
|
//Check if the default stop loss you are setting in above the minimum and below the maximum
|
||||||
//+------------------------------------------------------------------+
|
if(InpDefaultStopLoss<InpMinStopLoss || InpDefaultStopLoss>InpMaxStopLoss)
|
||||||
//| EX5 imports |
|
{
|
||||||
//+------------------------------------------------------------------+
|
gIsPreChecksOk=false;
|
||||||
// #import "stdlib.ex5"
|
Print("Default Stop Loss must be between Minimum and Maximum Stop Loss Allowed");
|
||||||
// string ErrorDescription(int error_code);
|
return;
|
||||||
// #import
|
}
|
||||||
//+------------------------------------------------------------------+
|
//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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,23 +5,45 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
||||||
#property link "https://www.mql5.com"
|
#property link "https://www.mql5.com"
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| defines |
|
//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
|
||||||
// #define MacrosHello "Hello, world!"
|
bool ScanPositions()
|
||||||
// #define MacrosYear 2010
|
{
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| DLL imports |
|
//Scan all the orders, retrieving some of the details
|
||||||
//+------------------------------------------------------------------+
|
gTotalOpenOrders = 0;
|
||||||
// #import "user32.dll"
|
gTotalOpenBuy = 0;
|
||||||
// int SendMessageA(int hWnd,int Msg,int wParam,int lParam);
|
gTotalOpenSell = 0;
|
||||||
// #import "my_expert.dll"
|
for(int i=0; i<PositionsTotal(); i++)
|
||||||
// int ExpertRecalculate(int wParam,int lParam);
|
{
|
||||||
// #import
|
//If there is a problem reading the order print the error, exit the function and return false
|
||||||
//+------------------------------------------------------------------+
|
if(PositionGetTicket(i) == 0)
|
||||||
//| EX5 imports |
|
{
|
||||||
//+------------------------------------------------------------------+
|
int Error=GetLastError();
|
||||||
// #import "stdlib.ex5"
|
string ErrorText=GetLastErrorText(Error);
|
||||||
// string ErrorDescription(int error_code);
|
Print("ERROR - Unable to select the order - ",Error," - ",ErrorText);
|
||||||
// #import
|
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;
|
||||||
|
}
|
||||||
@@ -5,23 +5,41 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
||||||
#property link "https://www.mql5.com"
|
#property link "https://www.mql5.com"
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| defines |
|
//Check and return if it is operation hours or not
|
||||||
//+------------------------------------------------------------------+
|
void CheckOperationHours()
|
||||||
// #define MacrosHello "Hello, world!"
|
{
|
||||||
// #define MacrosYear 2010
|
//If we are not using operating hours then IsOperatingHours is true and I skip the other checks
|
||||||
//+------------------------------------------------------------------+
|
if(!InpUseTradingHours)
|
||||||
//| DLL imports |
|
{
|
||||||
//+------------------------------------------------------------------+
|
gIsOperatingHours=true;
|
||||||
// #import "user32.dll"
|
return;
|
||||||
// int SendMessageA(int hWnd,int Msg,int wParam,int lParam);
|
}
|
||||||
// #import "my_expert.dll"
|
//Check if the current hour is between the allowed hours of operations, if so IsOperatingHours is set true
|
||||||
// int ExpertRecalculate(int wParam,int lParam);
|
Print("1 this is ", (InpTradingHourStart==InpTradingHourEnd && dt.hour==InpTradingHourStart));
|
||||||
// #import
|
|
||||||
//+------------------------------------------------------------------+
|
if(InpTradingHourStart==InpTradingHourEnd && dt.hour==InpTradingHourStart)
|
||||||
//| EX5 imports |
|
{
|
||||||
//+------------------------------------------------------------------+
|
gIsOperatingHours=true;
|
||||||
// #import "stdlib.ex5"
|
return;
|
||||||
// string ErrorDescription(int error_code);
|
}
|
||||||
// #import
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user