策略 API 参考
全部函数:订单、持仓、序列、指标、FVG、news() 与 near_news()。
talios 包的完整参考:你的策略可以读取和调用的一切。两条导入语句就能涵盖全部。
from talios import Strategy, Long, Short
from talios.indicators import ema, sma, rsi, atr, highest, lowest, crossover, crossunder, fvg, killzone, session, news, near_newsStrategy
| 名称 | 说明 |
|---|---|
setup(self) | 可选。每个货币对在第一根 K线之前调用一次。 |
on_bar(self, bar) | 必需。在每根 K线收盘时调用。没有它,运行会停止并提示“请在你的 Strategy 中定义 on_bar(self, bar)”。 |
self.data | 该货币对的价格序列(参见 Series)。 |
self.pair | 正在运行的货币对,例如 "EUR/USD"。 |
self.timeframe | 回测的时间周期,例如 "1H"。 |
self.minutes | K线的长度,以分钟计(1H 为 60)。 |
self.position | 当前持有的 Position,或 None。 |
self.order | 挂起的 Order,或 None。 |
self.trades | 该货币对到目前为止已平仓的交易(Trade 的列表)。 |
cost_r = 0.0 | 类属性:从每笔交易中扣除的 R,用于计入点差和佣金(0 或更大)。不设置时,交易没有成本。 |
| 参数 | 任何其他保存数字、字符串或布尔值的类属性(最多 50 个)。 |
Talios 为每个货币对创建一个全新的类实例,因此保存在 self 上的状态绝不会从一个货币对泄漏到下一个。
Series 和 Bar
| 名称 | 说明 |
|---|---|
self.data.open / .high / .low / .close / .volume | 长度相同的列表,最早的在前。 |
self.data.time | 每根 K线的开盘时间,UTC 纪元秒。 |
self.data.killzone / .session | 每根 K线一个标签(或 None),便于在 setup 中预先计算筛选条件。 |
len(self.data), self.data[i] | K线的数量,以及索引 i 处的 Bar(支持负索引和切片)。 |
bar.i | 该 K线在序列中的索引。 |
bar.time / bar.dt | 它的开盘时间,UTC 纪元秒 / UTC datetime。 |
bar.local | 它在项目时区下的开盘时间,夏令时和冬令时都已考虑在内:bar.local.hour == 9 全年都是你当地的 9:00。talios.indicators 中的 local(t) 对任意时间做同样的转换。 |
bar.open / .high / .low / .close / .volume | 它的价格和成交量。 |
bar.pair / .killzone / .session | 它的货币对、Killzone 和交易时段。 |
绝不要读取未来
整个序列都在self.data 中,这样指标可以在 setup 中一次算完。在 on_bar 内,只读取不超过 bar.i 的索引:超出的部分都是未来数据,得出的结果无法在实盘中交易。订单和仓位
每个货币对同一时间只有一个仓位,最多一张挂单。Long 和 Short 就是字符串 "Long" 和 "Short"。
self.enter(direction, stop, target=None, rr=None, price=None, expiry=None, note=None)
| 参数 | 含义 |
|---|---|
| direction | Long 或 Short。 |
| stop | 必需。止损价格。 |
| target / rr | 止盈,以价格(target)给出,或以从成交价起算的风险倍数(rr)给出。给其中一个或都不给,绝不能两个都给。都不给时,交易在止损、exit() 或数据结束时了结。 |
| price | None:市价单,在下一根 K线开盘时成交。给出价格:从下一根 K线起生效的挂单。对于 Long,价格等于或低于信号 K线的收盘价时是限价单(最低价触及时成交),高于收盘价时是突破入场单(最高价触及时成交);Short 则相反。它按自己的价格成交,如果某根 K线开盘时已越过该价格,则按开盘价成交。 |
| expiry | 带价格的订单保持有效的 K线数量(整数,1 或更大)。None:直到成交、被取消或被替换为止。 |
| note | 最多 200 个字符的文本,会随交易一起保留。 |
订单下达成功时返回 True。在以下情况下订单会被拒绝,控制台中会有一条提示,并返回 False:已有持仓;止损或目标位于入场价(挂单的价格,或市价单对应的本根 K线收盘价)错误的一侧;rr 不大于 0。新的 enter 会替换挂单。成交价落在止损或目标之外的订单会被取消:这个交易形态已经不存在了。
| 调用 | 作用 |
|---|---|
self.cancel() | 撤销挂单。 |
self.exit(note=None) | 在本根 K线收盘时平仓。没有持仓时返回 False。 |
self.move_stop(price) | 移动持仓的止损(保本、移动止损)。R 仍按初始止损计算。 |
self.move_target(price) | 移动目标;move_target(None) 移除目标。 |
# Market order: filled at the next bar's open, stop at a price, target at 2R
self.enter(Long, stop=bar.low, rr=2)
# Limit order at 1.0850, valid for 5 bars, fixed target price
self.enter(Long, stop=1.0820, target=1.0920, price=1.0850, expiry=5)
# Break-even once the trade is 1R in profit
pos = self.position
if pos and pos.r(bar.close) >= 1:
self.move_stop(pos.entry)
# Time exit
if pos and pos.bars(bar) >= 24:
self.exit("time")Position、Order 和 Trade
| 对象 | 字段 |
|---|---|
| Position (self.position) | .direction、.entry(成交价)、.stop、.target、.initial_stop、.entry_i、.entry_time、.risk(1R 对应的价格距离)、.note、.r(price)(该价格处的 R,未扣除 cost_r)、.bars(bar)(已持有的 K线数,成交的那根为 0)。 |
| Order (self.order) | .direction、.price、.stop、.target、.rr、.placed_i、.expiry、.note、.kind("market"、"limit" 或 "stop")。 |
| Trade (self.trades) | .direction、.entry、.exit、.r、.entry_i、.exit_i、.entry_time、.exit_time、.initial_stop、.stop、.target、.reason("stop"、"target"、"exit" 或 "end")、.open、.note、.mae(价格朝不利方向走了多远,以 R 计)。 |
talios.indicators
每个指标只使用截至其计算位置的那根 K线为止的数据,返回与输入等长的列表,预热期间为 None。列表参数也可以是序列本身(使用它的收盘价)。
| 函数 | 返回 |
|---|---|
ema(values, n) | 指数移动平均线(从前 n 个值的平均值开始)。 |
sma(values, n) | 简单移动平均线。 |
rsi(values, n=14) | Wilder 的 RSI,0 到 100。 |
atr(data, n=14) | Wilder 的平均真实波幅。接收序列(self.data),而不是列表。 |
highest(values, n), lowest(values, n) | 最近 n 个值(含当前值)的滚动最大值 / 最小值。 |
crossover(a, b, i), crossunder(a, b, i) | 当列表 a 在索引 i(或某个 Bar)处上穿 / 下穿 b(列表或数字)时为 True。 |
fvg(data, min_size=0.0, max_age=None) | 序列的 fair value gap,与 Afterburner 图表上的完全一致(见下文)。 |
killzone(bar) | 该 K线开盘时所处的 Killzone,取自你自己图表的 Killzone 设置(巴黎时间;默认 "Asia" 02:00–06:00、"London" 08:00–11:00、"NY" 13:00–16:00),或 None。 |
session(bar) | 该 K线开盘时在日志中对应的交易时段,按 UTC:"Asia"(00–07)、"London"(07–12)、"Overlap"(12–16)、"New York"(16–21)或 "Off Hours"。 |
news(pair=None, impact="high") | 回测区间内会影响该货币对相关货币的经济日历数据发布,按时间排序(见下文)。 |
near_news(bar, minutes=30, impact="high", pair=None) | 当该 K线开盘前后 minutes 分钟内(含边界)有相应重要度的数据发布时为 True。 |
Fair value gap
当第 i 根 K线的最低价高于第 i−2 根的最高价时,缺口为看涨;当第 i 根的最高价低于第 i−2 根的最低价时,缺口为看跌。min_size 是以价格计的最小高度;max_age 会在经过指定根数的 K线后丢弃未被触及的缺口。fvg() 返回一个 Gaps 对象:
gaps.formed_at(i):在第 i 根 K线完成的缺口。gaps.active(i):在 i 之前形成且尚未被触及的缺口,最新的排在最后。gaps.first_touch(bar):本根 K线回踩进入的最早的活跃缺口(随后标记为已触及),或 None。一个缺口只计一次。- Gap 具有
.bullish、.bearish、.top、.bottom、.height、.mid、.formed(其第三根 K线的索引)、.time和.touched(索引或 None)。
新闻函数
Talios 策略读取的经济日历与你图表上的 News 指标相同(13 个国家,自 2020 年起)。impact 为 "high"、"medium"、"low" 或它们组成的列表。当一次数据发布会影响某个货币对的其中一种货币时,它就计入该货币对:黄金和美国指数跟随 USD,GER40 跟随 EUR,以此类推。
news()返回数据发布的列表,每一项都带有.time(UTC 纪元秒)、.dt、.title、.currency、.country和.impact。每次调用都会构建一个新列表:在setup中调用一次并保存下来。near_news()直接回答最常见的问题:这根 K线是否离数据发布太近?
from talios import Strategy, Long, Short
from talios.indicators import ema, near_news, news
class TrendNoNews(Strategy):
"""EMA 50 / 200 trend, sitting out 30 min around high-impact news."""
news_window = 30 # minutes before and after a release
def setup(self):
self.fast = ema(self.data.close, 50)
self.slow = ema(self.data.close, 200)
self.releases = news(impact="high") # once, in setup
print(len(self.releases), "high-impact releases for", self.pair)
def on_bar(self, bar):
i = bar.i
if self.position or self.slow[i] is None:
return
if near_news(bar, minutes=self.news_window, impact="high"):
return
if self.fast[i] > self.slow[i] and bar.close > self.fast[i]:
self.enter(Long, stop=bar.low, rr=2)
elif self.fast[i] < self.slow[i] and bar.close < self.fast[i]:
self.enter(Short, stop=bar.high, rr=2)同样可以导入
来自 talios 的 Bar、Series、Position、Order、Trade;来自 talios.indicators 的 Gap、Gaps、NewsEvent。在你自己的辅助文件中写类型标注时很有用。