def main(): tasty_client = tasty_session.create_new_session('your_username', 'your_password_here') streamer = DataStreamer(tasty_client) LOGGER.info('Streamer token: %s' % streamer.get_streamer_token()) loop = asyncio.get_event_loop() try: loop.run_until_complete(main_loop(tasty_client, streamer)) finally: # find all futures/tasks still running and wait for them to finish pending_tasks = [ task for task in asyncio.Task.all_tasks() if not task.done() ] loop.run_until_complete(asyncio.gather(*pending_tasks)) loop.close()
def main(): from tastyworks.__auth import USERNAME, PASSWORD tasty_client = tasty_session.create_new_session(USERNAME, PASSWORD) streamer = DataStreamer(tasty_client) LOGGER.info('Streamer token: %s' % streamer.get_streamer_token()) loop = asyncio.get_event_loop() try: loop.run_until_complete(main_loop(tasty_client, streamer)) except Exception: LOGGER.exception('Exception in main loop') finally: # find all futures/tasks still running and wait for them to finish pending_tasks = [ task for task in asyncio.Task.all_tasks() if not task.done() ] loop.run_until_complete(asyncio.gather(*pending_tasks)) loop.close()
async def main_loop(session: TastyAPISession, streamer: DataStreamer): # sub_values = { # "Greeks": [ # ".VIX180718C21", # ".YUM180518C95" # ] # } sub_values = { "Quote": ["/ES"] } accounts = await TradingAccount.get_remote_accounts(session) acct = accounts[0] LOGGER.info('Accounts available: %s', accounts) orders = await Order.get_remote_orders(session, acct) LOGGER.info('Number of active orders: %s', len(orders)) # Execute an order details = OrderDetails( type=OrderType.LIMIT, price=400, price_effect=OrderPriceEffect.CREDIT) new_order = Order(details) opt = Option( ticker='AKS', quantity=1, expiry=date(2018, 10, 19), strike=3.0, option_type=OptionType.CALL, underlying_type=UnderlyingType.EQUITY ) new_order.add_leg(opt) res = await acct.execute_order(new_order, session, dry_run=True) LOGGER.info('Order executed successfully: %s', res) # Get an options chain undl = underlying.Underlying('AKS') chain = await option_chain.get_option_chain(session, undl) LOGGER.info('Chain strikes: %s', chain.get_all_strikes()) await streamer.add_data_sub(sub_values) async for item in streamer.listen(): LOGGER.info('Received item: %s' % item.data)
else: next_year = this_year + 1 return date(next_year, month, day) def get_profit_percent(position: Position): entry_fee = Decimal('1.00') * Decimal(position.quantity) entry = position.average_open_price * position.multiplier if entry_fee > 10: entry = entry + Decimal('10') else: entry = entry + entry_fee currentpl = (position.mark_price - position.average_open_price) * position.multiplier return Decimal((currentpl / entry) * 100) tasty_client = tasty_session.create_new_session(Settings.tasty_user, Settings.tasty_password) streamer = DataStreamer(tasty_client) LOGGER.info('TW Streamer token: %s' % streamer.get_streamer_token()) tw_accounts = asyncio.new_event_loop().run_until_complete(TradingAccount.get_remote_accounts(tasty_client)) tasty_acct = None for account in tw_accounts: if account.account_number == Settings.tasty_account_number and account.is_margin == False: tasty_acct = account LOGGER.info('TW Account found: %s', tasty_acct) if not tasty_acct: raise Exception('Could not find a TastyWorks cash account with account number {} in the list of accounts: {}'.format(Settings.tasty_account_number, tw_accounts)) loop = asyncio.get_event_loop() try: task1 = loop.create_task(client.start(Settings.Discord_Token, bot=False)) task2 = loop.create_task(WatchAlertsAndExitIfTriggered())