Example #1
0
 def _loadline(self, linetokens):
     res = super(CSVAdjustTime, self)._loadline(linetokens)
     if self.p.adjstarttime:
         # move time to start time of next candle
         # and subtract 0.1 miliseconds (ensures no
         # rounding issues, 10 microseconds is minimum)
         new_date = getstarttime(
             self._timeframe,
             self._compression,
             self.datetime.datetime(0),
             self.p.sessionstart,
             -1) - timedelta(microseconds=100)
         self.datetime[0] = date2num(new_date)
     else:
         self.datetime[0] = date2num(self.datetime.datetime(0))
     return res
Example #2
0
    def start(self):
        if self.f is None:
            if self.p.tickersource is None:
                dataname = self.p.dataname
            else:
                modpath = os.path.dirname(os.path.realpath(__file__))
                dataname = self.p.tickersource
                dataname = dataname.replace("%{instrument}", self.p.dataname)
                dataname = os.path.join(modpath, '..', 'data', dataname)
            if hasattr(dataname, 'readline'):
                self.f = dataname
            else:
                # Let an exception propagate to let the caller know
                open(dataname, 'a').close()
                self.f = io.open(dataname, 'r')

        super(SharpPointCSVData, self).start()
        if self.p.newdata:
            self.f.seek(0, 2)
            self.forward()
            dt = datetime.datetime.now()
            self.lines.datetime[0] = date2num(dt)
            self.lines.open[0] = self.p.nullvalue
            self.lines.high[0] = self.p.nullvalue
            self.lines.low[0] = self.p.nullvalue
            self.lines.close[0] = self.p.nullvalue
            self.lines.volume[0] = self.p.nullvalue
            self.lines.openinterest[0] = self.p.nullvalue
        if self.p.streaming:
            self.o.streaming_prices(self.p.dataname)
Example #3
0
    def start(self):
        if self.f is None:
            if self.p.tickersource is None:
                dataname = self.p.dataname
            else:
                modpath = os.path.dirname(os.path.realpath(__file__))
                dataname = self.p.tickersource
                dataname = dataname.replace("%{instrument}",
                                            self.p.dataname)
                dataname = os.path.join(modpath, '..', 'data', dataname)
            if hasattr(dataname, 'readline'):
                self.f = dataname
            else:
                # Let an exception propagate to let the caller know
                open(dataname, 'a').close()
                self.f = io.open(dataname, 'r')

        super(SharpPointCSVData, self).start()
        if self.p.newdata:
            self.f.seek(0, 2)
            self.forward()
            dt = datetime.datetime.now()
            self.lines.datetime[0] = date2num(dt)
            self.lines.open[0] = self.p.nullvalue
            self.lines.high[0] = self.p.nullvalue
            self.lines.low[0] = self.p.nullvalue
            self.lines.close[0] = self.p.nullvalue
            self.lines.volume[0] = self.p.nullvalue
            self.lines.openinterest[0] = self.p.nullvalue
        if self.p.streaming:
            self.o.streaming_prices(self.p.dataname)
Example #4
0
 def _load_rtbar(self, rtbar, hist=False):
     res = super(IBDataAdjustTime, self)._load_rtbar(rtbar, hist)
     if res and hist:
         new_date = getstarttime(
             self._timeframe,
             self._compression,
             self.datetime.datetime(0),
             self.p.sessionstart,
             -1) - timedelta(microseconds=100)
         self.lines.datetime[0] = date2num(new_date)
     return res
    def _load_kline(self):
        try:
            kline = self._data.popleft()
        except IndexError:
            return None

        timestamp, open_, high, low, close, volume = kline

        self.lines.datetime[0] = date2num(timestamp)
        self.lines.open[0] = open_
        self.lines.high[0] = high
        self.lines.low[0] = low
        self.lines.close[0] = close
        self.lines.volume[0] = volume
        return True
Example #6
0
    def _load(self):
        try:
            bar = next(self.bars_iter)
        except StopIteration:
            return False

        self.l.datetime[0] = date2num(bar['date'])
        self.l.open[0] = bar['open']
        self.l.high[0] = bar['high']
        self.l.low[0] = bar['low']
        self.l.close[0] = bar['close']
        self.l.volume[0] = bar['volume']
        self.l.turn[0] = bar['turn'] or -1
        self.l.transNum[0] = bar['transNum'] or -1

        return True
Example #7
0
    def _loadline(self, linetokens):
        itoken = iter(linetokens)

        # pre places token are useless
        # next(itoken)
        # next(itoken)

        dttxt = next(itoken)
        dt = datetime(int(dttxt[0:4]), int(dttxt[5:7]), int(dttxt[8:]))
        self.lines.datetime[0] = date2num(dt)

        self.lines.open[0] = float(next(itoken))
        self.lines.high[0] = float(next(itoken))
        self.lines.low[0] = float(next(itoken))
        self.lines.close[0] = float(next(itoken))
        self.lines.volume[0] = float(next(itoken))
        self.lines.outstanding_share[0] = float(next(itoken))
        self.lines.turnover[0] = float(next(itoken))

        return True
Example #8
0
    def _load(self):
        if self.index >= len(self.table.index):
            return False
        else:
            for column in self.table.columns:
                if column == "date":
                    label = "datetime"
                    value = date2num(self.table[column].iloc[self.index])
                else:
                    label = column
                    value = self.table[column].iloc[self.index]

                try:
                    line = getattr(self.lines, label)
                except AttributeError:
                    continue  # don't worry about lines that are not used

                line[0] = value

            self.index += 1
            return True
