-
Notifications
You must be signed in to change notification settings - Fork 7
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
Showing
11 changed files
with
194 additions
and
37 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
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 |
---|---|---|
@@ -1,26 +1,24 @@ | ||
from components.positions import Position | ||
from loguru import logger | ||
|
||
|
||
def remove_overlapping_positions(positions: Position, max_overlap): | ||
positions_copy = positions[:] | ||
class ComponentManager: | ||
|
||
# Iterate over the positions, comparing each one to all subsequent positions | ||
for i in range(len(positions_copy)): | ||
num_overlaps = 0 | ||
for j in range(i + 1, len(positions_copy)): | ||
position_i = positions_copy[i] | ||
position_j = positions_copy[j] | ||
_components = [] | ||
|
||
# Check if the positions overlap | ||
i_start = position_i.opened_timestamp | ||
i_end = position_i.closed_timestamp | ||
j_start = position_j.opened_timestamp | ||
j_end = position_j.closed_timestamp | ||
if (i_start <= j_end) and (j_start <= i_end): | ||
num_overlaps += 1 # Increment the overlap counter | ||
if num_overlaps > max_overlap: | ||
positions_copy.pop(j) | ||
break | ||
@classmethod | ||
def register(cls, component): | ||
if component in cls._components: | ||
return | ||
cls._components.append(component) | ||
logger.debug(f'Registered component {component} ({component.__module__})') | ||
|
||
# Return the modified copy of the list | ||
return positions_copy | ||
@classmethod | ||
def all(cls): | ||
return [o() for o in cls._components] | ||
|
||
@classmethod | ||
def get(cls, name): | ||
for component in cls._components: | ||
if component.__name__ == name: | ||
return component() | ||
raise ValueError(f'Component "{name}" not found.') |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from components.strategy.builtins.ta.sma import * | ||
from components.strategy.builtins.ta.correlation import * | ||
from components.strategy.builtins.ta.kalman_filter import * | ||
from components.strategy.builtins.ta.atr import * | ||
from components.strategy.builtins.ta.logic import Logic as logic |
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,31 @@ | ||
import numpy as np | ||
|
||
from components.strategy import Series | ||
|
||
|
||
def atr(high: Series, low: Series, close: Series, period: int = 12) -> Series: | ||
""" Calculate the average true range """ | ||
|
||
high = np.array(high) | ||
low = np.array(low) | ||
close = np.array(close) | ||
|
||
if len(high) != len(low) != len(close): | ||
raise ValueError("Input lists must have the same length") | ||
|
||
if len(high) < period: | ||
raise ValueError("Input lists must have at least 'period' number of elements") | ||
|
||
true_range = [] | ||
|
||
for i in range(1, len(high)): | ||
tr = max(high[i] - low[i], abs(high[i] - close[i - 1]), abs(low[i] - close[i - 1])) | ||
true_range.append(tr) | ||
|
||
result = [] | ||
|
||
for i in range(period, len(true_range) + 1): | ||
average = np.mean(true_range[i - period: i]) | ||
result.append(average) | ||
|
||
return Series(result) |
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 |
---|---|---|
@@ -1,12 +1,28 @@ | ||
import numpy as np | ||
import pandas as pd | ||
|
||
from components.strategy import Series | ||
|
||
|
||
def correlation_coefficient(x: Series, y: Series, period: int) -> Series: | ||
""" Calculate the correlation coefficient between two lists """ | ||
x = np.array(x) | ||
y = np.array(y) | ||
if len(x) != len(y): | ||
raise ValueError("Input arrays must have the same length") | ||
|
||
# Calculate the correlation coefficient | ||
return Series(np.corrcoef(x, y)[0, 1]) | ||
if len(x) < period: | ||
raise ValueError("Period must be less than or equal to the length of input arrays") | ||
|
||
x = np.asarray(x.as_list()) | ||
y = np.asarray(y.as_list()) | ||
|
||
result = np.zeros(len(x) - period + 1) | ||
|
||
for i in range(len(result)): | ||
x_window = x[i:i + period] | ||
y_window = y[i:i + period] | ||
result[i] = np.corrcoef(x_window, y_window)[0, 1] | ||
|
||
pad_size = period - 1 | ||
result = np.pad(result, (pad_size, 0), mode='constant', constant_values=np.nan) | ||
|
||
return Series(list(result)) |
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,23 @@ | ||
import math | ||
|
||
import pandas as pd | ||
from loguru import logger | ||
|
||
from components.strategy import Series | ||
from components.strategy.builtins.ta.nz import nz | ||
|
||
import numpy as np | ||
|
||
|
||
def kalman_filter(src: pd.Series, gain): | ||
src = list(src) | ||
kf = np.zeros(len(src)) | ||
velo = np.zeros(len(src)) | ||
smooth = np.zeros(len(src)) | ||
for i in range(len(src)): | ||
dk = src[i] - kf[i-1] if i > 0 else src[i] | ||
smooth[i] = kf[i-1] + dk * np.sqrt((gain / 10000) * 2) | ||
velo[i] = velo[i-1] + ((gain / 10000) * dk) | ||
kf[i] = smooth[i] + velo[i] | ||
return Series(list(kf)) | ||
|
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