rename fake_range_candle => range_candles and fake_range_candle_from_range_prices => candles_from_close_prices

This commit is contained in:
Saleh Mir
2022-01-14 11:59:42 +01:00
parent 8e3a255926
commit 9fede6adae
10 changed files with 42 additions and 80 deletions

View File

@@ -1,4 +1,4 @@
from .candle_factory import fake_candle
from .candle_factory import fake_range_candle
from .candle_factory import fake_range_candle_from_range_prices
from .candle_factory import range_candles
from .candle_factory import candles_from_close_prices
from .order_factory import fake_order

View File

@@ -14,7 +14,10 @@ min_price = min(open_price, close_price)
low_price = min_price if randint(0, 1) else randint(min_price, min_price + 10)
def fake_range_candle(count: int) -> np.ndarray:
def range_candles(count: int) -> np.ndarray:
"""
Generates a range of candles with random values.
"""
fake_candle(reset=True)
arr = np.zeros((count, 6))
for i in range(count):
@@ -22,7 +25,11 @@ def fake_range_candle(count: int) -> np.ndarray:
return arr
def fake_range_candle_from_range_prices(prices: Union[list, range]) -> np.ndarray:
def candles_from_close_prices(prices: Union[list, range]) -> np.ndarray:
"""
Generates a range of candles from a list of close prices.
The first candle has the timestamp of "2021-01-01T00:00:00+00:00"
"""
fake_candle(reset=True)
global first_timestamp
arr = []

View File

@@ -34,8 +34,8 @@ class TestCompletedTradeAfterExitingTrade(Strategy):
assert trade.entry_price == 10
assert trade.exit_price == 12
assert trade.qty == 10
assert trade.opened_at == 1552309906171.0
assert trade.closed_at == 1552310026171.0
assert trade.opened_at == 1609459800000.0
assert trade.closed_at == 1609459920000.0
assert trade.leverage == 2
# assert all orders have their trade_id set

View File

@@ -2,7 +2,7 @@ import jesse.helpers as jh
import jesse.services.selectors as selectors
from jesse.config import reset_config
from jesse.enums import timeframes, exchanges
from jesse.factories import fake_range_candle
from jesse.factories import range_candles
from jesse.modes import backtest_mode
from jesse.routes import router
from jesse.store import store
@@ -21,7 +21,7 @@ def test_backtesting_one_route():
candles[key] = {
'exchange': exchanges.SANDBOX,
'symbol': 'BTC-USDT',
'candles': fake_range_candle(5 * 20)
'candles': range_candles(5 * 20)
}
# run backtest (dates are fake just to pass)
@@ -78,7 +78,7 @@ def test_backtesting_three_routes():
candles[key] = {
'exchange': r['exchange'],
'symbol': r['symbol'],
'candles': fake_range_candle(5 * 3 * 20)
'candles': range_candles(5 * 3 * 20)
}
# run backtest (dates are fake just to pass)

View File

@@ -1,4 +1,4 @@
from jesse.factories import fake_range_candle
from jesse.factories import range_candles
from jesse.services.candle import *
@@ -14,7 +14,7 @@ def test_candle_includes_price():
def test_generate_candle_from_one_minutes():
candles = fake_range_candle(5)
candles = range_candles(5)
five_minutes_candle = generate_candle_from_one_minutes('5m', candles)

View File

@@ -1,9 +1,8 @@
import jesse.helpers as jh
from jesse.config import reset_config
from jesse.enums import exchanges, timeframes, order_roles
from jesse.factories import fake_range_candle_from_range_prices
from jesse.enums import exchanges, order_roles
from jesse.factories import candles_from_close_prices
from jesse.models import CompletedTrade
from jesse.modes import backtest_mode
from jesse.routes import router
from jesse.store import store
from jesse.config import config
@@ -15,7 +14,7 @@ def get_btc_candles():
jh.key(exchanges.SANDBOX, 'BTC-USDT'): {
'exchange': exchanges.SANDBOX,
'symbol': 'BTC-USDT',
'candles': fake_range_candle_from_range_prices(range(1, 100)),
'candles': candles_from_close_prices(range(1, 100)),
}
}

View File

