程序代码如下:

import numpy as np import pandas as pd import talib import tushare as ts import time

获取实时数据

def get_realtime_data(): df = ts.get_realtime_quotes('fu2107') return df

计算指标

def calculate_indicators(df): # 计算120日均线 df['MA120'] = talib.MA(df['close'], timeperiod=120) # 计算KD指标 df['slowk'], df['slowd'] = talib.STOCH(df['high'], df['low'], df['close'], fastk_period=18, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) # 计算RSI指标 df['RSI'] = talib.RSI(df['close'], timeperiod=5) return df

判断多空趋势

def trend_direction(df): if df['close'] > df['MA120']: return '多' else: return '空'

判断做多信号

def long_signal(df): if df['slowk'] > df['slowd'] and df['RSI'] > 30: return True else: return False

判断做空信号

def short_signal(df): if df['slowk'] < df['slowd'] and df['RSI'] < 70: return True else: return False

计算止损价格

def calculate_stop_loss(df, direction): if direction == '多': stop_loss = df['close'] - df['close']*0.01 else: stop_loss = df['close'] + df['close']*0.01 return stop_loss

计算加仓价格

def calculate_add_position_price(df, direction, add_count): if direction == '多': if add_count == 1: add_price = df['low'].iloc[-2] else: add_price = df['low'].iloc[-1] else: if add_count == 1: add_price = df['high'].iloc[-2] else: add_price = df['high'].iloc[-1] return add_price

计算止盈价格

def calculate_take_profit(df, direction): if direction == '多': take_profit = df['close'] + df['close']*0.01 else: take_profit = df['close'] - df['close']*0.01 return take_profit

计算交易手数

def calculate_trade_count(direction, add_count): if add_count == 0: trade_count = 2 else: trade_count = 1 if direction == '多': return trade_count else: return -trade_count

自动交易策略

def auto_trade(): df = get_realtime_data() df = calculate_indicators(df) direction = trend_direction(df)

if direction == '多':
    if long_signal(df):
        stop_loss = calculate_stop_loss(df, direction)
        take_profit = calculate_take_profit(df, direction)
        trade_count = calculate_trade_count(direction, 0)
        print('做多信号,开仓')
        print('开仓价格:', df['close'])
        print('止损价格:', stop_loss)
        print('止盈价格:', take_profit)
        print('交易手数:', trade_count)
        # 下单交易
        # ...
        # 记录持仓信息
        # ...
        # 循环监控并处理加仓和止盈止损

    else:
        # 持仓状态下处理加仓和止盈止损
        # ...
        # 判断是否需要平仓
        # ...
        pass

else:
    if short_signal(df):
        stop_loss = calculate_stop_loss(df, direction)
        take_profit = calculate_take_profit(df, direction)
        trade_count = calculate_trade_count(direction, 0)
        print('做空信号,开仓')
        print('开仓价格:', df['close'])
        print('止损价格:', stop_loss)
        print('止盈价格:', take_profit)
        print('交易手数:', trade_count)
        # 下单交易
        # ...
        # 记录持仓信息
        # ...
        # 循环监控并处理加仓和止盈止损

    else:
        # 持仓状态下处理加仓和止盈止损
        # ...
        # 判断是否需要平仓
        # ...
        pass

主函数

if name == 'main': # 程序自动开机运行15分钟K线图交易 while True: auto_trade() time.sleep(900


原文地址: https://www.cveoy.top/t/topic/fssv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录