1
- from Trading .model .history import History
1
+ from Trading .model .history import History , OHLC
2
2
from abc import ABC , abstractmethod
3
3
from typing import Dict , List , Optional
4
4
from pydantic import BaseModel , ConfigDict
5
+ from Trading .utils .custom_logging import get_logger
5
6
7
+ LOGGER = get_logger (__file__ )
6
8
class ScoreCalculator (BaseModel ):
7
9
'''
8
10
ScoreCalculator class is used to quantify a set of traits that is desirable
@@ -16,8 +18,6 @@ class ScoreCalculator(BaseModel):
16
18
stocks ranging behavior based on their historical data.
17
19
'''
18
20
window : int
19
- is_bigger_better : bool = False
20
-
21
21
22
22
@abstractmethod
23
23
def calculate (self , history : History ) -> float :
@@ -35,33 +35,29 @@ class RangeScorer(ScoreCalculator):
35
35
levels and returns the ratio as the score.
36
36
'''
37
37
x_percent : float = 10.0
38
- is_bigger_better : bool = False
39
38
40
39
def calculate (self , history : History ):
41
- # order the highs and lows of the last periods
42
- ordered_highs = sorted (history .high )
43
- ordered_lows = sorted ( history . low )
40
+ # Calculate the number of top elements needed
41
+ length = len (history .high )
42
+ top_count = int ( length / self . x_percent )
44
43
45
- # get top lowest x% highs and top x% highest lows
46
- length = len (ordered_highs )
47
- top_highs = ordered_highs [int (length / self .x_percent )]
48
- top_lows = ordered_lows [- int (length / self .x_percent )]
44
+ # Get the top lowest x% highs and top highest x% lows
45
+ #top_highs = history.calculate_percentile(OHLC.HIGH, 100 - self.x_percent)
46
+ #top_lows = history.calculate_percentile(OHLC.LOW, self.x_percent)
49
47
50
- # this represents the diminished range height
51
- ratio = top_highs / top_lows
48
+ # Calculate the diminished range height
49
+ # ratio = top_highs / top_lows
52
50
53
51
# minimize the standard deviation of highs and lows
54
- from Trading .utils .calculations import calculate_standard_deviation , calculate_mean
55
-
56
- highs_std = calculate_standard_deviation (ordered_highs )
57
- highs_mean = calculate_mean (ordered_highs )
58
- lows_std = calculate_standard_deviation (ordered_lows )
59
- lows_mean = calculate_mean (ordered_lows )
52
+ highs_std = history .calculate_std (OHLC .HIGH )
53
+ highs_mean = history .calculate_mean (OHLC .HIGH )
54
+ lows_std = history .calculate_std (OHLC .LOW )
55
+ lows_mean = history .calculate_mean (OHLC .LOW )
60
56
61
57
highs_std = highs_std / highs_mean
62
58
lows_std = lows_std / lows_mean
63
59
64
- return ratio / (highs_std + lows_std )
60
+ return 1 / (highs_std + lows_std )
65
61
66
62
67
63
class Ordering (BaseModel ):
@@ -72,6 +68,7 @@ class Ordering(BaseModel):
72
68
'''
73
69
top_n : int
74
70
score_calculator : RangeScorer
71
+ is_bigger_better : bool = True
75
72
scores : Optional [Dict [str , float ]] = dict ()
76
73
77
74
model_config = ConfigDict (arbitrary_types_allowed = True )
@@ -81,8 +78,7 @@ def add_history(self, history: History):
81
78
score = round (score , 2 )
82
79
self .scores [history .symbol ] = score
83
80
84
- is_bigger_better = self .score_calculator .is_bigger_better
85
- if is_bigger_better :
81
+ if self .is_bigger_better :
86
82
self .scores = dict (sorted (self .scores .items (), key = lambda item : item [1 ],
87
83
reverse = True )[:self .top_n ])
88
84
else :
0 commit comments