Example #1
0
def test_Current_immutable():
    current = Current(
        high=100.0,
        low=50.0,
        close=75.0,
        bid=2.0,
        bid_size=10,
        ask=3.0,
        ask_size=20,
        last=2.5,
        last_size=5,
        volume=1000,
        time=datetime.datetime.now(),
    )
    with pytest.raises(FrozenInstanceError):
        current.high = 300.0
Example #2
0
def test_Current_init():
    test_time = datetime.datetime.now()
    current = Current(
        high=100.0,
        low=50.0,
        close=75.0,
        bid=2.0,
        bid_size=10,
        ask=3.0,
        ask_size=20,
        last=2.5,
        last_size=5,
        volume=1000,
        time=test_time,
    )

    assert current.high == 100.0
    assert current.low == 50.0
    assert current.close == 75.0
    assert current.bid == 2.0
    assert current.bid_size == 10
    assert current.ask == 3.0
    assert current.ask_size == 20
    assert current.last == 2.5
    assert current.last_size == 5
    assert current.volume == 1000
    assert current.time == test_time
Example #3
0
def test_Asset_init():
    id = AssetId("SPY", AssetType.Stock, Currency.USDollar, None)
    asset = Asset(id)
    asset.current = Current(
        high=100.0,
        low=50.0,
        close=75.0,
        bid=2.0,
        bid_size=10,
        ask=3.0,
        ask_size=20,
        last=2.5,
        last_size=5,
        volume=1000,
        time=datetime.datetime.now(),
    )
    bar = Bar(
        count=44,
        open=50.0,
        high=70.0,
        low=40.0,
        close=60.0,
        average=45.5,
        volume=2000,
        time=datetime.datetime.now(),
    )
    m = Measures(
        iv=0.45,
        iv_rank=0.78,
        iv_percentile=0.91,
        iv_pct=0.03,
        stdev=0.04,
        beta=0.2,
        correlation=0.5,
        price_percentile=0.56,
        price_pct=0.02,
        directional_assumption=Direction.Bullish,
    )

    asset.price_history = History((bar, ))
    asset.iv_history = History((bar, bar))
    asset.measures = m

    assert asset.id.code == "SPY"
    assert asset.id.asset_type == AssetType.Stock
    assert asset.id.currency == Currency.USDollar
    assert asset.current.high == 100.0
    assert len(asset.price_history.values) == 1
    assert len(asset.iv_history.values) == 2
    assert asset.measures.iv == 0.45
Example #4
0
def test_Current_market_price_bid_last_ask():
    current = Current(
        high=100.0,
        low=50.0,
        close=75.0,
        bid=2.0,
        bid_size=10,
        ask=3.0,
        ask_size=20,
        last=2.75,
        last_size=5,
        volume=1000,
        time=datetime.datetime.now(),
    )
    assert current.market_price == 2.75
Example #5
0
def test_Current_midprice():
    current = Current(
        high=100.0,
        low=50.0,
        close=75.0,
        bid=2.0,
        bid_size=10,
        ask=3.0,
        ask_size=20,
        last=2.5,
        last_size=5,
        volume=1000,
        time=datetime.datetime.now(),
    )
    assert current.midpoint == 2.5
Example #6
0
 def update_assets(self, assets: Dict[str, Asset]) -> Dict[str, Current]:
     contracts = [a.id.contract for a in assets.values()]
     tickers = self._broker.reqTickers(*contracts)
     current_values = {}
     for t in tickers:
         c = Current(
             high=t.high,
             low=t.low,
             close=t.close,
             bid=t.bid,
             bid_size=t.bidSize,
             ask=t.ask,
             ask_size=t.askSize,
             last=t.last,
             last_size=t.lastSize,
             volume=t.volume,
             time=t.time,
         )
         current_values[t.contract.symbol] = c
     return current_values