update to several methods and EAs
This commit is contained in:
@@ -31,17 +31,6 @@ bool MarketDataUtils::is_new_bar(string symbol, ENUM_TIMEFRAMES time_frame, stri
|
||||
return false; // No new bar
|
||||
}
|
||||
|
||||
// Retrieves the latest value from an indicator buffer (shift 0)
|
||||
double MarketDataUtils::get_latest_buffer_value(int handle) {
|
||||
double val[];
|
||||
ArraySetAsSeries(val, true); // Aligns array with bar indexing (0 = latest)
|
||||
|
||||
if (CopyBuffer(handle, 0, 0, 1, val) == 1)
|
||||
return val[0]; // Latest value at shift 0
|
||||
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// shift = 0 refers to the live candle (still forming)
|
||||
// shift = 1 is the most recently closed candle
|
||||
// shift = 2 is the one before that, etc.
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
#include <MyLibs/Utils/SignalStateTracker.mqh>
|
||||
|
||||
// This class manages a collection of SignalStateTracker instances — one per symbol.
|
||||
// It allows you to track signals separately for each symbol in a multi-symbol EA.
|
||||
class MultiSymbolSignalTracker {
|
||||
private:
|
||||
// Struct to hold one (symbol → tracker) mapping
|
||||
struct SymbolTracker {
|
||||
string symbol; // Symbol name (e.g. "EURUSD", "HSI")
|
||||
SignalStateTracker* tracker; // Pointer to the signal tracker for that symbol
|
||||
};
|
||||
|
||||
SymbolTracker trackers[]; // Dynamic array of symbol-tracker pairs
|
||||
|
||||
// Helper function: Find the index of the given symbol in the array
|
||||
int find_index(const string& symbol) {
|
||||
for (int i = 0; i < ArraySize(trackers); i++) {
|
||||
if (trackers[i].symbol == symbol) return i; // Found symbol, return its index
|
||||
}
|
||||
return -1; // Not found
|
||||
}
|
||||
|
||||
public:
|
||||
// Destructor: automatically called when this object is destroyed (e.g. at EA shutdown)
|
||||
~MultiSymbolSignalTracker() {
|
||||
clear(); // Clean up memory when done
|
||||
}
|
||||
|
||||
// Returns the SignalStateTracker for the given symbol.
|
||||
// If it doesn't exist yet, it creates one and stores it.
|
||||
SignalStateTracker* get_tracker(const string& symbol) {
|
||||
int idx = find_index(symbol);
|
||||
if (idx != -1) return trackers[idx].tracker; // Tracker already exists — return it
|
||||
|
||||
// If not found, create a new tracker for this symbol
|
||||
SignalStateTracker* tracker = new SignalStateTracker();
|
||||
|
||||
SymbolTracker item;
|
||||
item.symbol = symbol;
|
||||
item.tracker = tracker;
|
||||
|
||||
// Add new item to dynamic array
|
||||
ArrayResize(trackers, ArraySize(trackers) + 1);
|
||||
trackers[ArraySize(trackers) - 1] = item;
|
||||
|
||||
return tracker;
|
||||
}
|
||||
|
||||
// Deletes all trackers and resets the array.
|
||||
// Should be called in OnDeinit() to free memory.
|
||||
void clear() {
|
||||
for (int i = 0; i < ArraySize(trackers); i++) {
|
||||
delete trackers[i].tracker; // Manually free each dynamically created tracker
|
||||
}
|
||||
ArrayResize(trackers, 0); // Reset array to empty
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
Example usage in an EA:
|
||||
---------------------------------------------------------------------------
|
||||
|
||||
// Declare the tracker globally (outside OnTick/OnTimer)
|
||||
MultiSymbolSignalTracker track_trigger;
|
||||
|
||||
// Inside your strategy() or OnTick():
|
||||
void strategy(string symbol, ...) {
|
||||
bool trig_long = ...; // Your signal logic
|
||||
bool trig_short = ...;
|
||||
|
||||
// Update the signal tracker for this symbol
|
||||
track_trigger.get_tracker(symbol).update_signal(trig_long, trig_short);
|
||||
|
||||
// Example: Check if a recent long signal occurred
|
||||
if (track_trigger.get_tracker(symbol).long_signal_recent(5)) {
|
||||
// Do something like open a trade
|
||||
}
|
||||
}
|
||||
|
||||
// In OnDeinit():
|
||||
void OnDeinit(const int reason) {
|
||||
track_trigger.clear(); // Clean up memory
|
||||
}
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,56 @@
|
||||
class SignalStateTracker {
|
||||
private:
|
||||
int last_signal_long;
|
||||
int last_signal_short;
|
||||
|
||||
public:
|
||||
// Constructor initializes both directions to a default "unset" state.
|
||||
SignalStateTracker() {
|
||||
reset();
|
||||
}
|
||||
|
||||
// Resets the tracked signal bar indexes to an invalid default value (-1000).
|
||||
void reset() {
|
||||
last_signal_long = -1000;
|
||||
last_signal_short = -1000;
|
||||
}
|
||||
|
||||
// Updates the signal for both long and short directions in one call, if the respective triggers are true.
|
||||
// curr_bar should refer to the last CLOSED bar (typically bar index 1).
|
||||
void update_signal(bool trig_long, bool trig_short, int curr_bar=1) {
|
||||
if (trig_long) last_signal_long = curr_bar;
|
||||
if (trig_short) last_signal_short = curr_bar;
|
||||
}
|
||||
|
||||
// Returns true if a long signal occurred within the last `max_bars` bars.
|
||||
bool long_signal_recent(int max_bars) const {
|
||||
// Assumes current bar is always bar index 1 (last closed bar).
|
||||
return has_long_signal() && (1 - last_signal_long <= max_bars);
|
||||
}
|
||||
|
||||
// Returns true if a short signal occurred within the last `max_bars` bars.
|
||||
bool short_signal_recent(int max_bars) const {
|
||||
// Assumes current bar is always bar index 1 (last closed bar).
|
||||
return has_short_signal() && (1 - last_signal_short <= max_bars);
|
||||
}
|
||||
|
||||
// Returns true if a signal has been recorded for the long direction.
|
||||
bool has_long_signal() const {
|
||||
return last_signal_long != -1000;
|
||||
}
|
||||
|
||||
// Returns true if a signal has been recorded for the short direction.
|
||||
bool has_short_signal() const {
|
||||
return last_signal_short != -1000;
|
||||
}
|
||||
|
||||
// Returns the last recorded signal bar index for the long direction.
|
||||
int get_long_signal() const {
|
||||
return last_signal_long;
|
||||
}
|
||||
|
||||
// Returns the last recorded signal bar index for the short direction.
|
||||
int get_short_signal() const {
|
||||
return last_signal_short;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user