Skip to content

directly call timeframe_to_one_minute dictionary instead of calling… #502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jesse/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,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 @@ -459,6 +460,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 @@ -350,7 +350,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 @@ -434,7 +434,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 @@ -451,7 +451,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 @@ -825,12 +825,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 @@ -855,7 +875,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 @@ -987,7 +1007,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 @@ -1008,7 +1028,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
7 changes: 1 addition & 6 deletions 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 All @@ -22,25 +22,21 @@ def get_orders(exchange: str, symbol: str) -> List[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
return store.orders.get_orders(exchange, symbol)


def get_time() -> int:
from jesse.store import store
return store.app.time


def get_starting_time() -> int:
from jesse.store import store
return store.app.starting_time


def get_exchange(name: str) -> Any:
if name is None:
raise ValueError(f"name cannot be None. name: {name}")

from jesse.store import store
return store.exchanges.storage.get(name, None)


Expand All @@ -53,7 +49,6 @@ def get_trading_exchange():


def get_all_exchanges() -> ValuesView[Any]:
from jesse.store import store
return store.exchanges.storage.values()


Expand Down
Loading