Esempio n. 1
0
    def work(_1, _2):
        portfolio = {}
        dtit = dtstart
        prevs = []
        canBuy = True
        canSell = False

        traceA = core.createNewScatterTrace("traceA", "y")
        traceA['prev_len'] = _1

        traceB = core.createNewScatterTrace("traceB", "y")
        traceB['prev_len'] = _2 

        while dtit <= dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']

                price = (o+c)/2   # ok
                # price = c         # ok
                #price = o + (c-o)*random.randint(0,10)/10 # ok
                #price = random.uniform(o, c) if c > o else random.uniform(c, o) 
                # price = random.uniform(l, h)  # much worse than [open, close]

                buyprice = price
                sellprice = price

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)
            
                core.addToScatterTrace(traceA, dtit, sig(traceA['prev_len'], traceA['y'], prevs, price))
                core.addToScatterTrace(traceB, dtit, sig(traceB['prev_len'], traceB['y'], prevs, price))

                if len(traceA['y']) > 1:

                    if canBuy and (traceA['y'][-2] < traceB['y'][-2] and traceA['y'][-1] > traceB['y'][-1]):
                            core.portfolioBuy(portfolio, dtit, buyprice, uncertainty_margin)
                            canSell = True
                            canBuy = False
                    elif canSell and (traceA['y'][-2] > traceB['y'][-2] and traceA['y'][-1] < traceB['y'][-1]):
                            core.portfolioSell(portfolio, dtit, sellprice, uncertainty_margin)
                            canSell = False
                            canBuy = True

                prevs.append(price)
            dtit += datetime.timedelta(minutes=interval)

        # beautify (replacing 0's by None )
        for i,v in enumerate(traceB['y']):
            if v == 0:
                traceB['y'][i]=None

        proc = core.processPortfolio(portfolio, 1)
        return (proc, portfolio, [traceA, traceB])
Esempio n. 2
0
    def work(_1, _2, _3, _4):
        portfolio = {}
        dtit = dtstart
        canBuy = True
        canSell = False

        traceA = core.createNewScatterTrace("traceA", "y")
        traceB = core.createNewScatterTrace("traceB", "y")
        
        buyPrice = None
        prevC = None
        prevCavg = None
        bucket=[]
        while dtit <= dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']

                #price = (o+c+l+h)/4   # ok
                #price = (o+c)/2   # ok
                #price = c         # ok
                #price = o + (c-o)*random.randint(0,10)/10 # ok
                #price = random.uniform(o, c) if c > o else random.uniform(c, o) 
                price = random.uniform(l, h)  # reality

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)
                
                shouldBuy=False
                if idx in preds:
                    x1,y1=list(preds[idx].items())[0]
                    x2,y2=list(preds[idx].items())[1]
                    _2 = 1.002
                    if ((y2['open']+y2['high']+y2['low']+y2['close'])/4) > ((y1['open']+y1['high']+y1['low']+y1['close'])/4)*_2:
                    	shouldBuy=True


                #if canBuy and (prevCavg < 0 and pCavg > 0) and pE > 0:
                if canBuy and shouldBuy :
                        core.portfolioBuy(portfolio, dtit, price, uncertainty_margin)
                        canSell = True
                        canBuy = False
                        buyPrice = price
                elif canSell and (price > buyPrice*_3 or price < buyPrice*_4):
                        core.portfolioSell(portfolio, dtit, price, uncertainty_margin)
                        canSell = False
                        canBuy = True

            dtit += datetime.timedelta(minutes=interval)

        proc = core.processPortfolio(portfolio, 1)
        return (proc, portfolio, [ traceA,  traceB ])
