通达信的主力买卖指标来实现交易策略:
- 定义三条均线:白线、黄线、紫线,分别对应短期、中期、长期趋势线。
- 在主力进出指标的基础上,根据均线交叉形态、底部构成等多种因素,判断买入/卖出信号。
- 根据买入/卖出信号,设置对应的买入/卖出价格和数量。
- 在交易过程中,需要注意控制交易风险。
基于以上策略,我们可以通过easytrader库实现自动交易。具体实现过程如下:
from easytrader import *
import talib
# 初始化easytrader接口
user = use('ht') # 使用华泰证券交易接口
user.prepare('account.json')
# 获取需要交易的股票代码
target_stock = '600519' # 股票代码
amount = 100 # 购买数量
# 定义均线参数
short_period = 5 # 短期均线
middle_period = 10 # 中期均线
long_period = 20 # 长期均线
# 获取股票的历史数据
history_data = user.get_history_data(target_stock, 'D', 60) # 获取近60个交易日的日线数据
(注:获取股票的历史数据已失效,请另寻可用的数据来源)
# 计算白线、黄线、紫线三条均线
close_prices = [float(item['price']) for item in history_data]
white_line = talib.SMA(close_prices, short_period)
yellow_line = talib.SMA(close_prices, middle_period)
purple_line = talib.SMA(close_prices, long_period)
# 获取最新的股票价格和日期
latest_price = float(history_data[-1]['price'])
latest_date = history_data[-1]['date']
# 判断交易信号
if white_line[-1] > yellow_line[-1] and yellow_line[-1] > purple_line[-1]:
# 白线向上且处于黄、紫线之上,买入股票
buy_price = latest_price
user.buy(target_stock, price=buy_price, amount=amount)
print('{} {} 在 {} 买入 {} 股,价格 {}'.format(latest_date, target_stock, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), amount, buy_price))
elif white_line[-1] < yellow_line[-1] and yellow_line[-1] < purple_line[-1]:
# 白线向下且处于黄、紫间之下,卖出股票
sell_price = latest_price
user.sell(target_stock, price=sell_price, amount=amount)
print('{} {} 在 {} 卖出 {} 股,价格 {}'.format(latest_date, target_stock, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), amount, sell_price))
elif white_line[-1] > yellow_line[-1] and yellow_line[-1] < purple_line[-1] and white_line[-2] < yellow_line[-2] and yellow_line[-2] < purple_line[-2]:
# 最佳短线买点交叉形态,买入股票
buy_price = latest_price
user.buy(target_stock, price=buy_price, amount=amount)
print('{} {} 在 {} 买入 {} 股,价格 {}'.format(latest_date, target_stock, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), amount, buy_price))
elif white_line[-1] > yellow_line[-1] and yellow_line[-1] < purple_line[-1] and white_line[-2] > yellow_line[-2] and yellow_line[-2] < purple_line[-2]:
# 最佳中线买点交叉形态,买入股票
buy_price = latest_price
user.buy(target_stock, price=buy_price, amount=amount)
print('{} {} 在 {} 买入 {} 股,价格 {}'.format(latest_date, target_stock, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), amount, buy_price))
elif white_line[-1] < yellow_line[-1] and white_line[-2] > yellow_line[-2]) and (white_line[-1] - yellow_line[-1]) / yellow_line[-1] > 0.03:
# 最佳短线卖点交叉形态,卖出股票
sell_price = latest_price
user.sell(target_stock, price=sell_price, amount=amount)
print(‘{} {} 在 {} 卖出 {} 股,价格 {}’.format(latest_date, target_stock, time.strftime(“%Y-%m-%d %H:%M:%S”, time.localtime()), amount, sell_price))
elif white_line[-1] < yellow_line[-1] and yellow_line[-1] < purple_line[-1] and white_line[-2] > yellow_line[-2] and yellow_line[-2] > purple_line[-2]:
# 最佳中线卖点交叉形态,卖出股票
sell_price = latest_price
user.sell(target_stock, price=sell_price, amount=amount)
print(‘{} {} 在 {} 卖出 {} 股,价格 {}’.format(latest_date, target_stock, time.strftime(“%Y-%m-%d %H:%M:%S”, time.localtime()), amount, sell_price))
else:
# 其他情况不执行任何操作
pass
需要注意,以上代码仅为示例代码,可能并不完善或准确,实际使用时需要根据具体业务需求进行修改和调整,同时需要谨慎执行交易,避免因操纵失误导致资金损失。
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站不拥有所有权,不承担相关法律责任。如发现有侵权/违规的内容, 联系QQ15101117,本站将立刻清除。