策略逻辑(示例:20日均线上穿60日均线买入,下穿卖出) ```python import tushare as ts, pandas as pd, datetime as dt def get_signal(code): df = ts.pro_bar(ts_code=code, adj='qfq', start_date=(dt.date.today()-dt.timedelta(120)).strftime('Ymd')) df = df.sort_values('trade_date') df['ma20'] = df['close'].rolling(20).mean() df['ma60'] = df['close'].rolling(60).mean() if df.iloc[-1]['ma20'] > df.iloc[-1]['ma60'] and df.iloc[-2]['ma20'] <= df.iloc[-2]['ma60']: return 'BUY' elif df.iloc[-1]['ma20'] < df.iloc[-1]['ma60'] and df.iloc[-2]['ma20'] >= df.iloc[-2]['ma60']: return 'SELL' return 'HOLD' ```
3. 交易执行(以华泰客户端为例) ```python import easytrader, time user = easytrader.use('ht_client') # 同花顺、银河等替换关键字 user.prepare(user='你的账号', password='你的密码') while True: for code in ['600519.SH', '000858.SZ']: signal = get_signal(code) if signal == 'BUY': user.buy(code, price=user.get_quote(code)['now']*01, amount=100) elif signal == 'SELL': user.sell(code, price=user.get_quote(code)['now']*99, amount=100) time.sleep(60) ```