Example #1
0
 def wrapper(self, *args, **kwargs):
     self.elapsed, id, cash, chain = time.time(), True if len(
         args) == 4 else False, self.get_cash(), Chain(
             args[0]) if not len(args) == 4 else None
     option = chain.get_option(
         args[1], args[2],
         args[3]) if not id else ClientMethods.fetch_by_id(
             self.client, args[1])
     self.direction = 'debit' if args[
         4 if not id else 2] == 'buy' else 'credit'
     self.legs = [{
         'option': option['url'],
         'side': args[4 if not id else 2],
         'position_effect': args[5 if not id else 3],
         'ratio_quantity': kwargs.get('ratio_quantity', 1)
     }]
     self.price = option[
         'high_fill_rate_buy_price' if args[4 if not id else 2] ==
         'buy' else 'high_fill_rate_sell_price'] if not kwargs.get(
             'price') else kwargs['price']
     self.quantity = kwargs.get(
         'quantity', 1) if not kwargs.get('max_quantity') else (
             math.floor(cash / self.price) if math.floor(
                 cash / self.price) <= kwargs['max_quantity'] else 1)
     return func(self, *args, **kwargs)
Example #2
0
 def get_theoretical_mark_of_positions(self, **kwargs):
     """ A method to get the theoretical mark price of current positions
     Returns
     -------
     pd.DataFrame
         a dataframe of each option and other data including the theoretical mark
     """
     positions = self.get_option_positions(ids=True)
     data = [
         ClientMethods.fetch_by_id(self.client, id)
         for id in positions['id']
     ]
     prices = {
         sym: ClientMethods.fetch_price(self.client, sym)
         for sym in list(pd.DataFrame(data)['chain_symbol'].unique())
     }
     for option in data:
         option['theoretical_mark'] = black_scholes(
             prices[option['chain_symbol']], float(option['strike_price']),
             dte(option['expiration_date']) / 365, .0094,
             float(option['implied_volatility']), option['type'])
     res = pd.merge(pd.DataFrame(data), positions, on='instrument')[[
         'chain_symbol', 'strike_price', 'type', 'side', 'quantity',
         'expiration_date', 'theoretical_mark', 'mark_price', 'volume',
         'open_interest'
     ]]
     res['theoretical_value'] = [
         float(r['theoretical_mark']) * float(r['quantity'])
         for i, r in res.iterrows()
     ]
     return res