Esempio n. 3
0
    def work():
        portfolio = {}
        dtit = dtstart
        canBuy = True
        canSell = False

        traceA = core.createNewScatterTrace("traceA", "y")
        traceC = core.createNewScatterTrace("traceC", "y")

        hp_arrL = 35
        hp_arrEL = 4

        while dtit <= dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                volume = inp[idx]['volume']
                trades = inp[idx]['trades']
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']

                price = random.randint(
                    int(l), int(h)) if int(h) > int(l) else random.randint(
                        int(h), int(l)
                    )  # still "okay" but much worse than [open, close]

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)

                pA = sigA(traceA['y'], traceA['z'], price, hp_arrL, hp_arrEL)
                pC = sigC(traceC['y'], traceA['z'], price, hp_arrL, hp_arrEL)
                core.addToScatterTrace(traceA, dtit, pA)
                core.addToScatterTrace(traceC, dtit, pC)

                #if canBuy and (pC >= o and pC <= c):
                if canBuy and (pC >= l and pC <= h):
                    core.portfolioBuy(portfolio, dtit, price,
                                      uncertainty_margin)
                    canSell = True
                    canBuy = False
                #elif canSell and (pA >= c and pA <= o):
                elif canSell and (pA >= l and pA <= h):
                    core.portfolioSell(portfolio, dtit, price,
                                       uncertainty_margin)
                    canSell = False
                    canBuy = True

            dtit += datetime.timedelta(minutes=interval)

        finalizeTrace(traceA)
        finalizeTrace(traceC)

        proc = core.processPortfolio(portfolio, 1)
        return (proc, portfolio, [traceA, traceC])
Esempio n. 4
0
    def work(dtstart, dtend):
        portfolio = {}
        prevPrice = None
        prev_slope_pct = None
        
        canBuy = True
        canSell = False
        
        dtit = dtstart
        while dtit < dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']

                # let price be any random value in range [open, close]
                #price = random.uniform(o, c) if c > o else random.uniform(c, o) 
                #price = random.uniform(l, h)  # much worse than [open, close]
                # let the price be any random value in range [open ; (close-open)*x ] where x is in range [0.0 ; 1.0]
                #price = o + (c-o)*random.randint(0,10)/10
                price = o + (c-o)*random.randint(0,10)/10
                # price = o


                
                buyprice = price 
                sellprice = price

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)
                if prevPrice != None:
                    slope_pct = round((price-prevPrice)/prevPrice*100,2)
                    
                    if canBuy:
                        if slope_pct > slope_pct_threshold : 
                            # buy
                            core.portfolioBuy(portfolio, dtit, buyprice, uncertainty_margin)
                            canSell = True
                            canBuy = False
                    elif canSell and slope_pct <= 0:
                        # sell
                        core.portfolioSell(portfolio, dtit, sellprice, uncertainty_margin)
                        canSell = False
                        canBuy = True

                    prev_slope_pct = slope_pct

                prevPrice = c

            dtit += datetime.timedelta(minutes=interval)
            
        proc = core.processPortfolio(portfolio, 1, 1)
        return (proc, portfolio)
Esempio n. 5
0
    def work(dtstart, dtend):
        portfolio = {}
        prevPrice = None
        slopes = []
        dtit = dtstart
        while dtit < dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']

                # let price be any random value in range [open, close]
                # price = random.uniform(o, c) if c > o else random.uniform(c, o) 
                # price = random.uniform(l, h)  # much worse than [open, close]
                # let the price be any random value in range [open ; (close-open)*x ] where x is in range [0.0 ; 1.0]
                price = o + (c-o)*random.randint(0,10)/10
                # this strategy seems to stop working when we use range [low, high]
                # however in reality there is a chance the system takes near-low/high values, as a result the ROI will be low or even negative.
                buyprice = price 
                sellprice = price

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)
                if prevPrice != None:
                    slope_pct = round((price-prevPrice)/prevPrice*100,2)
                    slopes.append(slope_pct)
                    if price > prevPrice:
                        slopes.append( slope_pct )
                        avgS = sum(slopes)/len(slopes)
                        if avgS > slope_pct_threshold : # len(slopes) > 1 and
                            # buy
                            core.portfolioBuy(portfolio, dtit, buyprice, uncertainty_margin)
                    else:
                        if len(slopes) > 0:
                            # sell
                            core.portfolioSell(portfolio, dtit, sellprice, uncertainty_margin)
                        slopes = []

                prevPrice = c

            dtit += datetime.timedelta(minutes=interval)
            
        proc = core.processPortfolio(portfolio, 1)
        return (proc, portfolio)
