Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
before_trading = self._user_before_trading
handle_bar = self._user_handle_bar
exchange_on_dt_change = simu_exchange.on_dt_change
exchange_on_bar_close = simu_exchange.on_bar_close
exchange_on_day_open = simu_exchange.on_day_open
exchange_on_day_close = simu_exchange.on_day_close
exchange_update_portfolio = simu_exchange.update_portfolio
is_show_progress_bar = self.trading_params.show_progress
def on_dt_change(dt):
self._current_dt = dt
exchange_on_dt_change(dt)
with ExecutionContext(self, EXECUTION_PHASE.INIT):
init(strategy_context)
try:
for dt, event in self._event_source:
on_dt_change(dt)
bar_dict = BarMap(dt, self.current_universe, data_proxy)
if event == EVENT_TYPE.DAY_START:
with ExecutionContext(self, EXECUTION_PHASE.BEFORE_TRADING, bar_dict):
exchange_on_day_open()
before_trading(strategy_context, None)
elif event == EVENT_TYPE.HANDLE_BAR:
with ExecutionContext(self, EXECUTION_PHASE.HANDLE_BAR, bar_dict):
exchange_update_portfolio(bar_dict)
@ExecutionContext.enforce_phase(EXECUTION_PHASE.BEFORE_TRADING,
EXECUTION_PHASE.HANDLE_BAR,
EXECUTION_PHASE.SCHEDULED)
def get_order(order_id):
"""
Get a specified order by the unique order_id. The order object will be
discarded at end of handle_bar.
:param int order_id: order’s unique identifier returned by function
like `order_shares`
:return: an `Order` object.
"""
return get_simu_exchange().get_order(order_id)
def before_trading(self, event):
self._force_run_before_trading = False
with ExecutionContext(EXECUTION_PHASE.BEFORE_TRADING):
with ModifyExceptionFromType(EXC_TYPE.USER_EXC):
self._before_trading(self._user_context)
@ExecutionContext.enforce_phase(EXECUTION_PHASE.INIT)
def benchmark(self, value):
assert isinstance(value, six.string_types)
ExecutionContext.get_trading_params().benchmark = value
@ExecutionContext.enforce_phase(EXECUTION_PHASE.HANDLE_BAR,
EXECUTION_PHASE.SCHEDULED)
def order_shares(id_or_ins, amount, style=None):
"""
Place an order by specified number of shares. Order type is also
passed in as parameters if needed. If style is omitted, it fires a
market order by default.
:PARAM id_or_ins: the instrument to be ordered
:type id_or_ins: str or Instrument
:param float amount: Number of shares to order. Positive means buy,
negative means sell. It will be rounded down to the closest
integral multiple of the lot size
:param style: Order type and default is `MarketOrder()`. The
available order types are: `MarketOrder()` and
`LimitOrder(limit_price)`
:return: A unique order id.
:rtype: int
def wrapped_handler(event):
with ExecutionContext(EXECUTION_PHASE.GLOBAL):
with ModifyExceptionFromType(EXC_TYPE.USER_EXC):
return handler(self._user_context, event)
return wrapped_handler
def next_bar_(self, event):
bars = event.bar_dict
with ExecutionContext(EXECUTION_PHASE.SCHEDULED):
self._current_minute = self._minutes_since_midnight(self._ucontext.now.hour, self._ucontext.now.minute)
for day_rule, time_rule, func in self._registry:
if day_rule() and time_rule():
with ModifyExceptionFromType(EXC_TYPE.USER_EXC):
func(self._ucontext, bars)
self._last_minute = self._current_minute
@ExecutionContext.enforce_phase(EXECUTION_PHASE.ON_INIT,
EXECUTION_PHASE.BEFORE_TRADING,
EXECUTION_PHASE.ON_BAR,
EXECUTION_PHASE.AFTER_TRADING,
EXECUTION_PHASE.SCHEDULED)
def get_csv_as_df():
data = pd.read_csv(self._csv_path)
return data
@export_as_api
@ExecutionContext.enforce_phase(EXECUTION_PHASE.ON_INIT,
EXECUTION_PHASE.BEFORE_TRADING,
EXECUTION_PHASE.ON_BAR,
EXECUTION_PHASE.ON_TICK,
EXECUTION_PHASE.AFTER_TRADING,
EXECUTION_PHASE.SCHEDULED)
@apply_rules(verify_that('underlying_symbol').is_instance_of(str))
def get_future_contracts(underlying_symbol):
"""
获取某一期货品种在策略当前日期的可交易合约order_book_id列表。按照到期月份,下标从小到大排列,返回列表中第一个合约对应的就是该品种的近月合约。
:param str underlying_symbol: 期货合约品种,例如沪深300股指期货为'IF'
:return: list[`str`]
:example:
获取某一天的主力合约代码(策略当前日期是20161201):
.. code-block:: python
[In]
@ExecutionContext.enforce_phase(EXECUTION_PHASE.ON_INIT)
def run_weekly(func, weekday=None, tradingday=None, time_rule=None):
_scheduler.run_weekly(func, weekday=weekday, tradingday=tradingday, time_rule=time_rule)