edtechre / pybroker

Algorithmic Trading in Python with Machine Learning

Home Page:https://www.pybroker.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to set a different ctx.sell_fill_price

goodsm0 opened this issue · comments

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

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!