How to use the xalpha.policy.policy function in xalpha

To help you get started, we’ve selected a few xalpha examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github refraction-ray / xalpha / xalpha / policy.py View on Github external
始终选择分红再投入
    """

    def status_gen(self, date):
        if date == self.start:
            return self.totmoney
        elif date in self.aim.specialdate:
            if self.price[self.price["date"] == date].iloc[0].comment > 0:
                return 0.05
            else:
                return 0
        else:
            return 0


class scheduled(policy):
    """
    fixed schduled purchase for given date list

    :param infoobj: info obj
    :param totmoney: float, money value for purchase every time
    :param times: datelist of datetime object for purchase date, eg ['2017-01-01','2017-07-07',...]
        we recommend you use pd.date_range() to generate the schduled list
    """

    def __init__(self, infoobj, totmoney, times):
        start = times[0]
        end = times[-1]
        self.times = times
        super().__init__(infoobj, start, end, totmoney)

    def status_gen(self, date):
github refraction-ray / xalpha / xalpha / policy.py View on Github external
"""
        self.piece = piece
        super().__init__(infoobj, totmoney, times)

    def status_gen(self, date):
        if date in self.times:
            value = self.price[self.price["date"] >= date].iloc[0].netvalue
            for term in self.piece:
                if value <= term[0]:
                    return term[1] * self.totmoney
            return 0
        else:
            return 0


class grid(policy):
    """
    网格投资类,用于指导网格投资策略的生成和模拟。这一简单的网格,买入仓位基于均分总金额,每次的卖出仓位基于均分总份额。
    因此实际上每次卖出的份额都不到对应原来档位买入的份额,从而可能实现更多的收益。

    :param infoobj: info object, trading aim of the grid policy
    :param buypercent: list of positive int or float, the grid of points when purchasing, in the unit of percent
        比如 [5,5,5,5] 表示以 start 这天的价格为基准,每跌5%,就加一次仓,总共加四次仓
    :param sellpercent: list of positive int or float, the grid of points for selling
        比如 [8,8,8,8] 分别对应上面各买入仓位应该涨到的百分比从而决定卖出的点位,两个列表都是靠前的是高价位仓,两列表长度需一致
    :param start: date str of policy starting
    :param end: date str of policy ending
    :param totmoney: 总钱数,平均分给各个网格买入仓位
    """

    def __init__(
        self,
github refraction-ray / xalpha / xalpha / policy.py View on Github external
return 0
        value = self.price[self.price["date"] <= date].iloc[-1].loc["netvalue"]
        valueb = self.price[self.price["date"] <= date].iloc[-2].loc["netvalue"]
        action = 0
        for i, buypt in enumerate(self.buypts):
            if (value - buypt) <= 0 and (valueb - buypt) > 0 and self.pos <= i:
                self.pos += 1
                action += myround(self.totmoney / self.division)
        for j, sellpt in enumerate(self.sellpts):
            if (value - sellpt) >= 0 and (valueb - sellpt) < 0 and self.pos > j:
                action += -1 / self.pos
                self.pos += -1
        return action


class indicator_cross(policy):
    """
    制定两个任意技术指标之间(或和净值之间)交叉时买入卖出的策略。若收盘时恰好交叉,不操作,等第二日趋势确认。

    :param info: info object, trading aim of the policy
    :param col: a tuple with two strings, eg ('netvalue','MA10'), when the left one is over the 
        right one, we buy and otherwise we sell, that is the core of cross policy, you can choose 
        any two columns as you like, as long as you generate them on the info object before input
        也即左栏数据从下向上穿过右栏数据时,买入;反之亦然
    :param start: date str of policy starting
    :param end: date str of policy ending
    :param totmoney: float or int, total money, in the cross policy, we dont have position division,
        instead we buy all or sell all on the given cross
    """

    def __init__(self, infoobj, col, start, end=yesterdaydash(), totmoney=100000):
        self.col = col
github refraction-ray / xalpha / xalpha / policy.py View on Github external
datel.append(date)
                    actionl.append(action * 0.005)
            df = pd.DataFrame(data={"date": datel, self.aim.code: actionl})
            self.status = df

    def status_gen(self, date):
        """
        give policy decision based on given date

        :param date: date object
        :returns: float, positive for buying money, negative for selling shares
        """
        raise NotImplementedError


class buyandhold(policy):
    """
    simple policy class where buy at the start day and hold forever,
    始终选择分红再投入
    """

    def status_gen(self, date):
        if date == self.start:
            return self.totmoney
        elif date in self.aim.specialdate:
            if self.price[self.price["date"] == date].iloc[0].comment > 0:
                return 0.05
            else:
                return 0
        else:
            return 0
github refraction-ray / xalpha / xalpha / policy.py View on Github external
if (cond == 0 and (valuer - valuel) != 0) or cond < 0:
            if valuer > valuel:
                if self.pos == 1:
                    self.pos = 0
                    return -1
                else:
                    return 0
            else:
                if self.pos == 0:
                    self.pos = 1
                    return self.totmoney
                else:
                    return 0


class indicator_points(policy):
    """
    基于技术指标的策略生成类之一,给出技术指标的多个阈值,基于这些点数值进行交易

    :param infoobj: info object, trading aim of the policy
    :param col: str, stands for the tracking column of price table, eg. 'netvalue' or 'PSY'
    :param buy: list of tuple, eg [(0.1,1),(0.2,2),(0.3,5)]. buy 1/(1+2+5) of totmoney, when the col
        value approach 0.1 and so on.
    :param sell: similar list of tuple as buy input. the difference is you can omit setting of sell list,
        this implies you don't want to sell. 初始化不设置sell参数,在col设为netvalue时,用于进行金字塔底仓购买特别有效.
        注意不论是 sell 还是 buy 列表,都要将更难实现(离中间值更远)的点位列在后边。比如如果现在是低买模式,
        那么 buy 列表越考后的点数就越小。此外,不建议设置的买点卖点有重叠区域,可能会出现策略逻辑错误。
    :param buylow: Bool, Ture 代表,对应点位是跌破买,涨破卖,如果是 False 则反之,默认是 True
    :param start: date str of policy starting
    :param end: date str of policy ending
    :param totmoney: float or int, total money, in the points policy, we share them as different positions, based on
        the instruction of sell and buy list