1414import click
1515import numpy as np
1616import base64
17-
17+ from jesse .constants import CANDLE_SOURCE_MAPPING
18+ from jesse .constants import TIMEFRAME_PRIORITY
19+ from jesse .constants import SUPPORTED_COLORS
20+ from jesse .enums import timeframes
1821
1922CACHED_CONFIG = dict ()
2023
@@ -73,23 +76,10 @@ def color(msg_text: str, msg_color: str) -> str:
7376 if not msg_text :
7477 return ''
7578
76- if msg_color == 'black' :
77- return click .style (msg_text , fg = 'black' )
78- if msg_color == 'red' :
79- return click .style (msg_text , fg = 'red' )
80- if msg_color == 'green' :
81- return click .style (msg_text , fg = 'green' )
82- if msg_color == 'yellow' :
83- return click .style (msg_text , fg = 'yellow' )
84- if msg_color == 'blue' :
85- return click .style (msg_text , fg = 'blue' )
86- if msg_color == 'magenta' :
87- return click .style (msg_text , fg = 'magenta' )
88- if msg_color == 'cyan' :
89- return click .style (msg_text , fg = 'cyan' )
90- if msg_color in {'white' , 'gray' }:
79+ if msg_color in SUPPORTED_COLORS :
80+ return click .style (msg_text , fg = msg_color )
81+ if msg_color == 'gray' :
9182 return click .style (msg_text , fg = 'white' )
92-
9383 raise ValueError ('unsupported color' )
9484
9585
@@ -112,53 +102,27 @@ def dashless_symbol(symbol: str) -> str:
112102
113103
114104def dashy_symbol (symbol : str ) -> str :
115- # if already has '-' in symbol, return symbol
116105 if '-' in symbol :
117106 return symbol
118107
119108 from jesse .config import config
120-
121109 for s in config ['app' ]['considering_symbols' ]:
122- compare_symbol = dashless_symbol (s )
123- if compare_symbol == symbol :
110+ if dashless_symbol (s ) == symbol :
124111 return s
125112
126- if symbol .endswith ('EUR' ):
127- return symbol [:- 3 ] + '-EUR'
128- if symbol .endswith ('EUT' ):
129- return symbol [:- 3 ] + '-EUT'
130- if symbol .endswith ('GBP' ):
131- return symbol [:- 3 ] + '-GBP'
132- if symbol .endswith ('JPY' ):
133- return symbol [:- 3 ] + '-JPY'
134- if symbol .endswith ('MIM' ):
135- return symbol [:- 3 ] + '-MIM'
136- if symbol .endswith ('TRY' ):
137- return symbol [:- 3 ] + '-TRY'
138- if symbol .endswith ('FDUSD' ):
139- return symbol [:- 5 ] + '-FDUSD'
140- if symbol .endswith ('TUSD' ):
141- return symbol [:- 4 ] + '-TUSD'
142- if symbol .endswith ('UST' ):
143- return symbol [:- 3 ] + '-UST'
144- if symbol .endswith ('USDT' ):
145- return symbol [:- 4 ] + '-USDT'
146- if symbol .endswith ('USDC' ):
147- return symbol [:- 4 ] + '-USDC'
148- if symbol .endswith ('USDS' ):
149- return symbol [:- 4 ] + '-USDS'
150- if symbol .endswith ('USDP' ):
151- return symbol [:- 4 ] + '-USDP'
152- if symbol .endswith ('USDU' ):
153- return symbol [:- 4 ] + '-USDU'
154- if symbol .endswith ('USD' ):
155- return symbol [:- 3 ] + '-USD'
156-
157- if len (symbol ) > 7 and symbol .endswith ('SUSDT' ):
158- # ex: SETHSUSDT => SETH-SUSDT
159- return symbol [:- 5 ] + '-' + symbol [- 5 :]
160-
161- return f"{ symbol [0 :3 ]} -{ symbol [3 :]} "
113+ suffixes = [
114+ 'FDUSD' , 'TUSD' , 'EUT' , 'EUR' , 'GBP' , 'JPY' , 'MIM' , 'TRY' , 'UST' , 'SUSDT'
115+ ]
116+
117+ for suffix in suffixes :
118+ if symbol .endswith (suffix ):
119+ return f"{ symbol [:- len (suffix )]} -{ suffix } "
120+
121+ if "USD" in symbol [- 4 :]: # Only look at the last 4 letters
122+ idx = symbol .rfind ("USD" )
123+ return f"{ symbol [:idx ]} -{ symbol [idx :]} "
124+
125+ return f"{ symbol [:3 ]} -{ symbol [3 :]} "
162126
163127
164128def underline_to_dashy_symbol (symbol : str ) -> str :
@@ -315,31 +279,12 @@ def get_arrow(timestamp: int) -> arrow.arrow.Arrow:
315279
316280def get_candle_source (candles : np .ndarray , source_type : str = "close" ) -> np .ndarray :
317281 """
318- Returns the candles corresponding the selected type.
319-
320- :param candles: np.ndarray
321- :param source_type: string
322- :return: np.ndarray
323- """
324-
325- if source_type == "close" :
326- return candles [:, 2 ]
327- elif source_type == "high" :
328- return candles [:, 3 ]
329- elif source_type == "low" :
330- return candles [:, 4 ]
331- elif source_type == "open" :
332- return candles [:, 1 ]
333- elif source_type == "volume" :
334- return candles [:, 5 ]
335- elif source_type == "hl2" :
336- return (candles [:, 3 ] + candles [:, 4 ]) / 2
337- elif source_type == "hlc3" :
338- return (candles [:, 3 ] + candles [:, 4 ] + candles [:, 2 ]) / 3
339- elif source_type == "ohlc4" :
340- return (candles [:, 1 ] + candles [:, 3 ] + candles [:, 4 ] + candles [:, 2 ]) / 4
341- else :
342- raise ValueError ('type string not recognised' )
282+ Returns the candles corresponding to the selected type.
283+ """
284+ try :
285+ return CANDLE_SOURCE_MAPPING [source_type ](candles )
286+ except KeyError :
287+ raise ValueError (f"Source type '{ source_type } ' not recognised" )
343288
344289
345290def get_config (keys : str , default : Any = None ) -> Any :
@@ -522,35 +467,11 @@ def key(exchange: str, symbol: str, timeframe: str = None):
522467
523468
524469def max_timeframe (timeframes_list : list ) -> str :
525- from jesse .enums import timeframes
526-
527- if timeframes .DAY_1 in timeframes_list :
528- return timeframes .DAY_1
529- if timeframes .HOUR_12 in timeframes_list :
530- return timeframes .HOUR_12
531- if timeframes .HOUR_8 in timeframes_list :
532- return timeframes .HOUR_8
533- if timeframes .HOUR_6 in timeframes_list :
534- return timeframes .HOUR_6
535- if timeframes .HOUR_4 in timeframes_list :
536- return timeframes .HOUR_4
537- if timeframes .HOUR_3 in timeframes_list :
538- return timeframes .HOUR_3
539- if timeframes .HOUR_2 in timeframes_list :
540- return timeframes .HOUR_2
541- if timeframes .HOUR_1 in timeframes_list :
542- return timeframes .HOUR_1
543- if timeframes .MINUTE_45 in timeframes_list :
544- return timeframes .MINUTE_45
545- if timeframes .MINUTE_30 in timeframes_list :
546- return timeframes .MINUTE_30
547- if timeframes .MINUTE_15 in timeframes_list :
548- return timeframes .MINUTE_15
549- if timeframes .MINUTE_5 in timeframes_list :
550- return timeframes .MINUTE_5
551- if timeframes .MINUTE_3 in timeframes_list :
552- return timeframes .MINUTE_3
553470
471+ times = set (timeframes_list )
472+ for tf in TIMEFRAME_PRIORITY :
473+ if tf in times :
474+ return tf
554475 return timeframes .MINUTE_1
555476
556477
0 commit comments