edtechre / pybroker

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

How to set a different ctx.sell_fill_price #121

Closed goodsm0 closed 6 months ago

goodsm0 commented 6 months ago

Hi, @edtechre,

Thank you for your wonderful tool.

I have a trading scenario but don't know how to implement in pybroker. It's appreciated if you could give some suggestions.

1, Day 1, plan tomorrow's price_buy and the day after tomorrow's price_sell 2, Day 2, if 'ctx.buy_limit_price = price_buy' can work ,then buy it 3, Day 3 , if 'ctx.sell_fill_price = price_sell' can work, then use price_sell to sell all pos , otherwise use PriceType.CLOSE to sell all pos

Thanks

edtechre commented 6 months ago

Hi @goodsm0,

I am not sure what you mean by "X can work". You can't lookahead and test whether a limit price will be filled, because that would introduce leakage into your backtest. But here is an example:

pos = ctx.long_pos()
if not pos and ctx.close[-1] >= some_price:
  ctx.buy_limit_price = price_buy
elif pos.bars == 1:
    ctx.sell_fill_price = price_sell
    ctx.sell_all_shares()

Let me know if this helps!