mirror of
https://github.com/rithsila/MT5-EA-Sniper-Strategy.git
synced 2026-08-23 15:48:12 +00:00
MAGrid initial commit. First position and orders
This commit is contained in:
Binary file not shown.
@@ -120,7 +120,7 @@ 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=Spread;
|
long SpreadCurr=Spread;
|
||||||
Print("Spread ", SpreadCurr);
|
Print("Spread ", SpreadCurr);
|
||||||
if(SpreadCurr<=MaxSpread)
|
if(SpreadCurr<=MaxSpread)
|
||||||
{
|
{
|
||||||
|
|||||||
Binary file not shown.
@@ -0,0 +1,99 @@
|
|||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| MAGrid.mq5 |
|
||||||
|
//| Copyright 2021, Nkondog Anselme Venceslas |
|
||||||
|
//| https://www.mql5.com |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
||||||
|
#property link "https://www.mql5.com"
|
||||||
|
#property version "1.00"
|
||||||
|
|
||||||
|
// Moving Average grid strategy
|
||||||
|
/*
|
||||||
|
Set pending orders x poinst above and below price.
|
||||||
|
If price above SMA, buy and set buy orders x time the ATR above and below price.
|
||||||
|
If price below SMA, sell and set sell orders x time the ATR above and below price.
|
||||||
|
Close all position at the close of the first candle crossing the moving average.
|
||||||
|
|
||||||
|
Open positions and set orders if there's nothing. At take profit, close all pending orders and reopen others
|
||||||
|
*/
|
||||||
|
#include <Indicators/Trend.mqh>
|
||||||
|
#include <Indicators/Oscilators.mqh>
|
||||||
|
CiMA* ma;
|
||||||
|
CiATR* atr;
|
||||||
|
|
||||||
|
#include <Nkanven\MAGrid\Parameters.mqh> // Description of variables
|
||||||
|
//#include <DL_ErrorHandling.mqh> // Error library
|
||||||
|
//#include <Nkanven\MAGrid\PreChecks.mqh> // Prechecks
|
||||||
|
//#include <Nkanven\MAGrid\TradingHour.mqh> //
|
||||||
|
//#include <Trade\Trade.mqh>
|
||||||
|
#include <Nkanven\MAGrid\ScanPositions.mqh> // Scan for opened positions
|
||||||
|
//#include <Nkanven\MAGrid\CheckHistory.mqh> //Check transaction history
|
||||||
|
//#include <Nkanven\MAGrid\TradeManager.mqh> //Manage trade dynamic open and close conditions
|
||||||
|
#include <Nkanven\MAGrid\EntriesManager.mqh> // Check buy and sell entries signals and execute them
|
||||||
|
#include <Nkanven\MAGrid\LotSizeCal.mqh> // Lot size calculate
|
||||||
|
//#include <Nkanven\MAGrid\ClosePositions.mqh> // Close opened positions
|
||||||
|
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Expert initialization function |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
int OnInit()
|
||||||
|
{
|
||||||
|
//---
|
||||||
|
ma = new CiMA();
|
||||||
|
ma.Create(gSymbol, PERIOD_CURRENT, InpFastPeriods, InpFastAppliedPrice, InpFastMethod, PRICE_CLOSE);
|
||||||
|
|
||||||
|
atr = new CiATR();
|
||||||
|
atr.Create(gSymbol, PERIOD_CURRENT, InpAtrPeriod);
|
||||||
|
//---
|
||||||
|
return(INIT_SUCCEEDED);
|
||||||
|
}
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Expert deinitialization function |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void OnDeinit(const int reason)
|
||||||
|
{
|
||||||
|
//---
|
||||||
|
|
||||||
|
}
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Expert tick function |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void OnTick()
|
||||||
|
{
|
||||||
|
//---
|
||||||
|
SymbolInfoTick(_Symbol,last_tick);
|
||||||
|
ma.Refresh(-1);
|
||||||
|
gMa = ma.Main(1);
|
||||||
|
|
||||||
|
atr.Refresh(-1);
|
||||||
|
gAtr = atr.Main(1);
|
||||||
|
ScanPositions();
|
||||||
|
|
||||||
|
Print("Total transaction ", gTotalTransactions);
|
||||||
|
if(gTotalTransactions>0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
CheckSpread();
|
||||||
|
EvaluateEntry();
|
||||||
|
ExecuteEntry();
|
||||||
|
}
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
|
||||||
|
//Check and return if the spread is not too high
|
||||||
|
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
|
||||||
|
long SpreadCurr=SymbolInfoInteger(gSymbol, SYMBOL_SPREAD);
|
||||||
|
Print("Spread ", SpreadCurr);
|
||||||
|
if(SpreadCurr<=InpMaxSpread)
|
||||||
|
{
|
||||||
|
gIsSpreadOK=true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gIsSpreadOK=false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
Binary file not shown.
@@ -57,6 +57,23 @@ void OnDeinit(const int reason)
|
|||||||
void OnTick()
|
void OnTick()
|
||||||
{
|
{
|
||||||
//---
|
//---
|
||||||
|
fsma.Refresh(-1);
|
||||||
|
ssma.Refresh(-1);
|
||||||
|
|
||||||
|
gSsma = ssma.Main(1);
|
||||||
|
//isQualifiedCandle(0);
|
||||||
|
OrderClose();
|
||||||
|
if(!ScanPositions())
|
||||||
|
return;
|
||||||
|
if(OrdersTotal()>0)
|
||||||
|
return;
|
||||||
|
CheckSpread();
|
||||||
|
entryConditions();
|
||||||
|
EvaluateEntry();
|
||||||
|
ExecuteEntry();
|
||||||
|
|
||||||
|
Comment(
|
||||||
|
"Expert Advisor by Anselme Nkondog (c) 2021\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -5,23 +5,34 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#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"
|
||||||
|
|
||||||
|
CTrade trade;
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| defines |
|
//| |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
// #define MacrosHello "Hello, world!"
|
bool OrderClose()
|
||||||
// #define MacrosYear 2010
|
{
|
||||||
//+------------------------------------------------------------------+
|
bool result = true;
|
||||||
//| DLL imports |
|
int cnt = OrdersTotal();
|
||||||
//+------------------------------------------------------------------+
|
if(cnt == 1)
|
||||||
// #import "user32.dll"
|
{
|
||||||
// int SendMessageA(int hWnd,int Msg,int wParam,int lParam);
|
|
||||||
// #import "my_expert.dll"
|
|
||||||
// int ExpertRecalculate(int wParam,int lParam);
|
for(int i = cnt-1; i>=0; i--)
|
||||||
// #import
|
{
|
||||||
//+------------------------------------------------------------------+
|
ulong ticket = OrderGetTicket(i);
|
||||||
//| EX5 imports |
|
if(OrderSelect(ticket))
|
||||||
//+------------------------------------------------------------------+
|
{
|
||||||
// #import "stdlib.ex5"
|
|
||||||
// string ErrorDescription(int error_code);
|
result &= trade.OrderDelete(ticket);
|
||||||
// #import
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return(result);
|
||||||
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Binary file not shown.
@@ -5,23 +5,53 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#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 |
|
|
||||||
//+------------------------------------------------------------------+
|
//Lot Size Calculator
|
||||||
// #define MacrosHello "Hello, world!"
|
void LotSizeCalculate(double SL=0)
|
||||||
// #define MacrosYear 2010
|
{
|
||||||
//+------------------------------------------------------------------+
|
//If the position size is dynamic
|
||||||
//| DLL imports |
|
if(InpRiskDefaultSize==RISK_DEFAULT_AUTO)
|
||||||
//+------------------------------------------------------------------+
|
{
|
||||||
// #import "user32.dll"
|
//If the stop loss is not zero then calculate the lot size
|
||||||
// int SendMessageA(int hWnd,int Msg,int wParam,int lParam);
|
if(SL!=0)
|
||||||
// #import "my_expert.dll"
|
{
|
||||||
// int ExpertRecalculate(int wParam,int lParam);
|
double RiskBaseAmount=0;
|
||||||
// #import
|
double RiskBase=0;
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| EX5 imports |
|
//TickValue is the value of the individual price increment for 1 lot of the instrument, expressed in the account currenty
|
||||||
//+------------------------------------------------------------------+
|
double TickValue=SymbolInfoDouble(gSymbol,SYMBOL_TRADE_TICK_VALUE);
|
||||||
// #import "stdlib.ex5"
|
//Define the base for the risk calculation depending on the parameter chosen
|
||||||
// string ErrorDescription(int error_code);
|
if(RiskBase==RISK_BASE_BALANCE)
|
||||||
// #import
|
RiskBaseAmount=AccountInfoDouble(ACCOUNT_BALANCE);
|
||||||
//+------------------------------------------------------------------+
|
if(RiskBase==RISK_BASE_EQUITY)
|
||||||
|
RiskBaseAmount=AccountInfoDouble(ACCOUNT_EQUITY);
|
||||||
|
if(RiskBase==RISK_BASE_FREEMARGIN)
|
||||||
|
RiskBaseAmount=AccountInfoDouble(ACCOUNT_FREEMARGIN);
|
||||||
|
|
||||||
|
//Calculate the Position Size
|
||||||
|
LotSize=((RiskBaseAmount*InpMaxRiskPerTrade/100)/(SL*TickValue));
|
||||||
|
}
|
||||||
|
//If the stop loss is zero then the lot size is the default one
|
||||||
|
if(SL==0)
|
||||||
|
{
|
||||||
|
LotSize=InpDefaultLotSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Normalize the Lot Size to satisfy the allowed lot increment and minimum and maximum position size
|
||||||
|
LotSize=MathFloor(LotSize/SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_STEP))*SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_STEP);
|
||||||
|
|
||||||
|
//Limit the lot size in case it is greater than the maximum allowed by the user
|
||||||
|
if(LotSize>InpMaxLotSize)
|
||||||
|
LotSize=InpMaxLotSize;
|
||||||
|
//Limit the lot size in case it is greater than the maximum allowed by the broker
|
||||||
|
if(LotSize>SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_MAX))
|
||||||
|
LotSize=SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_MAX);
|
||||||
|
Print("Lot ", LotSize, " Max lot ", SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_MAX));
|
||||||
|
//If the lot size is too small then set it to 0 and don't trade
|
||||||
|
if(LotSize<InpMinLotSize || LotSize < SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_MIN))
|
||||||
|
{
|
||||||
|
LotSize=0;
|
||||||
|
Print("Lot size too small : ", LotSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,8 +10,7 @@
|
|||||||
enum ENUM_SIGNAL_ENTRY
|
enum ENUM_SIGNAL_ENTRY
|
||||||
{
|
{
|
||||||
SIGNAL_ENTRY_NEUTRAL=0, //SIGNAL ENTRY NEUTRAL
|
SIGNAL_ENTRY_NEUTRAL=0, //SIGNAL ENTRY NEUTRAL
|
||||||
SIGNAL_ENTRY_BUY=1, //SIGNAL ENTRY BUY
|
SIGNAL_ENTRY_ENTER=1, //SIGNAL PENDING BUY/SELL
|
||||||
SIGNAL_ENTRY_SELL=-1, //SIGNAL ENTRY SELL
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//Enumerative for the exit signal value
|
//Enumerative for the exit signal value
|
||||||
@@ -115,7 +114,7 @@ input ENUM_RISK_BASE InpRiskBase=RISK_BASE_BALANCE; //Risk B
|
|||||||
input double InpMaxRiskPerTrade=0.5; //Percentage To Risk Each Trade
|
input double InpMaxRiskPerTrade=0.5; //Percentage To Risk Each Trade
|
||||||
input double InpMinLotSize=0.01; //Minimum Position Size Allowed
|
input double InpMinLotSize=0.01; //Minimum Position Size Allowed
|
||||||
input double InpMaxLotSize=100; //Maximum Position Size Allowed
|
input double InpMaxLotSize=100; //Maximum Position Size Allowed
|
||||||
input double InpMaxSpread=10.0; //Maximum Spread Allowed
|
input int InpMaxSpread=20; //Maximum Spread Allowed
|
||||||
input int InpSlippage=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 ENUM_MODE_SL InpStopLossMode=SL_AUTO; //Stop Loss Mode
|
||||||
input int InpDefaultStopLoss=0; //Default Stop Loss In Points (0=No Stop Loss)
|
input int InpDefaultStopLoss=0; //Default Stop Loss In Points (0=No Stop Loss)
|
||||||
@@ -150,7 +149,6 @@ input string InpTradingHourEnd="23"; //Trading End Hour (Br
|
|||||||
input string InpTradingStartMin="30"; //Trading Start minute (Broker Server Hour)
|
input string InpTradingStartMin="30"; //Trading Start minute (Broker Server Hour)
|
||||||
input string InpTradingEndMin="00"; //Trading End minute
|
input string InpTradingEndMin="00"; //Trading End minute
|
||||||
|
|
||||||
bool IsOperatingHours=false;
|
|
||||||
bool gIsNewCandle=false;
|
bool gIsNewCandle=false;
|
||||||
bool gIsTradedThisBar=false;
|
bool gIsTradedThisBar=false;
|
||||||
bool gIsOperatingHours=false;
|
bool gIsOperatingHours=false;
|
||||||
@@ -163,10 +161,19 @@ int gTickValue=0;
|
|||||||
int gTotalOpenBuy=0;
|
int gTotalOpenBuy=0;
|
||||||
int gTotalOpenSell=0;
|
int gTotalOpenSell=0;
|
||||||
int gTotalOpenOrders=0;
|
int gTotalOpenOrders=0;
|
||||||
|
int gOrderOpRetry=5;
|
||||||
|
double gBuyStopLossPrice;
|
||||||
|
double gSellStopLossPrice;
|
||||||
|
double gBuyEntryPrice;
|
||||||
|
double gSellEntryPrice;
|
||||||
|
|
||||||
string gSymbol = Symbol();
|
string gSymbol = Symbol();
|
||||||
|
|
||||||
datetime gLastBarTraded=NULL;
|
datetime gLastBarTraded=NULL;
|
||||||
|
|
||||||
|
double gFsma, gSsma;
|
||||||
|
double LotSize=0;
|
||||||
|
|
||||||
MqlTick last_tick;
|
MqlTick last_tick;
|
||||||
MqlDateTime dt;
|
MqlDateTime dt;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| CheckHistory.mqh |
|
||||||
|
//| Copyright 2021, Nkondog Anselme Venceslas |
|
||||||
|
//| https://www.mql5.com |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
#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
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| ClosePositions.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;
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
bool OrderClose()
|
||||||
|
{
|
||||||
|
bool result = true;
|
||||||
|
int cnt = OrdersTotal();
|
||||||
|
if(cnt == 1)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
for(int i = cnt-1; i>=0; i--)
|
||||||
|
{
|
||||||
|
ulong ticket = OrderGetTicket(i);
|
||||||
|
if(OrderSelect(ticket))
|
||||||
|
{
|
||||||
|
|
||||||
|
result &= trade.OrderDelete(ticket);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return(result);
|
||||||
|
}
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
Binary file not shown.
@@ -0,0 +1,57 @@
|
|||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| LotSizeCal.mqh |
|
||||||
|
//| Copyright 2021, Nkondog Anselme Venceslas |
|
||||||
|
//| https://www.mql5.com |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
||||||
|
#property link "https://www.mql5.com"
|
||||||
|
|
||||||
|
|
||||||
|
//Lot Size Calculator
|
||||||
|
void LotSizeCalculate(double SL=0)
|
||||||
|
{
|
||||||
|
//If the position size is dynamic
|
||||||
|
if(InpRiskDefaultSize==RISK_DEFAULT_AUTO)
|
||||||
|
{
|
||||||
|
//If the stop loss is not zero then calculate the lot size
|
||||||
|
if(SL!=0)
|
||||||
|
{
|
||||||
|
double RiskBaseAmount=0;
|
||||||
|
double RiskBase=0;
|
||||||
|
|
||||||
|
//TickValue is the value of the individual price increment for 1 lot of the instrument, expressed in the account currenty
|
||||||
|
double TickValue=SymbolInfoDouble(gSymbol,SYMBOL_TRADE_TICK_VALUE);
|
||||||
|
//Define the base for the risk calculation depending on the parameter chosen
|
||||||
|
if(RiskBase==RISK_BASE_BALANCE)
|
||||||
|
RiskBaseAmount=AccountInfoDouble(ACCOUNT_BALANCE);
|
||||||
|
if(RiskBase==RISK_BASE_EQUITY)
|
||||||
|
RiskBaseAmount=AccountInfoDouble(ACCOUNT_EQUITY);
|
||||||
|
if(RiskBase==RISK_BASE_FREEMARGIN)
|
||||||
|
RiskBaseAmount=AccountInfoDouble(ACCOUNT_FREEMARGIN);
|
||||||
|
|
||||||
|
//Calculate the Position Size
|
||||||
|
LotSize=((RiskBaseAmount*InpMaxRiskPerTrade/100)/(SL*TickValue));
|
||||||
|
}
|
||||||
|
//If the stop loss is zero then the lot size is the default one
|
||||||
|
if(SL==0)
|
||||||
|
{
|
||||||
|
LotSize=InpDefaultLotSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Normalize the Lot Size to satisfy the allowed lot increment and minimum and maximum position size
|
||||||
|
LotSize=MathFloor(LotSize/SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_STEP))*SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_STEP);
|
||||||
|
|
||||||
|
//Limit the lot size in case it is greater than the maximum allowed by the user
|
||||||
|
if(LotSize>InpMaxLotSize)
|
||||||
|
LotSize=InpMaxLotSize;
|
||||||
|
//Limit the lot size in case it is greater than the maximum allowed by the broker
|
||||||
|
if(LotSize>SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_MAX))
|
||||||
|
LotSize=SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_MAX);
|
||||||
|
Print("Lot ", LotSize, " Max lot ", SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_MAX));
|
||||||
|
//If the lot size is too small then set it to 0 and don't trade
|
||||||
|
if(LotSize<InpMinLotSize || LotSize < SymbolInfoDouble(gSymbol,SYMBOL_VOLUME_MIN))
|
||||||
|
{
|
||||||
|
LotSize=0;
|
||||||
|
Print("Lot size too small : ", LotSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,184 @@
|
|||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Parameters.mqh |
|
||||||
|
//| Copyright 2021, Nkondog Anselme Venceslas |
|
||||||
|
//| https://www.mql5.com |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
||||||
|
#property link "https://www.mql5.com"
|
||||||
|
|
||||||
|
//Enumerative for the entry signal value
|
||||||
|
enum ENUM_SIGNAL_ENTRY
|
||||||
|
{
|
||||||
|
SIGNAL_ENTRY_NEUTRAL=0, //SIGNAL ENTRY NEUTRAL
|
||||||
|
SIGNAL_ENTRY_ENTER=1, //SIGNAL PENDING BUY/SELL
|
||||||
|
};
|
||||||
|
|
||||||
|
//Enumerative for the exit signal value
|
||||||
|
enum ENUM_SIGNAL_EXIT
|
||||||
|
{
|
||||||
|
SIGNAL_EXIT_NEUTRAL=0, //SIGNAL EXIT NEUTRAL
|
||||||
|
SIGNAL_EXIT_BUY=1, //SIGNAL EXIT BUY
|
||||||
|
SIGNAL_EXIT_SELL=-1, //SIGNAL EXIT SELL
|
||||||
|
SIGNAL_EXIT_ALL=2, //SIGNAL EXIT ALL
|
||||||
|
};
|
||||||
|
|
||||||
|
//Enumerative for the allowed trading direction
|
||||||
|
enum ENUM_TRADING_ALLOW_DIRECTION
|
||||||
|
{
|
||||||
|
TRADING_ALLOW_BOTH=0, //ALLOW BOTH BUY AND SELL
|
||||||
|
TRADING_ALLOW_BUY=1, //ALLOW BUY ONLY
|
||||||
|
TRADING_ALLOW_SELL=-1, //ALLOW SELL ONLY
|
||||||
|
};
|
||||||
|
|
||||||
|
//Enumerative for the base used for risk calculation
|
||||||
|
enum ENUM_RISK_BASE
|
||||||
|
{
|
||||||
|
RISK_BASE_EQUITY=1, //EQUITY
|
||||||
|
RISK_BASE_BALANCE=2, //BALANCE
|
||||||
|
RISK_BASE_FREEMARGIN=3, //FREE MARGIN
|
||||||
|
};
|
||||||
|
|
||||||
|
//Enumerative for the default risk size
|
||||||
|
enum ENUM_RISK_DEFAULT_SIZE
|
||||||
|
{
|
||||||
|
RISK_DEFAULT_FIXED=1, //FIXED SIZE
|
||||||
|
RISK_DEFAULT_AUTO=2, //AUTOMATIC SIZE BASED ON RISK
|
||||||
|
};
|
||||||
|
|
||||||
|
//Enumerative for the Stop Loss mode
|
||||||
|
enum ENUM_MODE_SL
|
||||||
|
{
|
||||||
|
SL_FIXED=0, //FIXED STOP LOSS
|
||||||
|
SL_AUTO=1, //AUTOMATIC STOP LOSS
|
||||||
|
};
|
||||||
|
|
||||||
|
//Enumerative for the Take Profit Mode
|
||||||
|
enum ENUM_MODE_TP
|
||||||
|
{
|
||||||
|
TP_FIXED=0, //FIXED TAKE PROFIT
|
||||||
|
TP_AUTO=1, //AUTOMATIC TAKE PROFIT
|
||||||
|
};
|
||||||
|
|
||||||
|
//Enumerative for the stop loss calculation
|
||||||
|
enum ENUM_MODE_SL_BY
|
||||||
|
{
|
||||||
|
SL_BY_POINTS=0, //STOP LOSS PASSED IN POINTS
|
||||||
|
SL_BY_PRICE=1, //STOP LOSS PASSED BY PRICE
|
||||||
|
};
|
||||||
|
|
||||||
|
//Enumerative for candle type
|
||||||
|
enum ENUM_CANDLE_TYPE
|
||||||
|
{
|
||||||
|
NEUTRAL_CANDLE=0,
|
||||||
|
BEARISH_CANDLE=1,
|
||||||
|
BULLISH_CANDLE=2,
|
||||||
|
};
|
||||||
|
|
||||||
|
//Enumerative for price momentum
|
||||||
|
enum ENUM_PRICE_MOMENTUM
|
||||||
|
{
|
||||||
|
UP=2,
|
||||||
|
DOWN=1,
|
||||||
|
NEUTRAL=0,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct LastTransaction
|
||||||
|
{
|
||||||
|
string time;
|
||||||
|
int type;
|
||||||
|
double profit;
|
||||||
|
} lt;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Input Section
|
||||||
|
//
|
||||||
|
// Fast moving average
|
||||||
|
input int InpFastPeriods = 10; // Fast periods
|
||||||
|
input ENUM_MA_METHOD InpFastMethod = MODE_SMA; // Fast method
|
||||||
|
input ENUM_APPLIED_PRICE InpFastAppliedPrice = PRICE_CLOSE; // Fast price
|
||||||
|
|
||||||
|
// Slow moving average
|
||||||
|
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 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 int InpMaxSpread=20; //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 InpAtrMultiplier=3; //Multiplicator for ATR
|
||||||
|
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
|
||||||
|
input int InStartMin = 30; // Trading starting minute
|
||||||
|
input int InEndHour = 12; // Trading starting hour
|
||||||
|
input int InEndMin = 30; // Trading starting minute
|
||||||
|
|
||||||
|
//
|
||||||
|
// Some standard inputs,
|
||||||
|
// remember to change the default magic for each EA
|
||||||
|
//
|
||||||
|
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 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 gLotSize=InpDefaultLotSize;
|
||||||
|
int gTickValue=0;
|
||||||
|
|
||||||
|
int gTotalBuyPositions=0;
|
||||||
|
int gTotalSellPositions=0;
|
||||||
|
int gTotalPositions=0;
|
||||||
|
int gTotalOpenOrders=0;
|
||||||
|
int gOrderOpRetry=5;
|
||||||
|
int gTotalTransactions=0;
|
||||||
|
double gBuyStopLossPrice;
|
||||||
|
double gSellStopLossPrice;
|
||||||
|
double gBuyEntryPrice;
|
||||||
|
double gSellEntryPrice;
|
||||||
|
double gAtr;
|
||||||
|
|
||||||
|
string gSymbol = Symbol();
|
||||||
|
|
||||||
|
datetime gLastBarTraded=NULL;
|
||||||
|
|
||||||
|
double gMa;
|
||||||
|
double LotSize=0;
|
||||||
|
|
||||||
|
MqlTick last_tick;
|
||||||
|
MqlDateTime dt;
|
||||||
|
|
||||||
|
ENUM_SIGNAL_ENTRY gSignalEntry=SIGNAL_ENTRY_NEUTRAL; //Entry signal variable
|
||||||
|
ENUM_SIGNAL_EXIT gSignalExit=SIGNAL_EXIT_NEUTRAL;
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Prechecks.mqh |
|
||||||
|
//| Copyright 2021, Nkondog Anselme Venceslas |
|
||||||
|
//| https://www.mql5.com |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
||||||
|
#property link "https://www.mql5.com"
|
||||||
|
|
||||||
|
//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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| ScanPositions.mqh |
|
||||||
|
//| Copyright 2021, Nkondog Anselme Venceslas |
|
||||||
|
//| https://www.mql5.com |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
||||||
|
#property link "https://www.mql5.com"
|
||||||
|
|
||||||
|
//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 = OrdersTotal();
|
||||||
|
gTotalPositions = PositionsTotal();
|
||||||
|
gTotalBuyPositions = 0;
|
||||||
|
gTotalSellPositions = 0;
|
||||||
|
gTotalTransactions = gTotalOpenOrders+gTotalPositions;
|
||||||
|
|
||||||
|
for(int i=0; i<gTotalPositions; 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);
|
||||||
|
Print("ERROR - Unable to select the order - ",Error," - ",Error);
|
||||||
|
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)
|
||||||
|
gTotalBuyPositions++;
|
||||||
|
//If it is a sell order then increment the total count of sell orders
|
||||||
|
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
|
||||||
|
gTotalSellPositions++;
|
||||||
|
|
||||||
|
//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 ", gTotalPositions, " - Total buys ", gTotalBuyPositions, " - Total sells ", gTotalSellPositions);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| TradeManager.mqh |
|
||||||
|
//| Copyright 2021, Nkondog Anselme Venceslas |
|
||||||
|
//| https://www.mql5.com |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
#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
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| TradingHour.mqh |
|
||||||
|
//| Copyright 2021, Nkondog Anselme Venceslas |
|
||||||
|
//| https://www.mql5.com |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
|
||||||
|
#property link "https://www.mql5.com"
|
||||||
|
|
||||||
|
//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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
Reference in New Issue
Block a user