Example #9
0
    def _loadline(self, linetokens):
        try:
            itoken = iter(linetokens)
            sdate = next(itoken)
            sopen = float(next(itoken))
            shigh = float(next(itoken))
            slow = float(next(itoken))
            sclose = float(next(itoken))
            svolume = int(next(itoken))

            ldata = [int(x) for x in sdate.split(self.p.separatordate)]
            self.lines.datetime[0] = date2num(datetime.datetime(*ldata))
            self.lines.open[0] = sopen
            self.lines.high[0] = shigh
            self.lines.low[0] = slow
            self.lines.close[0] = sclose
            self.lines.volume[0] = svolume
            self.lines.openinterest[0] = 0.0
            return True
        except Exception as e:
            print("error reading line", type(e), str(linetokens))
            self.error = "error reading line" + str(linetokens)
            return None
Example #10
0
    def _loadline(self, linetokens):
        try:
            itoken = iter(linetokens)
            sdate = next(itoken)
            sopen = float(next(itoken))
            shigh = float(next(itoken))
            slow = float(next(itoken))
            sclose = float(next(itoken))
            svolume = int(next(itoken))

            ldata = [int(x) for x in sdate.split(self.p.separatordate)]
            self.lines.datetime[0] = date2num(datetime.datetime(*ldata))
            self.lines.open[0] = sopen
            self.lines.high[0] = shigh
            self.lines.low[0] = slow
            self.lines.close[0] = sclose
            self.lines.volume[0] = svolume
            self.lines.openinterest[0] = 0.0
            return True
        except Exception as e:
            print("error reading line", type(e), str(linetokens))
            self.error = "error reading line" + str(linetokens)
            return None
Example #11
0
    def _loadline(self, linetokens):

        i = itertools.count(0)

        dttxt = linetokens[next(i)]  # YYYY-MM-DD or YYYY-MM-DD HH:MM:SS
        dt = date(int(dttxt[0:4]), int(dttxt[5:7]), int(dttxt[8:10]))
        tm = self.p.sessionend

        # use actual timestamp for INTRADAY time series
        if 'INTRADAY' in self.p.datatype:
            tm = time(int(dttxt[11:13]), int(dttxt[14:16]), int(dttxt[17:19]))

        self.lines.datetime[0] = date2num(datetime.combine(dt, tm))
        self.lines.open[0] = float(linetokens[next(i)])
        self.lines.high[0] = float(linetokens[next(i)])
        self.lines.low[0] = float(linetokens[next(i)])
        self.lines.close[0] = float(linetokens[next(i)])

        if 'ADJUSTED' in self.p.datatype:
            self.lines.adjclose[0] = float(linetokens[next(i)])
            self.lines.volume[0] = float(linetokens[next(i)])
            self.lines.div[0] = float(linetokens[next(i)])
            # no split data available for WEEKLY or MONTHLY time series
            if ('WEEKLY' in self.p.datatype) or ('MONTHLY' in self.p.datatype):
                self.lines.split[0] = 0.0
            else:
                self.lines.split[0] = float(linetokens[next(i)])
        else:
            self.lines.volume[0] = float(linetokens[next(i)])
            self.lines.adjclose[0] = 0.0
            self.lines.div[0] = 0.0
            self.lines.split[0] = 0.0

        self.lines.openinterest[0] = 0.0

        return True
Example #12
0
    def _load(self):
        if self._state == self._ST_LIVE:
            bar = self._getNextTick_()
            if bar is None:
                return None

            if self._influxdb and self.p.saveticks:
                self.setHistoInflux(bar)

            self.l.datetime[0] = date2num(bar['time'])

            self.l.open[0] = bar['bid']
            self.l.high[0] = bar['bid']
            self.l.low[0] = bar['bid']
            self.l.close[0] = bar['bid']
            self.l.volume[0] = 0.0
            self.l.openinterest[0] = 0.0

            return True

        elif self._state == self._ST_HISTORBACK:
            try:
                bar = next(self.biter)
            except StopIteration:
                print("Finish Historical Data")
                if self.p.historical:
                    print("End Feed data")
                    return False
                else:
                    print("Turn On Live Data")
                    self._state = self._ST_LIVE
                    return None

            if self._influxdb:
                local_tz = self._local_gmt

                dt_local = datetime.strptime(
                    bar['time'].replace("Z", "")[0:26],
                    "%Y-%m-%dT%H:%M:%S.%f")  # + timedelta(hours=local_tz)

                utc = timezone('UTC')
                dt_local = utc.localize(dt_local)

                self.l.datetime[0] = date2num(dt_local.astimezone())

                self.l.open[0] = bar['close']
                self.l.high[0] = bar['close']
                self.l.low[0] = bar['close']
                self.l.close[0] = bar['close']
                self.l.volume[0] = 0.0
                self.l.openinterest[0] = 0.0

            else:
                broker_tz = self._broker_gmt
                local_tz = self._local_gmt
                dt_local = datetime.strptime(
                    bar[0],
                    '%Y.%m.%d %H:%M') + timedelta(hours=local_tz - broker_tz)
                #dt_str = dt_local.astimezone().strftime("%Y-%m-%d %H:%M:%SZ")
                self.l.datetime[0] = date2num(dt_local.astimezone())

                self.l.open[0] = bar[1]
                self.l.high[0] = bar[1]
                self.l.low[0] = bar[1]
                self.l.close[0] = bar[1]
                self.l.volume[0] = 0.0
                self.l.openinterest[0] = 0.0

            return True

        return False