Ejemplo n.º 1
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.º 2
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.º 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)
            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.º 4
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):
            """"""