You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Running interval in nanoseconds.
while hbt.elapse(100_000_000) == 0:
# Clears cancelled, filled or expired orders.
hbt.clear_inactive_orders(asset_no)
depth = hbt.depth(asset_no)
position = hbt.position(asset_no)
orders = hbt.orders(asset_no)
best_bid = depth.best_bid
best_ask = depth.best_ask
mid_price = (best_bid + best_ask) / 2.0
order_qty = 0.1 # np.round(notional_order_qty / mid_price / hbt.depth(asset_no).lot_size) * hbt.depth(asset_no).lot_size
# Aligns the prices to the grid.
bid_price = np.floor((mid_price - half_spread) / grid_interval) * grid_interval
ask_price = np.ceil((mid_price + half_spread) / grid_interval) * grid_interval
#--------------------------------------------------------
# Updates quotes.
# Creates a new grid for buy orders.
new_bid_orders = Dict.empty(np.uint64, np.float64)
if position < max_position and np.isfinite(bid_price): # position * mid_price < max_notional_position
for i in range(grid_num):
bid_price_tick = round(bid_price / tick_size)
# order price in tick is used as order id.
new_bid_orders[uint64(bid_price_tick)] = bid_price
bid_price -= grid_interval
# Creates a new grid for sell orders.
new_ask_orders = Dict.empty(np.uint64, np.float64)
if position > -max_position and np.isfinite(ask_price): # position * mid_price > -max_notional_position
for i in range(grid_num):
ask_price_tick = round(ask_price / tick_size)
# order price in tick is used as order id.
new_ask_orders[uint64(ask_price_tick)] = ask_price
ask_price += grid_interval
order_values = orders.values();
while order_values.has_next():
order = order_values.get()
# Cancels if a working order is not in the new grid.
if order.cancellable:
if (
(order.side == BUY and order.order_id not in new_bid_orders)
or (order.side == SELL and order.order_id not in new_ask_orders)
):
hbt.cancel(asset_no, order.order_id, False)
for order_id, order_price in new_bid_orders.items():
# Posts a new buy order if there is no working order at the price on the new grid.
if order_id not in orders:
hbt.submit_buy_order(asset_no, order_id, order_price, order_qty, GTX, LIMIT, False)
for order_id, order_price in new_ask_orders.items():
# Posts a new sell order if there is no working order at the price on the new grid.
if order_id not in orders:
hbt.submit_sell_order(asset_no, order_id, order_price, order_qty, GTX, LIMIT, False)
# Records the current state for stat calculation.
recorder.record(hbt)
return True
from hftbacktest import BacktestAsset, ROIVectorMarketDepthBacktest, Recorder
Please ensure that you have correctly input the tick size, lot size, ROI upper bound (roi_ub), and ROI lower bound (roi_lb). See the tutorials and documentation for guidance.
Hi,I apologize if there is any silly mistake
I am facing this issue
import numpy as np
from numba import njit, uint64, float64
from numba.typed import Dict
from hftbacktest import BUY, SELL, GTX, LIMIT
@njit
def gridtrading(hbt, recorder):
asset_no = 0
tick_size = hbt.depth(asset_no).tick_size
grid_num = 20
max_position = 5
grid_interval = tick_size * 10
half_spread = tick_size * 20
from hftbacktest import BacktestAsset, ROIVectorMarketDepthBacktest, Recorder
asset = (
BacktestAsset()
.data([
'data/btcusdt_20240819.npz',
'data/btcusdt_20240820.npz',
'data/btcusdt_20240821.npz',
'data/btcusdt_20240822.npz',
'data/btcusdt_20240823.npz'
])
.initial_snapshot('data/btcusdt_20240818.npz')
.linear_asset(1.0)
.intp_order_latency([
'data/feed_latency_20240819.npz',
'data/feed_latency_20240820.npz',
'data/feed_latency_20240821.npz',
'data/feed_latency_20240822.npz',
'data/feed_latency_20240823.npz'
])
.power_prob_queue_model(2.0)
.no_partial_fill_exchange()
.trading_value_fee_model(-0.00005, 0.0007)
.tick_size(0.01)
.lot_size(0.001)
.roi_lb(0.0)
.roi_ub(3000.0)
)
hbt = ROIVectorMarketDepthBacktest([asset])
recorder = Recorder(1, 5_000_000)
%%time
gridtrading(hbt, recorder.recorder)
_ = hbt.close()
from hftbacktest.stats import LinearAssetRecord
stats = LinearAssetRecord(recorder.get(0)).stats(book_size=1000)
stats.summary()
start | end | SR | Sortino | Return | MaxDrawdown | DailyNumberOfTrades | DailyTurnover | ReturnOverMDD | ReturnOverTrade | MaxPositionValue
2024-08-19 00:00:00 | 2024-08-23 23:59:50 | NaN | NaN | NaN | 0.6 | 12.000278 | 148.706542 | NaN | NaN | 743822.4
why ami getting Nan
The text was updated successfully, but these errors were encountered: