def draw_order_signals(trader_name, render='html'): df_account = get_account(trader_name=trader_name) start_timestamp = df_account['timestamp'][0] end_timestamp = df_account['timestamp'][-1] df_orders = get_orders(trader_name=trader_name) grouped = df_orders.groupby('security_id') page = Page() for security_id, order_df in grouped: kdata = get_kdata(security_id=security_id, provider='netease', start_timestamp=start_timestamp, end_timestamp=end_timestamp) mark_points = order_df kline = draw_kline(df_list=[kdata], markpoints_list=[mark_points], render=None) page.add(kline) if render == 'html': file_name = '{}_signals'.format(trader_name) page.render(get_ui_path(file_name)) elif render == 'notebook': page.render_notebook() return page
def __init__(self, trader_name, timestamp, base_capital=1000000, buy_cost=0.001, sell_cost=0.001, slippage=0.001): self.base_capital = base_capital self.buy_cost = buy_cost self.sell_cost = sell_cost self.slippage = slippage self.trader_name = trader_name self.session = get_db_session('zvt', StoreCategory.business) self.provider = 'netease' account = get_account(session=self.session, trader_name=self.trader_name, return_type='domain', limit=1) if account: self.logger.warning("trader:{} has run before,old result would be deleted".format(trader_name)) self.session.query(SimAccount).filter(SimAccount.trader_name == self.trader_name).delete() self.session.query(Position).filter(Position.trader_name == self.trader_name).delete() self.latest_account = SimAccount(trader_name=self.trader_name, cash=self.base_capital, positions=[], all_value=self.base_capital, value=0, timestamp=timestamp) self.save_account(self.latest_account)
def draw_account(trader_name): df_account = get_account(trader_name=trader_name) df_position = get_position(trader_name=trader_name) xdata = [to_time_str(timestamp) for timestamp in df_account.index] ydata = df_account.loc[:, 'all_value'].values.tolist() line = (Line().add_xaxis(xdata).add_yaxis("市值曲线", ydata).set_global_opts( title_opts=opts.TitleOpts(title="Grid-Line", pos_top="48%"), legend_opts=opts.LegendOpts(pos_top="48%"), )) time_line = Timeline() for timestamp in df_position.index: positions = zip( df_position.loc[timestamp, ['security_id']].values.tolist(), df_position.loc[timestamp, ['value']].values.tolist()) security_positions = [(x[0], y[0]) for x, y in positions] print(security_positions) pie = Pie().add("持仓", security_positions) time_line.add(pie, to_time_str(timestamp)) page = Page() page.add(line, time_line) return page
def get_account_at_time(self, timestamp): """ :param timestamp: :type timestamp: :return: :rtype:SimAccount """ return get_account(session=self.session, trader_name=self.trader_name, return_type='domain', end_timestamp=timestamp, limit=1)[0]
def draw_account(trader_name_list): df_list = [] for trader_name in trader_name_list: df_account = get_account(trader_name=trader_name) df_list.append(df_account) return draw_compare(df_list, chart_type=Line, columns=['all_value'], name='trader_name')
def save_account(self, account: SimAccount): the_id = '{}_{}'.format(self.trader_name, to_time_str(account.timestamp, TIME_FORMAT_ISO8601)) account_domain = get_account(session=self.session, trader_name=self.trader_name, filters=[SimAccount.id == the_id], return_type='domain') if account_domain: account_domain = account_domain[0] else: account_domain = SimAccount(id=the_id, trader_name=self.trader_name, cash=account.cash, positions=account.positions, all_value=account.all_value, value=account.value, timestamp=account.timestamp) self.session.add(account_domain) self.session.commit()
def draw_account_list(trader_name_list, render='html'): df_list = [] for trader_name in trader_name_list: df_account = get_account(trader_name=trader_name) df_list.append(df_account) if len(trader_name_list) == 1: file_name = trader_name_list[0] else: file_name = '_vs_'.join(trader_name_list) return draw_line(df_list, columns=['all_value'], name_field='trader_name', file_name=file_name, render=render)
def __init__(self, trader_name, timestamp, provider=Provider.NETEASE, level=TradingLevel.LEVEL_1DAY, base_capital=1000000, buy_cost=0.001, sell_cost=0.001, slippage=0.001): self.base_capital = base_capital self.buy_cost = buy_cost self.sell_cost = sell_cost self.slippage = slippage self.trader_name = trader_name self.session = get_db_session('zvt', StoreCategory.business) self.provider = provider self.level = level self.start_timestamp = timestamp account = get_account(session=self.session, trader_name=self.trader_name, return_type='domain', limit=1) if account: self.logger.warning( "trader:{} has run before,old result would be deleted".format( trader_name)) self.session.query(SimAccount).filter( SimAccount.trader_name == self.trader_name).delete() self.session.query(Position).filter( Position.trader_name == self.trader_name).delete() self.session.query(Order).filter( Order.trader_name == self.trader_name).delete() self.session.commit() account = SimAccount(trader_name=self.trader_name, cash=self.base_capital, positions=[], all_value=self.base_capital, value=0, closing=False, timestamp=timestamp) self.latest_account = sim_account_schema.dump(account).data
def on_trading_open(self, timestamp): if is_same_date(timestamp, self.start_timestamp): return # get the account for trading at the date account = get_account(session=self.session, trader_name=self.trader_name, return_type='domain', end_timestamp=timestamp, limit=1, order=SimAccount.timestamp.desc())[0] positions = [] # FIXME:dump all directly for position_domain in account.positions: position_dict = position_schema.dump(position_domain).data self.logger.info('current position:{}'.format(position_dict)) del position_dict['sim_account'] positions.append(position_dict) self.latest_account = sim_account_schema.dump(account).data self.latest_account['positions'] = positions self.logger.info('on_trading_open:{},latest_account:{}'.format(timestamp, self.latest_account))
def __init__(self, trader_name, model_name, timestamp, base_capital=1000000, buy_cost=0.001, sell_cost=0.001, slippage=0.001): self.base_capital = base_capital self.buy_cost = buy_cost self.sell_cost = sell_cost self.slippage = slippage self.trader_name = trader_name self.model_name = model_name self.session = get_db_session('trader') account = get_account(trader_name=self.trader_name, model_name=self.model_name, limit=1) if account: self.logger.warning( "trader:{} has run before,old result would be deleted".format( trader_name)) self.session.query(SimAccount).filter( SimAccount.trader_name == self.trader_name).delete() self.account = SimAccount() self.account.trader_name = trader_name self.account.model_name = model_name self.account.cash = self.base_capital self.account.positions = [] self.account.all_value = self.base_capital self.account.value = 0 self.account.timestamp = timestamp self.account_to_queue() t = threading.Thread(target=self.saving_account_worker) t.start()