Ejemplo n.º 1
0
    class DoubleMaStrategy(LooperApi):

        allow_max_price = 5000  # 设置价格上限 当价格达到这个就卖出 防止突然跌 止盈
        allow_low_price = 2000  # 设置价格下限 当价格低出这里就卖 防止巨亏 止损

        def __init__(self, name):
            super().__init__(name)
            self.count = 1
            self.api = Interface()
            self.api.open_json("../zn1912.SHFE.json")
            self.pos = 0

        def on_bar(self, bar):
            # todo: 均线
            """ """
            am = self.api
            am.add_bar(bar)
            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):
            """"""
Ejemplo n.º 2
0
    class DoubleMaStrategy(LooperApi):

        allow_max_price = 5000  # 设置价格上限 当价格达到这个就卖出 防止突然跌 止盈
        allow_low_price = 2000  # 设置价格下限 当价格低出这里就卖 防止巨亏 止损

        def __init__(self, name):
            super().__init__(name)
            self.count = 1
            self.api = Interface()
            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)
            # 收盘
            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 init_params(self, data):
            """"""
Ejemplo n.º 3
0
    class SmaStrategy(LooperApi):
        def __init__(self, name):
            super().__init__(name)
            self.count = 1
            self.pos = 0

            self.bar_3 = Interface()  # 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)

            top, middle, bottom = self.bar_3.boll()

            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 bar.close_price > middle[-1]:
                # 没有就买
                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 bar.close_price <= middle[-1]:
                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):
            """"""
Ejemplo n.º 4
0
    class DoubleMaStrategy(LooperApi):

        allow_max_price = 5000  # 设置价格上限 当价格达到这个就卖出 防止突然跌 止盈
        allow_low_price = 2000  # 设置价格下限 当价格低出这里就卖 防止巨亏 止损

        def __init__(self, name):
            super().__init__(name)
            self.count = 1
            self.api = Interface()
            self.api.open_json("../zn1912.SHFE.json")
            self.pos = 0

        def on_bar(self, bar):
            # todo: 均线
            """ """
            am = self.api
            am.add_bar(bar)
            # 收盘
            close = am.close
            # 允许最大价格小于当前收盘价
            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 close[-1] > close[-2] > close[-3]:
                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 close[-1] < close[-2] < close[-3]:
                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):
            """"""
Ejemplo n.º 5
0
    class SmaStrategy(LooperApi):
        def __init__(self, name):
            super().__init__(name)
            self.count = 1
            self.pos = 0

            self.bar_3 = Interface()  # 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)

            mtm = self.bar_3.mtm()

            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 mtm[-1] > 0 and mtm[-1] > mtm[-2]:
                # 没有就买
                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

        def init_params(self, data):
            """"""
Ejemplo n.º 6
0
    class SmaStrategy(LooperApi):

        def __init__(self, name):
            super().__init__(name)
            self.count = 1
            self.pos = 0

            self.bar_3 = Interface()  # 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)
            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):
            """"""
Ejemplo n.º 7
0
    class SmaStrategy(LooperApi):

        def __init__(self, name):
            super().__init__(name)
            self.count = 1
            self.pos = 0

            self.bar_3 = Interface()  # 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: 简单平均指标 SMMA
            """ """
            self.bar_3.add_bar(bar)

            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)

            ###############
            #   暂时不写   #
            ###############
            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):
            """"""