用信号产出之后第二根k线买入
This commit is contained in:
18
trend.py
18
trend.py
@@ -1,3 +1,5 @@
|
|||||||
|
from turtle import width
|
||||||
|
from plotly.io import renderers
|
||||||
import vectorbt as vbt
|
import vectorbt as vbt
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
@@ -6,9 +8,11 @@ import pandas as pd
|
|||||||
# ========================
|
# ========================
|
||||||
# 从本地读取 ETH/USDT 1天K线数据(来自OKX)
|
# 从本地读取 ETH/USDT 1天K线数据(来自OKX)
|
||||||
df = pd.read_feather("/Users/aszer/Documents/vscode/cta/user_data/data/okx/ADA_USDT-1d.feather")
|
df = pd.read_feather("/Users/aszer/Documents/vscode/cta/user_data/data/okx/ADA_USDT-1d.feather")
|
||||||
|
# df = df[df['date'] >= '2025-07-01']
|
||||||
|
df.set_index("date", inplace=True)
|
||||||
# 提取收盘价作为交易价格
|
# 提取收盘价作为交易价格
|
||||||
price = df["close"]
|
price = df["close"]
|
||||||
|
open = df["open"]
|
||||||
# ========================
|
# ========================
|
||||||
# 技术指标计算
|
# 技术指标计算
|
||||||
# ========================
|
# ========================
|
||||||
@@ -40,7 +44,7 @@ rsi_oversold_bounce = (rsi_6.shift(1).rolling(3).max() < 21) & \
|
|||||||
(rsi_12.shift(1).rolling(3).max() < 26.25)
|
(rsi_12.shift(1).rolling(3).max() < 26.25)
|
||||||
|
|
||||||
# 合并所有买入信号:任一条件满足即产生买入信号
|
# 合并所有买入信号:任一条件满足即产生买入信号
|
||||||
entries = ema_bullish_cross | rsi_oversold_bounce
|
entries = ema_bullish_cross
|
||||||
|
|
||||||
# ========================
|
# ========================
|
||||||
# 卖出条件定义
|
# 卖出条件定义
|
||||||
@@ -66,8 +70,9 @@ exits = sell_cond
|
|||||||
# 使用 vectorbt 的信号回测引擎,构建投资组合
|
# 使用 vectorbt 的信号回测引擎,构建投资组合
|
||||||
pf = vbt.Portfolio.from_signals(
|
pf = vbt.Portfolio.from_signals(
|
||||||
price, # 价格序列
|
price, # 价格序列
|
||||||
entries, # 买入信号
|
entries.vbt.fshift(1), # 买入信号
|
||||||
exits, # 卖出信号
|
exits.vbt.fshift(1), # 卖出信号
|
||||||
|
price=open,
|
||||||
init_cash=100, # 初始资金 100 USDT
|
init_cash=100, # 初始资金 100 USDT
|
||||||
fees=0.001, # 交易手续费 0.1%(买卖均收)
|
fees=0.001, # 交易手续费 0.1%(买卖均收)
|
||||||
sl_stop=0.05, # 止损:从最高价回撤5%时触发
|
sl_stop=0.05, # 止损:从最高价回撤5%时触发
|
||||||
@@ -78,4 +83,7 @@ pf = vbt.Portfolio.from_signals(
|
|||||||
# 输出回测统计结果
|
# 输出回测统计结果
|
||||||
# ========================
|
# ========================
|
||||||
print(pf.stats())
|
print(pf.stats())
|
||||||
|
# fig = pf.plot_orders(width=1200, height=800)
|
||||||
|
# fig.show(renderer='browser')
|
||||||
|
orders_df = pf.orders.records_readable
|
||||||
|
print(orders_df)
|
||||||
Reference in New Issue
Block a user