talib/_ta_lib.c:747:10: fatal error: 'ta-lib/ta_defs.h' file not found
时间: 2023-06-12 09:04:11 浏览: 170
这个错误提示是找不到TA-Lib库的头文件`ta_defs.h`。你需要确认你的系统中是否已经安装了TA-Lib库并且路径已经正确配置。如果没有安装,你可以在TA-Lib的官网上下载相应版本的库文件并进行安装。如果已经安装,你需要检查一下库文件的路径是否正确,可以尝试在编译命令中添加相关的库文件路径参数。
相关问题
程序报错:Traceback (most recent call last): File "/tmp/jqcore/jqboson/jqboson/core/entry.py", line 373, in _run engine.start() File "/tmp/jqcore/jqboson/jqboson/core/engine.py", line 231, in start self._dispatcher.start() File "/tmp/jqcore/jqboson/jqboson/core/dispatcher.py", line 273, in start self._run_loop() File "/tmp/jqcore/jqboson/jqboson/core/dispatcher.py", line 240, in _run_loop self._loop.run() File "/tmp/jqcore/jqboson/jqboson/core/loop/loop.py", line 105, in run self._handle_queue() File "/tmp/jqcore/jqboson/jqboson/core/loop/loop.py", line 151, in _handle_queue message.callback(**message.callback_data) File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_subscriber.py", line 228, in broadcast consumer.send(market_data) File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_consumer_manager.py", line 59, in consumer_gen msg_callback() File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_consumer_manager.py", line 52, in msg_callback callback(market_data) File "/tmp/jqcore/jqboson/jqboson/core/mds/market_data_consumer_manager.py", line 122, in wrapper result = callback(*args, **kwargs) File "/tmp/jqcore/jqboson/jqboson/core/strategy.py", line 407, in _wrapper self._context.current_dt File "/tmp/strategy/user_code.py", line 60, in handle_data mancd, hist = mancd(context) UnboundLocalError: local variable 'mancd' referenced before assignment
报错信息显示 `UnboundLocalError: local variable 'mancd' referenced before assignment`,这是因为 `mancd` 函数在计算过程中出现了错误,导致其没有返回结果,而在调用 `mancd` 函数时,`mancd` 没有被正确赋值,因此出现了该错误。
可以尝试检查 `mancd` 函数的计算过程,查看是否有语法错误或逻辑错误。另外,建议在 `mancd` 函数中加入一些调试信息,以便更好地定位问题。例如:
```python
def mancd(context):
# 获取股票价格数据
prices = history(g.period, '1d', 'close')[context.stock]
# 计算macd指标
macd, signal, hist = talib.MACD(prices)
# 计算mancd指标
mancd, signal, hist = talib.MACDEXT(prices, fastperiod=12, slowperiod=26, signalperiod=9)
print('mancd:', mancd)
# 返回计算结果
return mancd, hist
```
这样,在出现问题时,就可以通过查看日志,定位问题所在。
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)
```
阅读全文