Skip to content

How to long the highest score and short the lowest score? #191

@river-walras

Description

@river-walras

Hi, I want to long the highest score and short the lowest score

Here is my code:

def volume(ctx: ExecContext):
    # If there are no long positions across all tickers being traded:
    if not tuple(ctx.long_positions()):
        ctx.buy_shares = ctx.calc_target_shares(1/6)
        ctx.hold_bars = 1
        ctx.score = ctx.quote_volume[-1]

    if not tuple(ctx.short_positions()):
        ctx.sell_shares = ctx.calc_target_shares(1 / 3)
        ctx.hold_bars = 1
        ctx.score = 1 / ctx.quote_volume[-1]

strategy = Strategy(
    data_source=source,
    start_date=start_date,
    end_date=end_date,
    config=StrategyConfig(
        fee_mode=FeeMode.ORDER_PERCENT,
        fee_amount=0.0005,
        exit_on_last_bar=True,
        round_fill_price=False,
        max_long_positions=3,
        max_short_positions=3,
    ),
)

Since the score is rank from the highest to the lowest, so for short symbols I need to set score = 1/ctx.quote_volume[-1]. However, I get the error:

ValueError: For each symbol, only one of buy_shares or sell_shares can be set per bar.

I know I can only set buy_shares and sell_shares for one symbol, But before I know the rank of the symbol, I need to pass the value to rank. How to solve this problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions