打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
利用CHO指标实现智能交易策略,超越预测水平
userphoto

2023.06.14 浙江

关注

CHO指标是一个基于改进型指标的交易指标,它反映了价格的变动与成交量的关系。在本策略中,我们将使用easytrader库来实现CHO指标交易策略。CHO指标是基于累积/派生线(Accumulation/Distribution Line)和变量指标(volatility index)的改进型指标,其中CHO代表的是Chaikin Oscillator,在技术分析中也被称为佳庆指标。因此,CHO指标和佳庆指标可以互换使用。

首先,我们需要安装easytrader库。可以使用以下命令在终端中安装:

pip install easytrader

接下来,我们需要使用easytrader库连接我们的券商账户。由于每个券商的API接口和配置略微不同,因此这里需要使用与您的券商兼容的适当技术。

一旦我们已经成功连接了券商账户,我们可以开始编写策略。

首先,我们需要定义一个函数来计算CHO指标。以下是一个简单的实现:

def compute_cho(close_price: List[float], high_price: List[float], low_price: List[float], volume: List[float], period: int, cycle: int) -> List[float]:    typical_price = [(close_price[i] + high_price[i] + low_price[i]) / 3 for i in range(len(close_price))]    mf_multipliers = [((tp[i] - tp[max(0, i - 1)]) / tp[max(0, i - 1)]) * volume[i] for i in range(len(tp))]    mf_volume = [sum(mf_multipliers[max(0, i - period + 1):i + 1]) for i in range(len(tp))]    ema_1 = ema(mf_volume, cycle)    ema_2 = ema(ema_1, cycle)    cho = [2 * (e1 - e2)/(e1 + e2) for e1, e2 in zip(ema_1, ema_2)]    return cho

该函数取一个股票在一段时间内的收盘价列表、最高价列表、最低价列表、成交量列表以及计算周期和循环周期作为参数,然后计算出CHO指标。

接下来,我们需要获取每个股票的历史价格数据。此处可以使用easytrader库中的函数来获取数据。

from easytrader import usefrom easytrader import exceptions# 连接券商账户user = use('ht', debug=True)user.connect()# 获取历史股票价格数据def get_stock_price_history(stock_code:str, exchange:str, start_date:str, end_date:str) -> Tuple[List[float], List[float], List[float], List[float]]: close_price = [] high_price = [] low_price = [] volume = [] try: stock_price_history = user.get_history_price(stock_code=stock_code, exchange=exchange, start_date=start_date, end_date=end_date) close_price = [float(price['price']) for price in stock_price_history] high_price = [float(price['high']) for price in stock_price_history] low_price = [float(price['low']) for price in stock_price_history] volume = [float(price['volume']) for price in stock_price_history] except exceptions.TradeError as e: print(e) return close_price, high_price, low_price, volume

这个函数使用get_history_price方法从券商的接口获取一个股票的历史价格数据。我们需要提供股票代码、交易所、开始和结束日期作为参数。默认情况下,价格数据返回为字典格式,我们需要使用一个列表来表示所有的价格。

接着,我们可以写一个函数来执行交易。

# 执行交易def execute_trade(stock_code:str, exchange:str, cho_threshold:float, trade_amount:int):    # 获取股票历史价格数据和成交量    close_price, high_price, low_price, volume = get_stock_price_history(stock_code=stock_code, exchange=exchange, start_date='2023-05-01', end_date='2023-05-29')        # 计算CHO指标    cho = compute_cho(close_price=close_price, high_price=high_price, low_price=low_price, volume=volume, period=10, cycle=3)        # 取最新一条CHO指标    last_cho = cho[-1]        # 比较CHO指标与阈值    if last_cho > cho_threshold:    # 买入股票current_price = close_price[-1]user.buy(stock_code, current_price, trade_amount)    else:    # 卖出股票current_price = close_price[-1]user.sell(stock_code, current_price, trade_amount)

该函数获取股票历史价格数据、计算CHO指标和执行买卖操作。如果最新的CHO指标超过阈值,则买入股票;否则,卖出股票。买入和卖出的价格都选择了每日的收盘价。

最后,要注意在运行策略时必须保持连接券商账户,可以使用以下代码:

```python# 连接券商账户user = use('ht', debug=True)user.connect()# 执行交易execute_trade(stock_code='AAPL', exchange='NASDAQ', cho_threshold=0.3, trade_amount=100)

在这个例子中,我们连接了ht券商账户,并执行了一次AAPL股票的交易。CHO指标阈值设置为0.3。由于这只是一个示例,我们只考虑了股票价格和成交量,真实情况下还需要考虑其他因素,例如股票市场的趋势。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【Python量化】灵活运用,掌握三阳开泰技巧,easytrader库帮您!
It's official! Alibaba is the new IPO king after pricing at $68
金融词汇表(P)-MORGAN STANLEY
ETF - Exchange Funds (Exchange Traded Funds)
Program Trading
OBYC中的Transaction key详细解释以及用法-Group(RMK)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服