forked from idanya/algo-trader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Idan Yael
committed
Feb 8, 2022
1 parent
5e9143f
commit bfd8f7a
Showing
14 changed files
with
249 additions
and
14 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"groups": [ | ||
["AAPL", "MSFT"], | ||
["CHKP", "FB"] | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"groups": [ | ||
[ | ||
"AAPL", | ||
"MSFT" | ||
] | ||
] | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import json | ||
from typing import Optional, List, Dict | ||
|
||
from scipy import spatial | ||
|
||
from entities.candle import Candle | ||
from entities.event import Event | ||
from entities.generic_candle_attachment import GenericCandleAttachment | ||
from pipeline.processor import Processor | ||
from pipeline.processors.candle_cache import CandleCache | ||
from pipeline.processors.technicals import IndicatorValue | ||
from pipeline.shared_context import SharedContext | ||
|
||
CORRELATIONS_ATTACHMENT_KEY = 'correlations' | ||
CORRELATION_ELEMENTS_COUNT = 4 | ||
|
||
|
||
class AssetCorrelation(GenericCandleAttachment[IndicatorValue]): | ||
pass | ||
|
||
|
||
AssetCorrelation() | ||
|
||
|
||
class CorrelationConfig: | ||
def __init__(self, groups: List[List[str]]) -> None: | ||
self.groups: List[List[str]] = groups | ||
|
||
|
||
class AssetCorrelationProcessor(Processor): | ||
def __init__(self, config_path: str, next_processor: Optional[Processor]) -> None: | ||
with open(config_path, 'r') as config_content: | ||
c: Dict = json.loads(config_content.read()) | ||
self.config = CorrelationConfig(c.get('groups', [])) | ||
|
||
super().__init__(next_processor) | ||
|
||
def process(self, context: SharedContext, candle: Candle): | ||
super().process(context, candle) | ||
|
||
def event(self, context: SharedContext, event: Event): | ||
if event == event.TimeSpanChange: | ||
self._calculate_correlations(context) | ||
|
||
super().event(context, event) | ||
|
||
def _calculate_correlations(self, context: SharedContext): | ||
cache_reader = CandleCache.context_reader(context) | ||
symbols = cache_reader.get_symbols_list() | ||
|
||
for symbol in symbols: | ||
self._calculate_symbol_correlations(context, symbol) | ||
|
||
def _calculate_symbol_correlations(self, context: SharedContext, symbol: str): | ||
cache_reader = CandleCache.context_reader(context) | ||
asset_correlation = AssetCorrelation() | ||
|
||
group_symbols = self._get_symbol_group(symbol) | ||
|
||
if group_symbols: | ||
current_symbol_candles = cache_reader.get_symbol_candles(symbol) or [] | ||
current_symbol_values = self._get_correlation_measurable_values(current_symbol_candles) | ||
|
||
for paired_symbol in group_symbols: | ||
if paired_symbol == symbol: | ||
continue | ||
|
||
symbol_candles = cache_reader.get_symbol_candles(paired_symbol) or [] | ||
symbol_values = self._get_correlation_measurable_values(symbol_candles) | ||
|
||
if len(symbol_values) != len(current_symbol_values) or len(current_symbol_values) <= CORRELATION_ELEMENTS_COUNT: | ||
continue | ||
|
||
correlation = spatial.distance.correlation(current_symbol_values[-CORRELATION_ELEMENTS_COUNT:], | ||
symbol_values[-CORRELATION_ELEMENTS_COUNT:]) | ||
asset_correlation.set(paired_symbol, correlation) | ||
|
||
latest_candle = current_symbol_candles[-1] | ||
latest_candle.attachments.add_attachement(CORRELATIONS_ATTACHMENT_KEY, asset_correlation) | ||
|
||
self.reprocess(context, latest_candle) | ||
|
||
def _get_symbol_group(self, symbol: str) -> Optional[List[str]]: | ||
for group in self.config.groups: | ||
if symbol in group: | ||
return group | ||
|
||
@staticmethod | ||
def _get_correlation_measurable_values(candles: List[Candle]) -> List[float]: | ||
return [c.close for c in candles] |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"groups": [ | ||
[ | ||
"X", | ||
"Y", | ||
"Z" | ||
] | ||
] | ||
} |
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
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
Oops, something went wrong.