MAGrid initial commit. First position and orders

This commit is contained in:
Nkondog Anselme
2021-11-23 02:04:41 +01:00
parent 46a2c49025
commit 8afa56f9cb
22 changed files with 698 additions and 42 deletions
+28 -17
View File
@@ -5,23 +5,34 @@
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Nkondog Anselme Venceslas"
#property link "https://www.mql5.com"
CTrade trade;
//+------------------------------------------------------------------+
//| 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
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.
+50 -20
View File
@@ -5,23 +5,53 @@
//+------------------------------------------------------------------+
#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
//+------------------------------------------------------------------+
//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);
}
}
+11 -4
View File
@@ -10,8 +10,7 @@
enum ENUM_SIGNAL_ENTRY
{
SIGNAL_ENTRY_NEUTRAL=0, //SIGNAL ENTRY NEUTRAL
SIGNAL_ENTRY_BUY=1, //SIGNAL ENTRY BUY
SIGNAL_ENTRY_SELL=-1, //SIGNAL ENTRY SELL
SIGNAL_ENTRY_ENTER=1, //SIGNAL PENDING BUY/SELL
};
//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 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 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)
@@ -150,7 +149,6 @@ input string InpTradingHourEnd="23"; //Trading End Hour (Br
input string InpTradingStartMin="30"; //Trading Start minute (Broker Server Hour)
input string InpTradingEndMin="00"; //Trading End minute
bool IsOperatingHours=false;
bool gIsNewCandle=false;
bool gIsTradedThisBar=false;
bool gIsOperatingHours=false;
@@ -163,10 +161,19 @@ int gTickValue=0;
int gTotalOpenBuy=0;
int gTotalOpenSell=0;
int gTotalOpenOrders=0;
int gOrderOpRetry=5;
double gBuyStopLossPrice;
double gSellStopLossPrice;
double gBuyEntryPrice;
double gSellEntryPrice;
string gSymbol = Symbol();
datetime gLastBarTraded=NULL;
double gFsma, gSsma;
double LotSize=0;
MqlTick last_tick;
MqlDateTime dt;