Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _frozen_cash_of_order(order):
order_cost = margin_of(
order.order_book_id, order.quantity, order.frozen_price
) if order.position_effect == POSITION_EFFECT.OPEN else 0
return order_cost + Environment.get_instance().get_order_transaction_cost(
DEFAULT_ACCOUNT_TYPE.FUTURE, order
)
def register_event(self):
event_bus = Environment.get_instance().event_bus
event_bus.add_listener(EVENT.TRADE, self._on_trade)
event_bus.add_listener(EVENT.ORDER_PENDING_NEW, self._on_order_pending_new)
event_bus.add_listener(EVENT.ORDER_CREATION_REJECT, self._on_order_unsolicited_update)
event_bus.add_listener(EVENT.ORDER_UNSOLICITED_UPDATE, self._on_order_unsolicited_update)
event_bus.add_listener(EVENT.ORDER_CANCELLATION_PASS, self._on_order_unsolicited_update)
event_bus.add_listener(EVENT.PRE_BEFORE_TRADING, self._on_before_trading)
event_bus.add_listener(EVENT.SETTLEMENT, self._on_settlement)
event_bus.add_listener(EVENT.BAR, self._update_last_price)
event_bus.add_listener(EVENT.TICK, self._update_last_price)
def run(config, source_code=None, user_funcs=None):
env = Environment(config)
persist_helper = None
init_succeed = False
mod_handler = ModHandler()
try:
# avoid register handlers everytime
# when running in ipython
set_loggers(config)
basic_system_log.debug("\n" + pformat(config.convert_to_dict()))
env.set_strategy_loader(init_strategy_loader(env, source_code, user_funcs, config))
env.set_global_vars(GlobalVars())
mod_handler.set_env(env)
mod_handler.start_up()
if not env.data_source:
def is_de_listed(self):
"""
判断合约是否过期
"""
instrument = Environment.get_instance().get_instrument(self.order_book_id)
current_date = Environment.get_instance().trading_dt
if instrument.de_listed_date is not None and current_date >= instrument.de_listed_date:
return True
return False
def margin_rate(self):
margin_rate = ExecutionContext.get_future_margin_rate(self.order_book_id)
margin_multiplier = Environment.get_instance().config.base.margin_multiplier
return margin_rate * margin_multiplier
def __init__(self, path, redis_uri):
super(RedisDataSource, self).__init__(path)
self._env = Environment.get_instance()
import redis
self._redis_client = redis.from_url(redis_uri)
def next_day_(self, event):
if len(self._registry) == 0:
return
self._today = Environment.get_instance().trading_dt.date()
self._last_minute = 0
self._current_minute = 0
if not self._this_week or self._today > self._this_week[-1]:
self._fill_week()
if not self._this_month or self._today > self._this_month[-1]:
self._fill_month()
def _on_settlement(self, event):
env = Environment.get_instance()
for position in list(self._positions.values()):
order_book_id = position.order_book_id
if position.is_de_listed() and position.quantity != 0:
try:
transform_data = env.data_proxy.get_share_transformation(order_book_id)
except NotImplementedError:
pass
else:
if transform_data is not None:
self._pending_transform[order_book_id] = transform_data
continue
if env.config.mod.sys_accounts.cash_return_by_stock_delisted:
self._total_cash += position.market_value
user_system_log.warn(
_(u"{order_book_id} is expired, close all positions by system").format(
order_book_id=order_book_id)
def __init__(self, order_book_id):
self._order_book_id = order_book_id
self._env = Environment.get_instance()
self._daily_returns = 0
self._total_returns = 0
self._env.event_bus.prepend_listener(EVENT.BAR, self._on_bar)
def __init__(self, total_cash, positions, backward_trade_set, dividend_receivable=None):
super(BenchmarkAccount, self).__init__(total_cash, positions, backward_trade_set, dividend_receivable)
self.benchmark = Environment.get_instance().config.base.benchmark