Skip to content

Commit

Permalink
directly call timeframe_to_one_minute dictionary instead of calling…
Browse files Browse the repository at this point in the history
… the function also cache and move imports to modules
  • Loading branch information
yakiraid committed Feb 14, 2025
1 parent 21f8935 commit ccf3051
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
2 changes: 2 additions & 0 deletions jesse/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ def is_importing_candles() -> bool:
return config['app']['trading_mode'] == 'candles'


@lru_cache
def is_live() -> bool:
return is_livetrading() or is_paper_trading()

Expand All @@ -457,6 +458,7 @@ def is_optimizing() -> bool:
return config['app']['trading_mode'] == 'optimize'


@lru_cache
def is_paper_trading() -> bool:
from jesse.config import config
return config['app']['trading_mode'] == 'papertrade'
Expand Down
34 changes: 27 additions & 7 deletions jesse/modes/backtest_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def _handle_warmup_candles(warmup_candles: dict, start_date: str) -> None:
warmup_num = jh.get_config('env.data.warmup_candles_num', 210)
max_timeframe = jh.max_timeframe(config['app']['considering_timeframes'])
# Convert max_timeframe to minutes and multiply by warmup_num
warmup_minutes = jh.timeframe_to_one_minutes(max_timeframe) * warmup_num
warmup_minutes = timeframe_to_one_minutes[max_timeframe] * warmup_num
warmup_start_timestamp = jh.date_to_timestamp(start_date) - (warmup_minutes * 60_000)
warmup_start_date = jh.timestamp_to_date(warmup_start_timestamp)
# Publish the missing candles error to the frontend
Expand Down Expand Up @@ -433,7 +433,7 @@ def _step_simulator(
if timeframe == '1m':
continue

count = jh.timeframe_to_one_minutes(timeframe)
count = timeframe_to_one_minutes[timeframe]
# until = count - ((i + 1) % count)

if (i + 1) % count == 0:
Expand All @@ -450,7 +450,7 @@ def _step_simulator(

# now that all new generated candles are ready, execute
for r in router.routes:
count = jh.timeframe_to_one_minutes(r.timeframe)
count = timeframe_to_one_minutes[r.timeframe]
# 1m timeframe
if r.timeframe == timeframes.MINUTE_1:
r.strategy._execute()
Expand Down Expand Up @@ -823,12 +823,32 @@ def _calculate_minimum_candle_step():
# config["app"]["considering_timeframes"] use '1m' also even if not required by the user so take only what the user
# is requested.
consider_time_frames = [
jh.timeframe_to_one_minutes(route["timeframe"])
timeframe_to_one_minutes[route["timeframe"]]
for route in router.all_formatted_routes
]
return np.gcd.reduce(consider_time_frames)


timeframe_to_one_minutes = {
timeframes.MINUTE_1: 1,
timeframes.MINUTE_3: 3,
timeframes.MINUTE_5: 5,
timeframes.MINUTE_15: 15,
timeframes.MINUTE_30: 30,
timeframes.MINUTE_45: 45,
timeframes.HOUR_1: 60,
timeframes.HOUR_2: 60 * 2,
timeframes.HOUR_3: 60 * 3,
timeframes.HOUR_4: 60 * 4,
timeframes.HOUR_6: 60 * 6,
timeframes.HOUR_8: 60 * 8,
timeframes.HOUR_12: 60 * 12,
timeframes.DAY_1: 60 * 24,
timeframes.DAY_3: 60 * 24 * 3,
timeframes.WEEK_1: 60 * 24 * 7,
timeframes.MONTH_1: 60 * 24 * 30,
}

def _simulate_new_candles(candles: dict, candle_index: int, candles_step: int) -> None:
i = candle_index
# add candles
Expand All @@ -853,7 +873,7 @@ def _simulate_new_candles(candles: dict, candle_index: int, candles_step: int) -
if timeframe == "1m":
continue

count = jh.timeframe_to_one_minutes(timeframe)
count = timeframe_to_one_minutes[timeframe]

if (i + candles_step) % count == 0:
generated_candle = generate_candle_from_one_minutes(
Expand Down Expand Up @@ -985,7 +1005,7 @@ def _update_all_routes_a_partial_candle(
continue
if timeframe == '1m':
continue
tf_minutes = jh.timeframe_to_one_minutes(timeframe)
tf_minutes = timeframe_to_one_minutes[timeframe]
number_of_needed_candles = int(storable_temp_candle[0] % (tf_minutes * 60_000) // 60000) + 1
candles_1m = store.candles.get_candles(exchange, symbol, '1m')[-number_of_needed_candles:]
generated_candle = generate_candle_from_one_minutes(
Expand All @@ -1006,7 +1026,7 @@ def _update_all_routes_a_partial_candle(
def _execute_routes(candle_index: int, candles_step: int) -> None:
# now that all new generated candles are ready, execute
for r in router.routes:
count = jh.timeframe_to_one_minutes(r.timeframe)
count = timeframe_to_one_minutes[r.timeframe]
# 1m timeframe
if r.timeframe == timeframes.MINUTE_1:
r.strategy._execute()
Expand Down
2 changes: 1 addition & 1 deletion jesse/services/selectors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from jesse.store import store
from typing import List, ValuesView, Optional, Any


Expand All @@ -13,7 +14,6 @@ def get_position(exchange: str, symbol: str) -> Any:
if exchange is None or symbol is None:
raise ValueError(f"exchange and symbol cannot be None. exchange: {exchange}, symbol: {symbol}")

from jesse.store import store
key = f'{exchange}-{symbol}'
return store.positions.storage.get(key, None)

Expand Down

0 comments on commit ccf3051

Please sign in to comment.