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
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)
def get_option_positions(self, **kwargs): """A method for getting current option positions Parameters ---------- symbol : str, optional the symbol to get positions for type : str, optional the type of position (options are 'short','long') ids : bool, optional if True, will return the option ids for the requested position (default is False) Returns ------- pd.DataFrame a data frame of the option positions that meet the parameters """ return ClientMethods.fetch_option_positions(self.client)
def order_stock(self, symbol, quantity, side, price=None): """ A method to place for a stock order. Parameters ---------- symbol : str the stock symbol to get the place the order on quantity : int, str the amount of shares to place the order for side : str the side of the order to take ('buy','sell') price : float, optional the price to place the order at, if None, will place at the bid price """ return ClientMethods.submit_stock_order(self.client, symbol, quantity, side, price=price)
def get_option_orders(self, **kwargs): """A method to get the option orders based on their state Parameters ---------- state : str, optional the state of the option order, default is ('confirmed'), others are 'unconfirmed','filled','cancelled', None. If None, will ignore the state opening_strategy : str, optional the strategy of the order to get, if None, will ignore the opening_strategy, (default is None), others are 'iron_condor' chain_symbol : str, optional the symbol to get orders for, works along with other params, (default is None), if None, will ignore ids : bool, optional if True, will return the option ids for the resulting orders (default is False) Returns ------- list a list of dictionaries, where each dictionary is an order """ return list(ClientMethods.fetch_all_option_orders(self.client))
def get_stock_orders(self, **kwargs): """A method to get the stock orders based on their params Parameters ---------- state : str, optional the state of the stock order, default is ('confirmed'), others are 'unconfirmed','filled','cancelled', None. If None, will ignore the state type : str, optional the type order to get, if None, will ignore the type, (default is None), options are: 'limit','market' symbol : str, optional the symbol to get orders for, works along with other params, (default is None), if None, will ignore ids : bool, optional if True, will return the stock order ids for the resulting orders (default is False) Returns ------- list a list of dictionaries, where each dictionary is an order """ return ClientMethods.fetch_all_stock_orders(self.client)
def replace_option(self, *args, **kwargs): """ A method to replace an order for an option. Parameters ---------- symbol : str the stock symbol to get the option for expiration or id: str, int; str the expiration date of the option; the id of the option strike or side: str, int; str the strike price for the option; the side of the option to take ('buy','sell') type or position_effect: str; str the type of option the action to take on the option ('open','close') side : str the side of the option to take ('buy','sell') position_effect : str the action to take on the option ('open','close') """ return ClientMethods.replace_option_order(self.client, self.cancel_url, self.direction, self.legs, self.price, self.quantity)
def get_stock_equity(self): return sum( ClientMethods.fetch_stock_positions( self.client)['equity'].to_list())
def get_portfolio(self): return ClientMethods.fetch_portfolio(self.client)
def get_account(self): return ClientMethods.fetch_account(self.client)
def get_stock_positions(self): return ClientMethods.fetch_stock_positions(self.client)
def process_q(self): while not self.q.empty(): self.dfs.append( ClientMethods.fetch_by_expiration(self.client, self.id, [self.q.get()])) self.q.task_done()