Create symbolic link to all MT platform for easy manipulation

This commit is contained in:
Nkondog A. Venceslas
2022-11-28 16:27:15 +01:00
parent cbb412a720
commit a70da4fc29
11 changed files with 433 additions and 0 deletions
Binary file not shown.
+211
View File
@@ -0,0 +1,211 @@
//+------------------------------------------------------------------+
//| LotCal.mq4 |
//| Copyright 2022, Nkondog Anselme Venceslas. |
//| https://www.linkedin/in/nkondog.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Nkondog Anselme Venceslas."
#property link "https://www.linkedin/in/nkondog.com "
#property version "1.00"
#property strict
#define KEY_B 66
#define KEY_S 83
//Parameters
//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
RISK_BASE_INPUT=4, //INPUT BASE
};
//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
};
input ENUM_RISK_DEFAULT_SIZE InpRiskDefaultSize=RISK_DEFAULT_AUTO; //Position Size Mode
input double InpBalance=10000.0; //Balance
input double InpDefaultLotSize=0.01; //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 Allowedv
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string Symb = Symbol();
double LotSize=InpDefaultLotSize;
double price=0.0;
double risk=0.0;
double StoplossPips=0.0;
//TickValue is the value of the individual price increment for 1 lot of the instrument, expressed in the account currenty
double TickValue=SymbolInfoDouble(Symb,SYMBOL_TRADE_TICK_VALUE);
int ticket;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Print("The Expert Advisor with name ",MQLInfoString(MQL_PROGRAM_NAME)," is running");
//--- enable object create events
ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_CREATE,true);
//--- enable object delete events
ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_DELETE,true);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
LotSizeCalculate(price);
//Comment("Lot size : ", LotSize);
double StopAmount = StoplossPips * LotSize * TickValue;
string text ="Lot size for "+ InpMaxRiskPerTrade +"% = " + DoubleToString(LotSize,2) + " lot (" + DoubleToString(StopAmount, 2) + " " + AccountInfoString(ACCOUNT_CURRENCY) + ")";
string name = "Lot";
ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name,text, 14, "Corbel Bold", YellowGreen);
ObjectSet(name, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
ObjectSet(name, OBJPROP_XDISTANCE, 350);
ObjectSet(name, OBJPROP_YDISTANCE, 10);
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, // Event identifier
const long& lparam, // Event parameter of long type
const double& dparam, // Event parameter of double type
const string& sparam) // Event parameter of string type
{
//--- the object has been deleted
if(id==CHARTEVENT_OBJECT_DELETE)
{
Print("The object with name ",sparam," has been deleted");
}
//--- the object has been created
if(id==CHARTEVENT_OBJECT_CREATE)
{
Print("The object with name ",sparam," has been created");
}
//--- the object has been moved or its anchor point coordinates has been changed
if(id==CHARTEVENT_OBJECT_DRAG)
{
price = ObjectGetDouble(0, sparam, OBJPROP_PRICE, 0);
///Print("The anchor point coordinates of the object with name ",sparam," has been changed. Price ", price);
}
if(id==CHARTEVENT_KEYDOWN)
{
switch(lparam)
{
case KEY_B:
///SendOrder(TRADE_ACTION_DEAL, ORDER_TYPE_BUY,Symb,last_tick.ask,price,LotSize);
ticket = OrderSend(Symb, OP_BUY, LotSize, Ask, 1, price,0);
Alert("Buy " + LotSize + " lot " + Symb + " at " + Ask + " SL at " + price);
break;
case KEY_S:
ticket = OrderSend(Symb, OP_SELL, LotSize, Bid, 1, price,0);
Alert("Sell " + LotSize + " lot " + Symb + " at " + Bid + " SL at " + price);
break;
default:
//Print("Do nothing");
break;
}
if(ticket<=0)
{
int error=GetLastError();
//---- not enough money
if(error==134);
//---- 10 seconds wait
Sleep(10000);
//---- refresh price data
RefreshRates();
}
else
{
OrderSelect(ticket,SELECT_BY_TICKET);
OrderPrint();
}
}
}
//Lot Size Calculator
void LotSizeCalculate(double stopLoss)
{
double SL=0;
double PriceAsk=MarketInfo(0,MODE_ASK);
double PriceBid=MarketInfo(0,MODE_BID);
if(stopLoss < PriceAsk)
{
SL = (PriceAsk-stopLoss)/_Point;
}
if(stopLoss > PriceAsk)
{
SL = (stopLoss-PriceBid)/_Point;
}
//Print("Stop loss distance ", SL);
//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;
//Define the base for the risk calculation depending on the parameter chosen
if(InpRiskBase==RISK_BASE_BALANCE)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_BALANCE);
if(InpRiskBase==RISK_BASE_EQUITY)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_EQUITY);
if(InpRiskBase==RISK_BASE_FREEMARGIN)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_FREEMARGIN);
if(InpRiskBase==RISK_BASE_INPUT)
RiskBaseAmount=InpBalance;
//Calculate the Position Size
//Print("RiskBaseAmount ", RiskBaseAmount, " MaxRiskPerTrade ", InpMaxRiskPerTrade, "Stop loss ", SL, " TickValue ", TickValue);
LotSize=((RiskBaseAmount*InpMaxRiskPerTrade/100)/(SL*TickValue));
StoplossPips = SL;
}
//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(Symb,SYMBOL_VOLUME_STEP))*SymbolInfoDouble(Symb,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(Symb,SYMBOL_VOLUME_MAX))
LotSize=SymbolInfoDouble(Symb,SYMBOL_VOLUME_MAX);
//Print("Lot ", LotSize, " Max lot ", SymbolInfoDouble(Symb,SYMBOL_VOLUME_MAX));
//If the lot size is too small then set it to 0 and don't trade
if(LotSize < SymbolInfoDouble(Symb,SYMBOL_VOLUME_MIN))
{
LotSize=0;
Alert("Lot size too small");
}
}
//+------------------------------------------------------------------+
Binary file not shown.
Binary file not shown.
+236
View File
@@ -0,0 +1,236 @@
//+------------------------------------------------------------------+
//| LotCal.mq4 |
//| Copyright 2022, Nkondog Anselme Venceslas. |
//| https://www.linkedin/in/nkondog.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Nkondog Anselme Venceslas."
#property link "https://www.linkedin/in/nkondog.com "
#property version "2.00" //Handle take profit ant dymanic horizontal line price detection
#property strict
#define KEY_B 66
#define KEY_S 83
//Parameters
//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
RISK_BASE_INPUT=4, //INPUT BASE
};
//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
};
input ENUM_RISK_DEFAULT_SIZE InpRiskDefaultSize=RISK_DEFAULT_AUTO; //Position Size Mode
input double InpBalance=10000.0; //Balance
input double InpDefaultLotSize=0.01; //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 InpTPMultiple=1; //TP multiple %
input double InpMinLotSize=0.01; //Minimum Position Size Allowed
input double InpMaxLotSize=100; //Maximum Position Size Allowedv
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string Symb = Symbol();
double LotSize=InpDefaultLotSize;
double stopLoss=0.0;
double TakeProfit=0.0;
double risk=0.0;
double StoplossPips=0.0;
//TickValue is the value of the individual price increment for 1 lot of the instrument, expressed in the account currenty
double TickValue=SymbolInfoDouble(Symb,SYMBOL_TRADE_TICK_VALUE);
int ticket;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Print("The Expert Advisor with name ",MQLInfoString(MQL_PROGRAM_NAME)," is running");
//--- enable object create events
ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_CREATE,true);
//--- enable object delete events
ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_DELETE,true);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
stopLoss = NormalizeDouble(ObjectGetDouble(0, "sl", OBJPROP_PRICE), _Digits);
displayOnChart();
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, // Event identifier
const long& lparam, // Event parameter of long type
const double& dparam, // Event parameter of double type
const string& sparam) // Event parameter of string type
{
//--- the object has been deleted
if(id==CHARTEVENT_OBJECT_DELETE)
{
Print("The object with name ",sparam," has been deleted");
}
//--- the object has been created
if(id==CHARTEVENT_OBJECT_CREATE)
{
Print("The object with name ",sparam," has been created");
}
/*--- the object has been moved or its anchor point coordinates has been changed
if(id==CHARTEVENT_OBJECT_DRAG)
{
price = ObjectGetDouble(0, sparam, OBJPROP_PRICE, 0);
//Print("The anchor point coordinates of the object with name ",sparam," has been changed. Price ", price);
displayOnChart();
}*/
if(id==CHARTEVENT_KEYDOWN)
{
switch(lparam)
{
case KEY_B:
///SendOrder(TRADE_ACTION_DEAL, ORDER_TYPE_BUY,Symb,last_tick.ask,price,LotSize);
ticket = OrderSend(Symb, OP_BUY, LotSize, Ask, 1, stopLoss, TakeProfit);
Alert("Buy " + (string)LotSize + " lot " + Symb + " at " + (string)Ask + " SL at " + (string)stopLoss);
break;
case KEY_S:
ticket = OrderSend(Symb, OP_SELL, LotSize, Bid, 1, stopLoss,TakeProfit);
Alert("Sell " + (string)LotSize + " lot " + Symb + " at " + (string)Bid + " SL at " + (string)stopLoss);
break;
default:
//Print("Do nothing");
break;
}
if(ticket<=0)
{
int error=GetLastError();
//---- not enough money
//if(error==134);
//---- 10 seconds wait
Sleep(10000);
//---- refresh price data
RefreshRates();
}
else
{
OrderSelect(ticket,SELECT_BY_TICKET);
OrderPrint();
}
}
}
//Lot Size Calculator
void LotSizeCalculate(double sLoss)
{
double SL=0;
double PriceAsk=MarketInfo(0,MODE_ASK);
double PriceBid=MarketInfo(0,MODE_BID);
double spread = MarketInfo(0,MODE_SPREAD) * _Point;
double pipDiff = 0.0;
if(sLoss < PriceAsk)
{
pipDiff = PriceAsk-sLoss;
SL = (PriceAsk-sLoss)/_Point;
//Print("TakeProfit ", TakeProfit, " PriceAsk ", PriceAsk, " pipDiff ", pipDiff, " InpTPMultiple ", InpTPMultiple, " spread ", spread);
TakeProfit = PriceAsk + (pipDiff * InpTPMultiple) + (spread*2);
}
if(sLoss > PriceAsk)
{
pipDiff = sLoss-PriceBid;
SL = (sLoss-PriceBid)/_Point;
//Print("TakeProfit ", TakeProfit, " PriceAsk ", PriceBid, " pipDiff ", pipDiff, " InpTPMultiple ", InpTPMultiple, " spread ", spread);
TakeProfit = PriceBid - (pipDiff * InpTPMultiple) - (spread*2);
}
TakeProfit = NormalizeDouble(TakeProfit, _Digits);
//Print("Stop loss distance ", SL);
//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;
//Define the base for the risk calculation depending on the parameter chosen
if(InpRiskBase==RISK_BASE_BALANCE)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_BALANCE);
if(InpRiskBase==RISK_BASE_EQUITY)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_EQUITY);
if(InpRiskBase==RISK_BASE_FREEMARGIN)
RiskBaseAmount=AccountInfoDouble(ACCOUNT_FREEMARGIN);
if(InpRiskBase==RISK_BASE_INPUT)
RiskBaseAmount=InpBalance;
//Calculate the Position Size
//Print("RiskBaseAmount ", RiskBaseAmount, " MaxRiskPerTrade ", InpMaxRiskPerTrade, "Stop loss ", SL, " TickValue ", TickValue);
LotSize=((RiskBaseAmount*InpMaxRiskPerTrade/100)/(SL*TickValue));
StoplossPips = SL;
}
//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(Symb,SYMBOL_VOLUME_STEP))*SymbolInfoDouble(Symb,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(Symb,SYMBOL_VOLUME_MAX))
LotSize=SymbolInfoDouble(Symb,SYMBOL_VOLUME_MAX);
//Print("Lot ", LotSize, " Max lot ", SymbolInfoDouble(Symb,SYMBOL_VOLUME_MAX));
//If the lot size is too small then set it to 0 and don't trade
if(LotSize < SymbolInfoDouble(Symb,SYMBOL_VOLUME_MIN))
{
LotSize=0;
//Print("Lot size too small");
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void displayOnChart()
{
LotSizeCalculate(stopLoss);
//Comment("Lot size : ", LotSize);
double StopAmount = StoplossPips * LotSize * TickValue;
string text ="Lot size for "+ (string)InpMaxRiskPerTrade +"% = " + DoubleToString(LotSize,2) + " lot (" + DoubleToString(StopAmount, 2) + " " + AccountInfoString(ACCOUNT_CURRENCY) + ")";
string name = "Lot";
ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name,text, 14, "Corbel Bold", YellowGreen);
ObjectSet(name, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
ObjectSet(name, OBJPROP_XDISTANCE, 350);
ObjectSet(name, OBJPROP_YDISTANCE, 10);
}
//+------------------------------------------------------------------+
@@ -0,0 +1,126 @@
//+------------------------------------------------------------------+
//| MuzzlingAlligatorWatcher.mq4 |
//| Copyright 2022, Nkondog Anselme Venceslas. |
//| https://www.linkedin.com/in/nkondog |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Nkondog Anselme Venceslas."
#property link "ttps://www.linkedin.com/in/nkondog"
#property version "1.00"
#property strict
#include <Lib/Navlib.mqh>
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
input string Comment_0="=========="; //Alligator parameters
input ENUM_TIMEFRAMES inpTimeframe = PERIOD_CURRENT; //Timeframe
input int inpJawsPeriod = 13; //Jaws period
input int inpJawsShift = 8; //Jaws shift
input int inpTeethPeriod = 8; //Teeth period
input int inpTeethShift = 5; //Teeth shift
input int inpLipsPeriod = 5; //Lips period
input int inpLipsShift = 3; //Lips shift
input ENUM_MA_METHOD inpMethod = MODE_SMMA; //Method
input ENUM_APPLIED_PRICE inpApplyedTo = PRICE_MEDIAN; //Applied to
input string Comment_1="=========="; //Moving average parameters
input ENUM_MA_METHOD inpMAMethod = MODE_SMA; //MA method
input int inpMAPeriod = 200; //MA period
input int inpMASHift = 0; //MA shift
input ENUM_APPLIED_PRICE inpMAApplyedTo = PRICE_CLOSE; //MA applied to
double jaws, teeth, lips, sma, prevCandleHigh, prevCandleLow, currentPrice, candleClose;
string comm = "";
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(!newCandle())
return;
jaws=iAlligator(NULL,0,inpJawsPeriod,inpJawsShift,inpTeethPeriod,inpTeethShift,inpLipsPeriod,inpLipsShift,MODE_SMMA,PRICE_MEDIAN,MODE_GATORJAW,0);
teeth=iAlligator(NULL,0,inpJawsPeriod,inpJawsShift,inpTeethPeriod,inpTeethShift,inpLipsPeriod,inpLipsShift,MODE_SMMA,PRICE_MEDIAN,MODE_GATORTEETH,0);
lips=iAlligator(NULL,0,inpJawsPeriod,inpJawsShift,inpTeethPeriod,inpTeethShift,inpLipsPeriod,inpLipsShift,MODE_SMMA,PRICE_MEDIAN,MODE_GATORLIPS,0);
sma = iMA(NULL,0,inpMAPeriod,inpMASHift,inpMAMethod,inpMAApplyedTo,1);
Print(" Jaws ", jaws, " teeth ", teeth, " lips ", lips, " sma ", sma);
//Get previous candle
prevCandleHigh = iHigh(NULL, PERIOD_CURRENT, 1);
prevCandleLow = iLow(NULL, PERIOD_CURRENT, 1);
currentPrice = iClose(NULL, PERIOD_CURRENT, 0);
candleClose = iLow(NULL, PERIOD_CURRENT, 0);
//comm = "jaws " + (string)jaws + " teeth " + (string)teeth + " lips " + (string)lips + " sma " + (string)sma;
comm = "Trade alert on " + Symbol();
comm += "\n";
comm += "";
if(sma < currentPrice)
{
//Alert for bullish continuation signal
if(prevCandleHigh > jaws && prevCandleHigh > teeth && prevCandleHigh > lips)
{
if(prevCandleLow < jaws || prevCandleLow < teeth ||prevCandleLow < lips)
{
comm += "LONG CONTINUATION SIGNAL: Price above SMA just moves above Alligator. \n";
Notify(comm);
}
}
//Alert for bearish counter trend signal
if(lips > teeth && teeth > jaws)
{
if(candleClose < lips && candleClose < teeth && candleClose < jaws)
{
comm += " SHORT COUNTER TREND SIGNAL: Price above SMA moves below Alligator in a trending market \n";
Notify(comm);
}
}
}
if(sma > currentPrice)
{
//Alert for bearish continuation signal
if(prevCandleLow < jaws && prevCandleLow < teeth && prevCandleLow < lips)
{
if(prevCandleHigh > jaws || prevCandleHigh > teeth ||prevCandleHigh > lips)
{
comm += " SHORT CONTINUATION SIGNAL: Price below SMA just moves below Alligator. \n";
Notify(comm);
}
}
//Alert for bearish counter trend signal
if(lips < teeth && teeth < jaws)
{
if(candleClose > lips && candleClose > teeth && candleClose > jaws)
{
comm += " LONG COUNTER TREND SIGNAL: Price below SMA just closes above Alligator in a down trending market \n";
Notify(comm);
}
}
}
}
//+------------------------------------------------------------------+
Binary file not shown.
+51
View File
@@ -0,0 +1,51 @@
//+------------------------------------------------------------------+
//| NewCandleAlert.mq4 |
//| Copyright 2022, Nkondog Anselme Venceslas. |
//| https://www.linkedin.com/in/nkondog |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Nkondog Anselme Venceslas."
#property link "https://www.linkedin.com/in/nkondog"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
newBar();
}
//+------------------------------------------------------------------+
bool newBar()
{
static datetime prevTime = 0;
datetime currentTime = iTime(Symbol(), PERIOD_CURRENT, 0);
if(currentTime != prevTime)
{
prevTime = currentTime;
Alert("New candle");
return(true);
}
return(false);
}
Binary file not shown.
+236
View File
@@ -0,0 +1,236 @@
//+------------------------------------------------------------------+
//| Telegram2MT4.mq4 |
//| Copyright 2022, Nkondog Anselme Venceslas. |
//| https://www.linkedin.com/in/nkondog |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Nkondog Anselme Venceslas."
#property link "https://www.linkedin.com/in/nkondog"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
enum ENUM_RISK_DEFAULT
{
RISK_DEFAULT_FIXED = 1,
RISK_DEFAULT_AUTO = 2,
};
input ENUM_RISK_DEFAULT inpRiskDefault = RISK_DEFAULT_FIXED; //Lot sizing mode
input double inpDefaultLotSize = 0.01; //Default lot
input double inpRiskPercent = 0.1; //Risk percentage
input double inpMaxLotSize = 10; //Max lot
input int inpMaxSlippage = 3; //Max slippage
input int inpMagicNumber = 1987; //Magic number
input string inpSymbolPrefix = ""; //Symbol prefix
input string inpSymbolSuffix = ""; //Symbol suffix
double vbid,vask,vpoint;
int vdigits, vspread;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
//---
checkForPermissions();
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
//read file writen by TelegramReader
readTelegramFile();
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void readTelegramFile()
{
//--- open the file
string fileName = "order.bin";
ResetLastError();
string table[];
int file_handle=FileOpen("Telegramreader"+"//"+fileName,FILE_READ|FILE_ANSI, " ");
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is available for reading",fileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
//--- additional variables
int str_size;
string str;
int i = 0;
//--- read data from the file
while(!FileIsEnding(file_handle))
{
//--- find out how many symbols are used for writing the time
str_size=FileReadInteger(file_handle,INT_VALUE);
//--- read the string
str=FileReadString(file_handle,str_size);
//--- print the string
Print("i" + i + " " + str);
ArrayResize(table, ArraySize(table) + 1);
table[i] = str;
i += 1;
}
//--- close the file
FileClose(file_handle);
//PrintFormat("Data is read, %s file is closed",fileName);
FileDelete("Telegramreader"+"//"+fileName);
sendOrder(table[2], table[0], table[1], NormalizeDouble(table[3], Digits), NormalizeDouble(table[4], Digits), NormalizeDouble(table[5], Digits));
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void sendOrder(string type, string direction, string symbol, double entryPrice, double takeProfit, double stopLoss)
{
int ticket = 0;
symbol = inpSymbolPrefix+symbol+inpSymbolSuffix;
double lotSize;
vbid = MarketInfo(symbol,MODE_BID);
vask = MarketInfo(symbol,MODE_ASK);
vpoint = MarketInfo(symbol,MODE_POINT);
vdigits = (int)MarketInfo(symbol,MODE_DIGITS);
vspread = (int)MarketInfo(symbol,MODE_SPREAD);
Print(type, direction, symbol, entryPrice, takeProfit, stopLoss);
if(type == "NOW")
{
if(direction == "BUY")
{
lotSize = LotSizeCalculate(OP_BUY, stopLoss, symbol, vpoint);
Print("Lot size ", lotSize);
ticket=OrderSend(symbol,OP_BUY,lotSize,vask,inpMaxSlippage, stopLoss,takeProfit,"Trade from eInvestors",inpMagicNumber,0,Green);
}
if(direction == "SELL")
{
lotSize = LotSizeCalculate(OP_SELL, stopLoss, symbol, vpoint);
Print("Lot size ", lotSize);
ticket=OrderSend(symbol,OP_SELL,lotSize,vbid,inpMaxSlippage, stopLoss,takeProfit,"Trade from eInvestors",inpMagicNumber,0,Green);
}
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else
Print("Error opening BUY order : ",GetLastError());
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Compute lot size function |
//+------------------------------------------------------------------+
double LotSizeCalculate(int ordertype, double stoploss, string symbol, double vpoint)
{
double tickValue = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE);
double riskBaseAmount;
double lotSize = 0;
double SL = 0;
if(stoploss > 0)
{
if(ordertype == OP_BUY)
{
SL = (vask-stoploss)/vpoint;
}
else
if(ordertype == OP_SELL)
{
SL = (stoploss-vbid)/vpoint;
}
}
//Print("SL ", SL, " risk base ", AccountInfoDouble(ACCOUNT_BALANCE), "tick value ", tickValue);
Print("if statement ", SL != 0 && inpRiskDefault == RISK_DEFAULT_AUTO);
if(SL != 0 && inpRiskDefault == RISK_DEFAULT_AUTO)
{
riskBaseAmount = AccountInfoDouble(ACCOUNT_BALANCE);
lotSize = ((riskBaseAmount*inpRiskPercent/100)/(SL*tickValue));
}
else
{
lotSize = inpDefaultLotSize;
}
lotSize = MathFloor(lotSize/SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP))*SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP);
if(lotSize > inpMaxLotSize)
{
lotSize = inpMaxLotSize;
}
if(lotSize > SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX))
{
lotSize = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX);
}
if(lotSize < SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN))
{
lotSize = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN);
}
return lotSize;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void checkForPermissions()
{
if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
Alert("Check if automated trading is allowed in the terminal settings!");
if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
Alert("Check if automated trading is allowed in the terminal settings!");
else
{
if(!MQLInfoInteger(MQL_TRADE_ALLOWED))
Alert("Automated trading is forbidden in the program settings for ",__FILE__);
}
if(!AccountInfoInteger(ACCOUNT_TRADE_EXPERT))
Alert("Automated trading is forbidden for the account ",AccountInfoInteger(ACCOUNT_LOGIN),
" at the trade server side");
if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED))
Comment("Trading is forbidden for the account ",AccountInfoInteger(ACCOUNT_LOGIN),
".\n Perhaps an investor password has been used to connect to the trading account.",
"\n Check the terminal journal for the following entry:",
"\n\'",AccountInfoInteger(ACCOUNT_LOGIN),"\': trading has been disabled - investor mode.");
}
//+------------------------------------------------------------------+