class DoubleMaStrategy(LooperApi): allow_max_price = 5000 # 设置价格上限 当价格达到这个就卖出 防止突然跌 止盈 allow_low_price = 2000 # 设置价格下限 当价格低出这里就卖 防止巨亏 止损 def __init__(self, name): super().__init__(name) self.count = 1 self.api = Indicator() self.instrument_set = ['rb2001.SHFE'] self.api.open_json("zn1912.SHFE.json") self.pos = 0 def on_bar(self, bar): # todo: 均线 和 MACD 和 BOLL 结合使用 """ """ am = self.api am.add_bar(bar) if not am.inited: return # 收盘 close = am.close # 压力 平均 支撑 # top, middle, bottom = am.boll() # DIF DEA DIF-DEA macd, signal, histo = am.macd() if self.allow_max_price <= close[-1] and self.pos > 0: self.action.sell(bar.close_price, self.pos, bar) if self.allow_low_price >= close[-1] and self.pos > 0: self.action.sell(bar.close_price, self.pos, bar) # 金叉做多 和 均线>平均 if histo[-1] > 0: if self.pos == 0: self.action.buy(bar.close_price, 1, bar) elif self.pos < 0: self.action.cover(bar.close_price, 1, bar) self.action.buy(bar.close_price, 1, bar) # 死叉做空 elif histo[-1] < 0: if self.pos == 0: pass elif self.pos > 0: self.action.sell(bar.close_price, 1, bar) self.action.short(bar.close_price, 1, bar) def on_trade(self, trade): if trade.direction == Direction.LONG: self.pos += trade.volume else: self.pos -= trade.volume def on_order(self, order): pass def init_params(self, data): """"""
class DoubleMaStrategy(LooperApi): allow_max_price = 5000 # 设置价格上限 当价格达到这个就卖出 防止突然跌 止盈 allow_low_price = 2000 # 设置价格下限 当价格低出这里就卖 防止巨亏 止损 def __init__(self, name): super().__init__(name) self.count = 1 self.api = Indicator() self.api.open_json("../zn1912.SHFE.json") self.pos = 0 def on_bar(self, bar): # todo: 均线 """ """ am = self.api am.add_bar(bar) if not am.inited: return cci = am.cci() pass def on_trade(self, trade): if trade.direction == Direction.LONG: self.pos += trade.volume else: self.pos -= trade.volume def init_params(self, data): """"""
class SmaStrategy(LooperApi): def __init__(self, name): super().__init__(name) self.count = 1 self.pos = 0 self.bar_3 = Indicator() # 3分钟bar线 self.bar_3.open_json('../zn1912.SHFE.json') # 读取本地数据 self.allow_max_price = 5000 # 设置价格上限 当价格达到这个就卖出 防止突然跌 self.allow_low_price = 2000 # 设置价格下限 当价格低出这里就卖 防止巨亏 def on_bar(self, bar): # todo: 移动平均线 """ """ self.bar_3.add_bar(bar) if not self.bar_3.inited: return ema = self.bar_3.ema() if self.allow_max_price < bar.close_price and self.pos > 0: self.action.sell(bar.close_price, self.pos, bar) if self.allow_low_price > bar.close_price and self.pos > 0: self.action.sell(bar.close_price, self.pos, bar) # 均线大于中线 if ema[-1] > ema[-2] and ema[-1] < bar.close_price: # 没有就买 if self.pos == 0: self.action.buy(bar.close_price, 1, bar) if self.pos < 0: self.action.cover(bar.close_price, 1, bar) self.action.buy(bar.close_price, 1, bar) # 跌 elif ema[-1] < ema[-2] or ema[-1] > bar.close_price: if self.pos == 0: pass elif self.pos > 0: self.action.sell(bar.close_price, 1, bar) self.action.short(bar.close_price, 1, bar) def on_trade(self, trade): if trade.direction == Direction.LONG: self.pos += trade.volume else: self.pos -= trade.volume def init_params(self, data): """"""
class SmaStrategy(LooperApi): def __init__(self, name): super().__init__(name) self.count = 1 self.pos = 0 self.bar_3 = Indicator() # 3分钟bar线 self.bar_3.open_json('../zn1912.SHFE.json') # 读取本地数据 self.allow_max_price = 5000 # 设置价格上限 当价格达到这个就卖出 防止突然跌 self.allow_low_price = 2000 # 设置价格下限 当价格低出这里就卖 防止巨亏 def on_bar(self, bar): # todo: 抛物线指标 SAR """ """ self.bar_3.add_bar(bar) if not self.bar_3.inited: return sar = self.bar_3.sar() if self.allow_max_price < bar.close_price and self.pos > 0: self.action.sell(bar.close_price, self.pos, bar) if self.allow_low_price > bar.close_price and self.pos > 0: self.action.sell(bar.close_price, self.pos, bar) # 接连涨 if sar[-1]['sar'] > sar[-2]['sar']: # 没有就买 if self.pos == 0: self.action.buy(bar.close_price, 1, bar) elif self.pos < 0: self.action.cover(bar.close_price, 1, bar) self.action.buy(bar.close_price, 1, bar) else: if self.pos > 0: self.action.sell(bar.close_price, 1, bar) self.action.short(bar.close_price, 1, bar) def on_trade(self, trade): if trade.direction == Direction.LONG: self.pos += trade.volume else: self.pos -= trade.volume print(self.pos, '-----------') def init_params(self, data): """"""
class SmaStrategy(LooperApi): def __init__(self, name): super().__init__(name) self.count = 1 self.pos = 0 self.bar_3 = Indicator() # 3分钟bar线 self.bar_3.open_json('../zn1912.SHFE.json') # 读取本地数据 self.allow_max_price = 5000 # 设置价格上限 当价格达到这个就卖出 防止突然跌 self.allow_low_price = 2000 # 设置价格下限 当价格低出这里就卖 防止巨亏 def on_bar(self, bar): # todo: 随机指标 """ """ self.bar_3.add_bar(bar) if not self.bar_3.inited: return close = self.bar_3.close k, d = self.bar_3.kd() if self.allow_max_price < close[-1] and self.pos > 0: self.action.sell(bar.close_price, self.pos, bar) if self.allow_low_price > close[-1] and self.pos > 0: self.action.sell(bar.close_price, self.pos, bar) # 金叉 if k[-2] > d[-2] and k[-1] == d[-1]: # 没有就买 if self.pos == 0: self.action.buy(bar.close_price, 1, bar) # 死叉 if k[-2] == d[-2] and k[-1] < d[-1]: if self.pos > 0: self.action.sell(bar.close_price, 1, bar) def on_trade(self, trade): if trade.direction == Direction.LONG: self.pos += trade.volume else: self.pos -= trade.volume def init_params(self, data): """"""
class SmaStrategy(LooperApi): def __init__(self, name): super().__init__(name) self.count = 1 self.pos = 0 self.bar_3 = Indicator() # 3分钟bar线 self.bar_3.open_json('../zn1912.SHFE.json') # 读取本地数据 self.allow_max_price = 5000 # 设置价格上限 当价格达到这个就卖出 防止突然跌 self.allow_low_price = 2000 # 设置价格下限 当价格低出这里就卖 防止巨亏 def on_bar(self, bar): # todo: 标准偏差 stdDev """ """ self.bar_3.add_bar(bar) if not self.bar_3.inited: return stdDev = self.bar_3.stdDev() if self.allow_max_price < bar.close_price and self.pos > 0: self.action.sell(bar.close_price, self.pos, bar) if self.allow_low_price > bar.close_price and self.pos > 0: self.action.sell(bar.close_price, self.pos, bar) ############ # 暂时不写 # ########### pass def on_trade(self, trade): if trade.direction == Direction.LONG: self.pos += trade.volume else: self.pos -= trade.volume print(self.pos, '-----------') def init_params(self, data): """"""