Exemplo n.º 1
0
def test_Option_midpoint():
    id = AssetId("SPY", AssetType.Stock, Currency.USDollar, None)
    opt_id = OptionId(
        underlying_id=id,
        asset_type=AssetType.Option,
        expiration=datetime.date(2018, 9, 21),
        strike=100,
        right=RightType.Call,
        multiplier=100,
        contract=None,
    )
    time = datetime.datetime.now()
    opt = Option(id=opt_id,
                 high=10.0,
                 low=5.0,
                 close=8.0,
                 bid=6.0,
                 bid_size=100,
                 ask=7.0,
                 ask_size=130,
                 last=7.5,
                 last_size=67.0,
                 option_price=2.1,
                 volume=1000,
                 delta=0.98,
                 gamma=0.12,
                 theta=0.34,
                 vega=0.78,
                 iv=0.8,
                 underlying_price=102.0,
                 underlying_dividends=2.1,
                 time=time)

    assert opt.midpoint == 6.5
Exemplo n.º 2
0
    def create_assets(self, watchlist: Tuple[AssetDefinition]) -> List[Asset]:
        watchlist_dict = {i.code: i for i in watchlist}
        contracts = []
        for item in watchlist:
            if item.asset_type == AssetType.Stock or item.asset_type == AssetType.ETF:
                contracts.append(
                    IBStock(item.code,
                            exchange="SMART",
                            currency=item.currency.value))
            if item.asset_type == AssetType.Index:
                contracts.append(IBIndex(item.code, exchange=item.exchange))
        # TODO: Remove the limit
        # It works if len(contracts) < 50. IB limit.
        q_contracts = self._broker.qualifyContracts(*contracts)
        if len(q_contracts) == len(watchlist):
            assets = {}
            for qc in q_contracts:
                id = AssetId(
                    code=qc.symbol,
                    asset_type=watchlist_dict[qc.symbol].asset_type,
                    currency=self._translator._currency_translation[
                        qc.currency],
                    contract=qc,
                )
                if id.asset_type == AssetType.Stock:
                    assets[id.code] = Stock(id)
                elif id.asset_type == AssetType.ETF:
                    assets[id.code] = ETF(id)
                elif id.asset_type == AssetType.Index:
                    assets[id.code] = Index(id)

        else:
            raise ValueError("Error: ambiguous contracts")

        return assets
Exemplo n.º 3
0
def test_Stock_wrong_asset_type():
    id = AssetId("SPY", AssetType.Option, Currency.USDollar, None)
    with pytest.raises(ValueError):
        Stock(id)


# TODO: Index and ETF tests
Exemplo n.º 4
0
def test_OptionId_immutable():
    id = AssetId("SPY", AssetType.Stock, Currency.USDollar, None)
    opt_id = OptionId(underlying_id=id,
                      asset_type=AssetType.Option,
                      expiration=datetime.date(2018, 9, 21),
                      strike=100,
                      right=RightType.Call,
                      multiplier=100,
                      contract=None)
    with pytest.raises(FrozenInstanceError):
        opt_id.underlying_id = id
Exemplo n.º 5
0
def test_Option_init():
    id = AssetId("SPY", AssetType.Stock, Currency.USDollar, None)
    opt_id = OptionId(
        underlying_id=id,
        asset_type=AssetType.Option,
        expiration=datetime.date(2018, 9, 21),
        strike=100,
        right=RightType.Call,
        multiplier=100,
        contract=None,
    )
    time = datetime.datetime.now()
    opt = Option(id=opt_id,
                 high=10.0,
                 low=5.0,
                 close=8.0,
                 bid=6.0,
                 bid_size=100,
                 ask=7.0,
                 ask_size=130,
                 last=7.5,
                 last_size=67.0,
                 option_price=2.1,
                 volume=1000,
                 delta=0.98,
                 gamma=0.12,
                 theta=0.34,
                 vega=0.78,
                 iv=0.8,
                 underlying_price=102.0,
                 underlying_dividends=2.1,
                 time=time)

    assert opt.id.underlying_id.code == 'SPY'
    assert opt.high == 10.0
    assert opt.low == 5.0
    assert opt.close == 8.0
    assert opt.bid == 6.0
    assert opt.bid_size == 100
    assert opt.ask == 7.0
    assert opt.ask_size == 130
    assert opt.last == 7.5
    assert opt.last_size == 67.0
    assert opt.option_price == 2.1
    assert opt.volume == 1000
    assert opt.delta == 0.98
    assert opt.gamma == 0.12
    assert opt.theta == 0.34
    assert opt.vega == 0.78
    assert opt.iv == 0.8
    assert opt.underlying_price == 102.0
    assert opt.underlying_dividends == 2.1
    assert opt.time == time
Exemplo n.º 6
0
def test_OptionId_init():
    id = AssetId("SPY", AssetType.Stock, Currency.USDollar, None)
    opt_id = OptionId(underlying_id=id,
                      asset_type=AssetType.Option,
                      expiration=datetime.date(2018, 9, 21),
                      strike=100,
                      right=RightType.Call,
                      multiplier=100,
                      contract=None)
    assert opt_id.underlying_id.code == "SPY"
    assert opt_id.asset_type == AssetType.Option
    assert opt_id.expiration == datetime.date(2018, 9, 21)
    assert opt_id.strike == 100
    assert opt_id.right == RightType.Call
Exemplo n.º 7
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
Exemplo n.º 8
0
def test_Stock_entity_init():
    id = AssetId("SPY", AssetType.Stock, Currency.USDollar, None)
    stock = Stock(id)
    assert stock.id.code == "SPY"
    assert stock.id.asset_type == AssetType.Stock
    assert stock.id.currency == Currency.USDollar
Exemplo n.º 9
0
def test_Asset_init():
    id = AssetId("SPY", AssetType.Stock, Currency.USDollar, None)
    assert id.code == "SPY"
    assert id.asset_type == AssetType.Stock
    assert id.currency == Currency.USDollar
Exemplo n.º 10
0
def test_Asset_not_change_asset_id():
    id = AssetId("SPY", AssetType.Stock, Currency.USDollar, None)
    asset = Asset(id)
    with pytest.raises(AttributeError):
        asset.id = id
Exemplo n.º 11
0
def test_Asset_init_immutable():
    id = AssetId("SPY", AssetType.Stock, Currency.USDollar, None)
    with pytest.raises(FrozenInstanceError):
        id.code = "XXX"