RenkoBR/CustomCharts.mqh
super.admin 63d7c86ec6 convert
2025-05-30 16:20:23 +02:00

643 lines
47 KiB
MQL5

//+------------------------------------------------------------------+
//| CustomCharts.mqh |
//| Copyright 2019, Guilherme Santos. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Guilherme Santos."
#property link "fishguil@gmail.com"
#property version "1.1"
//+------------------------------------------------------------------+
//| defines |
//+------------------------------------------------------------------+
#define TIME_MIN D'1970.01.01 00:00'
#define TIME_MAX D'3000.12.31 23:59'
#define CUSTOM_SYMBOL_PATH "RenkoBR"
//+------------------------------------------------------------------+
//| types |
//+------------------------------------------------------------------+
enum ENUM_CUSTOM_TYPE
{
RENKO_TYPE_TICKS, //Renko Ticks
RENKO_TYPE_PIPS, //Renko Pips
RENKO_TYPE_POINTS, //Renko Points
RENKO_TYPE_R, //Renko R (-1 Tick)
POINTS_TYPE_TICKS, //Point Ticks (Ponto Inversão)
POINTS_TYPE_PIPS, //Point Pips
POINTS_TYPE_POINTS, //Point
POINTS_TYPE_P, //Point R (-1 Tick)
};
//+------------------------------------------------------------------+
//| variaveis globais |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| class |
//+------------------------------------------------------------------+
class CustomCharts
{
//Internal Variables
private:
//Buffers
MqlTick ticks[], //Ticks buffer
custom_ticks[]; //Custom Ticks buffer
MqlRates rates[], //Rates buffer
custom_rates[]; //Custom Rates buffer
datetime real_time[]; //Real time buffer
//Cursor
long last_tick; //Tick cursor
datetime last_rate; //Rate cursor
//Counters
int rates_count, //Rates Count
ticks_count, //Ticks Count
custom_rates_count, //Custom Rates Count
custom_ticks_count; //Custom Ticks Count
//Config
ENUM_CUSTOM_TYPE custom_type; //Custom Type
string chart_symbol, //Original symbol
custom_symbol; //Custom symbol
double chart_size, //Chart size
brick_size, //Brick size
tick_size, //Tick size
up_wick, //Upper wick size
down_wick; //Down wick size
bool show_wicks, //Show wicks
asymetric_reversal, //Asymetric Reversal
gaps, //Gaps
level; //Level
//History
datetime rates_history; //Rates History
bool real_ticks, //Real Ticks
clear_history, //Clear History
advance; //Ajust time
//Internals
int tick_msc; //Tick msc
//Tick counters
datetime calc_tick_time;
//Methods
public:
CustomCharts();
~CustomCharts();
bool Setup(string symbol, ENUM_CUSTOM_TYPE type, double size, bool wicks, bool asymetric, bool gaps, bool level, bool advance
, datetime rates_history, bool real_ticks, bool clear_history
);
datetime GetTime();
int UpdateRates(datetime from);
int UpdateTicks(datetime from);
//Custom Symbol Methods
bool CreateCustomSymbol(string name);
int UpdateCustomRates();
int AddCustomTicks();
//Event Methods
void Refresh();
void CarregaHistorico();
//Internal Methods
private:
int AddOne(datetime time, double price);
int CloseUp(double points, datetime time);
int CloseDown(double points, datetime time);
int OnCalculate(double price, datetime time);
int OnCalculate(const MqlRates &price);
int OnCalculate(const MqlTick &price);
int OnCalculateOHLC(const MqlRates &price);
};
//+------------------------------------------------------------------+
//| methods |
//+------------------------------------------------------------------+
//Default Constructors
CustomCharts::CustomCharts()
{
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CustomCharts::~CustomCharts()
{
ArrayFree(rates);
ArrayFree(custom_rates);
ArrayFree(ticks);
ArrayFree(custom_ticks);
SymbolSelect(custom_symbol, false);
CustomSymbolDelete(custom_symbol);
}
//Setup Renko
bool CustomCharts::Setup(
string wp_symbol = "", // Symbol (Default = current)
ENUM_CUSTOM_TYPE wp_type = RENKO_TYPE_TICKS, // Type
double wp_size = 14, // Brick Size (Ticks, Pips, Points)
bool wp_wicks = true, // Show Wicks
bool wp_asymetric = true, // Asymetric Reversals (Artificial open on reversal. RENKO ONLY!)
bool wp_gaps = false, // Gaps
bool wp_level = true, // Level Bricks
bool wp_advance = true, // Advance in time
datetime wp_rates_history = 0, // History: Rates (Default is 7 Days 1M OHLC rates.)
bool wp_real_ticks = false, // History: Real Ticks (History based on real ticks. SLOWER!)
bool wp_clear_history = true // History: Clear Custom Symbol
)
{
//Check Symbol
if(wp_symbol == "" || wp_symbol == NULL)
{
Print("Line:", __LINE__, " - Invalid symbol selected.");
return(false);
}
//Select Symbol
if(SymbolSelect(wp_symbol, true) == false)
{
Print("Line:", __LINE__, " - Symbol selection error.");
return(false);
}
//Buffers
this.rates_count = this.custom_rates_count = ArrayResize(custom_rates, 0, 1000);
this.ticks_count = this.custom_ticks_count = ArrayResize(custom_ticks, 0, 1000);
this.last_rate = 0;
this.last_tick = 0;
//Chart setup
this.chart_symbol = wp_symbol;
this.custom_type = wp_type;
this.chart_size = wp_size;
this.show_wicks = wp_wicks;
this.asymetric_reversal = wp_asymetric;
this.gaps = ((ENUM_SYMBOL_CALC_MODE)SymbolInfoInteger(wp_symbol, SYMBOL_TRADE_CALC_MODE) == SYMBOL_CALC_MODE_FOREX) ? false : wp_gaps;
this.level = wp_level;
//History
if(wp_rates_history == 0)
wp_rates_history = TruncateTime(TimeTradeServer(), PERIOD_D1) - 7 * 86400; //Default is 7 days
this.rates_history = wp_rates_history;
this.real_ticks = wp_real_ticks;
this.clear_history = wp_clear_history;
this.advance = wp_advance;
//Brick size
int digits = (int) SymbolInfoInteger(wp_symbol, SYMBOL_DIGITS);
double points = SymbolInfoDouble(wp_symbol, SYMBOL_POINT);
this.tick_size = SymbolInfoDouble(wp_symbol, SYMBOL_TRADE_TICK_SIZE);
double pip_size = (digits == 5 || digits == 3) ? points * 10 : points;
if(custom_type == POINTS_TYPE_TICKS || custom_type == RENKO_TYPE_TICKS) brick_size = chart_size * tick_size;
else if(custom_type == POINTS_TYPE_PIPS || custom_type == RENKO_TYPE_PIPS) brick_size = chart_size * pip_size;
else if(custom_type == POINTS_TYPE_P || custom_type == RENKO_TYPE_R) brick_size = chart_size * tick_size - tick_size;
else brick_size = chart_size;
brick_size = NormalizeDouble(brick_size, digits);
//Invalid brick size
if(brick_size <= 0)
{
Print("Line:", __LINE__, " - Invalid brick size. Value of ", brick_size, " selected.");
return(false);
}
//Minimum brick size
if(brick_size < tick_size)
{
Print("Line:", __LINE__, " - Invalid brick size. Minimum value of ", tick_size, " will be used.");
brick_size = tick_size;
}
//Success
return(true);
}
//Add one to buffer array
int CustomCharts::AddOne(datetime time = 0, double price = 0)
{
//Resize buffers
int index = ArrayResize(custom_rates, ArraySize(custom_rates) + 1, 1000) - 1;
ArrayResize(real_time, ArraySize(custom_rates), 1000);
if(index <= 0) return 0;
//Time
if(time == 0) time = TimeTradeServer();
real_time[index] = time;
tick_msc = 0;
//M1 Rates indexing
time = TruncateTime(time, PERIOD_M1);
if(time <= custom_rates[index - 1].time)
custom_rates[index].time = custom_rates[index - 1].time + 60;
else
custom_rates[index].time = time;
//Defaults
if(price == 0) price = custom_rates[index - 1].close;
custom_rates[index].open = custom_rates[index].high = custom_rates[index].low = custom_rates[index].close = price;
return index;
}
//Add positive points bar
int CustomCharts::CloseUp(double points, datetime time = 0)
{
int index = ArraySize(custom_rates) - 1;
//OHLC
double price = custom_rates[index - 1].close;
if(TruncateTime(time, PERIOD_D1) != TruncateTime(custom_rates[index - 1].time, PERIOD_D1))
price = custom_rates[index].open;
if(asymetric_reversal) custom_rates[index].open = price + points - brick_size;
else custom_rates[index].open = price;
custom_rates[index].high = custom_rates[index].close = price + points;
//Wicks
if(show_wicks) custom_rates[index].low = down_wick;
else custom_rates[index].low = custom_rates[index].open;
down_wick = custom_rates[index].close;
//Add one
return AddOne(time);
}
//Add negative points bar
int CustomCharts::CloseDown(double points, datetime time = 0)
{
int index = ArraySize(custom_rates) - 1;
//OHLC
double price = custom_rates[index - 1].close;
if(TruncateTime(time, PERIOD_D1) != TruncateTime(custom_rates[index - 1].time, PERIOD_D1))
price = custom_rates[index].open;
if(asymetric_reversal) custom_rates[index].open = price - points + brick_size;
else custom_rates[index].open = price;
custom_rates[index].low = custom_rates[index].close = price - points;
//Wicks
if(show_wicks) custom_rates[index].high = up_wick;
else custom_rates[index].high = custom_rates[index].open;
up_wick = custom_rates[index].close;
//Add one
return AddOne(time);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CustomCharts::OnCalculate(double price, datetime time = 0)
{
//Wicks
up_wick = MathMax(up_wick, price);
down_wick = MathMin(down_wick, price);
if(down_wick <= 0) down_wick = price;
//Time
datetime current_time = TruncateTime(SymbolInfoInteger(chart_symbol, SYMBOL_TIME), PERIOD_M1);
//Buffer
int size = ArraySize(custom_rates);
int index = size - 1;
//First bricks
if(size == 0)
{
//1st Buffers
ArrayResize(custom_rates, 2, 1000);
custom_rates[0].time = time - 120;
custom_rates[0].open = NormalizeDouble(MathFloor(price / brick_size) * brick_size, _Digits) - brick_size;
custom_rates[0].high = custom_rates[0].low = custom_rates[0].close = custom_rates[0].open;
custom_rates[1].time = time - 60;
custom_rates[1].open = custom_rates[1].low = custom_rates[0].close;
custom_rates[1].high = custom_rates[1].close = custom_rates[0].close + brick_size;
//Current Buffer
up_wick = down_wick = price;
index = AddOne(time);
}
// New Daily Open
if(this.gaps)
if(TruncateTime(time, PERIOD_D1) != TruncateTime(real_time[index], PERIOD_D1))
{
if(this.level)
price = NormalizeDouble(MathFloor(price / brick_size) * brick_size, _Digits);
up_wick = down_wick = price;
index = AddOne(time, price);
}
// Points
if(custom_type == POINTS_TYPE_TICKS
|| custom_type == POINTS_TYPE_PIPS
|| custom_type == POINTS_TYPE_POINTS)
{
//Fill
//Up
if(price >= custom_rates[index].open + brick_size)
for(; price >= custom_rates[index].open + brick_size;)
index = CloseUp(brick_size, time);
//Down
if(price <= custom_rates[index].open - brick_size)
for(; price <= custom_rates[index].open - brick_size;)
index = CloseDown(brick_size, time);
}
else if(custom_type == POINTS_TYPE_P)
{
//Up
if(price > custom_rates[index].open)
for(; price > custom_rates[index].open + brick_size;)
index = CloseUp(brick_size, time);
//Down
if(price < custom_rates[index].open)
for(; price < custom_rates[index].open - brick_size;)
index = CloseDown(brick_size, time);
}
//Renko
else if(custom_type == RENKO_TYPE_TICKS
|| custom_type == RENKO_TYPE_PIPS
|| custom_type == RENKO_TYPE_POINTS)
{
//Up
if(custom_rates[index - 1].close >= custom_rates[index - 2].close)
{
if(price >= custom_rates[index - 1].close + brick_size)
{
for(; price >= custom_rates[index - 1].close + brick_size;)
index = CloseUp(brick_size, time);
}
//Reversal
else if(price <= custom_rates[index - 1].close - brick_size * 2.0)
{
index = CloseDown(brick_size * 2.0, time);
for(; price <= custom_rates[index - 1].close - brick_size;)
index = CloseDown(brick_size, time);
}
}
//Down
if(custom_rates[index - 1].close <= custom_rates[index - 2].close)
{
if(price <= custom_rates[index - 1].close - brick_size)
{
for(; price <= custom_rates[index - 1].close - brick_size;)
index = CloseDown(brick_size, time);
}
//Reversal
else if(price >= custom_rates[index - 1].close + brick_size * 2.0)
{
index = CloseUp(brick_size * 2.0, time);
for(; price >= custom_rates[index - 1].close + brick_size;)
index = CloseUp(brick_size, time);
}
}
}
else if(custom_type == RENKO_TYPE_R)
{
//Up
if(custom_rates[index - 1].close >= custom_rates[index - 2].close)
{
if(price > custom_rates[index - 1].close + brick_size)
{
for(; price > custom_rates[index - 1].close + brick_size;)
index = CloseUp(brick_size, time);
}
//Reversal
else if(price < custom_rates[index - 1].close - brick_size * 2.0)
{
index = CloseDown(brick_size * 2.0, time);
for(; price < custom_rates[index - 1].close - brick_size;)
index = CloseDown(brick_size, time);
}
}
//Down
if(custom_rates[index - 1].close <= custom_rates[index - 2].close)
{
if(price < custom_rates[index - 1].close - brick_size)
{
for(; price < custom_rates[index - 1].close - brick_size;)
index = CloseDown(brick_size, time);
}
//Reversal
else if(price > custom_rates[index - 1].close + brick_size * 2.0)
{
index = CloseUp(brick_size * 2.0, time);
for(; price > custom_rates[index - 1].close + brick_size;)
index = CloseUp(brick_size, time);
}
}
}
//Advance in time
if(!this.advance)
if(custom_rates[index].time > current_time)
{
datetime from = 0;
custom_rates[index].time = current_time;
CustomTicksDelete(custom_symbol, current_time * 1000, LONG_MAX);
for(int i = index; i > 1; i--)
if(custom_rates[i].time <= custom_rates[i - 1].time)
custom_rates[i - 1].time = from = custom_rates[i].time - 60;
else
break;
}
//Buffer
custom_rates[index].high = up_wick;
custom_rates[index].low = down_wick;
custom_rates[index].close = price;
last_rate = custom_rates[index].time;
size = rates_count = ArraySize(custom_rates);
return size;
}
//Load price rates information
int CustomCharts::OnCalculate(const MqlRates &price)
{
//Price
return this.OnCalculate(price.close, price.time);
}
//Load price tick information
int CustomCharts::OnCalculate(const MqlTick &price)
{
//Truncate current time
datetime time = TruncateTime(price.time, PERIOD_M1);
//Calculate bars
int size = 0;
if(price.bid > price.ask) //Auction
size = 0;
else if(SymbolInfoInteger(custom_symbol, SYMBOL_CHART_MODE) == SYMBOL_CHART_MODE_BID && price.bid > 0)
size = this.OnCalculate(price.bid, time);
else if(SymbolInfoInteger(custom_symbol, SYMBOL_CHART_MODE) == SYMBOL_CHART_MODE_LAST && price.last > 0)
size = this.OnCalculate(price.last, time);
calc_tick_time = time;
return size;
}
//Load OHLC price rates information
int CustomCharts::OnCalculateOHLC(const MqlRates &price)
{
this.OnCalculate(price.open, price.time);
if(price.close > price.open)
{
this.OnCalculate(price.low, price.time);
this.OnCalculate(price.high, price.time);
}
else
{
this.OnCalculate(price.high, price.time);
this.OnCalculate(price.low, price.time);
}
return this.OnCalculate(price.close, price.time);
}
//Update rates
int CustomCharts::UpdateRates(datetime from = 0)
{
ResetLastError();
int copied = 0;
//Copy rates history
if(from == 0) from = TimeTradeServer();
if(rates_count == 0)
{
//History
copied = CopyRates(chart_symbol, PERIOD_M1, from, TimeTradeServer(), rates);
if(copied > 0)
for(int i = 0; i < copied; i++)
this.OnCalculateOHLC(rates[i]);
}
else
{
//Last rate
copied = CopyRates(chart_symbol, PERIOD_M1, last_rate, TimeTradeServer(), rates);
if(copied > 0)
for(int i = 0; i < copied; i++)
this.OnCalculate(rates[i]);
}
//Return
copied = ArraySize(custom_rates);
return copied;
}
//Update ticks
int CustomCharts::UpdateTicks(datetime from = 0)
{
ResetLastError();
//Copy ticks history
if(from == 0) from = TimeTradeServer();
int copied = ArrayResize(custom_ticks, 0, 1000);
if(ticks_count == 0)
if(rates_count == 0)
copied = CopyTicksRange(chart_symbol, custom_ticks, COPY_TICKS_ALL, 1000 * (long) from);
else
copied = CopyTicks(chart_symbol, custom_ticks, COPY_TICKS_ALL, 0, 1);
else
copied = CopyTicks(chart_symbol, custom_ticks, COPY_TICKS_ALL, last_tick + 1, 1000);
//Discard repeated ticks
if(copied <= 0) return 0;
if(custom_ticks[0].time_msc <= last_tick) return 0;
ticks_count += copied;
//Update custom tick
if(tick_msc < 999) tick_msc++;
for(int i = 0; i < copied; i++)
{
this.OnCalculate(custom_ticks[i]);
last_tick = custom_ticks[i].time_msc;
custom_ticks[i].time = GetTime();
custom_ticks[i].time_msc = custom_ticks[i].time * 1000 + tick_msc;
if(custom_ticks[i].last > 0)
{
custom_ticks[i].ask = custom_ticks[i].last;
custom_ticks[i].bid = custom_ticks[i].last;
}
else
{
custom_ticks[i].ask = custom_ticks[i].bid;
}
}
return copied;
}
//+------------------------------------------------------------------+
//| output methods |
//+------------------------------------------------------------------+
//Get values
datetime CustomCharts::GetTime()
{
int index = ArraySize(custom_rates) - 1;
if(index < 0) return 0;
return custom_rates[index].time;
}
//+------------------------------------------------------------------+
//| custom symbol methods |
//+------------------------------------------------------------------+
//Create points custom symbol
bool CustomCharts::CreateCustomSymbol(string name = "")
{
ResetLastError();
custom_symbol = name;
bool is_custom = false;
if(SymbolExist(custom_symbol, is_custom))
{
CustomTicksDelete(custom_symbol, 1000 * TIME_MIN, 1000 * TIME_MAX);
CustomRatesDelete(custom_symbol, TIME_MIN, TIME_MAX);
return true;
}
//Create symbol
return CustomSymbolCreate(custom_symbol, CUSTOM_SYMBOL_PATH, chart_symbol);
}
//Update custom rates
int CustomCharts::UpdateCustomRates()
{
ResetLastError();
if(rates_count <= 0) return 0;
if(rates_count == custom_rates_count) return 0;
int copied = CustomRatesUpdate(custom_symbol, custom_rates);
if(copied > 0) custom_rates_count = copied;
return copied;
}
//Update custom tick
int CustomCharts::AddCustomTicks()
{
ResetLastError();
if(ArraySize(custom_ticks) <= 0) return 0;
int copied = CustomTicksAdd(custom_symbol, custom_ticks);
if(copied > 0) custom_ticks_count += copied;
return copied;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CustomCharts::CarregaHistorico()
{
if(rates_count == 0)
{
if(real_ticks)
UpdateTicks(rates_history);
else
UpdateRates(rates_history);
UpdateCustomRates();
return;
}
}
//Refresh custom data
void CustomCharts::Refresh()
{
//Update custom symbol
if(UpdateTicks() > 0) AddCustomTicks();
UpdateCustomRates();
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string StringPeriod(ENUM_TIMEFRAMES value)
{
if(value == PERIOD_CURRENT) value = (ENUM_TIMEFRAMES) _Period;
string period = EnumToString(value);
return StringSubstr(period, 7);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string StringMethod(ENUM_MA_METHOD value)
{
string method = EnumToString(value);
return StringSubstr(method, 5);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
datetime TruncateTime(datetime tm, ENUM_TIMEFRAMES tf)
{
MqlDateTime ts;
switch (tf)
{
case PERIOD_MN1:
TimeToStruct(tm, ts);
return tm - (tm % 86400) - ((ts.day - 1) * 86400);
case PERIOD_W1:
return (((tm - 259200) / 604800) * 604800) + 259200;
default:
return tm - (tm % (PeriodSeconds(tf)));
}
}
//+------------------------------------------------------------------+