Added support for inverse correlations
This commit is contained in:
@@ -10,7 +10,6 @@ from scipy.stats.stats import pearsonr
|
||||
import pickle
|
||||
import inspect
|
||||
import sys
|
||||
import numpy as np
|
||||
|
||||
from mt5_correlation.mt5 import MT5
|
||||
|
||||
@@ -82,6 +81,9 @@ class Correlation:
|
||||
# below this threshold will be considered as having diverged
|
||||
divergence_threshold = 0.8
|
||||
|
||||
# Flag to determine we monitor and report on inverse correlations
|
||||
monitor_inverse = False
|
||||
|
||||
# Toggle on whether we are monitoring or not. Set through start_monitor and stop_monitor
|
||||
__monitoring = False
|
||||
|
||||
@@ -106,13 +108,14 @@ class Correlation:
|
||||
# Dict: {Symbol: [retrieved datetime, ticks dataframe]}
|
||||
__monitor_tick_data = {}
|
||||
|
||||
def __init__(self, monitoring_threshold=0.9, divergence_threshold=0.8):
|
||||
def __init__(self, monitoring_threshold=0.9, divergence_threshold=0.8, monitor_inverse=False):
|
||||
"""
|
||||
Initialises the Correlation class.
|
||||
:param monitoring_threshold: Only correlations that are greater than or equal to this threshold will be
|
||||
monitored.
|
||||
:param divergence_threshold: Correlations that are being monitored and fall below this threshold are considered
|
||||
to have diverged.
|
||||
:param monitor_inverse: Whether we will monitor and report on negative / inverse correlations.
|
||||
"""
|
||||
# Logger
|
||||
self.__log = logging.getLogger(__name__)
|
||||
@@ -126,19 +129,28 @@ class Correlation:
|
||||
# Create timer for continuous monitoring
|
||||
self.__scheduler = sched.scheduler(time.time, time.sleep)
|
||||
|
||||
# Set thresholds
|
||||
# Set thresholds and flags
|
||||
self.monitoring_threshold = monitoring_threshold
|
||||
self.divergence_threshold = divergence_threshold
|
||||
self.monitor_inverse = monitor_inverse
|
||||
|
||||
@property
|
||||
def filtered_coefficient_data(self):
|
||||
"""
|
||||
:return: Coefficient data filtered so that all base coefficients >= monitoring_threshold
|
||||
"""
|
||||
filtered_data = None
|
||||
if self.coefficient_data is not None:
|
||||
return self.coefficient_data.loc[self.coefficient_data['Base Coefficient'] >= self.monitoring_threshold]
|
||||
else:
|
||||
return None
|
||||
if self.monitor_inverse:
|
||||
filtered_data = self.coefficient_data \
|
||||
.loc[(self.coefficient_data['Base Coefficient'] >= self.monitoring_threshold) |
|
||||
(self.coefficient_data['Base Coefficient'] <= self.monitoring_threshold * -1)]
|
||||
|
||||
else:
|
||||
filtered_data = self.coefficient_data.loc[self.coefficient_data['Base Coefficient'] >=
|
||||
self.monitoring_threshold]
|
||||
|
||||
return filtered_data
|
||||
|
||||
def load(self, filename):
|
||||
"""
|
||||
@@ -495,7 +507,7 @@ class Correlation:
|
||||
if self.coefficient_data is not None and len(self.coefficient_data.index) > 0:
|
||||
data = self.coefficient_data.copy()
|
||||
|
||||
# Filter by symols if specified
|
||||
# Filter by symbols if specified
|
||||
data = data.loc[data['Symbol 1'] == symbol1] if symbol1 is not None else data
|
||||
data = data.loc[data['Symbol 2'] == symbol2] if symbol2 is not None else data
|
||||
|
||||
@@ -511,6 +523,23 @@ class Correlation:
|
||||
|
||||
return last_calc
|
||||
|
||||
def get_base_coefficient(self, symbol1, symbol2):
|
||||
"""
|
||||
Returns the base coefficient for the specified symbol pair
|
||||
:param symbol1:
|
||||
:param symbol2:
|
||||
:return:
|
||||
"""
|
||||
base_coefficient = None
|
||||
if self.coefficient_data is not None:
|
||||
row = self.coefficient_data[(self.coefficient_data['Symbol 1'] == symbol1) &
|
||||
(self.coefficient_data['Symbol 2'] == symbol2)]
|
||||
|
||||
if row is not None and len(row) == 1:
|
||||
base_coefficient = row.iloc[0]['Base Coefficient']
|
||||
|
||||
return base_coefficient
|
||||
|
||||
def __monitor(self):
|
||||
"""
|
||||
The actual monitor method. Private. This should not be called outside of this class. Use start_monitoring and
|
||||
@@ -664,8 +693,11 @@ class Correlation:
|
||||
(self.coefficient_data['Symbol 2'] == symbol2),
|
||||
'Last Calculation'] = now
|
||||
|
||||
# Are we an inverse correlation
|
||||
inverse = self.get_base_coefficient(symbol1, symbol2) <= self.monitoring_threshold * -1
|
||||
|
||||
# Calculate status and update
|
||||
status = self.__calculate_status(coefficients=coefficients)
|
||||
status = self.__calculate_status(coefficients=coefficients, inverse=inverse)
|
||||
self.coefficient_data.loc[(self.coefficient_data['Symbol 1'] == symbol1) &
|
||||
(self.coefficient_data['Symbol 2'] == symbol2),
|
||||
'Status'] = status
|
||||
@@ -676,22 +708,32 @@ class Correlation:
|
||||
data=[[symbol1, symbol2, coefficients[key], key, date_to]])
|
||||
self.coefficient_history = self.coefficient_history.append(row)
|
||||
|
||||
def __calculate_status(self, coefficients):
|
||||
def __calculate_status(self, coefficients, inverse):
|
||||
"""
|
||||
Calculates the status from the supplied set of coefficients
|
||||
:param coefficients: Dict of timeframes and coefficients {timeframe: coefficient} to calculate status from
|
||||
:param: Whether we are calculating status based on normal or inverse correlation
|
||||
:return: status
|
||||
"""
|
||||
status = None
|
||||
status = STATUS_NOT_CALCULATED
|
||||
values = coefficients.values()
|
||||
|
||||
if None in values:
|
||||
status = STATUS_NOT_CALCULATED
|
||||
elif all(i >= self.divergence_threshold for i in values):
|
||||
status = STATUS_ABOVE_DIVERGENCE_THRESHOLD
|
||||
elif all(i < self.divergence_threshold for i in values):
|
||||
status = STATUS_BELOW_DIVERGENCE_THRESHOLD
|
||||
else:
|
||||
status = STATUS_INCONSISTENT
|
||||
if None not in values:
|
||||
if self.monitor_inverse and inverse:
|
||||
# Calculation for inverse calculations
|
||||
if all(i <= self.divergence_threshold * -1 for i in values):
|
||||
status = STATUS_ABOVE_DIVERGENCE_THRESHOLD
|
||||
elif all(i > self.divergence_threshold * -1 for i in values):
|
||||
status = STATUS_BELOW_DIVERGENCE_THRESHOLD
|
||||
else:
|
||||
status = STATUS_INCONSISTENT
|
||||
else:
|
||||
# Calculation for standard correlations
|
||||
if all(i >= self.divergence_threshold for i in values):
|
||||
status = STATUS_ABOVE_DIVERGENCE_THRESHOLD
|
||||
elif all(i < self.divergence_threshold for i in values):
|
||||
status = STATUS_BELOW_DIVERGENCE_THRESHOLD
|
||||
else:
|
||||
status = STATUS_INCONSISTENT
|
||||
|
||||
return status
|
||||
|
||||
+21
-8
@@ -52,7 +52,8 @@ class MonitorFrame(wx.Frame):
|
||||
|
||||
# Create correlation instance to maintain state of calculated coefficients. Set min coefficient from config
|
||||
self.__cor = cor.Correlation(monitoring_threshold=self.__config.get("monitor.monitoring_threshold"),
|
||||
divergence_threshold=self.__config.get("monitor.divergence_threshold"))
|
||||
divergence_threshold=self.__config.get("monitor.divergence_threshold"),
|
||||
monitor_inverse=self.__config.get("monitor.monitor_inverse"))
|
||||
|
||||
# Status bar. 2 fields, one for monitoring status and one for general status. On open, monitoring status is not
|
||||
# monitoring. SetBackgroundColour will change colour of both. Couldn't find a way to set on single field only.
|
||||
@@ -221,12 +222,8 @@ class MonitorFrame(wx.Frame):
|
||||
"""
|
||||
self.__log.debug(f"Refreshing grid. Timer running: {self.timer.IsRunning()}")
|
||||
|
||||
# Get coefficient data and join to history data
|
||||
coef_data = self.__cor.coefficient_data.copy()
|
||||
hist_data = self.__cor.get_coefficient_history()
|
||||
|
||||
# Update data
|
||||
self.table.data = self.__cor.coefficient_data.copy()
|
||||
self.table.data = self.__cor.filtered_coefficient_data.copy()
|
||||
|
||||
# Format
|
||||
self.table.data.loc[:, 'Base Coefficient'] = self.table.data['Base Coefficient'].map('{:.5f}'.format)
|
||||
@@ -426,7 +423,9 @@ class MonitorFrame(wx.Frame):
|
||||
# Display if we have any data
|
||||
self.__log.debug(f"Refreshing history graph {symbol1}:{symbol2}.")
|
||||
self.__graph.draw(prices=[symbol_1_price_data, symbol_2_price_data], ticks=[symbol_1_ticks, symbol_2_ticks],
|
||||
history=[history_data_short, history_data_med, history_data_long], symbols=[symbol1, symbol2])
|
||||
history=[history_data_short, history_data_med, history_data_long], symbols=[symbol1, symbol2],
|
||||
divergence_threshold=self.__cor.divergence_threshold,
|
||||
monitor_inverse=self.__cor.monitor_inverse)
|
||||
|
||||
# Un-hide and layout if hidden
|
||||
if not self.__graph.IsShown():
|
||||
@@ -552,13 +551,17 @@ class GraphPanel(wx.Panel):
|
||||
self.__axes = None
|
||||
self.__fig = None
|
||||
|
||||
def draw(self, prices=None, ticks=None, history=None, symbols=None):
|
||||
def draw(self, prices=None, ticks=None, history=None, symbols=None, divergence_threshold=None,
|
||||
monitor_inverse=False):
|
||||
"""
|
||||
Plot the correlations.
|
||||
:param prices: Price data used to calculate base coefficient. List [Symbol1 Price Data, Symbol 2 Price Data]
|
||||
:param ticks: Ticks used to calculate last coefficient. List [Symbol1, Symbol2]
|
||||
:param history: Coefficient history data. List of data for one or more timeframes.
|
||||
:param symbols: Symbols. List [Symbol1, Symbol2]
|
||||
:param divergence_threshold: The divergence threshold. Will be plotted on the coefficients charts if specified.
|
||||
:param monitor_inverse: Are we monitoring inverse correlations. If so, a line for the inverse threshold will be
|
||||
plotted if the divergence threshold is specified.
|
||||
:return:
|
||||
"""
|
||||
|
||||
@@ -638,6 +641,10 @@ class GraphPanel(wx.Panel):
|
||||
f"{Config().get('monitor.calculations.medium.from')} Minutes",
|
||||
f"{Config().get('monitor.calculations.short.from')} Minutes"]]
|
||||
|
||||
# lines
|
||||
horiz_lines = [None, None, None, None, [divergence_threshold, divergence_threshold * -1
|
||||
if divergence_threshold is not None and monitor_inverse else None]]
|
||||
|
||||
# Draw 5 charts
|
||||
for index in range(0, len(self.__axes)):
|
||||
# Titles and axis labels
|
||||
@@ -679,6 +686,12 @@ class GraphPanel(wx.Panel):
|
||||
else:
|
||||
self.__axes[index].set_xticklabels([])
|
||||
|
||||
# Lines
|
||||
if horiz_lines[index] is not None and isinstance(horiz_lines[index], list):
|
||||
for line_pos in horiz_lines[index]:
|
||||
if line_pos is not None:
|
||||
self.__axes[index].axhline(y=line_pos, color="red", label='_nolegend_', linewidth=1)
|
||||
|
||||
# Legends
|
||||
if legends[index] is not None:
|
||||
self.__axes[index].legend(legends[index])
|
||||
|
||||
Reference in New Issue
Block a user