Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _future_validator(self, account, order):
if order.position_effect != POSITION_EFFECT.OPEN:
return True
instrument = self._env.get_instrument(order.order_book_id)
margin = order.frozen_price * order.quantity * instrument.contract_multiplier * instrument.margin_rate
cost_money = margin * self._env.config.base.margin_multiplier
cost_money += self._env.get_order_transaction_cost(DEFAULT_ACCOUNT_TYPE.FUTURE, order)
if cost_money <= account.cash:
return True
user_system_log.warn(
_("Order Creation Failed: not enough money to buy {order_book_id}, needs {cost_money:.2f},"
" cash {cash:.2f}").format(
order_book_id=order.order_book_id,
cost_money=cost_money,
cash=account.cash,
)
)
return False
raise RuntimeError("data source type [%s] is not supported" % mod_config.source)
if mod_config.enable_cache:
if mod_config.cache_length:
CacheMixin.set_cache_length(int(mod_config.cache_length))
if mod_config.max_cache_space:
CacheMixin.set_max_cache_space(int(mod_config.max_cache_space))
data_source = data_source_cls(*args)
mod_config.redis_uri = mod_config.redis_url # fit rqalpha
if env.config.base.run_type is RUN_TYPE.BACKTEST and env.config.base.persist_mode == PERSIST_MODE.ON_NORMAL_EXIT:
# generate user context using backtest
persist_provider = DiskPersistProvider(mod_config.persist_path)
env.set_persist_provider(persist_provider)
is_real_time = env.config.base.run_type in (RUN_TYPE.PAPER_TRADING, RUN_TYPE.LIVE_TRADING)
if is_real_time or type_ == DataSourceType.REAL_TIME:
user_system_log.warn(_("[Warning] When you use this version of RealtimeTradeMod, history_bars can only "
"get data from yesterday."))
if type_ == DataSourceType.QUANTOS:
inday_bars = QuantOsIndayBars(mod_config.quantos_url,
mod_config.quantos_user,
mod_config.quantos_token)
elif mod_config.redis_url:
inday_bars = RedisIndayBars(mod_config.redis_url)
system_log.info(_("RealtimeTradeMod using market from redis"))
else:
raise RuntimeError("No Inday bar data source with valid config")
data_source = RealtimeDataSource(inday_bars=inday_bars, hist_source=data_source)
if is_real_time:
event_source = RealTimeEventSource(mod_config.fps, mod_config)
# add persist
persist_provider = DiskPersistProvider(mod_config.persist_path)
env.set_persist_provider(persist_provider)
@staticmethod
def _stock_validator(account, order):
if order.side != SIDE.SELL:
return True
position = account.positions[order.order_book_id]
if order.quantity <= position.sellable:
return True
user_system_log.warn(_(
"Order Creation Failed: not enough stock {order_book_id} to sell, you want to sell {quantity},"
" sellable {sellable}").format(
order_book_id=order.order_book_id,
quantity=order.quantity,
sellable=position.sellable,
))
return False
order_book_id,
amount,
side,
style,
POSITION_EFFECT.CLOSE
))
else:
if env.portfolio:
position = env.portfolio.positions[order_book_id]
buy_quantity, buy_old_quantity = position.buy_quantity, position.buy_old_quantity
else:
position = env.booking.get_position(order_book_id, POSITION_DIRECTION.LONG)
buy_quantity, buy_old_quantity = position.quantity, position.old_quantity
if amount > buy_quantity:
user_system_log.warn(
_(u"Order Creation Failed: close amount {amount} is larger than position "
u"quantity {quantity}").format(amount=amount, quantity=buy_quantity)
)
return []
buy_old_quantity = buy_old_quantity
if amount > buy_old_quantity:
if buy_old_quantity != 0:
orders.append(Order.__from_create__(
order_book_id,
buy_old_quantity,
side,
style,
POSITION_EFFECT.CLOSE
))
orders.append(Order.__from_create__(
order_book_id,
def set_state(self, state):
dict_data = pickle.loads(state)
for key, value in six.iteritems(dict_data):
try:
self.__dict__[key] = pickle.loads(value)
system_log.debug("restore g.{} {}", key, type(self.__dict__[key]))
except Exception:
user_detail_log.exception("g.{} can not restore", key)
user_system_log.warn("g.{} can not restore", key)
def mark_rejected(self, reject_reason):
if not self.is_final():
self._message = reject_reason
self._status = ORDER_STATUS.REJECTED
user_system_log.warn(reject_reason)
def total_orders(self):
"""abandon"""
user_system_log.warn(_(u"[abandon] {} is no longer valid.").format('position.total_orders'))
return 0
def can_submit_order(self, order, account=None):
instrument = self._env.data_proxy.instruments(order.order_book_id)
if instrument.listed_date > self._env.trading_dt:
user_system_log.warn(_(u"Order Creation Failed: {order_book_id} is not listed!").format(
order_book_id=order.order_book_id,
))
return False
if instrument.de_listed_date.date() < self._env.trading_dt.date():
user_system_log.warn(_(u"Order Creation Failed: {order_book_id} has been delisted!").format(
order_book_id=order.order_book_id,
))
return False
if instrument.type == 'CS' and self._env.data_proxy.is_suspended(order.order_book_id, self._env.trading_dt):
user_system_log.warn(_(u"Order Creation Failed: security {order_book_id} is suspended on {date}").format(
order_book_id=order.order_book_id,
date=self._env.trading_dt
))
return False
if instrument.type == 'PublicFund':
if order.side == SIDE.BUY and self._env.data_proxy.non_subscribable(order.order_book_id,
self._env.trading_dt):
user_system_log.warn(_(u"Order Creation Failed: security {order_book_id} cannot be subscribed on {date}").format(
order_book_id=order.order_book_id,
date=self._env.trading_dt
))
return False
elif order.side == SIDE.SELL and self._env.data_proxy.non_redeemable(order.order_book_id,
self._env.trading_dt):
user_system_log.warn(_(u"Order Creation Failed: security {order_book_id} cannot be redeemed on {date}").format(
))
return False
if order.side == SIDE.SELL and order.position_effect == POSITION_EFFECT.CLOSE_TODAY \
and order.quantity > position.closable_today_buy_quantity:
user_system_log.warn(_(
"Order Creation Failed: not enough today position {order_book_id} to sell close, target"
" quantity is {quantity}, closable today quantity {closable}").format(
order_book_id=order.order_book_id,
quantity=order.quantity,
closable=position.closable_today_buy_quantity,
))
return False
if order.side == SIDE.BUY and order.quantity > position.closable_sell_quantity:
user_system_log.warn(_(
"Order Creation Failed: not enough securities {order_book_id} to buy close, target"
" sell quantity is {quantity}, sell_closable_quantity {closable}").format(
order_book_id=order.order_book_id,
quantity=order.quantity,
closable=position.closable_sell_quantity,
))
return False
elif order.side == SIDE.SELL and order.quantity > position.closable_buy_quantity:
user_system_log.warn(_(
"Order Creation Failed: not enough securities {order_book_id} to sell close, target"
" sell quantity is {quantity}, buy_closable_quantity {closable}").format(
order_book_id=order.order_book_id,
quantity=order.quantity,
closable=position.closable_buy_quantity,
))