@Channelchan
2018-11-12T14:16:17.000000Z
字数 4222
阅读 37324
1/3,1/3,1/3
import rqalpha
from rqalpha.api import *
import talib
def init(context):
context.s1 = "000001.XSHE"
context.SHORTPERIOD = 10
context.LONGPERIOD = 30
context.time = 0
def 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].quantity
shares = context.portfolio.cash/bar_dict[context.s1].close
if 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=0
if 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=1
elif context.time == 1:
order_shares(context.s1, shares/3)
context.time=2
elif context.time == 2:
order_shares(context.s1, shares/3)
context.time=3
config = {
"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 rqalpha
from rqalpha.api import *
import talib
def init(context):
context.s1 = "000001.XSHE"
context.SHORTPERIOD = 10
context.LONGPERIOD = 30
context.time = 0
def 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].quantity
shares = context.portfolio.cash/bar_dict[context.s1].close
if 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=0
if 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=1
elif context.time == 1:
order_shares(context.s1, shares*0.3)
context.time=2
elif context.time == 2:
order_shares(context.s1, shares*0.2)
context.time=3
config = {
"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 rqalpha
from rqalpha.api import *
from queue import Queue
import talib
def init(context):
context.s1 = "000001.XSHE"
context.SHORTPERIOD = 10
context.LONGPERIOD = 30
context.pos = [5000, 3000, 2000]
context.queue = Queue()
context.buy=0
context.sell=0
def 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 True
def 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].quantity
if short_avg[-1] - long_avg[-1] < 0 and short_avg[-2] - long_avg[-2] > 0 and cur_position > 0:
return True
def handle_bar(context, bar_dict):
size = context.queue.qsize()
if size == 0:
if buy(context):
context.buy=1
volume = context.pos[size]
## order volume
order_shares(context.s1, volume)
context.queue.put(volume)
return
if context.buy:
if sell(context):
context.sell=1
context.buy=0
elif size < 3:
volume = context.pos[size]
## order volume
order_shares(context.s1, volume)
context.queue.put(volume)
if context.sell:
if size > 0:
volume = context.queue.get()
## sell volume
order_shares(context.s1, -volume)
else:
context.sell=0
config = {
"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: 投注比例