@@ -1,7 +1,7 @@
import numpy as np
import jesse.indicators as ta
from jesse.factories import fake_range_candle_from_range_prices
from jesse.factories import candles_from_close_prices
from .data.test_candles_indicators import *
matypes = 39
@@ -549,7 +549,7 @@ def test_ema():
118.73, 110.74409879, 111.72, 124.04, 118.52, 113.64, 119.65, 117.11129288, 109.23, 110.77, 102.65,
91.99
]
candles = fake_range_candle_from_range_prices(close_prices)
candles = candles_from_close_prices(close_prices)
single = ta.ema(candles, 8)
seq = ta.ema(candles, 8, sequential=True)
@@ -1762,7 +1762,7 @@ def test_skew():
def test_sma():
close_prices = [22.27, 22.19, 22.08, 22.17, 22.18, 22.13, 22.23, 22.43, 22.24, 22.29]
candles = fake_range_candle_from_range_prices(close_prices)
candles = candles_from_close_prices(close_prices)
single = ta.sma(candles, 10)
seq = ta.sma(candles, 10, sequential=True)

View File

@@ -8,7 +8,7 @@ import jesse.services.selectors as selectors
from jesse import exceptions
from jesse.config import reset_config
from jesse.enums import exchanges, timeframes, order_roles, order_types
from jesse.factories import fake_range_candle, fake_range_candle_from_range_prices
from jesse.factories import range_candles, candles_from_close_prices
from jesse.models import CompletedTrade
from jesse.models import Order
from jesse.modes import backtest_mode
@@ -73,7 +73,7 @@ def test_can_perform_backtest_with_multiple_routes():
candles[key] = {
'exchange': r['exchange'],
'symbol': r['symbol'],
'candles': fake_range_candle((5 * 3) * 20)
'candles': range_candles((5 * 3) * 20)
}
# run backtest (dates are fake just to pass)
@@ -274,7 +274,7 @@ def test_modifying_stop_loss_after_part_of_position_is_already_reduced_with_stop
{'exchange': exchanges.SANDBOX, 'symbol': 'BTC-USDT', 'timeframe': timeframes.MINUTE_1, 'strategy': 'Test14'}
]
generated_candles = fake_range_candle_from_range_prices(
generated_candles = candles_from_close_prices(
list(range(1, 10)) + list(range(10, 1, -1))
)
@@ -335,7 +335,7 @@ def test_multiple_routes_can_communicate_with_each_other():
candles[key] = {
'exchange': r['exchange'],
'symbol': r['symbol'],
'candles': fake_range_candle((5 * 3) * 20)
'candles': range_candles((5 * 3) * 20)
}
# run backtest (dates are fake just to pass)
@@ -541,7 +541,7 @@ def test_should_buy_and_execute_buy():
candles[key] = {
'exchange': r['exchange'],
'symbol': r['symbol'],
'candles': fake_range_candle((5 * 3) * 20)
'candles': range_candles((5 * 3) * 20)
}
# run backtest (dates are fake just to pass)
@@ -587,7 +587,7 @@ def test_should_sell_and_execute_sell():
candles[key] = {
'exchange': r['exchange'],
'symbol': r['symbol'],
'candles': fake_range_candle((5 * 3) * 20)
'candles': range_candles((5 * 3) * 20)
}
# run backtest (dates are fake just to pass)
@@ -683,13 +683,6 @@ def test_terminate_closes_trades_at_the_end_of_backtest():
assert store.app.total_open_trades == 1
assert store.app.total_open_pl == 97
# TODO
# assert {
# 'id': 2,
# 'time': 1552315246171.0,
# 'message': 'Closed open Sandbox-BTC-USDT position at 99.0 with PNL: 97.0(4850.0%) because we reached the end of the backtest session.'
# } in store.logs.info
#
def test_updating_stop_loss_and_take_profit_after_opening_the_position():
set_up()
@@ -822,7 +815,7 @@ def test_positions():
candles[key] = {
'exchange': r['exchange'],
'symbol': r['symbol'],
'candles': fake_range_candle((5 * 3) * 20)
'candles': range_candles((5 * 3) * 20)
}
# run backtest (dates are fake just to pass)
backtest_mode.run(False, {}, routes, [], '2019-04-01', '2019-04-02', candles)
@@ -844,7 +837,7 @@ def test_portfolio_value():
candles[key] = {
'exchange': r['exchange'],
'symbol': r['symbol'],
'candles': fake_range_candle((5 * 3) * 20)
'candles': range_candles((5 * 3) * 20)
}
# run backtest (dates are fake just to pass)
backtest_mode.run(False, {}, routes, [], '2019-04-01', '2019-04-02', candles)
@@ -884,38 +877,3 @@ def test_take_profit_price_is_replaced_with_market_order():
single_route_backtest('TestTakeProfitPriceIsReplacedWithMarketOrderWhenMoreConvenientLongPosition')
# # short position
single_route_backtest('TestTakeProfitPriceIsReplacedWithMarketOrderWhenMoreConvenientShortPosition')
# TODO: implement liquidation in backtest mode for cross mode
# def test_liquidation_in_cross_mode_for_short_trades():
# single_route_backtest(
# 'TestLiquidationInCrossModeForShortTrade', is_futures_trading=True, leverage=10,
# leverage_mode='cross'
# )
# def test_route_capital_isolation():
# set_up(
# [
# (exchanges.SANDBOX, 'BTC-USDT', timeframes.MINUTE_1, 'TestRouteCapitalIsolation1'),
# (exchanges.SANDBOX, 'ETH-USDT', timeframes.MINUTE_1, 'TestRouteCapitalIsolation2'),
# ],
# )
#
# # run backtest (dates are fake just to pass)
# backtest_mode.run('2019-04-01', '2019-04-02', get_btc_and_eth_candles())
# def test_inputs_get_rounded_behind_the_scene():
# set_up([(exchanges.SANDBOX, 'EOS-USDT', timeframes.MINUTE_1, 'Test44')])
# candles = {}
# candles[jh.key(exchanges.SANDBOX, 'EOS-USDT')] = {
# 'exchange': exchanges.SANDBOX,
# 'symbol': 'EOS-USDT',
# 'candles': fake_range_candle_from_range_prices(range(1, 100))
# }
# backtest_mode.run('2019-04-01', '2019-04-02', candles)
#
# t: CompletedTrade = store.completed_trades.trades[0]
#
# assert len(store.completed_trades.trades) == 1
# assert t.qty == 1.5
# assert t.entry_price == 5.123

View File

@@ -1,7 +1,7 @@
import numpy as np
from jesse.config import config, reset_config
from jesse.factories import fake_candle, fake_range_candle
from jesse.factories import fake_candle, range_candles
from jesse.services.candle import generate_candle_from_one_minutes
from jesse.store import store
@@ -26,7 +26,7 @@ def test_batch_add_candles():
assert len(store.candles.get_candles('Sandbox', 'BTC-USD', '1m')) == 0
# create 100 candles
candles_to_add = fake_range_candle(100)
candles_to_add = range_candles(100)
assert len(candles_to_add) == 100
store.candles.batch_add_candle(candles_to_add, 'Sandbox', 'BTC-USD', '1m')
@@ -72,7 +72,7 @@ def test_can_update_candle():
def test_get_candles_including_forming():
set_up()
candles_to_add = fake_range_candle(14)
candles_to_add = range_candles(14)
store.candles.batch_add_candle(candles_to_add, 'Sandbox', 'BTC-USD', '1m')
store.candles.add_candle(
generate_candle_from_one_minutes(
@@ -113,7 +113,7 @@ def test_get_candles_including_forming():
def test_get_forming_candle():
set_up()
candles_to_add = fake_range_candle(13)
candles_to_add = range_candles(13)
store.candles.batch_add_candle(candles_to_add[0:4], 'Sandbox', 'BTC-USD', '1m')
forming_candle = store.candles.get_current_candle('Sandbox', 'BTC-USD', '5m')
assert forming_candle[0] == candles_to_add[0][0]

View File

@@ -1,10 +1,8 @@
import jesse.helpers as jh
from jesse.config import reset_config
from jesse.enums import exchanges, timeframes
from jesse.factories import fake_range_candle_from_range_prices
from jesse.enums import exchanges
from jesse.factories import candles_from_close_prices
from jesse.modes import backtest_mode
from jesse.routes import router
from jesse.store import store
from jesse.config import config
@@ -13,14 +11,14 @@ def get_btc_and_eth_candles():
jh.key(exchanges.SANDBOX, 'BTC-USDT'): {
'exchange': exchanges.SANDBOX,
'symbol': 'BTC-USDT',
'candles': fake_range_candle_from_range_prices(range(101, 200)),
'candles': candles_from_close_prices(range(101, 200)),
}
}
candles[jh.key(exchanges.SANDBOX, 'ETH-USDT')] = {
'exchange': exchanges.SANDBOX,
'symbol': 'ETH-USDT',
'candles': fake_range_candle_from_range_prices(range(1, 100))
'candles': candles_from_close_prices(range(1, 100))
}
return candles
@@ -30,7 +28,7 @@ def get_btc_candles():
jh.key(exchanges.SANDBOX, 'BTC-USDT'): {
'exchange': exchanges.SANDBOX,
'symbol': 'BTC-USDT',
'candles': fake_range_candle_from_range_prices(range(1, 100)),
'candles': candles_from_close_prices(range(1, 100)),
}
}
@@ -40,7 +38,7 @@ def get_downtrend_candles():
jh.key(exchanges.SANDBOX, 'BTC-USDT'): {
'exchange': exchanges.SANDBOX,
'symbol': 'BTC-USDT',
'candles': fake_range_candle_from_range_prices(range(100, 10, -1)),
'candles': candles_from_close_prices(range(100, 10, -1)),
}
}