@Morningstar @菜菜午出头 @莫邪的救赎
大哥,帮忙加下止盈止损策略,万分感谢,我还不会写代码:
止损:亏损百分之十卖出止损
止盈:当前价格为20日均线的0.98倍就卖出止盈
原策略里面的在下面,如何更改请各位大侠帮忙,万分感谢在线等
```
```
# 个股止损
def stock_stop_loss(context, data):
for stock in context.portfolio.positions.keys():
cur_price = data[stock].close
if g.last_high[stock] < cur_price:
g.last_high[stock] = cur_price
threshold = get_stop_loss_threshold(stock, g.period)
#log.debug("个股止损阈值, stock: %s, threshold: %f" %(stock, threshold))
if cur_price < g.last_high[stock] * (1 - threshold):
log.info("==> 个股止损, stock: %s, cur_price: %f, last_high: %f, threshold: %f"
%(stock, cur_price, g.last_high[stock], threshold))
position = context.portfolio.positions[stock]
if close_position(position):
g.day_count = 0
# 个股止盈
def stock_stop_profit(context, data):
for stock in context.portfolio.positions.keys():
position = context.portfolio.positions[stock]
cur_price = data[stock].close
threshold = get_stop_profit_threshold(stock, g.period)
#log.debug("个股止盈阈值, stock: %s, threshold: %f" %(stock, threshold))
if cur_price > position.avg_cost * (1 + threshold):
log.info("==> 个股止盈, stock: %s, cur_price: %f, avg_cost: %f, threshold: %f"
%(stock, cur_price, g.last_high[stock], threshold))
position = context.portfolio.positions[stock]
if close_position(position):
g.day_count = 0
```
```
2016-09-07