本文整理匯總了Python中backtrader.num2date方法的典型用法代碼示例。如果您正苦於以下問題:Python backtrader.num2date方法的具體用法?Python backtrader.num2date怎麽用?Python backtrader.num2date使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類backtrader
的用法示例。
在下文中一共展示了backtrader.num2date方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: convert_to_pandas
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def convert_to_pandas(strat_clk, obj: bt.LineSeries, start: datetime = None, end: datetime = None, name_prefix: str = "", num_back=None) -> pd.DataFrame:
lines_clk = obj.lines.datetime.plotrange(start, end)
df = pd.DataFrame()
# iterate all lines
for lineidx in range(obj.size()):
line = obj.lines[lineidx]
linealias = obj.lines._getlinealias(lineidx)
if linealias == 'datetime':
continue
# get data limited to time range
data = line.plotrange(start, end)
ndata = convert_by_line_clock(data, lines_clk, strat_clk)
df[name_prefix + linealias] = ndata
df[name_prefix + 'datetime'] = [bt.num2date(x) for x in strat_clk]
return df
示例2: _get_datas
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def _get_datas(strategy: bt.Strategy) -> str:
md = '\n# Data Feeds\n'
for data in strategy.datas:
md += f'## {data.__class__.__name__}\n'
tabdata = {
'DataName:': str(data._dataname).replace("|", "\\|"),
'Timezone:': data._tz,
'Number of bars:': len(data),
'Bar Length:': f"{data._compression} {bt.TimeFrame.getname(data._timeframe, data._compression)}",
}
# live trading does not have valid data parameters (other datas might also not have)
if not math.isinf(data.fromdate):
tabdata['Time From:'] = bt.num2date(data.fromdate)
if not math.isinf(data.todate):
tabdata['Time To:'] = bt.num2date(data.todate)
md += _get_table(['Property', 'Value'], tabdata)
return md
示例3: _st_start
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def _st_start(self):
if self.p.historical:
self.put_notification(self.DELAYED)
dtend = None
if self.todate < float('inf'):
dtend = num2date(self.todate)
dtbegin = None
if self.fromdate > float('-inf'):
dtbegin = num2date(self.fromdate)
self.qhist = self.ib.reqHistoricalDataEx(
contract=self.contract, enddate=dtend, begindate=dtbegin,
timeframe=self._timeframe, compression=self._compression,
what=self.p.what, useRTH=self.p.useRTH, tz=self._tz,
sessionend=self.p.sessionend)
self._state = self._ST_HISTORBACK
return True # continue before
# Live is requested
if not self.ib.reconnect(resub=True):
self.put_notification(self.DISCONNECTED)
self._state = self._ST_OVER
return False # failed - was so
self._statelivereconn = self.p.backfill_start
if self.p.backfill_start:
self.put_notification(self.DELAYED)
self._state = self._ST_LIVE
return True # no return before - implicit continue
示例4: _st_start
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def _st_start(self, instart=True, tmout=None):
if self.p.historical:
self.put_notification(self.DELAYED)
dtend = None
if self.todate < float('inf'):
dtend = num2date(self.todate)
dtbegin = None
if self.fromdate > float('-inf'):
dtbegin = num2date(self.fromdate)
self.qhist = self.o.candles(
self.p.dataname, dtbegin, dtend,
self._timeframe, self._compression,
candleFormat=self._candleFormat,
includeFirst=self.p.includeFirst)
self._state = self._ST_HISTORBACK
return True
self.qlive = self.o.streaming_prices(self.p.dataname, tmout=tmout)
if instart:
self._statelivereconn = self.p.backfill_start
else:
self._statelivereconn = self.p.backfill
if self._statelivereconn:
self.put_notification(self.DELAYED)
self._state = self._ST_LIVE
if instart:
self._reconns = self.p.reconnections
return True # no return before - implicit continue
示例5: log
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def log(self, txt, dt=None):
dt = dt or self.data.datetime[0]
dt = bt.num2date(dt)
print('%s, %s' % (dt.isoformat(), txt))
示例6: log
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def log(self, txt, dt=None, nodate=False):
if not nodate:
dt = dt or self.data.datetime[0]
dt = bt.num2date(dt)
print('%s, %s' % (dt.isoformat(), txt))
else:
print('---------- %s' % (txt))
示例7: log
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def log(self, txt, dt=None):
if self.p.printout:
dt = dt or self.data.datetime[0]
dt = bt.num2date(dt)
print('%s, %s' % (dt.isoformat(), txt))
示例8: notify_order
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def notify_order(self, order):
if not order.alive():
print('{} {} {}@{}'.format(
bt.num2date(order.executed.dt),
'buy' if order.isbuy() else 'sell',
order.executed.size,
order.executed.price)
)
示例9: log
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = dt or self.data.datetime[0]
if isinstance(dt, float):
dt = bt.num2date(dt)
print('%s, %s' % (dt.isoformat(), txt))
示例10: notify_order
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def notify_order(self, order):
if order.status != order.Completed:
return
self.order = None
print('{} {} Executed at price {}'.format(
bt.num2date(order.executed.dt).date(),
'Buy' * order.isbuy() or 'Sell', order.executed.price)
)
示例11: notify_order
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def notify_order(self, order):
curdtstr = self.data.datetime.datetime().strftime('%a %Y-%m-%d %H:%M:%S')
if order.status in [order.Completed]:
dtstr = bt.num2date(order.executed.dt).strftime('%a %Y-%m-%d %H:%M:%S')
if order.isbuy():
print('%s: BUY EXECUTED, on:' % curdtstr, dtstr)
self.order = None
else: # Sell
print('%s: SELL EXECUTED, on:' % curdtstr, dtstr)
示例12: _st_start
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def _st_start(self):
self.put_notification(self.DELAYED)
date_begin = num2date(
self.fromdate) if self.fromdate > float('-inf') else None
date_end = num2date(
self.todate) if self.todate < float('inf') else None
self.qhist = self.o.price_data(self.p.dataname, date_begin, date_end, self.p.timeframe,
self.p.compression, self.p.include_last)
self._state = self._ST_HISTORBACK
return True
示例13: log_status
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def log_status(self):
status_dict = {}
# general status
status_dict["date"] = bt.num2date(self.data0.datetime[0])
status_dict["data0"] = self.data0[0]
status_dict["data1"] = self.data1[0]
status_dict["lookback"] = self.lookback
status_dict["max_lookback"] = self.max_lookback
status_dict["enter_threshold_size"] = self.enter_threshold_size
status_dict["exit_threshold_size"] = self.exit_threshold_size
status_dict["loss_limit"] = self.loss_limit
status_dict["status"] = self.status
status_dict["qty0"] = self.qty0
status_dict["qty1 "] = self.qty1
status_dict["initial_price_data0"] = self.initial_price_data0
status_dict["initial_price_data1"] = self.initial_price_data1
status_dict["initial_cash"] = self.initial_cash
status_dict["initial_long_pv"] = self.initial_long_pv
status_dict["initial_short_pv"] = self.initial_short_pv
status_dict["upper_limit"] = self.upper_limit
status_dict["lower_limit"] = self.lower_limit
status_dict["up_medium"] = self.up_medium
status_dict["low_medium"] = self.low_medium
status_dict["portfolio_value"] = self.broker.getvalue()
status_dict["latest_trade_action"] =self.latest_trade_action
status_dict["sell_stk"] = self.sell_stk
status_dict["buy_stk"] = self.buy_stk
status_dict["sell_amt"] = self.sell_amt
status_dict["buy_amt"] = self.buy_amt
# strategy-specific status
status_dict["spread"] = self.get_spread()
status_dict["allow_trade"] = self.allow_trade
status_dict["resid_mean"] = self.resid_mean
status_dict["resid_std"] = self.resid_std
# log the dictionary
PTStrategy.log("[strategy-status]: {}".format(status_dict), None, self.data0)
示例14: log
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def log(txt, dt=None, data=None):
dt = dt or data.datetime[0]
dt = bt.num2date(dt)
_logger.info('%s, %s' % (dt.isoformat(), txt))
示例15: log_status
# 需要導入模塊: import backtrader [as 別名]
# 或者: from backtrader import num2date [as 別名]
def log_status(self):
status_dict = {}
# general status
status_dict["date"] = bt.num2date(self.data0.datetime[0])
status_dict["data0"] = self.data0[0]
status_dict["data1"] = self.data1[0]
status_dict["lookback"] = self.lookback
status_dict["max_lookback"] = self.max_lookback
status_dict["enter_threshold_size"] = self.enter_threshold_size
status_dict["exit_threshold_size"] = self.exit_threshold_size
status_dict["loss_limit"] = self.loss_limit
status_dict["status"] = self.status
status_dict["qty0"] = self.qty0
status_dict["qty1"] = self.qty1
status_dict["initial_price_data0"] = self.initial_price_data0
status_dict["initial_price_data1"] = self.initial_price_data1
status_dict["initial_cash"] = self.initial_cash
status_dict["initial_long_pv"] = self.initial_long_pv
status_dict["initial_short_pv"] = self.initial_short_pv
status_dict["upper_limit"] = self.upper_limit
status_dict["lower_limit"] = self.lower_limit
status_dict["up_medium"] = self.up_medium
status_dict["low_medium"] = self.low_medium
status_dict["portfolio_value"] = self.broker.getvalue()
status_dict["latest_trade_action"] =self.latest_trade_action
status_dict["sell_stk"] = self.sell_stk
status_dict["buy_stk"] = self.buy_stk
status_dict["sell_amt"] = self.sell_amt
status_dict["buy_amt"] = self.buy_amt
# strategy-specific status
status_dict["spread"] = self.get_spread()
status_dict["allow_trade"] = self.allow_trade
status_dict["alpha"] = self.alpha
status_dict["intercept"] = self.intercept
status_dict["resid_mean"] = self.resid_mean
status_dict["resid_std"] = self.resid_std
# log the dictionary
PTStrategy.log("[strategy-status]: {}".format(status_dict), None, self.data0)