Skip to content

Customized stop function #186

@none2003

Description

@none2003

Hi @edtechre,

Pybroker's built-in stop mechanism only includes stop after certain bars, stop-loss, stop-profit, and trailing stop-loss, which will execute the stop order on the same day's bar when triggered. If users want to develop their own stop-loss mechanism, they can only use the normal buy and sell order mechanism, and these orders will be scheduled and be executed on the next day's bar after triggered, which may cause the backtest result to be different from the expected one.

Is it possible let user set a function in context, and it will be triggered at the same stage of build-in stop(eg, "check_stops" at line 281 of strategy.py, line 1144 of portfolio.py), here is my rough idea:

def custom_stop(ctx): # <- I haven't thought it through, maybe one ctx parameter is not enough
    ...
    return fill_price

def buy_with_stop_loss(ctx):
    if not ctx.long_pos():
        ctx.buy_shares = ctx.calc_target_shares(1)
        ctx.stop_funtion = custom_stop

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions