Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def order(id_or_ins, amount, side, position_effect, style):
if not isinstance(style, OrderStyle):
raise RuntimeError
if amount < 0:
raise RuntimeError
if amount == 0:
user_system_log.warn(_(u"Order Creation Failed: Order amount is 0."))
return None
if isinstance(style, LimitOrder) and style.get_limit_price() <= 0:
raise RQInvalidArgument(_(u"Limit order price should be positive"))
order_book_id = assure_future_order_book_id(id_or_ins)
env = Environment.get_instance()
if env.config.base.run_type != RUN_TYPE.BACKTEST:
if "88" in order_book_id:
raise RQInvalidArgument(_(u"Main Future contracts[88] are not supported in paper trading."))
if "99" in order_book_id:
raise RQInvalidArgument(_(u"Index Future contracts[99] are not supported in paper trading."))
price = env.get_last_price(order_book_id)
if not is_valid_price(price):
user_system_log.warn(
_(u"Order Creation Failed: [{order_book_id}] No market data").format(order_book_id=order_book_id)
)
return
amount = int(amount)
env = Environment.get_instance()
orders = []
if position_effect == POSITION_EFFECT.CLOSE:
def check_greater_or_equal_than(func_name, value):
if value < low:
raise RQInvalidArgument(
_(u"function {}: invalid {} argument, expect a value >= {}, got {} (type: {})").format(
func_name, self._arg_name, low, value, type(value)
))
def assure_order_book_id(id_or_symbols):
if isinstance(id_or_symbols, Instrument):
return id_or_symbols.order_book_id
elif isinstance(id_or_symbols, six.string_types):
return assure_order_book_id(instruments(id_or_symbols))
else:
raise RQInvalidArgument(_(u"unsupported order_book_id type"))
def _is_number(self, func_name, value):
try:
v = float(value)
except (ValueError, TypeError):
raise RQInvalidArgument(
_(u"function {}: invalid {} argument, expect a number, got {} (type: {})").format(
func_name, self._arg_name, value, type(value))
)
def check_greater_than(func_name, value):
if value <= low:
raise RQInvalidArgument(
_(u"function {}: invalid {} argument, expect a value > {}, got {} (type: {})").format(
func_name, self._arg_name, low, value, type(value)
))
EXECUTION_PHASE.GLOBAL)
@apply_rules(verify_that('id_or_ins').is_valid_future(),
verify_that('amount').is_number().is_greater_or_equal_than(0),
verify_that('side').is_in([SIDE.BUY, SIDE.SELL]),
verify_that('position_effect').is_in([POSITION_EFFECT.OPEN, POSITION_EFFECT.CLOSE]),
verify_that('style').is_instance_of((LimitOrder, MarketOrder, type(None))))
def order(id_or_ins, amount, side, position_effect, style):
if not isinstance(style, OrderStyle):
raise RuntimeError
if amount < 0:
raise RuntimeError
if amount == 0:
user_system_log.warn(_(u"Order Creation Failed: Order amount is 0."))
return None
if isinstance(style, LimitOrder) and style.get_limit_price() <= 0:
raise RQInvalidArgument(_(u"Limit order price should be positive"))
order_book_id = assure_future_order_book_id(id_or_ins)
env = Environment.get_instance()
if env.config.base.run_type != RUN_TYPE.BACKTEST:
if "88" in order_book_id:
raise RQInvalidArgument(_(u"Main Future contracts[88] are not supported in paper trading."))
if "99" in order_book_id:
raise RQInvalidArgument(_(u"Index Future contracts[99] are not supported in paper trading."))
price = env.get_last_price(order_book_id)
if not is_valid_price(price):
user_system_log.warn(
_(u"Order Creation Failed: [{order_book_id}] No market data").format(order_book_id=order_book_id)
)
return
def assure_future_order_book_id(id_or_symbols):
if isinstance(id_or_symbols, Instrument):
if id_or_symbols.type != "Future":
raise RQInvalidArgument(
_(u"{order_book_id} is not supported in current strategy type").format(
order_book_id=id_or_symbols.order_book_id))
else:
return id_or_symbols.order_book_id
elif isinstance(id_or_symbols, six.string_types):
return assure_future_order_book_id(Environment.get_instance().data_proxy.instruments(id_or_symbols))
else:
raise RQInvalidArgument(_(u"unsupported order_book_id type"))
try:
return func(*args, **kwargs)
except RQInvalidArgument:
raise
except Exception as e:
exc_info = sys.exc_info()
t, v, tb = exc_info
if call_args is None:
call_args = get_call_args(func, args, kwargs, tb)
try:
for r in rules:
if r.pre_check:
continue
r.verify(func.__name__, call_args)
except RQInvalidArgument as e:
six.reraise(RQInvalidArgument, e, tb)
return
if getattr(e, EXC_EXT_NAME, EXC_TYPE.NOTSET) == EXC_TYPE.NOTSET:
patch_system_exc(e)
raise
def raise_not_valid_future_error(self, func_name, arg_name, value):
raise RQInvalidArgument(
_(u"function {}: invalid {} argument, expect a valid future instrument/order_book_id/symbol, "
u"got {} (type: {})").format(
func_name, self._arg_name, value, type(value)
))
def raise_invalid_instrument_error(self, func_name, arg_name, value):
raise RQInvalidArgument(
_(u"function {}: invalid {} argument, expect a valid instrument/order_book_id/symbol, "
u"got {} (type: {})").format(
func_name, self._arg_name, value, type(value)
))