"""Synthetic one-bar momentum example, not a trading recommendation."""
from decimal import Decimal


def target_positions(bars, parameters):
    """Observe a completed close. The runner executes at the NEXT bar open."""
    long_quantity = parameters["long_contracts"]
    short_quantity = parameters["short_contracts"]
    if type(long_quantity) is not int or type(short_quantity) is not int:
        raise ValueError("Contract quantities must be integers")
    if long_quantity < 1 or short_quantity < 1:
        raise ValueError("Contract quantities must be positive")
    targets = [0]
    for previous, current in zip(bars, bars[1:]):
        change = Decimal(current["close"]) - Decimal(previous["close"])
        targets.append(long_quantity if change > 0 else -short_quantity if change < 0 else 0)
    return targets
