def store_candles(candles: np.ndarray, exchange: str, symbol: str) -> None: """ Stores candles in the database. The stored data can later be used for being fetched again via get_candles or even for running backtests on them. A common use case for this function is for importing candles from a CSV file so you can later use them for backtesting. """ from jesse.services.db import store_candles as store_candles_from_list import jesse.helpers as jh # check if .env file exists if not jh.is_jesse_project(): raise FileNotFoundError( 'Invalid directory: ".env" file not found. To use Jesse inside notebooks, create notebooks inside the root of a Jesse project.' ) # TODO: add validation for timeframe to make sure it's `1m` arr = [{ 'id': jh.generate_unique_id(), 'symbol': symbol, 'exchange': exchange, 'timestamp': c[0], 'open': c[1], 'close': c[2], 'high': c[3], 'low': c[4], 'volume': c[5] } for c in candles] store_candles_from_list(arr)
def open_connection(self) -> None: if not jh.is_jesse_project() or jh.is_unit_testing(): return # if it's not None, then we already have a connection if self.db is not None: return options = { "keepalives": 1, "keepalives_idle": 60, "keepalives_interval": 10, "keepalives_count": 5 } self.db = PostgresqlExtDatabase( ENV_VALUES['POSTGRES_NAME'], user=ENV_VALUES['POSTGRES_USERNAME'], password=ENV_VALUES['POSTGRES_PASSWORD'], host=ENV_VALUES['POSTGRES_HOST'], port=int(ENV_VALUES['POSTGRES_PORT']), sslmode=ENV_VALUES.get('POSTGRES_SSLMODE', 'disable'), **options) # connect to the database self.db.connect()
def validate_cwd() -> None: """ make sure we're in a Jesse project """ if not jh.is_jesse_project(): print( jh.color( 'Current directory is not a Jesse project. You must run commands from the root of a Jesse project. Read this page for more info: https://docs.jesse.trade/docs/getting-started/#create-a-new-jesse-project', 'red')) os._exit(1)
from dotenv import load_dotenv, dotenv_values import jesse.helpers as jh import os import sys # fix directory issue sys.path.insert(0, os.getcwd()) ENV_VALUES = {} if jh.is_jesse_project(): # load env load_dotenv() # create and expose ENV_VALUES ENV_VALUES = dotenv_values('.env') if jh.is_unit_testing(): ENV_VALUES['POSTGRES_HOST'] = '127.0.0.1' ENV_VALUES['POSTGRES_NAME'] = 'jesse_db' ENV_VALUES['POSTGRES_PORT'] = '5432' ENV_VALUES['POSTGRES_USERNAME'] = '******' ENV_VALUES['POSTGRES_PASSWORD'] = '******' ENV_VALUES['REDIS_HOST'] = 'localhost' ENV_VALUES['REDIS_PORT'] = '6379' ENV_VALUES['REDIS_DB'] = 0 ENV_VALUES['REDIS_PASSWORD'] = '' # validation for existence of .env file if len(list(ENV_VALUES.keys())) == 0: jh.error(
def get_candles(exchange: str, symbol: str, timeframe: str, start_date: str, finish_date: str) -> np.ndarray: """ Returns candles from the database in numpy format :param exchange: str :param symbol: str :param timeframe: str :param start_date: str :param finish_date: str :return: np.ndarray """ import arrow import jesse.helpers as jh from jesse.models import Candle from jesse.exceptions import CandleNotFoundInDatabase from jesse.services.candle import generate_candle_from_one_minutes # check if .env file exists if not jh.is_jesse_project(): raise FileNotFoundError( 'Invalid directory: ".env" file not found. To use Jesse inside notebooks, create notebooks inside the root of a Jesse project.' ) symbol = symbol.upper() start_date = jh.arrow_to_timestamp(arrow.get(start_date, 'YYYY-MM-DD')) finish_date = jh.arrow_to_timestamp(arrow.get(finish_date, 'YYYY-MM-DD')) - 60000 # validate if start_date == finish_date: raise ValueError('start_date and finish_date cannot be the same.') if start_date > finish_date: raise ValueError('start_date cannot be bigger than finish_date.') if finish_date > arrow.utcnow().int_timestamp * 1000: raise ValueError('Can\'t backtest the future!') # fetch from database candles_tuple = Candle.select( Candle.timestamp, Candle.open, Candle.close, Candle.high, Candle.low, Candle.volume ).where( Candle.timestamp.between(start_date, finish_date), Candle.exchange == exchange, Candle.symbol == symbol).order_by(Candle.timestamp.asc()).tuples() candles = np.array(tuple(candles_tuple)) # validate that there are enough candles for selected period if len(candles) == 0 or candles[-1][0] != finish_date or candles[0][0] != start_date: raise CandleNotFoundInDatabase(f'Not enough candles for {symbol}. Try importing candles first.') if timeframe == '1m': return candles generated_candles = [] for i in range(len(candles)): num = jh.timeframe_to_one_minutes(timeframe) if (i + 1) % num == 0: generated_candles.append( generate_candle_from_one_minutes( timeframe, candles[(i - (num - 1)):(i + 1)], True ) ) return np.array(generated_candles)