Last active
April 8, 2023 13:19
-
-
Save tinogomes/6ec7ee48caa36ac1853d06343bd83e30 to your computer and use it in GitHub Desktop.
MQL5 Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#property copyright "Celestino Ferreira Gomes" | |
#property link "https://blog.tinogomes.com" | |
#property version "1.00" | |
#property indicator_chart_window | |
int OnInit() | |
{ | |
return(INIT_SUCCEEDED); | |
} | |
int OnCalculate(const int rates_total, | |
const int prev_calculated, | |
const datetime &time[], | |
const double &open[], | |
const double &high[], | |
const double &low[], | |
const double &close[], | |
const long &tick_volume[], | |
const long &volume[], | |
const int &spread[]) | |
{ | |
int firstCandle = 0; | |
int initial = prev_calculated > firstCandle ? prev_calculated - firstCandle : firstCandle; | |
for(int i=initial;i<rates_total && !IsStopped() ;i++) | |
{ | |
// Write your code here | |
} | |
return(rates_total); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int shiftStart = 0; | |
int initial = prev_calculated > shiftStart ? prev_calculated - shiftStart : shiftStart; | |
for(int i = initial; i < rates_total && !IsStopped(); i++) | |
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#property indicator_label1 "BDM(O);BDM(H);BDM(L);BDM(C)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.mql5.com/pt/articles/751 | |
#define ERROR -1; | |
//+------------------------------------------------------------------+ | |
//| Checking if a given filling mode is allowed | | |
//+------------------------------------------------------------------+ | |
bool IsFillingTypeAllowed(string symbol, int fill_type) | |
{ | |
//--- Get the value of the property describing the filling mode | |
int filling = (int)SymbolInfoInteger(symbol, SYMBOL_FILLING_MODE); | |
//--- Return true if the fill_type mode is allowed | |
return((filling & fill_type) == fill_type); | |
} | |
//+------------------------------------------------------------------+ | |
//| Checking if a given expiration mode is allowed | | |
//+------------------------------------------------------------------+ | |
bool IsExpirationTypeAllowed(string symbol, int exp_type) | |
{ | |
//--- Get the value of the property describing the allowable expiration modes | |
int expiration = (int)SymbolInfoInteger(symbol, SYMBOL_EXPIRATION_MODE); | |
//--- Return true if the exp_type mode is allowed | |
return((expiration & exp_type) == exp_type); | |
} | |
//+------------------------------------------------------------------+ | |
//| Returning string representation of the order expiration modes | | |
//+------------------------------------------------------------------+ | |
string ExpirationModeToString() | |
{ | |
string str = ""; // For string generation | |
//--- Variables for checking the modes | |
bool gtc = false; // The order is valid for an unlimited time until explicitly canceled | |
bool day = false; // The order is valid until the end of the day | |
bool specified = false; // The expiration time is specified in the order | |
bool specified_day = false; // The expiration date is specified in the order | |
//--- Check the modes | |
gtc = IsExpirationTypeAllowed(_Symbol, SYMBOL_EXPIRATION_GTC); | |
day = IsExpirationTypeAllowed(_Symbol, SYMBOL_EXPIRATION_DAY); | |
specified = IsExpirationTypeAllowed(_Symbol, SYMBOL_EXPIRATION_SPECIFIED); | |
specified_day = IsExpirationTypeAllowed(_Symbol, SYMBOL_EXPIRATION_SPECIFIED_DAY); | |
//--- Generate a string of the modes available | |
if(gtc) | |
{ | |
StringAdd(str, "GTC"); | |
if(day || specified || specified_day) | |
StringAdd(str, " / "); | |
} | |
//--- | |
if(day) | |
{ | |
StringAdd(str, "Day"); | |
if(specified || specified_day) | |
StringAdd(str, " / "); | |
} | |
//--- | |
if(specified) | |
{ | |
StringAdd(str, "Specified"); | |
if(specified_day) | |
StringAdd(str, " / "); | |
} | |
//--- | |
if(specified_day) | |
StringAdd(str, "Specified Day"); | |
//--- | |
return(str); | |
} | |
//+------------------------------------------------------------------+ | |
//| Returning string representation of the volume filling modes | | |
//+------------------------------------------------------------------+ | |
string FillingModeToString() | |
{ | |
//--- Variable for string generation | |
string str = ""; | |
//--- Variables for checking the modes: | |
// "Fill or Kill" - if the required order volume cannot be filled at the specified price, | |
// the order is canceled and the deal is not executed | |
bool fok = false; | |
// "Immediate or Cancel" - if the deal volume can only be partially filled at the price specified in the order, | |
// the deal is executed to the extent of the volume available. The remaining volume of the order is canceled | |
// and the new order is not placed | |
bool ioc = false; | |
// "Return" - The deal is executed to the extent of the volume available at the price specified in the order. | |
// A new order is placed for the remaining volume at the same price | |
bool return_remainder = false; | |
//--- Check the modes | |
fok = IsFillingTypeAllowed(_Symbol, SYMBOL_FILLING_FOK); | |
ioc = IsFillingTypeAllowed(_Symbol, SYMBOL_FILLING_IOC); | |
//--- For "Market execution" and "Exchange execution" modes | |
ENUM_SYMBOL_TRADE_EXECUTION symbol_trade_exemode = (ENUM_SYMBOL_TRADE_EXECUTION)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_EXEMODE); | |
return_remainder = (symbol_trade_exemode == SYMBOL_TRADE_EXECUTION_MARKET || | |
symbol_trade_exemode == SYMBOL_TRADE_EXECUTION_EXCHANGE) ? true : false; | |
//--- Generate a string of the modes available | |
if(fok) | |
{ | |
StringAdd(str, "Fill or Kill"); | |
if(ioc || return_remainder) | |
StringAdd(str, " / "); | |
} | |
//--- | |
if(ioc) | |
{ | |
StringAdd(str, "Immediate or Cancel"); | |
if(return_remainder) | |
StringAdd(str, " / "); | |
} | |
//--- | |
if(return_remainder) | |
StringAdd(str, "Return"); | |
//--- | |
return(str); | |
} | |
//+------------------------------------------------------------------+ | |
//| Returns true if a new bar has appeared for a symbol/period pair | | |
//+------------------------------------------------------------------+ | |
bool IsNewBar() | |
{ | |
//--- memorize the time of opening of the last bar in the static variable | |
static datetime last_time = 0; | |
//--- current time | |
datetime lastbar_time = (datetime) SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE); | |
bool newBar = last_time != lastbar_time; | |
last_time = lastbar_time; | |
return newBar; | |
//--- if it is the first call of the function | |
if(last_time == 0) | |
{ | |
//--- set the time and exit | |
last_time = lastbar_time; | |
return(false); | |
} | |
//--- if the time differs | |
if(last_time != lastbar_time) | |
{ | |
//--- memorize the time and return true | |
last_time = lastbar_time; | |
return(true); | |
} | |
//--- if we passed to this line, then the bar is not new; return false | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int ExtractWeekDay(datetime time) | |
{ | |
MqlDateTime structTime; | |
if(!TimeToStruct(time, structTime)) | |
return ERROR; | |
return structTime.day_of_week; | |
} | |
//+------------------------------------------------------------------+ | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int ExtractDay(datetime time) | |
{ | |
MqlDateTime structTime; | |
if(!TimeToStruct(time, structTime)) | |
return ERROR; | |
return structTime.day; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int ExtractMinute(datetime time) | |
{ | |
MqlDateTime structTime; | |
if(!TimeToStruct(time, structTime)) | |
return ERROR; | |
return structTime.min; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int ExtractHour(datetime time) | |
{ | |
MqlDateTime structTime; | |
if(!TimeToStruct(time, structTime)) | |
return ERROR; | |
return structTime.hour; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
bool IsNewDay(string IsNewDay_symbol, ENUM_TIMEFRAMES IsNewDay_period) | |
{ | |
MqlRates IsNewDay_rates[]; | |
CopyRates(IsNewDay_symbol, IsNewDay_period, 0, 2, IsNewDay_rates); | |
int prev = ExtractWeekDay(IsNewDay_rates[0].time), | |
curr = ExtractWeekDay(IsNewDay_rates[1].time); | |
return prev != curr; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int PrintError(string message) | |
{ | |
Print("ERROR: ", message); | |
return ERROR; | |
} | |
//+------------------------------------------------------------------+ | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
string ExtractSymbolFromDescription(string ExtractSymbolFromDescription_symbol) | |
{ | |
string ExtractSymbolFromDescription_description = SymbolInfoString(_Symbol, SYMBOL_DESCRIPTION); | |
string ExtractSymbolFromDescription__symbol = StringSubstr(ExtractSymbolFromDescription_symbol, 0, 3); | |
int pos = StringFind(ExtractSymbolFromDescription_description, "("+ExtractSymbolFromDescription__symbol); | |
if(pos < 0) | |
return ExtractSymbolFromDescription_symbol; | |
return StringSubstr(ExtractSymbolFromDescription_description, pos+1, 6); | |
} | |
//+------------------------------------------------------------------+ | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void CreateButton(string name, | |
int posX, int posY, | |
int width, int height, | |
string label, color labelColor, | |
color bgColor, color borderColor, | |
string fontName, int fontSize, | |
ENUM_BASE_CORNER corner = CORNER_LEFT_UPPER, | |
string tooltip = NULL) | |
{ | |
ObjectCreate(0, name, OBJ_BUTTON, 0, 0, 0); | |
ObjectSetInteger(0, name, OBJPROP_CORNER, corner); | |
ObjectSetInteger(0, name, OBJPROP_XDISTANCE, posX); | |
ObjectSetInteger(0, name, OBJPROP_YDISTANCE, posY); | |
ObjectSetInteger(0, name, OBJPROP_XSIZE, width); | |
ObjectSetInteger(0, name, OBJPROP_YSIZE, height); | |
ObjectSetInteger(0, name, OBJPROP_BGCOLOR, bgColor); | |
ObjectSetInteger(0, name, OBJPROP_BORDER_COLOR, borderColor); | |
ObjectSetString(0, name, OBJPROP_TEXT, label); | |
ObjectSetInteger(0, name, OBJPROP_COLOR, labelColor); | |
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, fontSize); | |
ObjectSetString(0, name, OBJPROP_FONT, fontName); | |
ObjectSetInteger(0, name, OBJPROP_BACK, false); | |
ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); | |
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false); | |
ObjectSetString(0, name, OBJPROP_TOOLTIP, tooltip); | |
} | |
//+------------------------------------------------------------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment