update to my mt5 libs to include better comments

This commit is contained in:
Matt Corcoran
2025-07-12 15:29:30 +02:00
parent ec3fc120ce
commit 136522ef1c
31 changed files with 1389 additions and 2024 deletions
-105
View File
@@ -1,105 +0,0 @@
class EntryState {
public:
int last_trigger_bar_long;
int last_trigger_bar_short;
int last_bl_cross_long;
int last_bl_cross_short;
int last_entry_long;
int last_entry_short;
// Constructor
EntryState() {
reset();
}
void reset() {
last_trigger_bar_long = -1000;
last_trigger_bar_short = -1000;
last_bl_cross_long = -1000;
last_bl_cross_short = -1000;
last_entry_long = -1000;
last_entry_short = -1000;
}
void update_trigger(bool trig_long, bool trig_short, int curr_bar) {
if (trig_long) last_trigger_bar_long = curr_bar;
if (trig_short) last_trigger_bar_short = curr_bar;
}
void update_baseline_cross(int curr_bar, double price, double baseline, double prev_price, double prev_baseline) {
if (prev_price < prev_baseline && price > baseline) last_bl_cross_long = curr_bar;
if (prev_price > prev_baseline && price < baseline) last_bl_cross_short = curr_bar;
}
void update_entry(bool is_long, int curr_bar) {
if (is_long)
last_entry_long = curr_bar;
else
last_entry_short = curr_bar;
}
int get_last_trigger(bool is_long) {
return is_long ? last_trigger_bar_long : last_trigger_bar_short;
}
int get_last_cross(bool is_long) {
return is_long ? last_bl_cross_long : last_bl_cross_short;
}
int get_last_entry(bool is_long) {
return is_long ? last_entry_long : last_entry_short;
}
};
// Snapshot of conditions for a potential entry signal
struct EntryContext {
bool trigger;
bool confirm;
bool volume;
bool recent;
bool base_ok;
bool near;
bool far;
int last_entry;
int last_cross;
};
// Utility: check if signal occurred recently
bool is_recent(int signal_bar, int curr_bar, int lookback) {
return (curr_bar - signal_bar) < lookback;
}
// Utility: build current entry condition context
EntryContext build_entry_context(bool is_long, double price, double baseline, double atr, int last_cross, int last_entry, bool trigger,
bool confirm, bool volume, bool recent) {
EntryContext ctx;
ctx.trigger = trigger;
ctx.confirm = confirm;
ctx.volume = volume;
ctx.recent = recent;
ctx.base_ok = is_long ? (price > baseline) : (price < baseline);
ctx.near = MathAbs(price - baseline) <= atr;
ctx.far = MathAbs(price - baseline) > atr;
ctx.last_entry = last_entry;
ctx.last_cross = last_cross;
return ctx;
}
// Class wrapper for entry logic
class EntryLogic {
public:
bool is_standard_entry(const EntryContext& ctx) {
return ctx.trigger && ctx.confirm && ctx.volume && ctx.recent && ctx.base_ok && ctx.near;
}
bool is_pullback_entry(const EntryContext& ctx, int curr_bar) {
return (curr_bar - ctx.last_cross <= 2) && ctx.confirm && ctx.volume && ctx.base_ok && ctx.far;
}
bool is_baseline_cross_entry(const EntryContext& ctx, double prev_price, double prev_baseline, double price, double baseline,
bool is_long) {
bool crossed = is_long ? (prev_price < prev_baseline && price > baseline) : (prev_price > prev_baseline && price < baseline);
return crossed && ctx.confirm && ctx.volume && ctx.near;
}
bool is_continuation_entry(const EntryContext& ctx, int curr_bar, int look_back) {
return (curr_bar - ctx.last_entry <= look_back) && ctx.last_entry > ctx.last_cross && ctx.trigger && ctx.confirm && ctx.base_ok;
}
};
@@ -50,7 +50,6 @@ class RangeCalculator : public CObject{
public:
void calculate_range();
double get_range_high();
double get_range_low();
double get_range_mid();
@@ -65,6 +64,18 @@ class RangeCalculator : public CObject{
};
// ---------------------------------------------------------------------
// Sets the allowed days for range calculation.
//
// Parameters:
// - _inp_sun : Allow Sunday.
// - _inp_mon : Allow Monday.
// - _inp_tue : Allow Tuesday.
// - _inp_wed : Allow Wednesday.
// - _inp_thu : Allow Thursday.
// - _inp_fri : Allow Friday.
// - _inp_sat : Allow Saturday.
// ---------------------------------------------------------------------
void RangeCalculator::range_days(bool _inp_sun, bool _inp_mon, bool _inp_tue, bool _inp_wed, bool _inp_thu, bool _inp_fri, bool _inp_sat){
sun = _inp_sun;
mon = _inp_mon;
@@ -76,6 +87,22 @@ void RangeCalculator::range_days(bool _inp_sun, bool _inp_mon, bool _inp_tue, bo
days_initlised = true;
}
// ---------------------------------------------------------------------
// Initializes the range parameters.
//
// Parameters:
// - inp_symbol : The symbol for the range.
// - _calc_period : Timeframe for range calculation.
// - t1 : Start time string.
// - t2 : End time string.
// - t3 : Expiry time string.
// - t4 : Close time string.
// - time_zone : Timezone name.
// - plot_range_inp : Whether to plot the range.
//
// Returns:
// - true if initialization was successful; false otherwise.
// ---------------------------------------------------------------------
bool RangeCalculator::initilise_range(string inp_symbol, ENUM_TIMEFRAMES _calc_period, string t1, string t2, string t3, string t4, string time_zone, bool plot_range_inp){
inp_r_start_string = t1;
inp_timezone = time_zone;
@@ -113,7 +140,18 @@ bool RangeCalculator::initilise_range(string inp_symbol, ENUM_TIMEFRAMES _calc_p
return true;
}
// ---------------------------------------------------------------------
// Converts time input strings to time deltas for range definition.
//
// Parameters:
// - t1 : Start time string.
// - t2 : End time string.
// - t3 : Expiry time string.
// - t4 : Close time string.
//
// Returns:
// - true if times were converted successfully; false on error.
// ---------------------------------------------------------------------
bool RangeCalculator::convert_input_time_strings(string t1, string t2, string t3, string t4){
datetime _t1 = StringToTime(t1);
@@ -149,44 +187,93 @@ bool RangeCalculator::convert_input_time_strings(string t1, string t2, string t3
return true;
}
// high of the range
// ---------------------------------------------------------------------
// Returns the high value of the current range.
//
// Returns:
// - High price of the range.
// ---------------------------------------------------------------------
double RangeCalculator::get_range_high(){
return high;
};
// low of the range
// ---------------------------------------------------------------------
// Returns the low value of the current range.
//
// Returns:
// - Low price of the range.
// ---------------------------------------------------------------------
double RangeCalculator::get_range_low(){
return low;
};
// mid of the range
// ---------------------------------------------------------------------
// Returns the mid value of the current range.
//
// Returns:
// - Mid price of the range.
// ---------------------------------------------------------------------
double RangeCalculator::get_range_mid(){
return mid;
};
// ---------------------------------------------------------------------
// Returns the start time of the current range.
//
// Returns:
// - Range start time.
// ---------------------------------------------------------------------
datetime RangeCalculator::get_range_start(){
return start_time;
};
// ---------------------------------------------------------------------
// Returns the end time of the current range.
//
// Returns:
// - Range end time.
// ---------------------------------------------------------------------
datetime RangeCalculator::get_range_end(){
return end_time;
};
// ---------------------------------------------------------------------
// Returns the expiration time for range-based orders.
//
// Returns:
// - Order expiration time.
// ---------------------------------------------------------------------
datetime RangeCalculator::get_order_expire_time(){
return order_expire_time;
};
// ---------------------------------------------------------------------
// Returns the close time of the current range.
//
// Returns:
// - Range close time.
// ---------------------------------------------------------------------
datetime RangeCalculator::get_range_close(){
return close_time;
};
// flag if a high breakout occurred
// ---------------------------------------------------------------------
// Returns the high breakout flag of the current range.
//
// Returns:
// - true if high breakout occurred; false otherwise.
// ---------------------------------------------------------------------
bool RangeCalculator::get_range_high_breakout(){
return f_high_breakout;
};
// flag if a low breakout occurred
// ---------------------------------------------------------------------
// Returns the low breakout flag of the current range.
//
// Returns:
// - true if low breakout occurred; false otherwise.
// ---------------------------------------------------------------------
bool RangeCalculator::get_range_low_breakout(){
return f_low_breakout;
};