edtechre / pybroker

Algorithmic Trading in Python with Machine Learning
https://www.pybroker.com
Other
2.09k stars 261 forks source link

"ValueError: DataSource is empty." for Akshare as data sources #79

Closed szz65 closed 12 months ago

szz65 commented 1 year ago

Hi, there. It's my first time to try pybroker. And when I test a script, I just met the error "ValueError: DataSource is empty." Here are my test script: import pybroker from pybroker import Strategy, StrategyConfig, ExecContext from pybroker.ext.data import AKShare

config = StrategyConfig(initial_cash=500_000)

strategy = Strategy(data_source=AKShare(), start_date='4/6/2022', end_date='11/24/2023', config=config)

def buy_low(ctx):

If shares were already purchased and are currently being held, then return.

if ctx.long_pos():
    return
# If the latest close price is less than the previous day's low price,
# then place a buy order.
if ctx.bars >= 2 and ctx.close[-1] < ctx.low[-2]:
    # Buy a number of shares that is equal to 25% the portfolio.
    ctx.buy_shares = ctx.calc_target_shares(0.25)
    # Set the limit price of the order.
    ctx.buy_limit_price = ctx.close[-1] - 0.01
    # Hold the position for 3 bars before liquidating (in this case, 3 days).
    ctx.hold_bars = 3

strategy.add_execution(buy_low, symbols=['sh113055'])

def short_high(ctx):

If shares were already shorted then return.

if ctx.short_pos():
    return
# If the latest close price is more than the previous day's high price,
# then place a sell order.
if ctx.bars >= 2 and ctx.close[-1] > ctx.high[-2]:
    # Short 100 shares.
    ctx.sell_shares = 100
    # Cover the shares after 2 bars (in this case, 2 days).
    ctx.hold_bars = 2

strategy.add_execution(short_high, symbols=['sh110079'])

result = strategy.backtest()"

edtechre commented 1 year ago

Thank you for posting the code @szz65. I tried querying with just the DataSource, and I also received an empty DataFrame:

ds = AKShare()
rows = ds.query(symbols=['sh110079'], start_date='4/6/2022', end_date='11/24/2023')

@albertandking do you know if there is an issue with the above symbol?

none2003 commented 1 year ago

Currently, pyb's support for akshare can only support stock queries. See line 67 of pybroker/ext/data.py:

temp_df = akshare.stock_zh_a_hist(
    symbols_simple[i],
    start_date=start_date_str,
    end_date=end_date_str,
    period="daily",
    adjust=adjust if adjust is not None else "",
)

sh110079 is a convertible bonds. Use a different API: https://akshare.akfamily.xyz/data/bond/bond.html#id24

import akshare as ak

bond_zh_hs_cov_daily_df = ak.bond_zh_hs_cov_daily(symbol="sh110079")
print(bond_zh_hs_cov_daily_df)
albertandking commented 1 year ago

Thank you for posting the code @szz65. I tried querying with just the DataSource, and I also received an empty DataFrame:

ds = AKShare()
rows = ds.query(symbols=['sh110079'], start_date='4/6/2022', end_date='11/24/2023')

@albertandking do you know if there is an issue with the above symbol?

yes, ref @none2003

edtechre commented 12 months ago

Thank you @none2003 and @albertandking!