예제 #1
0
def insertMonoPricedata(instrument, granularity, startprice, start, end,
                        direction, pipdiff):
    db = MSSQLDB()
    pricetbl = lib.names.getPriceTable(instrument, granularity)
    priceunit = tradelib.pip2Price(1, instrument)
    unitsecs = tradelib.getUnitSecs(granularity)
    start -= (start % unitsecs)
    end -= (end % unitsecs)

    curr = start
    price = startprice
    cnt = 0
    while curr <= end:
        i = cnt % 3
        if i == 2:
            price -= pipdiff * priceunit * direction
        else:
            price += pipdiff * priceunit * direction
        if direction == 1:
            o = price
            c = price + 2 * pipdiff * priceunit
        else:
            o = price + 2 * pipdiff * priceunit
            c = price
        sql = "insert into %s ([ep],[dt],[o],[h],[l],[c],[v]) \
            values (%d,'%s',%f,%f,%f,%f,%d)" % (
            pricetbl, curr, lib.epoch2str(curr, "%Y-%m-%d %H:%M:%S"), o,
            price + 2 * pipdiff * priceunit, price, c, (i + 1) * 10)
        db.execute(sql)
        curr += unitsecs
        cnt += 1
예제 #2
0
    def __init__(self,
                 instrument,
                 granularity,
                 startep=-1,
                 endep=-1,
                 peakspan=12 * 6,
                 peaksize=5,
                 analspan=12 * 4):

        self.instrument = instrument
        self.granularity = granularity
        self.subc = SubChart("PriceAction",
                             self.instrument,
                             self.granularity,
                             startep=startep,
                             endep=endep,
                             nbars=peakspan + peaksize * 2)

        self.peaks = PeakIndex(self.subc, peaksize, peakspan)
        self.analspan = analspan
        self.hist = MultiObjHistory(analspan)
        self.candleshapes = CandleShapeIndex(self.subc,
                                             statistics_span=analspan)
        self.unitsecs = tradelib.getUnitSecs(granularity)
        self.pipprice = tradelib.pip2Price(1, instrument)
        self.nowi = -1
        self.now = -1

        self.linehists = {
            const_candles.LINE_TPTREND: {},  # dict of History()
            const_candles.LINE_BTTREND: {},
            const_candles.LINE_MAX: {},
            const_candles.LINE_MIN: {}
        }
예제 #3
0
def prepare2legData(instrument, granularity, startprice, start, end, diffpips):
    reCreatePriceTable(instrument, granularity)
    midep = (start + end) / 2
    unitsecs = tradelib.getUnitSecs(granularity)
    insertMonoPricedata(instrument, granularity, startprice, start, midep, 1,
                        diffpips)
    insertMonoPricedata(instrument, granularity, startprice, midep + unitsecs,
                        end, -1, diffpips)
예제 #4
0
 def __init__(self, instrument, granularity="M1"):
     #self.now = tradelib.getNearEpoch(granularity, lib.nowepoch())
     self.now = -1
     self.instrument = instrument
     self.granularity = granularity
     self.oandaw = OandaWrapper()
     self.ticker_type = env.TICKTYPE_ONLINE
     self.unitsecs = tradelib.getUnitSecs(granularity)
예제 #5
0
 def __init__(self, childDG):
     self.instrument = childDG.instrument
     self.granularity = childDG.granularity
     self.unitsecs = tradelib.getUnitSecs(self.granularity)
     self.childDG = childDG
     self.pricetbl = lib.names.getPriceTable(self.instrument, self.granularity)
     self.db = MSSQLDB()
     self.db.createTable(self.pricetbl, "prices")
     self.maxID = -1
예제 #6
0
 def __init__(self, instrument, granularity, profitpips):
     self.instrument = instrument
     self.unitsecs = tradelib.getUnitSecs(granularity)
     self.granularity = granularity
     self.subc = None
     self.profit = tradelib.pip2Price(profitpips, instrument)
     self.id = -1
     self.curr_side = env.SIDE_BUY
     self.now = -1