Esempio n. 6
0
    def work():
        portfolio = {}
        dtit = dtstart
        prevs = []
        canBuy = True
        canSell = False

        traceA = core.createNewScatterTrace("traceA", "y2")
        traceA['prev_len'] = 10

        traceB = core.createNewScatterTrace("traceB", "y")
        traceB[
            'prev_len'] = 12  # make sure it's large enough ( >= 12) for isDownTrend formula

        upduration_A = 0
        downduration_A = 0

        prevBuyPrice = 0

        while dtit <= dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']

                price = (o + c) / 2  # ok
                # price = c         # ok
                #price = o + (c-o)*random.randint(0,10)/10 # ok
                # price = random.uniform(o, c) if c > o else random.uniform(c, o)
                # price = random.uniform(l, h)  # much worse than [open, close]

                buyprice = price
                sellprice = price

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)
                if len(prevs) > 0:
                    core.addToScatterTrace(
                        traceA, dtit, sig(traceA['prev_len'], prevs, price, 1))
                    core.addToScatterTrace(
                        traceB, dtit, sig2(traceB['prev_len'], prevs, price))

                    if len(traceA['y']) > 1:
                        if traceA['y'][-1] > 0 and traceA['y'][
                                -2] > 0:  # A up -> up
                            upduration_A += 1
                        if traceA['y'][-1] < 0 and traceA['y'][
                                -2] > 0:  # A up -> down
                            upduration_A = 0
                        if traceA['y'][-1] < 0 and traceA['y'][
                                -2] < 0:  # A down -> down
                            downduration_A += 1
                        if traceA['y'][-1] > 0 and traceA['y'][
                                -2] < 0:  # A down -> up
                            downduration_A = 0

                        if canBuy and isDownTrend(
                                traceB['y'][-traceB['prev_len']:]) == False:
                            if traceA['y'][-1] > 0 and traceA['y'][
                                    -2] < 0:  # B down -> up

                                core.portfolioBuy(portfolio, dtit, buyprice,
                                                  uncertainty_margin)
                                prevBuyPrice = buyprice
                                canSell = True
                                canBuy = False
                        elif canSell:
                            if traceA['y'][-1] < 0 and traceA['y'][
                                    -2] > 0:  # B up -> down
                                core.portfolioSell(portfolio, dtit, sellprice,
                                                   uncertainty_margin)
                                canSell = False
                                canBuy = True

                else:
                    core.addToScatterTrace(traceA, dtit,
                                           0)  # use 0 instead of None
                    core.addToScatterTrace(traceB, dtit,
                                           0)  # use 0 instead of None

                prevs.append(price)

            # else:
            #     print("missing: " + str(idx))

            dtit += datetime.timedelta(minutes=interval)

        # beautify (replacing 0's by None )
        for i, v in enumerate(traceB['y']):
            if v == 0:
                traceB['y'][i] = None

        proc = core.processPortfolio(portfolio, 1)
        return (proc, portfolio, [traceA, traceB])
Esempio n. 7
0
    def work(_1, _2, _3, _4, _5, _6):
        portfolio = {}
        dtit = dtstart

        traceA = core.createNewScatterTrace("traceA", "y")
        traceA['prev_len'] = _1
        traceB = core.createNewScatterTrace("traceB", "y")
        traceB['prev_len'] = _2

        traceC = core.createNewScatterTrace("W&L", "y2")

        usage = {
            'canBuy': True,
            'canSell': False,
            'buyPrice': None,
            'sellPrice': None,
            'prevPrice': None,
            'win': 0,
            'loss': 0,
        }
        bucket = []
        while dtit <= dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']

                price = (o + c + l + h) / 4  # ok
                #price = c         # ok
                #price = o + (c-o)*random.randint(0,10)/10 # ok
                #price = random.uniform(o, c) if c > o else random.uniform(c, o)
                #price = random.uniform(l, h)  # reality

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)

                pA = sig(
                    traceA['prev_len'],
                    np.average(traceA['y'][-_3:]) if len(traceA['y']) > 0 else
                    (o + c) / 2, (o + c) / 2)
                pB = sig(
                    traceB['prev_len'],
                    np.average(traceB['y'][-_3:]) if len(traceB['y']) > 0 else
                    (o + c) / 2, (o + c) / 2)
                core.addToScatterTrace(traceA, dtit, pA)
                core.addToScatterTrace(traceB, dtit, pB)

                bucket.append(pA)  # dummy

                def buyF():
                    if np.average(traceB['y'][-_4:]) > traceB['y'][-1]:
                        return False
                    if traceB['y'][-2] > traceA['y'][-2] and traceB['y'][
                            -1] < traceA['y'][-1]:
                        return True
                    if traceB['y'][-1] > traceB['y'][-2]:
                        return True

                def sellF():
                    if price > usage['buyPrice'] * _5 and not buyF():
                        usage['win'] += 1
                        return True
                    if price < usage['buyPrice'] * _6:
                        usage['loss'] += 1
                        return True

                if len(bucket) > 2:
                    if usage['canBuy'] and buyF():
                        core.portfolioBuy(portfolio, dtit, price,
                                          uncertainty_margin)
                        usage['canSell'] = True
                        usage['canBuy'] = False
                        usage['buyPrice'] = price
                    elif usage['canSell'] and sellF():
                        core.portfolioSell(portfolio, dtit, price,
                                           uncertainty_margin)
                        usage['canSell'] = False
                        usage['canBuy'] = True
                        usage['sellPrice'] = price

                    core.addToScatterTrace(traceC, dtit, (1 + usage['win']) /
                                           (1 + usage['loss']))

                usage['prevPrice'] = price

            dtit += datetime.timedelta(minutes=interval)

        for i, v in enumerate(
                traceB['y']):  # beautify (replacing 0's by None )
            if v == 0:
                traceB['y'][i] = None

        proc = core.processPortfolio(portfolio, 1)
        return (proc, portfolio, [
            traceA,
            traceB,
            traceC,
        ])
