Example #1
0
    def __init__(self, symbol, strategy, buyingRatio):
        ''' constructor '''
        self.__symbol = symbol
        self.__strategy = strategy
        self.__startDate = strategy.startDate
        self.__buyingRatio = buyingRatio
        self.__buyThreshold = -2
        self.__sellThreshold = 0.5
        self.__priceZscore = ZScore(120)
        self.__volumeZscore = ZScore(120)
        self.__toSell = False
        self.__toBuy = False

        # order id
        self.__position = 0
    def __init__(self, symbol, strategy, buyingRatio):
        ''' constructor '''
        self.__symbol = symbol
        self.__strategy = strategy
        self.__startDate = strategy.startDate
        self.__buyingRatio = buyingRatio
        self.__buyThreshold = 1.5
        self.__sellThreshold = 0.5
        self.__preZscore = None
        self.__priceZscore = ZScore(150)
        self.__volumeZscore = ZScore(150)
        self.__dayCounter = 0
        self.__dayCounterThreshold = 5

        # order id
        self.__position = 0
        self.__buyPrice = 0
Example #3
0
class OneTraker(object):
    ''' tracker for one stock '''
    def __init__(self, symbol, strategy, buyingRatio):
        ''' constructor '''
        self.__symbol = symbol
        self.__strategy = strategy
        self.__startDate = strategy.startDate
        self.__buyingRatio = buyingRatio
        self.__buyThreshold = -2
        self.__sellThreshold = 0.5
        self.__priceZscore = ZScore(120)
        self.__volumeZscore = ZScore(120)
        self.__toSell = False
        self.__toBuy = False

        # order id
        self.__position = 0

    def __getCashToBuyStock(self):
        ''' calculate the amount of money to buy stock '''
        account = self.__strategy.getAccountCopy()

        if (account.buyingPower >=
                account.getTotalValue() / self.__buyingRatio):
            return account.getTotalValue() / self.__buyingRatio
        else:
            return 0

    def __placeBuyOrder(self, tick):
        ''' place buy order'''
        cash = self.__getCashToBuyStock()
        if cash == 0 or self.__position > 0:
            return

        share = math.floor(cash / float(tick.close)) - self.__position
        order = Order(accountId=self.__strategy.accountId,
                      action=Action.BUY,
                      type=Type.MARKET,
                      symbol=self.__symbol,
                      share=share)
        if self.__strategy.placeOrder(order):
            self.__position = math.floor(cash / float(tick.close))

    def __placeSellOrder(self, tick):
        ''' place sell order '''
        if self.__position < 0:
            return

        share = self.__position
        order = Order(accountId=self.__strategy.accountId,
                      action=Action.SELL,
                      type=Type.MARKET,
                      symbol=self.__symbol,
                      share=-share)
        if self.__strategy.placeOrder(order):
            self.__position = 0

    def orderExecuted(self, orderId):
        ''' call back for executed order '''
        return

    def tickUpdate(self, tick):
        ''' consume ticks '''
        LOG.debug("tickUpdate %s with tick %s, price %s" %
                  (self.__symbol, tick.time, tick.close))
        self.__priceZscore(tick.close)
        self.__volumeZscore(tick.volume)

        #if haven't started, don't do any trading
        if tick.time <= self.__startDate:
            return

        # if not enough data, skip to reduce risk
        if not self.__priceZscore.getLastValue(
        ) or not self.__volumeZscore.getLastValue():
            return

        # get zscore
        priceZscore = self.__priceZscore.getLastValue()
        volumeZscore = self.__volumeZscore.getLastValue()
        if priceZscore is None or volumeZscore is None:
            return

        if priceZscore < self.__buyThreshold and self.__position <= 0 and abs(
                volumeZscore) > 1:
            self.__placeBuyOrder(tick)

        elif priceZscore > self.__sellThreshold and self.__position > 0 and abs(
                volumeZscore) > 1:
            self.__placeSellOrder(tick)
        """
class OneTraker(object):
    ''' tracker for one stock '''
    def __init__(self, symbol, strategy, buyingRatio):
        ''' constructor '''
        self.__symbol = symbol
        self.__strategy = strategy
        self.__startDate = strategy.startDate
        self.__buyingRatio = buyingRatio
        self.__buyThreshold = 1.5
        self.__sellThreshold = 0.5
        self.__preZscore = None
        self.__priceZscore = ZScore(150)
        self.__volumeZscore = ZScore(150)
        self.__dayCounter = 0
        self.__dayCounterThreshold = 5

        # order id
        self.__position = 0
        self.__buyPrice = 0


    def __getCashToBuyStock(self):
        ''' calculate the amount of money to buy stock '''
        account = self.__strategy.getAccountCopy()

        if (account.buyingPower >= account.getTotalValue() / self.__buyingRatio):
            return account.getTotalValue() / self.__buyingRatio
        else:
            return 0

    def __placeBuyOrder(self, tick):
        ''' place buy order'''
        cash = self.__getCashToBuyStock()
        if cash == 0:
            return

        share = math.floor(cash / float(tick.close))
        order = Order(accountId = self.__strategy.accountId,
                         action = Action.BUY,
                         type = Type.MARKET,
                         symbol = self.__symbol,
                         share = share)
        if self.__strategy.placeOrder(order):
            self.__position = share
            self.__buyPrice = tick.close

    def __placeSellOrder(self, tick):
        ''' place sell order '''
        if self.__position < 0:
            return

        share = self.__position
        order = Order(accountId = self.__strategy.accountId,
                         action = Action.SELL,
                         type = Type.MARKET,
                         symbol = self.__symbol,
                         share = -share)
        if self.__strategy.placeOrder(order):
            self.__position = 0
            self.__buyPrice = 0


    def orderExecuted(self, orderId):
        ''' call back for executed order '''
        return

    def tickUpdate(self, tick):
        ''' consume ticks '''
        LOG.debug("tickUpdate %s with tick %s, price %s" % (self.__symbol, tick.time, tick.close))
        self.__priceZscore(tick.close)
        self.__volumeZscore(tick.volume)

        # get zscore
        priceZscore = self.__priceZscore.getLastValue()
        volumeZscore = self.__volumeZscore.getLastValue()

        #if haven't started, don't do any trading
        if tick.time <= self.__startDate:
            return

        # if not enough data, skip to reduce risk
        if priceZscore is None or volumeZscore is None:
            return

        if self.__position > 0:
            self.__dayCounter += 1

        if priceZscore > self.__buyThreshold and self.__preZscore and self.__preZscore < self.__buyThreshold and self.__position <= 0 and abs(volumeZscore) > 1:
            self.__placeBuyOrder(tick)
        elif self.__position > 0:
            if (self.__dayCounter > self.__dayCounterThreshold and priceZscore < self.__sellThreshold)\
            or priceZscore < 0 or self.__buyPrice * 0.9 > tick.close:
                self.__placeSellOrder(tick)
                self.__dayCounter = 0

        self.__preZscore = priceZscore