예제 #7
0
    def __init__(self, instrument, granularity, priceToGet="M"):
        '''
        price:
            M = Mid point
            B = Bid
            A = Ask
        '''
        self.name = "%s_%s" % (instrument, granularity)
        self.instrument = instrument
        self.granularity = granularity
        self.priceToGet = priceToGet
        self.unitsecs = tradelib.getUnitSecs(granularity)

        self.oandaw = OandaWrapper()
예제 #8
0
 def __init__(self, name, instrument, granularity, 
              startep=-1, endep=-1, 
              nbars=0, maxsize=12*24*3, truncateOld=True):
     self.name = name
     self.instrument = instrument
     self.granularity = granularity
     self.truncateOld = truncateOld
     self.unitsecs = tradelib.getUnitSecs(granularity)
     self.epochsidx = None
     self.nowidx = -1
     self.now = -1
     self.maxsize = maxsize
     self.size_to_cleanup = int(maxsize*1.5)
     
     if endep == -1:
         endep = lib.nowepoch()
     if startep == -1:
         startep = min(endep, lib.nowepoch())
     startep = startep - (startep % self.unitsecs) - self.unitsecs
     (t,o,h,l,c,v) = getterlib.getPrices(instrument, 
                                         granularity, startep, endep)
     if nbars > 0:
         (t1,o1,h1,l1,c1,v1) = getterlib.getNPrices(instrument, 
                                                    granularity, startep-1, nbars)
         nbars = len(t1)
         t1.extend(t)
         o1.extend(o)
         h1.extend(h)
         l1.extend(l)
         c1.extend(c)
         v1.extend(v)
         (t,o,h,l,c,v) = (t1,o1,h1,l1,c1,v1)
     self.prices = (t,o,h,l,c,v)
     
     self._initCaches()
     
     self.now = t[nbars]
     self.nowidx = nbars
예제 #9
0
def makeSmallPeriodData(instrument, large_period, small_period):
    reCreatePriceTable(instrument, small_period)
    lus = tradelib.getUnitSecs(large_period)
    sus = tradelib.getUnitSecs(small_period)
    if lus <= sus:
        raise Exception("large_period must be greater than small_period!")
    db = MSSQLDB()
    lpricetbl = lib.names.getPriceTable(instrument, large_period)
    spricetbl = lib.names.getPriceTable(instrument, small_period)
    diffrate = lus / sus

    cnt = db.countTable(lpricetbl)
    if cnt == 0:
        raise Exception("No data in %s" % lpricetbl)

    for (ep, o, h, l, c, v) in db.execute("select ep,o,h,l,c,v from %s;" %
                                          lpricetbl).fetchall():
        bar_size = (h - l) / diffrate
        pdiff = (h - l - bar_size) / diffrate
        if c > o:
            direction = 1
            price = l
        else:
            direction = -1
            price = h

        vs = int(v / diffrate)
        for i in range(int(diffrate)):
            os = price
            if direction == 1:
                hs = price + bar_size
                ls = os
                cs = hs
            else:
                hs = price
                ls = price - bar_size
                cs = ls

            if i >= diffrate - 1:
                if direction == 1:
                    hs = h
                    cs = h
                else:
                    ls = l
                    cs = l

            sql = "insert into %s ([ep],[dt],[o],[h],[l],[c],[v]) \
            values (%d,'%s',%f,%f,%f,%f,%d)" % (
                spricetbl, ep, lib.epoch2str(
                    ep, "%Y-%m-%d %H:%M:%S"), os, hs, ls, cs, vs)
            db.execute(sql)

            shuff = i % 4
            if shuff == 2:
                price -= direction * pdiff
            elif shuff == 3:
                price += direction * pdiff * 2
            else:
                price += direction * pdiff

            ep += sus
예제 #10
0
 def __init__(self, name, subChart):
     self.name = name
     self.instrument = subChart.instrument
     self.granularity = subChart.granularity
     self.unitsecs = tradelib.getUnitSecs(self.granularity)
     self.now = -1