Esempio n. 8
0
    def work(_1, _2, _3):
        portfolio = {}
        dtit = dtstart

        traceD = core.createNewScatterTrace("traceD", "y2")

        usage = {
            'canBuy': True,
            'canSell': False,
            'buyPrice': None,
            'sellPrice': None,
            'prevPrice': None,
        }
        bucket = []
        bucketstd = []
        while dtit <= dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']

                price = (o + c + l + h) / 4  # ok
                #price = c         # ok
                #price = o + (c-o)*random.randint(0,10)/10 # ok
                #price = random.uniform(o, c) if c > o else random.uniform(c, o)
                price = random.uniform(l, h)  # reality

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)

                def buyF():
                    if len(traceD['y']) < 2:
                        return False
                    if traceD['y'][-2] == 1 and traceD['y'][-1] == -1:
                        return True
                    #if traceD['y'][-1] == 1 and traceD['y'][-2] == -1:
                    #    return True
                def sellF():
                    if price > usage['buyPrice'] * _1:
                        return True
                    if price < usage['buyPrice'] * _2:
                        return True

                if len(bucket) > 2:
                    pD = np.average(bucket[-_3:])
                    pD = 1 if pD > 1 else -1
                    core.addToScatterTrace(traceD, dtit, pD)

                    if usage['canBuy'] and buyF():
                        core.portfolioBuy(portfolio, dtit, price,
                                          uncertainty_margin)
                        usage['canSell'] = True
                        usage['canBuy'] = False
                        usage['buyPrice'] = price
                    elif usage['canSell'] and sellF():
                        core.portfolioSell(portfolio, dtit, price,
                                           uncertainty_margin)
                        usage['canSell'] = False
                        usage['canBuy'] = True
                        usage['sellPrice'] = price
                        usage['countSinceSell'] = 0

                if usage['prevPrice'] != None:
                    bucket.append(price / usage['prevPrice'])

                usage['prevPrice'] = price

            dtit += datetime.timedelta(minutes=interval)

        proc = core.processPortfolio(portfolio, 1)
        return (proc, portfolio, [traceD])
Esempio n. 9
0
    def work(_1, _2, _3, _4):
        portfolio = {}
        dtit = dtstart
        canBuy = True
        canSell = False

        traceA = core.createNewScatterTrace("traceA", "y")
        traceA['prev_len'] = _1

        traceB = core.createNewScatterTrace("traceB", "y")
        traceB['prev_len'] = _2

        traceC = core.createNewScatterTrace("traceC", "y2")
        traceD = core.createNewScatterTrace("traceD", "y2")

        buyPrice = None
        prevC = None
        prevCavg = None
        bucket = []
        while dtit <= dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']

                #price = (o+c+l+h)/4   # ok
                #price = (o+c)/2   # ok
                #price = c         # ok
                #price = o + (c-o)*random.randint(0,10)/10 # ok
                #price = random.uniform(o, c) if c > o else random.uniform(c, o)
                price = random.uniform(l, h)  # reality

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)

                pA = sig(traceA['prev_len'], traceA['y'], o)
                pB = sig(traceB['prev_len'], traceB['y'], c)
                pC = pA - pB
                pC = sig(3, [prevC if prevC != None else pC], pC)
                bucket.append(pC)
                pCavg = np.average(bucket[-10:])

                core.addToScatterTrace(traceA, dtit, pA)
                core.addToScatterTrace(traceB, dtit, pB)
                core.addToScatterTrace(traceC, dtit, pC)
                core.addToScatterTrace(traceD, dtit, pCavg)

                if len(bucket) > 2:

                    if canBuy and (prevCavg < 0 and pCavg > 0):
                        core.portfolioBuy(portfolio, dtit, price,
                                          uncertainty_margin)
                        canSell = True
                        canBuy = False
                        buyPrice = price
                    elif canSell and (price > buyPrice * _3
                                      or price < buyPrice * _4):
                        core.portfolioSell(portfolio, dtit, price,
                                           uncertainty_margin)
                        canSell = False
                        canBuy = True
                prevC = pC
                prevCavg = pCavg

            dtit += datetime.timedelta(minutes=interval)

        for i, v in enumerate(
                traceB['y']):  # beautify (replacing 0's by None )
            if v == 0:
                traceB['y'][i] = None

        proc = core.processPortfolio(portfolio, 1)
        return (proc, portfolio, [traceA, traceB, traceC, traceD])
