1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
import pandas as pd import numpy as np import datetime """ 示例说明:双均线实盘策略,通过计算快慢双均线,在金叉时买入,死叉时做卖出 """ class a(): pass A = a()
def init(C): A.stock= C.stockcode + '.' + C.market A.acct= account A.acct_type= accountType A.amount = 10000 A.line1=17 A.line2=27 A.waiting_list = [] A.buy_code = 23 if A.acct_type == 'STOCK' else 33 A.sell_code = 24 if A.acct_type == 'STOCK' else 34 C.set_universe([A.stock]) print(f'双均线实盘示例{A.stock} {A.acct} {A.acct_type} 单笔买入金额{A.amount}') def handlebar(C): if not C.is_last_bar(): return now = datetime.datetime.now() now_time = now.strftime('%H%M%S') if now_time < '093000' or now_time > "150000": return account = get_trade_detail_data(A.acct, A.acct_type, 'account') if len(account)==0: print(f'账号{A.acct} 未登录 请检查') return account = account[0] available_cash = int(account.m_dAvailable) if A.waiting_list: found_list = [] orders = get_trade_detail_data(A.acct, A.acct_type, 'order') for order in orders: if order.m_strRemark in A.waiting_list: found_list.append(order.m_strRemark) A.waiting_list = [i for i in A.waiting_list if i not in found_list] if A.waiting_list: print(f"当前有未查到委托 {A.waiting_list} 暂停后续报单") return holdings = get_trade_detail_data(A.acct, A.acct_type, 'position') holdings = {i.m_strInstrumentID + '.' + i.m_strExchangeID : i.m_nCanUseVolume for i in holdings} data = C.get_history_data(max(A.line1, A.line2)+1, '1d', 'close',dividend_type='front_ratio') close_list = data[A.stock] if len(close_list) < max(A.line1, A.line2)+1: print('行情长度不足(新上市或最近有停牌) 跳过运行') return pre_line1 = np.mean(close_list[-A.line1-1: -1]) pre_line2 = np.mean(close_list[-A.line2-1: -1]) current_line1 = np.mean(close_list[-A.line1:]) current_line2 = np.mean(close_list[-A.line2:]) vol = int(A.amount / close_list[-1] / 100) * 100 if A.amount < available_cash and vol >= 100 and A.stock not in holdings and pre_line1 < pre_line2 and current_line1 > current_line2: msg = f"双均线实盘 {A.stock} 上穿均线 买入 {vol}股" passorder(A.buy_code, 1101, A.acct, A.stock, 14, -1, vol, '双均线实盘', 1 , msg, C) print(msg) A.waiting_list.append(msg) if A.stock in holdings and holdings[A.stock] > 0 and pre_line1 > pre_line2 and current_line1 < current_line2: msg = f"双均线实盘 {A.stock} 下穿均线 卖出 {holdings[A.stock]}股" passorder(A.sell_code, 1101, A.acct, A.stock, 14, -1, holdings[A.stock], '双均线实盘', 1 , msg, C) print(msg) A.waiting_list.append(msg)
|