@Channelchan
2018-11-12T06:16:17.000000Z
字数 4222
阅读 37487
1/3,1/3,1/3
import rqalphafrom rqalpha.api import *import talibdef init(context):context.s1 = "000001.XSHE"context.SHORTPERIOD = 10context.LONGPERIOD = 30context.time = 0def handle_bar(context, bar_dict):prices = history_bars(context.s1, context.LONGPERIOD+1, '1d', 'close')short_avg = talib.SMA(prices, context.SHORTPERIOD)long_avg = talib.SMA(prices, context.LONGPERIOD)cur_position = context.portfolio.positions[context.s1].quantityshares = context.portfolio.cash/bar_dict[context.s1].closeif short_avg[-1] - long_avg[-1] < 0 and short_avg[-2] - long_avg[-2] > 0 and cur_position > 0:order_target_value(context.s1, 0)context.time=0if short_avg[-1] - long_avg[-1] > 0 and short_avg[-2] - long_avg[-2] < 0 and context.time==0:order_shares(context.s1, shares/3)context.time=1elif context.time == 1:order_shares(context.s1, shares/3)context.time=2elif context.time == 2:order_shares(context.s1, shares/3)context.time=3config = {"base": {"start_date": "2000-06-01","end_date": "2016-12-01","accounts": {'stock': 1000000},"benchmark": "000300.XSHG"# "strategy_file_path": os.path.abspath(__file__)},"extra": {"log_level": "verbose",},"mod": {"sys_analyser": {"report_save_path": '.',"enabled": True,"plot": True}}}rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)
import rqalphafrom rqalpha.api import *import talibdef init(context):context.s1 = "000001.XSHE"context.SHORTPERIOD = 10context.LONGPERIOD = 30context.time = 0def handle_bar(context, bar_dict):prices = history_bars(context.s1, context.LONGPERIOD+1, '1d', 'close')short_avg = talib.SMA(prices, context.SHORTPERIOD)long_avg = talib.SMA(prices, context.LONGPERIOD)cur_position = context.portfolio.positions[context.s1].quantityshares = context.portfolio.cash/bar_dict[context.s1].closeif short_avg[-1] - long_avg[-1] < 0 and short_avg[-2] - long_avg[-2] > 0 and cur_position > 0:order_target_value(context.s1, 0)context.time=0if short_avg[-1] - long_avg[-1] > 0 and short_avg[-2] - long_avg[-2] < 0 and context.time==0:order_shares(context.s1, shares*0.5)context.time=1elif context.time == 1:order_shares(context.s1, shares*0.3)context.time=2elif context.time == 2:order_shares(context.s1, shares*0.2)context.time=3config = {"base": {"start_date": "2000-06-01","end_date": "2016-12-01","accounts": {'stock': 1000000},"benchmark": "000300.XSHG"# "strategy_file_path": os.path.abspath(__file__)},"extra": {"log_level": "verbose",},"mod": {"sys_analyser": {"report_save_path": '.',"enabled": True,"plot": True}}}rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)
50/30/20/-50/-30/-20
import rqalphafrom rqalpha.api import *from queue import Queueimport talibdef init(context):context.s1 = "000001.XSHE"context.SHORTPERIOD = 10context.LONGPERIOD = 30context.pos = [5000, 3000, 2000]context.queue = Queue()context.buy=0context.sell=0def buy(context):prices = history_bars(context.s1, context.LONGPERIOD+1, '1d', 'close')short_avg = talib.SMA(prices, context.SHORTPERIOD)long_avg = talib.SMA(prices, context.LONGPERIOD)if short_avg[-1] - long_avg[-1] > 0 and short_avg[-2] - long_avg[-2] < 0:return Truedef sell(context):prices = history_bars(context.s1, context.LONGPERIOD+1, '1d', 'close')short_avg = talib.SMA(prices, context.SHORTPERIOD)long_avg = talib.SMA(prices, context.LONGPERIOD)cur_position = context.portfolio.positions[context.s1].quantityif short_avg[-1] - long_avg[-1] < 0 and short_avg[-2] - long_avg[-2] > 0 and cur_position > 0:return Truedef handle_bar(context, bar_dict):size = context.queue.qsize()if size == 0:if buy(context):context.buy=1volume = context.pos[size]## order volumeorder_shares(context.s1, volume)context.queue.put(volume)returnif context.buy:if sell(context):context.sell=1context.buy=0elif size < 3:volume = context.pos[size]## order volumeorder_shares(context.s1, volume)context.queue.put(volume)if context.sell:if size > 0:volume = context.queue.get()## sell volumeorder_shares(context.s1, -volume)else:context.sell=0config = {"base": {"start_date": "2000-06-01","end_date": "2016-12-01","accounts": {'stock': 1000000},"benchmark": "000300.XSHG"# "strategy_file_path": os.path.abspath(__file__)},"extra": {"log_level": "verbose",},"mod": {"sys_analyser": {"report_save_path": '.',"enabled": True,"plot": True}}}rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)
亏损加仓来平均持有成本,这种策略胜率需要很高。
r_w:赢赚比例
r_l: 输赔比例
p: 赢概率p
f: 投注比例