Esempio n. 10
0
def work(dtstart, dtend, inp, interval, uncertainty_margin, _1, _2, _3):
    portfolio = {}
    dtit = dtstart

    traceA = core.createNewScatterTrace("traceA", "y2")
    traceB = core.createNewScatterTrace("traceB", "y2")
    traceC = core.createNewScatterTrace("traceC", "y2")
    traceD = core.createNewScatterTrace("traceD", "y2")

    usage = {
        'canBuy': True,
        'canSell': False,
        'buyPrice': None,
        'sellPrice': None,
        'prevPrice': None,
        'prevClose': None,
    }
    bucket_gain = []
    bucket_loss = []
    counter = 0
    while dtit <= dtend:
        idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
        if idx in inp:
            c = inp[idx]['close']
            o = inp[idx]['open']
            l = inp[idx]['low']
            h = inp[idx]['high']

            price = (o + c + l + h) / 4  # ok
            #price = c         # ok
            #price = o + (c-o)*random.randint(0,10)/10 # ok
            #price = random.uniform(o, c) if c > o else random.uniform(c, o)
            #price = random.uniform(l, h)  # reality

            core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)

            if usage['prevClose']:
                dx = price - usage['prevClose']
                if (dx > 0):
                    bucket_gain.append(abs(dx))  # dummy
                    core.addToScatterTrace(traceA, dtit, 1)
                else:
                    bucket_loss.append(abs(dx))
                    core.addToScatterTrace(traceA, dtit, -1)

                counter += 1
                if counter > _1:
                    # print(bucket_gain)
                    # print(bucket_loss)
                    aGain = np.average(
                        bucket_gain[-_1:]) if len(bucket_gain[-_1:]) > 0 else 0
                    aLoss = np.average(
                        bucket_loss[-_1:]) if len(bucket_loss[-_1:]) > 0 else 0
                    #aGain = sig(_1, np.average(bucket_gain[-_1:-1]), bucket_gain[-1])  if len(bucket_gain[-_1:])>0 else 0
                    #aLoss = sig(_1, np.average(bucket_loss[-_1:-1]), bucket_loss[-1])  if len(bucket_loss[-_1:])>0 else 0
                    if (aLoss != 0):

                        RSI = (100 - (100 / (1 + (aGain / aLoss))))
                        #if (abs(RSI) > 100): print(aGain, aLoss)
                        core.addToScatterTrace(traceB, dtit, RSI)

                    if len(traceB['y']) > 0:
                        p = 10 if np.average(
                            traceB['y'][-10:]) > traceB['y'][-1] else 0
                        core.addToScatterTrace(traceC, dtit, p)

            def buyF():
                if len(traceC['y']) <= 1: return False
                # if traceC['y'][-1] < 35: return False
                # if traceC['y'][-1] > 60: return False

                # if np.average(traceC['y'][-5:]) > traceC['y'][-1]: return False

                # #if traceC['y'][-2] > traceD['y'][-2] and traceC['y'][-1] < traceD['y'][-1]:
                # if traceC['y'][-2] < traceD['y'][-2] and traceC['y'][-1] > traceD['y'][-1]:
                #     return True
                #return False
                if np.average(traceC['y'][-5:]) > 6:
                    return True

            def sellF():
                if price > usage['buyPrice'] * _2 and not buyF():
                    return True
                if price < usage['buyPrice'] * _3:
                    return True

            if counter > _1:
                if usage['canBuy'] and buyF():
                    core.portfolioBuy(portfolio, dtit, price,
                                      uncertainty_margin)
                    usage['canSell'] = True
                    usage['canBuy'] = False
                    usage['buyPrice'] = price
                elif usage['canSell'] and sellF():
                    core.portfolioSell(portfolio, dtit, price,
                                       uncertainty_margin)
                    usage['canSell'] = False
                    usage['canBuy'] = True
                    usage['sellPrice'] = price

            usage['prevPrice'] = price
            usage['prevClose'] = c

        dtit += datetime.timedelta(minutes=interval)

    proc = core.processPortfolio(portfolio, 1)
    return (proc, portfolio, [traceA, traceB, traceC, traceD])
