jupyter安装talib报错- ta-lib -> python[version='2.7.*|3.4.*|>=3.5,<3.6.0a0|>=3.6,<3.7.0a0|3.5.*']
时间: 2023-12-08 18:01:07 浏览: 190
这个报错的原因是因为你的 Python 版本不符合 ta-lib 包的要求。你可以考虑使用适合你 Python 版本的 ta-lib 包版本。可以使用以下命令安装适合 Python3.6 的 ta-lib 包:
```
pip install TA-Lib==0.4.0
```
或者使用以下命令安装适合 Python3.5 的 ta-lib 包:
```
pip install TA-Lib==0.4.17
```
如果还是报错,你可以尝试先升级你的 pip:
```
python -m pip install --upgrade pip
```
然后再重新安装 ta-lib 包。
相关问题
jupyter安装talib报错- ta-lib -> python[version='2.7.|3.4.|>=3.5,<3.6.0a0|>=3.6,<3.7.0a0|3.5.*']怎么解决
这个问题看起来是由于ta-lib针对某些较老的Python版本编译而成,无法在您当前安装的Python版本上使用所致。您可以尝试升级Python版本来解决这个问题,或者安装针对您当前Python版本编译而成的ta-lib。具体的步骤可以参考ta-lib的官方文档:http://ta-lib.org/hdr_dw.html。同时,您也可以在Stack Overflow等技术论坛上寻求更多的帮助。
import numpy as np import pandas as pd import talib def initialize(context): context.symbol = 'BTCUSDT' context.window_size = 5 context.deviation = 1 context.trade_size = 0.01 context.stop_loss = 0.05 context.take_profit = 0.1 schedule_function(rebalance, date_rules.every_day(), time_rules.market_open()) def rebalance(context, data): price = data.history(context.symbol, 'close', context.window_size + 1, '1d') signal = mean_reversion_signal(price, context.window_size, context.deviation) current_position = context.portfolio.positions[context.symbol].amount if signal[-1] == 1 and current_position <= 0: target_position_size = context.trade_size / data.current(context.symbol, 'close') order_target_percent(context.symbol, target_position_size) elif signal[-1] == -1 and current_position >= 0: order_target(context.symbol, 0) elif current_position > 0: current_price = data.current(context.symbol, 'close') stop_loss_price = current_price * (1 - context.stop_loss) take_profit_price = current_price * (1 + context.take_profit) if current_price <= stop_loss_price or current_price >= take_profit_price: order_target(context.symbol, 0) def moving_average(x, n): ma = talib.SMA(x, timeperiod=n) return ma def std_deviation(x, n): std = talib.STDDEV(x, timeperiod=n) return std def mean_reversion_signal(price, window_size, deviation): ma = moving_average(price, window_size) std = std_deviation(price, window_size) upper_band = ma + deviation * std lower_band = ma - deviation * std signal = np.zeros_like(price) signal[price > upper_band] = -1 # 卖出信号 signal[price < lower_band] = 1 # 买入信号 return signal ''' 运行回测 ''' start_date = pd.to_datetime('2019-01-01', utc=True) end_date = pd.to_datetime('2021-01-01', utc=True) results = run_algorithm( start=start_date, end=end_date, initialize=initialize, capital_base=10000, data_frequency='daily', bundle='binance' ) ''' 查看回测结果 ''' print(results.portfolio_value)格式错误
这段代码存在一些格式错误。具体来说,第一行和第二行之间需要有一个空行,否则会提示语法错误。另外,代码中的注释也需要使用 # 符号进行标记。
以下是修改后的代码:
```
import numpy as np
import pandas as pd
import talib
def initialize(context):
context.symbol = 'BTCUSDT'
context.window_size = 5
context.deviation = 1
context.trade_size = 0.01
context.stop_loss = 0.05
context.take_profit = 0.1
schedule_function(rebalance, date_rules.every_day(), time_rules.market_open())
def rebalance(context, data):
price = data.history(context.symbol, 'close', context.window_size + 1, '1d')
signal = mean_reversion_signal(price, context.window_size, context.deviation)
current_position = context.portfolio.positions[context.symbol].amount
if signal[-1] == 1 and current_position <= 0:
target_position_size = context.trade_size / data.current(context.symbol, 'close')
order_target_percent(context.symbol, target_position_size)
elif signal[-1] == -1 and current_position >= 0:
order_target(context.symbol, 0)
elif current_position > 0:
current_price = data.current(context.symbol, 'close')
stop_loss_price = current_price * (1 - context.stop_loss)
take_profit_price = current_price * (1 + context.take_profit)
if current_price <= stop_loss_price or current_price >= take_profit_price:
order_target(context.symbol, 0)
def moving_average(x, n):
ma = talib.SMA(x, timeperiod=n)
return ma
def std_deviation(x, n):
std = talib.STDDEV(x, timeperiod=n)
return std
def mean_reversion_signal(price, window_size, deviation):
ma = moving_average(price, window_size)
std = std_deviation(price, window_size)
upper_band = ma + deviation * std
lower_band = ma - deviation * std
signal = np.zeros_like(price)
signal[price > upper_band] = -1 # 卖出信号
signal[price < lower_band] = 1 # 买入信号
return signal
''' 运行回测 '''
start_date = pd.to_datetime('2019-01-01', utc=True)
end_date = pd.to_datetime('2021-01-01', utc=True)
results = run_algorithm(
start=start_date,
end=end_date,
initialize=initialize,
capital_base=10000,
data_frequency='daily',
bundle='binance'
)
''' 查看回测结果 '''
print(results.portfolio_value)
```
阅读全文