Skip to content
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

Issue in grid trading #149

Closed
SUSHANTH009 opened this issue Oct 7, 2024 · 2 comments
Closed

Issue in grid trading #149

SUSHANTH009 opened this issue Oct 7, 2024 · 2 comments

Comments

@SUSHANTH009
Copy link

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

# 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

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

@nkaz001
Copy link
Owner

nkaz001 commented Oct 8, 2024

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.

Repository owner deleted a comment Oct 21, 2024
@nkaz001 nkaz001 closed this as completed Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@nkaz001 @SUSHANTH009 and others