Esempio n. 11
0
    def work(_1, _2):
        portfolio = {}
        dtit = dtstart
        canBuy = True
        canSell = False

        traceA = core.createNewScatterTrace("traceA", "y")

        traceC = core.createNewScatterTrace("traceC", "y3")

        traceD = core.createNewScatterTrace("traceD", "y3")
        traceE = core.createNewScatterTrace("traceE", "y3")
        traceF = core.createNewScatterTrace("traceF", "y3")

        hp_arrL = 35
        hp_arrEL = 4

        usage = {
            'canBuy': True,
            'canSell': False,
            'buyPrice': None,
            'prevPrice': None
        }

        while dtit <= dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                volume = inp[idx]['volume']
                trades = inp[idx]['trades']
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']

                price = (o + c + l + h) / 4  # ok
                #price = (o+c)/2
                #price = c         # ok
                #price = o + (c-o)*random.randint(0,10)/10 # ok
                #price = random.uniform(o, c) if c > o else random.uniform(c, o)
                #price = random.uniform(l, h)  # reality

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)

                core.addToScatterTrace(traceC, dtit, (c - o))
                core.addToScatterTrace(
                    traceD, dtit,
                    np.std(traceC['y'][-16:]) * 2 +
                    np.average([abs(x) for x in traceC['y'][-16:]]))

                def buyF():
                    if len(traceD['y']) < 2: return False
                    if traceC['y'][-2] < traceD['y'][-2] and traceC['y'][
                            -1] > traceD['y'][-1]:
                        return True

                def sellF():
                    if price > usage['buyPrice'] * _1:  #and not buyF():
                        return True
                    if price < usage['buyPrice'] * _2:
                        return True

                if usage['canBuy'] and buyF():
                    core.portfolioBuy(portfolio, dtit, price,
                                      uncertainty_margin)
                    usage['canSell'] = True
                    usage['canBuy'] = False
                    usage['buyPrice'] = price
                elif usage['canSell'] and sellF():
                    core.portfolioSell(portfolio, dtit, price,
                                       uncertainty_margin)
                    usage['canSell'] = False
                    usage['canBuy'] = True
                    usage['sellPrice'] = price

                usage['prevPrice'] = price

            dtit += datetime.timedelta(minutes=interval)

        proc = core.processPortfolio(portfolio, 1)
        return (proc, portfolio, [
            traceA,
            traceC,
            traceD,
            traceE,
            traceF,
        ])
