Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.session = get_db_session('zvt', '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)
:type timestamp:
"""
the_id = '{}_{}'.format(self.trader_name, to_time_str(timestamp, TIME_FORMAT_ISO8601))
positions = []
for position in self.latest_account['positions']:
position_domain = Position()
fill_domain_from_dict(position_domain, position, None)
position_domain.id = '{}_{}_{}'.format(self.trader_name, position['entity_id'],
to_time_str(timestamp, TIME_FORMAT_ISO8601))
position_domain.timestamp = to_pd_timestamp(timestamp)
position_domain.sim_account_id = the_id
positions.append(position_domain)
account_domain = SimAccount(id=the_id, trader_name=self.trader_name, cash=self.latest_account['cash'],
positions=positions,
all_value=self.latest_account['all_value'], value=self.latest_account['value'],
timestamp=to_pd_timestamp(self.latest_account['timestamp']))
self.logger.info('persist_account:{}'.format(sim_account_schema.dump(account_domain)))
self.session.add(account_domain)
self.session.commit()
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', '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)
def get_account(trader_name=None, return_type='df', start_timestamp=None, end_timestamp=None,
filters=None, session=None, order=None, limit=None):
if trader_name:
if filters:
filters = filters + [SimAccount.trader_name == trader_name]
else:
filters = [SimAccount.trader_name == trader_name]
return get_data(data_schema=SimAccount, entity_id=None, codes=None, level=None, provider='zvt',
columns=None, return_type=return_type, start_timestamp=start_timestamp,
end_timestamp=end_timestamp, filters=filters, session=session, order=order, limit=limit)
def on_trading_open(self, timestamp):
self.logger.info('on_trading_open:{}'.format(timestamp))
if is_same_date(timestamp, self.start_timestamp):
return
# get the account for trading at the date
accounts = get_account(session=self.session, trader_name=self.trader_name, return_type='domain',
end_timestamp=to_time_str(timestamp), limit=1, order=SimAccount.timestamp.desc())
if accounts:
account = accounts[0]
else:
return
positions = []
# FIXME:dump all directly
for position_domain in account.positions:
position_dict = position_schema.dump(position_domain)
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)
self.latest_account['positions'] = positions
self.logger.info('on_trading_open:{},latest_account:{}'.format(timestamp, self.latest_account))
def get_traders() -> List[str]:
df = get_group(provider='zvt', data_schema=SimAccount, column=SimAccount.trader_name, group_func=None)
if pd_is_not_null(df):
return df['trader_name'].tolist()
return []