Пример #1
0
 def testNormal(self):
     env.run_mode = env.MODE_SIMULATE
     st = lib.str2epoch("2019/04/02 09:00", "%Y/%m/%d %H:%M")
     ed = lib.str2epoch("2019/04/02 12:00", "%Y/%m/%d %H:%M")
     s = SubChart("testNormal", "USD_JPY", "M5", st, ed, 5, 48)
     u = s.unitsecs
     
     (t,_,_,_,_,_) = s.getPrices()
     self.assertEqual(len(t), len(s.prices[0]))
     
     # 1
     i, now = s.getTime(st + 100)
     self.assertEqual(i,5)
     self.assertEqual(now,1554195600)
     
     # 2
     i, now = s.getTime(st + 400)
     self.assertEqual(i,6)
     self.assertEqual(now,1554195900)
     
     # 3
     i, now = s.getTime(ed + 400)
     self.assertEqual(i,42)
     self.assertEqual(now,1554206700)
     
     # 4
     i, now = s.getTime(ed - 500)
     self.assertEqual(i,39)
     self.assertEqual(now,1554205800)
     
     
     from event.tick import TickEvent
     tickEvent = TickEvent(1554196200+100, 1, 1)
     s.onTick(tickEvent)
     (t,o,h,l,c,v) = s.getPrices()
     self.assertEqual(t[-1], 1554196200)
     self.assertEqual(o[-1], 111.372)
     self.assertEqual(v[-1], 22)
     
     (t,o,h,l,c,v) = s.getPrices(5)
     self.assertEqual(t[0], 1554195600)
     self.assertEqual(t[-1], 1554196200)
     
     (t,o,h,l,c,v) = s.getPrices(5,6)
     self.assertEqual(t[-1], 1554195900)
     
     ep = ed + u*(48-len(s.prices[0])+48*0.5+1)
     tickEvent = TickEvent(ep, 1, 1)
     s.onTick(tickEvent)
     self.assertEqual(len(s.prices[0]), 48+48*0.5)
     (t,_,_,_,_,_) = s.getPrices()
     self.assertEqual(t[-1],ep)
     
     ep = t[-1] + u
     tickEvent = TickEvent(ep+100, 1, 1)
     s.onTick(tickEvent)
     self.assertEqual(len(s.prices[0]), 48)
     (t,_,_,_,_,_) = s.getPrices()
     self.assertEqual(t[-1],ep)
Пример #2
0
 def testWeekends(self):
     st = lib.str2epoch("2019/04/05 20:00", "%Y/%m/%d %H:%M")
     ed = lib.str2epoch("2019/04/08 01:00", "%Y/%m/%d %H:%M")
     s = SubChart("testWeekends", "USD_JPY", "H1", st, ed, 5)
     u = s.unitsecs
     
     i, now = s.getTime(st + 100)
     self.assertEqual(i,5)
     self.assertEqual(now,1554494400)
     
     i, now = s.getTime(st + u + 100)
     self.assertEqual(i,-1)
     
     i, now = s.getTime(1554670800 -u + 100)
     self.assertEqual(i,-1)
     
     i, now = s.getTime(1554670800 + 100)
     self.assertEqual(i,6)
     self.assertEqual(now,1554670800)
Пример #3
0
class EmaIndex(TechnicalIndex):
    def __init__(self, instrument, granularity, startep, endep, ema_span=20):
        super(EmaIndex, self).__init__("EmaIndex", instrument, granularity)
        self.subc = SubChart("EmaIndex", instrument, granularity, startep,
                             endep)
        (t, _, _, _, cl, _) = self.subc.getPrices()
        self.epochs = t[ema_span:]
        self.alpha = 2.0 / (ema_span + 1)
        self.ema_span = ema_span
        self.now = 0

        first_ema = sum(cl[:ema_span]) / ema_span
        self.ema = []
        self._calcEma(t[ema_span - 1:], cl[ema_span - 1:], first_ema)

    def _calcEma(self, t, cl, last_ema):
        ema = self.ema
        for i in range(1, len(cl)):
            if t[i] - t[i - 1] > self.unitsecs:
                preema = cl[i]
            else:
                preema = last_ema
            last_ema = preema + self.alpha * (cl[i] - preema)
            ema.append(last_ema)

        self.ema = ema

    def onTick(self, tickEvent):
        epoch = tickEvent.time
        i, epoch = self.subc.getTime(epoch)
        if epoch == self.now:
            return i, epoch, self.ema[i - self.ema_span]

        if i < 0:
            self.updateEma()

        self.now = epoch

        i = i - self.ema_span
        if i >= 0:
            return i, epoch, self.ema[i - self.ema_span]
        else:
            return i, epoch, -1

    def updateEma(self):
        (t1, _, _, _, cl1, _) = self.subc.getLatestChart(self.epochs[-1])
        self._calcEma(t1, cl1, self.ema[-1])

    def getPlotElement(self, color="k"):
        return PlotEleLineChart(self.epochs, self.ema)