量化策略系列教程:19AR-MA 策略
今天上個新策略~證經社量化社區 - 證經社
1.策略原理及邏輯
1.1策略原理
人氣指標(AR)和意願指標(BR)都是以分析歷史股價為手段的技術指標,其中人氣指標較重視開盤價格,從而反映市場買賣的人氣;意願指標則重視收盤價格,反映的是市場買賣意願的程度,兩項指標分別從不同角度股價波動進行分析,達到追蹤股價未來動向的共同目的。
人氣指標是以當天開市價為基礎,即以當天市價分別比較當天最高,最低價,通過一定時期內開市價在股價中的地位,反映市場買賣人氣。
計算公式:
其中:H=當日最高價;L=當日最低價;O=當日開市價
N為公式中的設定參數,一般設定為26日。
人氣指標的基本應用法則:
(1)AR值以100為中心地帶,其±20之間,即AR值在80-120之間波動時,屬盤整行情,股價走勢比較平穩,不會出現劇烈波動。
(2)AR值走高時表示行情活躍,人氣旺盛,過高則表示股價進入高價,應選擇時機退出,AR值的高度沒有具體標準,一般情況下,AR值上升至150以上時,股價隨時可能回檔下跌。
(3)AR值走低時表示人氣衰退,需要充實,過低則暗示股價可能跌入低谷,可考慮伺機介入,一般AR值跌至70以下時,股價有可能隨時反彈上升。
(4)從AR曲線可以看出一段時期的買賣氣勢,並具有先於股價到達峰或跌入谷底的功能,觀圖時主要憑藉經驗,此策略配合MA一同使用。
(5)MA:在上升行情進入穩定期,短周期、中周期、長周期移動平均線從上而下依次順序排列,向右上方移動
在下跌行情中,短周期、中周期、長周期移動平均線自下而上依次順序排列,向右下方移動,稱為空頭 排列,預示股價將大幅下跌。
1.2策略邏輯
- AR值<75,且MA(5)>MA(10)>MA(30),買入
- AR值>130,且MA(5)<MA(10)<MA(30),賣出
2.策略代碼
2.1配置文件【ar_ma_stock.ini】(提示ini配置文件,需要保存成UTF8格式)
[strategy]nusername= npassword= n;回測模式nmode=4ntd_addr=localhost:8001nstrategy_id= nsubscribe_symbols=nn[backtest]nstart_time=2015-03-01 09:00:00nend_time=2017-08-07 15:00:00nn;策略初始資金ninitial_cash=1000000nn;委託量成交比率,默認=1(每個委託100%成交)ntransaction_ratio=1nn;手續費率,默認=0(不計算手續費)ncommission_ratio=0.0003nn;滑點比率,默認=0(無滑點)nslippage_ratio=0.00246nn;行情復權模式,0=不復權,1=前復權nprice_type=1nn;基準nbench_symbol=SHSE.000300nn[para]n;數據訂閱周期nbar_type=86400nn;AR周期nar_period=26nn;AR人氣指標nar_upr=130nar_dwn=75nnn;MA周期nshort_period=5nmid_period=10nlong_period=30nn#止盈止損n;是否固定止盈止損nis_fixation_stop=0n;是否移動止盈nis_movement_stop=1nn;移動盈利開始比率及固定盈利比率nstop_fixation_profit=0.25n;虧損比率nstop_fixation_loss=0.068nn;移動止盈比率nstop_movement_profit=0.068nnn;累計開倉距離當前的最大交易日n;若開倉距今超過這個日期,則認為未開過倉nopen_max_days=22nn;歷史數據長度nhist_size=60nn;開倉量nopen_vol=2000nn##############################################################n# logger settingsn##############################################################n[loggers]nkeys=rootnn[logger_root]nlevel=INFOnhandlers=filenn[handlers]nkeys=filenn[handler_file]nclass=handlers.RotatingFileHandlernargs=(ar_ma_stock.log,a,1000,5)nformatter=simplenn[handler_console]nclass=StreamHandlernargs = (sys.stdout,)nformatter=simplenn[formatters]nkeys = simplenn[formatter_simple]nformat=%(asctime)s - %(name)s - %(levelname)s - %(message)sndatefmt=n
2.2策略文件【ar_ma_stock.py】
#!/usr/bin/env pythonn# encoding: utf-8nnimport sysnimport loggingnimport logging.confignimport configparsernimport csvnimport numpy as npnimport datetimenimport talibnimport arrownfrom gmsdk import *nn##人氣指標(AR)介紹nn人氣指標是以當天開市價為基礎,即以當天市價分別比較當天最高、最低價,通過一定時期內開市價在股價中的地位,反映市場買賣人氣。nn其計算公式如下:nAR=N日內(當日最高價—當日開市價)之和 / N日內(當日開市價—當日最低價)之和nN為公式中的設定參數,一般設定為26日。n使用法則n(1)AR值以100為中心地帶,其±20之間,即AR值在80-120之間波動時,屬於盤整行情,股價走勢比較平穩,不會出現劇烈波動。n(2)AR值走高時表示行情活躍,人氣旺盛,過高則表示股價進入高價,應選擇時機退出,AR值的高度沒有具體標準,一般情況下,AR值上升至150以上時,股價隨時可能回檔下跌。n(3)AR值走低時表示人氣衰退,需要充實,過低則暗示股價可能跌入低谷,可考慮伺機介入,一般AR值跌至70以下時,股價有可能隨時反彈上升。n(4)從AR曲線可以看出一段時期的買賣氣勢,並具有先於股價到達峰或跌入谷底的功能, 結合MA指標一起使用。nMA:在上升行情進入穩定期,短周期、中周期、長周期移動平均線從上而下依次順序排列,向右上方移動n 在下跌行情中,短周期、中周期、長周期移動平均線自下而上依次順序排列,向右下方移動,稱為空頭排列,預示股價將大幅下跌。nnEPS = 1e-6nINIT_LOW_PRICE = 10000000.0nINIT_HIGH_PRICE = -1.0nINIT_CLOSE_PRICE = 0.0nINIT_OPEN_PRICE = 0.0nnnclass AR_MA_STOCK(StrategyBase):n cls_config = Nonen cls_user_name = Nonen cls_password = Nonen cls_mode = Nonen cls_td_addr = Nonen cls_strategy_id = Nonen cls_subscribe_symbols = Nonen cls_stock_pool = []nn cls_backtest_start = Nonen cls_backtest_end = Nonen cls_initial_cash = 1000000n cls_transaction_ratio = 1n cls_commission_ratio = 0.0n cls_slippage_ratio = 0.0n cls_price_type = 1n cls_bench_symbol = Nonenn def __init__(self, *args, **kwargs):n super(AR_MA_STOCK, self).__init__(*args, **kwargs)n self.cur_date = Nonen self.dict_price = {}n self.dict_open_close_signal = {}n self.dict_entry_high_low = {}n self.dict_last_factor = {}n self.dict_open_cum_days = {}nn @classmethodn def read_ini(cls, ini_name):n """n 功能:讀取策略配置文件n """n cls.cls_config = configparser.ConfigParser()n cls.cls_config.read(ini_name)nn @classmethodn def get_strategy_conf(cls):n """n 功能:讀取策略配置文件strategy段落的值n """n if cls.cls_config is None:n returnnn cls.cls_user_name = cls.cls_config.get(strategy, username)n cls.cls_password = cls.cls_config.get(strategy, password)n cls.cls_strategy_id = cls.cls_config.get(strategy, strategy_id)n cls.cls_subscribe_symbols = cls.cls_config.get(strategy, subscribe_symbols)n cls.cls_mode = cls.cls_config.getint(strategy, mode)n cls.cls_td_addr = cls.cls_config.get(strategy, td_addr)n if len(cls.cls_subscribe_symbols) <= 0:n cls.get_subscribe_stock()n else:n subscribe_ls = cls.cls_subscribe_symbols.split(,)n for data in subscribe_ls:n index1 = data.find(.)n index2 = data.find(., index1 + 1, -1)n cls.cls_stock_pool.append(data[:index2])nn returnnn @classmethodn def get_backtest_conf(cls):n """n 功能:讀取策略配置文件backtest段落的值n """n if cls.cls_config is None:n returnnn cls.cls_backtest_start = cls.cls_config.get(backtest, start_time)n cls.cls_backtest_end = cls.cls_config.get(backtest, end_time)n cls.cls_initial_cash = cls.cls_config.getfloat(backtest, initial_cash)n cls.cls_transaction_ratio = cls.cls_config.getfloat(backtest, transaction_ratio)n cls.cls_commission_ratio = cls.cls_config.getfloat(backtest, commission_ratio)n cls.cls_slippage_ratio = cls.cls_config.getfloat(backtest, slippage_ratio)n cls.cls_price_type = cls.cls_config.getint(backtest, price_type)n cls.cls_bench_symbol = cls.cls_config.get(backtest, bench_symbol)nn returnnn @classmethodn def get_stock_pool(cls, csv_file):n """n 功能:獲取股票池中的代碼n """n csvfile = open(csv_file, r)n reader = csv.reader(csvfile)n for line in reader:n cls.cls_stock_pool.append(line[0])nn returnnn @classmethodn def get_subscribe_stock(cls):n """n 功能:獲取訂閱代碼n """n cls.get_stock_pool(stock_pool.csv)n bar_type = cls.cls_config.getint(para, bar_type)n if 86400 == bar_type:n bar_type_str = .bar. + dailyn else:n bar_type_str = .bar. + %d % cls.cls_config.getint(para, bar_type)nn cls.cls_subscribe_symbols = ,.join(data + bar_type_str for data in cls.cls_stock_pool)n returnnn def utc_strtime(self, utc_time):n """n 功能:utc轉字元串時間n """n str_time = %s % arrow.get(utc_time).to(local)n str_time.replace(T, )n str_time = str_time.replace(T, )n return str_time[:19]nn def get_para_conf(self):n """n 功能:讀取策略配置文件para(自定義參數)段落的值n """n if self.cls_config is None:n returnnn self.ar_period = self.cls_config.getint(para, ar_period)n self.ar_upr = self.cls_config.getint(para, ar_upr)n self.ar_dwn = self.cls_config.getint(para, ar_dwn)nn self.short_period = self.cls_config.getint(para, short_period)n self.mid_period = self.cls_config.getint(para, mid_period)n self.long_period = self.cls_config.getint(para, long_period)nn self.hist_size = self.cls_config.getint(para, hist_size)n self.open_vol = self.cls_config.getint(para, open_vol)n self.open_max_days = self.cls_config.getint(para, open_max_days)nn self.is_fixation_stop = self.cls_config.getint(para, is_fixation_stop)n self.is_movement_stop = self.cls_config.getint(para, is_movement_stop)nn self.stop_fixation_profit = self.cls_config.getfloat(para, stop_fixation_profit)n self.stop_fixation_loss = self.cls_config.getfloat(para, stop_fixation_loss)nn self.stop_movement_profit = self.cls_config.getfloat(para, stop_movement_profit)nn returnnn def init_strategy(self):n """n 功能:策略啟動初始化操作n """n if self.cls_mode == gm.MD_MODE_PLAYBACK:n self.cur_date = self.cls_backtest_startn self.end_date = self.cls_backtest_endn else:n self.cur_date = datetime.date.today().strftime(%Y-%m-%d) + 08:00:00n self.end_date = datetime.date.today().strftime(%Y-%m-%d) + 16:00:00nn self.dict_open_close_signal = {}n self.dict_entry_high_low = {}n self.get_last_factor()n self.init_data()n self.init_entry_high_low()n returnnn def init_data(self):n """n 功能:獲取訂閱代碼的初始化數據n """n for ticker in self.cls_stock_pool:n # 初始化開倉操作信號字典n self.dict_open_close_signal.setdefault(ticker, False)nn daily_bars = self.get_last_n_dailybars(ticker, self.hist_size - 1, self.cur_date)n if len(daily_bars) <= 0:n continuenn end_daily_bars = self.get_last_n_dailybars(ticker, 1, self.end_date)n if len(end_daily_bars) <= 0:n continuenn if ticker not in self.dict_last_factor:n continuenn end_adj_factor = self.dict_last_factor[ticker]n open_ls = [data.open * data.adj_factor / end_adj_factor for data in daily_bars]n open_ls.reverse()n high_ls = [data.high * data.adj_factor / end_adj_factor for data in daily_bars]n high_ls.reverse()n low_ls = [data.low * data.adj_factor / end_adj_factor for data in daily_bars]n low_ls.reverse()n cp_ls = [data.close * data.adj_factor / end_adj_factor for data in daily_bars]n cp_ls.reverse()nn # 留出一個空位存儲當天的一筆數據n open_ls.append(INIT_OPEN_PRICE)n open = np.asarray(open_ls, dtype=np.float)n high_ls.append(INIT_HIGH_PRICE)n high = np.asarray(high_ls, dtype=np.float)n low_ls.append(INIT_LOW_PRICE)n low = np.asarray(low_ls, dtype=np.float)n cp_ls.append(INIT_CLOSE_PRICE)n close = np.asarray(cp_ls, dtype=np.float)nn # 存儲歷史的open high low closen self.dict_price.setdefault(ticker, [open, high, low, close])nn def init_data_newday(self):n """n 功能:新的一天初始化數據n """n # 新的一天,去掉第一筆數據,並留出一個空位存儲當天的一筆數據n for key in self.dict_price:n if len(self.dict_price[key][0]) >= self.hist_size and self.dict_price[key][0][-1] - INIT_OPEN_PRICE > EPS:n self.dict_price[key][0] = np.append(self.dict_price[key][0][1:], INIT_OPEN_PRICE)n elif len(self.dict_price[key][0]) < self.hist_size and self.dict_price[key][0][-1] - INIT_OPEN_PRICE > EPS:n # 未取足指標所需全部歷史數據時回測過程中補充數據n self.dict_price[key][0] = np.append(self.dict_price[key][0][:], INIT_HIGH_PRICE)nn if len(self.dict_price[key][1]) >= self.hist_size and abs(n self.dict_price[key][1][-1] - INIT_HIGH_PRICE) > EPS:n self.dict_price[key][1] = np.append(self.dict_price[key][1][1:], INIT_HIGH_PRICE)n elif len(self.dict_price[key][1]) < self.hist_size and abs(n self.dict_price[key][1][-1] - INIT_HIGH_PRICE) > EPS:n self.dict_price[key][1] = np.append(self.dict_price[key][1][:], INIT_HIGH_PRICE)nn if len(self.dict_price[key][2]) >= self.hist_size and abs(n self.dict_price[key][2][-1] - INIT_LOW_PRICE) > EPS:n self.dict_price[key][2] = np.append(self.dict_price[key][2][1:], INIT_LOW_PRICE)n elif len(self.dict_price[key][2]) < self.hist_size and abs(n self.dict_price[key][2][-1] - INIT_LOW_PRICE) > EPS:n self.dict_price[key][2] = np.append(self.dict_price[key][2][:], INIT_LOW_PRICE)nn if len(self.dict_price[key][3]) >= self.hist_size and abs(n self.dict_price[key][3][-1] - INIT_CLOSE_PRICE) > EPS:n self.dict_price[key][3] = np.append(self.dict_price[key][3][1:], INIT_CLOSE_PRICE)n elif len(self.dict_price[key][3]) < self.hist_size and abs(n self.dict_price[key][3][-1] - INIT_CLOSE_PRICE) > EPS:n self.dict_price[key][3] = np.append(self.dict_price[key][3][:], INIT_CLOSE_PRICE)nn # 初始化開倉操作信號字典n for key in self.dict_open_close_signal:n self.dict_open_close_signal[key] = Falsenn # 開倉後到當前的交易日天數n keys = list(self.dict_open_cum_days.keys())n for key in keys:n if self.dict_open_cum_days[key] >= self.open_max_days:n del self.dict_open_cum_days[key]n else:n self.dict_open_cum_days[key] += 1nn def cal_ar_index(self, ticker):n """n 功能:計算ar指標n """n ar_index = Nonenn if (len(self.dict_price[ticker][0]) < self.ar_period or abs(n self.dict_price[ticker][0][-1] - INIT_OPEN_PRICE) < EPS) n or (len(self.dict_price[ticker][1]) < self.ar_period or abs(n self.dict_price[ticker][1][-1] - INIT_HIGH_PRICE) < EPS) n or (len(self.dict_price[ticker][2]) < self.ar_period or abs(n self.dict_price[ticker][2][-1] - INIT_LOW_PRICE) < EPS) n or (len(self.dict_price[ticker][3]) < self.ar_period or abs(n self.dict_price[ticker][3][-1] - INIT_CLOSE_PRICE) < EPS):n # 歷史數據不足n return ar_indexnn open_ls = self.dict_price[ticker][0][len(self.dict_price[ticker][0]) - self.ar_period:]n high_ls = self.dict_price[ticker][1][len(self.dict_price[ticker][1]) - self.ar_period:]n low_ls = self.dict_price[ticker][2][len(self.dict_price[ticker][2]) - self.ar_period:]nn high_minus_ls = [a_b[0] - a_b[1] for a_b in zip(high_ls, open_ls)]n low_minus_ls = [a_b1[0] - a_b1[1] for a_b1 in zip(open_ls, low_ls)]nn ar_index = 0.0n for pos in range(len(high_minus_ls)):n if low_minus_ls[pos] > EPS:n ar_index += high_minus_ls[pos] / low_minus_ls[pos]nn return ar_indexnn def get_last_factor(self):n """n 功能:獲取指定日期最新的復權因子n """n for ticker in self.cls_stock_pool:n daily_bars = self.get_last_n_dailybars(ticker, 1, self.end_date)n if daily_bars is not None and len(daily_bars) > 0:n self.dict_last_factor.setdefault(ticker, daily_bars[0].adj_factor)nn def init_entry_high_low(self):n """n 功能:獲取進場後的最高價和最低價,模擬或實盤交易啟動時載入n """n pos_list = self.get_positions()n high_list = []n low_list = []n for pos in pos_list:n symbol = pos.exchange + . + pos.sec_idn init_time = self.utc_strtime(pos.init_time)nn cur_time = datetime.datetime.now().strftime(%Y-%m-%d %H:%M:%S)nn daily_bars = self.get_dailybars(symbol, init_time, cur_time)nn high_list = [bar.high for bar in daily_bars]n low_list = [bar.low for bar in daily_bars]nn if len(high_list) > 0:n highest = np.max(high_list)n else:n highest = pos.vwapnn if len(low_list) > 0:n lowest = np.min(low_list)n else:n lowest = pos.vwapnn self.dict_entry_high_low.setdefault(symbol, [highest, lowest])nn def on_bar(self, bar):n if self.cls_mode == gm.MD_MODE_PLAYBACK:n if bar.strtime[0:10] != self.cur_date[0:10]:n self.cur_date = bar.strtime[0:10] + 08:00:00n # 新的交易日n self.init_data_newday()nn symbol = bar.exchange + . + bar.sec_idnn self.movement_stop_profit_loss(bar)n self.fixation_stop_profit_loss(bar)nn pos = self.get_position(bar.exchange, bar.sec_id, OrderSide_Bid)nn # 補充當天價格n if symbol in self.dict_price:n self.dict_price[symbol][0][-1] = bar.opennn if self.dict_price[symbol][1][-1] < bar.high:n self.dict_price[symbol][1][-1] = bar.highnn if self.dict_price[symbol][2][-1] > bar.low:n self.dict_price[symbol][2][-1] = bar.lownn self.dict_price[symbol][3][-1] = bar.closenn if self.dict_open_close_signal[symbol] is False:n # 當天未有對該代碼開、平倉n if symbol in self.dict_price:nn ma_short = talib.MA(self.dict_price[symbol][3], self.short_period)n ma_mid = talib.MA(self.dict_price[symbol][3], self.mid_period)n ma_long = talib.MA(self.dict_price[symbol][3], self.long_period)nn ar_index = self.cal_ar_index(symbol)n if ar_index is not None:n if pos is None and symbol not in self.dict_open_cum_days n and (ar_index < self.ar_dwn and (ma_short[-1] > ma_mid[-1] and ma_mid[-1] > ma_long[-1])):n # 有開倉機會則設置已開倉的交易天數n self.dict_open_cum_days[symbol] = 0nn cash = self.get_cash()n cur_open_vol = self.open_voln if cash.available / bar.close > self.open_vol:n cur_open_vol = self.open_voln else:n cur_open_vol = int(cash.available / bar.close / 100) * 100nn if cur_open_vol == 0:n print(no available cash to buy, available cash: %.2f % cash.available)n else:n self.open_long(bar.exchange, bar.sec_id, bar.close, cur_open_vol)n self.dict_open_close_signal[symbol] = Truen logging.info(open long, symbol:%s, time:%s, price:%.2f % (symbol, bar.strtime, bar.close))n elif pos is not None and (n ar_index > self.ar_upr and (ma_short[-1] < ma_mid[-1] and ma_mid[-1] < ma_long[-1])):n vol = pos.volume - pos.volume_todayn if vol > 0:n self.close_long(bar.exchange, bar.sec_id, bar.close, vol)n self.dict_open_close_signal[symbol] = Truen logging.info(n close long, symbol:%s, time:%s, price:%.2f % (symbol, bar.strtime, bar.close))n # print close long, symbol:%s, time:%s %(symbol, bar.strtime)nn def on_order_filled(self, order):n symbol = order.exchange + . + order.sec_idn if order.position_effect == PositionEffect_CloseYesterday n and order.side == OrderSide_Bid:n pos = self.get_position(order.exchange, order.sec_id, order.side)n if pos is None and self.is_movement_stop == 1:n self.dict_entry_high_low.pop(symbol)nn def fixation_stop_profit_loss(self, bar):n """n 功能:固定止盈、止損,盈利或虧損超過了設置的比率則執行止盈、止損n """n if self.is_fixation_stop == 0:n returnnn symbol = bar.exchange + . + bar.sec_idn pos = self.get_position(bar.exchange, bar.sec_id, OrderSide_Bid)n if pos is not None:n if pos.fpnl > 0 and pos.fpnl / pos.cost >= self.stop_fixation_profit:n self.close_long(bar.exchange, bar.sec_id, 0, pos.volume - pos.volume_today)n self.dict_open_close_signal[symbol] = Truen logging.info(n fixnation stop profit: close long, symbol:%s, time:%s, price:%.2f, vwap: %s, volume:%s % (symbol,n bar.strtime,n bar.close,n pos.vwap,n pos.volume))n elif pos.fpnl < 0 and pos.fpnl / pos.cost <= -1 * self.stop_fixation_loss:n self.close_long(bar.exchange, bar.sec_id, 0, pos.volume - pos.volume_today)n self.dict_open_close_signal[symbol] = Truen logging.info(n fixnation stop loss: close long, symbol:%s, time:%s, price:%.2f, vwap:%s, volume:%s % (symbol,n bar.strtime,n bar.close,n pos.vwap,n pos.volume))nn def movement_stop_profit_loss(self, bar):n """n 功能:移動止盈, 移動止盈止損按進場後的最高價乘以設置的比率與當前價格相比,n 並且盈利比率達到設定的盈虧比率時,執行止盈n """n if self.is_movement_stop == 0:n returnnn entry_high = Nonen entry_low = Nonen pos = self.get_position(bar.exchange, bar.sec_id, OrderSide_Bid)n symbol = bar.exchange + . + bar.sec_idnn is_stop_profit = Truenn if pos is not None and pos.volume > 0:n if symbol in self.dict_entry_high_low:n if self.dict_entry_high_low[symbol][0] < bar.close:n self.dict_entry_high_low[symbol][0] = bar.closen is_stop_profit = Falsen if self.dict_entry_high_low[symbol][1] > bar.close:n self.dict_entry_high_low[symbol][1] = bar.closen [entry_high, entry_low] = self.dict_entry_high_low[symbol]nn else:n self.dict_entry_high_low.setdefault(symbol, [bar.close, bar.close])n [entry_high, entry_low] = self.dict_entry_high_low[symbol]n is_stop_profit = Falsenn if is_stop_profit:n # 移動止盈n if bar.close <= (n 1 - self.stop_movement_profit) * entry_high and pos.fpnl / pos.cost >= self.stop_fixation_profit:n if pos.volume - pos.volume_today > 0:n self.close_long(bar.exchange, bar.sec_id, 0, pos.volume - pos.volume_today)n self.dict_open_close_signal[symbol] = Truen logging.info(n movement stop profit: close long, symbol:%s, time:%s, price:%.2f, vwap:%.2f, volume:%s % (n symbol,n bar.strtime, bar.close, pos.vwap, pos.volume))nn # 止損n if pos.fpnl < 0 and pos.fpnl / pos.cost <= -1 * self.stop_fixation_loss:n self.close_long(bar.exchange, bar.sec_id, 0, pos.volume - pos.volume_today)n self.dict_open_close_signal[symbol] = Truen logging.info(n movement stop loss: close long, symbol:%s, time:%s, price:%.2f, vwap:%.2f, volume:%s % (symbol,n bar.strtime,n bar.close,n pos.vwap,n pos.volume))nnnif __name__ == __main__:n print(get_version())n logging.config.fileConfig(ar_ma_stock.ini)n AR_MA_STOCK.read_ini(ar_ma_stock.ini)n AR_MA_STOCK.get_strategy_conf()nn ar_ma_stock = AR_MA_STOCK(username=AR_MA_STOCK.cls_user_name,n password=AR_MA_STOCK.cls_password,n strategy_id=AR_MA_STOCK.cls_strategy_id,n subscribe_symbols=AR_MA_STOCK.cls_subscribe_symbols,n mode=AR_MA_STOCK.cls_mode,n td_addr=AR_MA_STOCK.cls_td_addr)nn if AR_MA_STOCK.cls_mode == gm.MD_MODE_PLAYBACK:n AR_MA_STOCK.get_backtest_conf()n ret = ar_ma_stock.backtest_config(start_time=AR_MA_STOCK.cls_backtest_start,n end_time=AR_MA_STOCK.cls_backtest_end,n initial_cash=AR_MA_STOCK.cls_initial_cash,n transaction_ratio=AR_MA_STOCK.cls_transaction_ratio,n commission_ratio=AR_MA_STOCK.cls_commission_ratio,n slippage_ratio=AR_MA_STOCK.cls_slippage_ratio,n price_type=AR_MA_STOCK.cls_price_type,n bench_symbol=AR_MA_STOCK.cls_bench_symbol)nn ar_ma_stock.get_para_conf()n ar_ma_stock.init_strategy()n ret = ar_ma_stock.run()nnnprint(run result %s % ret)n
2.3股票池文件【stock_pool.csv】
SHSE.600000nSHSE.600010nSHSE.600011nSHSE.600015nSHSE.600016nSHSE.600018nSHSE.600019nSHSE.600023nSHSE.600028nSHSE.600030nSHSE.600031nSHSE.600036nSHSE.600048nSHSE.600050nSHSE.600104nSHSE.600111nSHSE.600115nSHSE.600150nSHSE.600276nSHSE.600340nSHSE.600372nSHSE.600398nSHSE.600485nSHSE.600518nSHSE.600519nSHSE.600585nSHSE.600637nSHSE.600690nSHSE.600705nSHSE.600795nSHSE.600837nSHSE.600886nSHSE.600887nSHSE.600893nSHSE.600900nSHSE.600958nSHSE.600959nSHSE.600999nSHSE.601006nSHSE.601018nSHSE.601088nSHSE.601111nSHSE.601166nSHSE.601169nSHSE.601186nSHSE.601211nSHSE.601225nSHSE.601288nSHSE.601318nSHSE.601328nSHSE.601336nSHSE.601377nSHSE.601390nSHSE.601398nSHSE.601600nSHSE.601601nSHSE.601618nSHSE.601628nSHSE.601633nSHSE.601668nSHSE.601669nSHSE.601688nSHSE.601727nSHSE.601766nSHSE.601788nSHSE.601800nSHSE.601808nSHSE.601818nSHSE.601857nSHSE.601898nSHSE.601899nSHSE.601901nSHSE.601939nSHSE.601985nSHSE.601988nSHSE.601989nSHSE.601998nSHSE.603288nSZSE.000001nSZSE.000002nSZSE.000063nSZSE.000069nSZSE.000166nSZSE.000333nSZSE.000538nSZSE.000625nSZSE.000651nSZSE.000725nSZSE.000776nSZSE.000858nSZSE.000895nSZSE.002024nSZSE.002252nSZSE.002304nSZSE.002415nSZSE.002594nSZSE.002736nSZSE.002739nSZSE.300059nSZSE.300104n
如果想了解相關的python函數和掘金介面函數,走下方通道:
http://zjshe.cn/q/forum.php?mod=viewthread&tid=107&extra=page%3D1
有不了解的可以給小編留言哦,小編會細心為大家解答~
推薦閱讀:
※雪球殺豬榜里的趨勢突破策略
※哈工大智能薦股靠譜嗎?你怎麼看?
※Python入門到精通視頻課程(11)
※【翻譯搬運】Matplotlib - 用Python繪製2D和3D圖像
※『事件驅動』指數成份股調整帶來的投資機會