@@ -19,15 +19,18 @@ class Returns(GenericCandleAttachment[float]):
1919
2020
2121class ReturnsCalculatorProcessor (Processor ):
22- def __init__ (self , returns_count : int , next_processor : Optional [Processor ] = None ):
22+ def __init__ (self , field_prefix : str , returns_count : int , next_processor : Optional [Processor ] = None ):
2323 super ().__init__ (next_processor )
24+ self .field_prefix = field_prefix
2425 self .returns_count = returns_count
2526
2627 def process (self , context : SharedContext , candle : Candle ):
2728 cache_reader = CandleCache .context_reader (context )
2829 symbol_candles = cache_reader .get_symbol_candles (candle .symbol ) or []
2930
30- if len (symbol_candles ) > self .returns_count :
31+ candle .attachments .add_attachement (RETURNS_ATTACHMENT_KEY , Returns ())
32+
33+ if len (symbol_candles ) >= self .returns_count :
3134 candle_returns = self ._calc_returns (candle , symbol_candles )
3235 candle .attachments .add_attachement (RETURNS_ATTACHMENT_KEY , candle_returns )
3336
@@ -36,16 +39,17 @@ def process(self, context: SharedContext, candle: Candle):
3639
3740 def _calc_returns (self , current_candle : Candle , candles : List [Candle ]) -> Returns :
3841 candle_returns = Returns ()
39- for i in range (1 , self .returns_count ):
40- candle_returns .set (f'ctc { i } ' , (1 - current_candle .close / candles [- i ].close ) * 100 )
42+ for i in range (1 , self .returns_count + 1 ):
43+ candle_returns .set (f'{ self . field_prefix } - { i } ' , (1 - current_candle .close / candles [- i ].close ) * 100 )
4144
4245 return candle_returns
4346
4447 def serialize (self ) -> Dict :
4548 return {
46- 'returnsCount' : self .returns_count
49+ 'returnsCount' : self .returns_count ,
50+ 'fieldPrefix' : self .field_prefix
4751 }
4852
4953 @classmethod
5054 def deserialize (cls , data : Dict ) -> Optional [Processor ]:
51- return cls (data ['returnsCount' ], cls ._deserialize_next_processor (data ))
55+ return cls (data ['fieldPrefix' ], data [ ' returnsCount' ], cls ._deserialize_next_processor (data ))
0 commit comments