Esempio n. 12
0
    def work(_1, _2):
        portfolio = {}
        dtit = dtstart
        canBuy = True
        canSell = False

        traceA = core.createNewScatterTrace("traceA" ,"y")
        traceC = core.createNewScatterTrace("traceC" ,"y2", 'markers')
        traceD = core.createNewScatterTrace("traceD" ,"y2", 'markers')
        traceE = core.createNewScatterTrace("traceE" ,"y3")

        hp_arrL = 35
        hp_arrEL = 4

        usage = {
            'canBuy': True,
            'canSell': False,

            'buyPrice': None,
            'timeHeld': 0,
        }

        while dtit <= dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                volume = inp[idx]['volume']
                trades = inp[idx]['trades']
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']


                #price = (o+c+l+h)/4   # ok
                price = (o+c)/2
                #price = c         # ok
                #price = o + (c-o)*random.randint(0,10)/10 # ok
                #price = random.uniform(o, c) if c > o else random.uniform(c, o) 
                #price = random.uniform(l, h)  # reality

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)
            
                
                core.addToScatterTrace(traceA, dtit, (o+h+l+c)/4)
                pC = 1-1/(1+(price/np.average(traceA['y'][-24:])))
                core.addToScatterTrace(traceC, dtit, pC)

                core.addToScatterTrace(traceD, dtit, np.average(traceC['y'][-24:]))

                if len(traceD['y']) > 8:
                    pD = 1 if np.average(traceD['y'][-8:-4]) > np.average(traceD['y'][-4:-2]) and np.average(traceD['y'][-4:-2]) < np.average(traceD['y'][-2:]) else 0
                    #pD = 1 if traceD['y'][-1] > traceD['y'][-3] and traceD['y'][-2] < traceD['y'][-3] and traceD['y'][-2] < traceD['y'][-1] else 0
                    core.addToScatterTrace(traceE, dtit, pD )

                def buyF():
                    if len(traceE['y']) < 2: return False
                    
                    if traceE['y'][-2] == 0 and traceE['y'][-1]==1:
                        return True
                def sellF():
                    if traceD['y'][-1] < .5 and usage['timeHeld'] > 5:
                        return True

                    if price > usage['buyPrice']*_1: #and not buyF():
                        return True 
                    if price < usage['buyPrice']*_2:
                        return True

                if usage['canBuy'] and buyF():
                        core.portfolioBuy(portfolio, dtit, price, uncertainty_margin)
                        usage['canSell'] = True
                        usage['canBuy'] = False
                        usage['timeHeld'] = 0
                        usage['buyPrice'] = price
                elif usage['canSell'] and sellF():
                        core.portfolioSell(portfolio, dtit, price, uncertainty_margin)
                        usage['canSell'] = False
                        usage['canBuy'] = True
                        usage['timeHeld'] = 0
                        usage['sellPrice'] = price

                usage['timeHeld']+=1

                
            dtit += datetime.timedelta(minutes=interval)

        proc = core.processPortfolio(portfolio, 1)
        return (proc, portfolio, [traceA, traceC, traceD, traceE, ])
Esempio n. 13
0
    def work(_1, _2):
        portfolio = {}
        dtit = dtstart
        canBuy = True
        canSell = False

        traceA = core.createNewScatterTrace("traceA", "y")
        traceC = core.createNewScatterTrace("traceC", "y")

        hp_arrL = 35
        hp_arrEL = 4

        usage = {
            'canBuy': True,
            'canSell': False,
            'buyPrice': None,
        }

        while dtit <= dtend:
            idx = datetime.datetime.strftime(dtit, '%Y-%m-%dT%H:%M')
            if idx in inp:
                volume = inp[idx]['volume']
                trades = inp[idx]['trades']
                c = inp[idx]['close']
                o = inp[idx]['open']
                l = inp[idx]['low']
                h = inp[idx]['high']

                #price = (o+c+l+h)/4   # ok
                price = (o + c) / 2
                #price = c         # ok
                #price = o + (c-o)*random.randint(0,10)/10 # ok
                #price = random.uniform(o, c) if c > o else random.uniform(c, o)
                price = random.uniform(l, h)  # reality

                core.portfolioPriceEntry(portfolio, dtit, price, o, c, l, h)

                pA = sigA(traceA['y'], traceA['z'], price, hp_arrL, hp_arrEL)
                pC = sigC(traceC['y'], traceA['z'], price, hp_arrL, hp_arrEL)
                core.addToScatterTrace(traceA, dtit, pA)
                core.addToScatterTrace(traceC, dtit, pC)

                def buyF():
                    #if pC >= o and pC <= c:
                    if pC >= l and pC <= h:
                        return True

                def sellF():
                    if price > usage['buyPrice'] * _1:  #and not buyF():
                        return True
                    if price < usage['buyPrice'] * _2:
                        return True

                if usage['canBuy'] and buyF():
                    core.portfolioBuy(portfolio, dtit, price,
                                      uncertainty_margin)
                    usage['canSell'] = True
                    usage['canBuy'] = False
                    usage['buyPrice'] = price
                elif usage['canSell'] and sellF():
                    core.portfolioSell(portfolio, dtit, price,
                                       uncertainty_margin)
                    usage['canSell'] = False
                    usage['canBuy'] = True
                    usage['sellPrice'] = price

            dtit += datetime.timedelta(minutes=interval)

        proc = core.processPortfolio(portfolio, 1)
        return (proc, portfolio, [traceA, traceC])