Exemple #1
0
    def addBar(self, instrument, bar, frequency):
        instrument = normalize_instrument(instrument)
        instrumentId = self.__getOrCreateInstrument(instrument)
        timeStamp = dt.datetime_to_timestamp(bar.getDateTime())

        try:
            sql = "insert into bar (instrument_id, frequency, timestamp, open, high, low, close, volume, adj_close) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
            params = [
                instrumentId, frequency, timeStamp,
                bar.getOpen(),
                bar.getHigh(),
                bar.getLow(),
                bar.getClose(),
                bar.getVolume(),
                bar.getAdjClose()
            ]
            self.__connection.execute(sql, params)
        except sqlite3.IntegrityError:
            sql = "update bar set open = ?, high = ?, low = ?, close = ?, volume = ?, adj_close = ?" \
                " where instrument_id = ? and frequency = ? and timestamp = ?"
            params = [
                bar.getOpen(),
                bar.getHigh(),
                bar.getLow(),
                bar.getClose(),
                bar.getVolume(),
                bar.getAdjClose(), instrumentId, frequency, timeStamp
            ]
            self.__connection.execute(sql, params)
Exemple #2
0
 def onBars(self, dateTime, bars):
     # Update the volume available for each instrument.
     volumeLeft = {}
     for instrument in bars.getInstruments():
         bar = bars[instrument]
         if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
             volumeLeft[instrument] = bar.getVolume()
         elif self.__volumeLimit is not None:
             volumeLeft[instrument] = bar.getVolume() * self.__volumeLimit
     self.__volumeLeft = volumeLeft
    def onBars(self, broker_, bars):
        volumeLeft = {}

        for instrument in bars.getInstruments():
            bar = bars[instrument]
            # Update the volume available for each instrument.
            if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
                volumeLeft[instrument] = bar.getVolume()
            elif self.__volumeLimit is not None:
                volumeLeft[instrument] = bar.getVolume() * self.__volumeLimit
            # Update the volume used for each instrument.
            self.__volumeUsed[instrument] = 0.0

        self.__volumeLeft = volumeLeft
Exemple #4
0
def adjustBars(instrument, bars, startdate, enddate):

    bars = []
    bars_in_dtrange = [bar for bar in bars if startdate.replace(tzinfo=None) <= bar.getDateTime() <= enddate.replace(tzinfo=None)]
    bars_in_dtrange.sort(key=lambda bar: bar.getDateTime(), reverse=True)
    k = 0
    splitdataList = []
    dividendList = []
    for bar in bars_in_dtrange:
        splitdata = bar.getSplit()
        dividend = bar.getDividend()
        if splitdata != 1.0:
            splitdataList.append(bar.getSplit())
        if dividend != 0.0:
            adjFactor = (bar.getClose() + bar.getDividend()) / bar.getClose()
            dividendList.append(adjFactor)
        #### Special case.... end date / analysis date nothing to do..
        if (k==0):
            bar = BasicBar(bar.getDateTime(), 
                    bar.getOpen() , bar.getHigh(), bar.getLow(), bar.getClose(), bar.getVolume(), bar.getClose(), Frequency.DAY)
            bars.append(bar)
        else:
            #### Adjust OHLC & Volume data for split adjustments and dividend adjustments
            Open = bar.getOpen()
            High = bar.getHigh()
            Low  = bar.getLow()
            Close = bar.getClose()
            Volume = bar.getVolume()
            ### adjust data for splits
            for split in splitdataList:
                Open = Open / split
                High = High / split
                Low  = Low / split
                Close = Close /split
                Volume = Volume * split

            ### adjust data for dividends
            for adjFactor in dividendList:
                Open = Open / adjFactor
                High = High / adjFactor
                Low  = Low / adjFactor
                Close = Close / adjFactor
                Volume = Volume * adjFactor

            bar = BasicBar(bar.getDateTime(), 
                    Open , High, Low, Close, Volume, Close, Frequency.DAY)
            bars.append(bar)
        k +=1
        feed = Feed(Frequency.DAY, 1024)
        return feed.loadBars(instrument+"_adjusted", bars)
Exemple #5
0
    def onBars(self, broker_, bars):
        volumeLeft = {}

        for instrument in bars.getInstruments():
            bar = bars[instrument]
            # Reset the volume available for each instrument.
            if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
                volumeLeft[instrument] = bar.getVolume()
            elif self.__volumeLimit is not None:
                # We can't round here because there is no order to request the instrument traits.
                volumeLeft[instrument] = bar.getVolume() * self.__volumeLimit
            # Reset the volume used for each instrument.
            self.__volumeUsed[instrument] = 0.0

        self.__volumeLeft = volumeLeft
Exemple #6
0
    def onBars(self, broker_, bars):
        volumeLeft = {}

        for instrument in bars.getInstruments():
            bar = bars[instrument]
            # Reset the volume available for each instrument.
            if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
                volumeLeft[instrument] = bar.getVolume()
            elif self.__volumeLimit is not None:
                # We can't round here because there is no order to request the instrument traits.
                volumeLeft[instrument] = bar.getVolume() * self.__volumeLimit
            # Reset the volume used for each instrument.
            self.__volumeUsed[instrument] = 0.0

        self.__volumeLeft = volumeLeft
Exemple #7
0
    def __getFillSize(self, order, bar):
        if self.__volumeLimit is not None:
            # If these are trade bars, then allow the whole volume to be used.
            if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
                maxQuantity = bar.getVolume()
            else:
                maxQuantity = bar.getVolume() * self.__volumeLimit

            if order.getQuantity() > maxQuantity:
                # Partial fills not supported yet.
                ret = None
            else:
                ret = order.getQuantity()
        else:
            ret = order.getQuantity()
        return ret
Exemple #8
0
 def __getFillSize(self, order, bar):
     if self.__volumeLimit != None:
         maxQuantity = bar.getVolume() * self.__volumeLimit
         if order.getQuantity() > maxQuantity:
             # Partial fills not supported yet.
             ret = None
         else:
             ret = order.getQuantity()
     else:
         ret = order.getQuantity()
     return ret
 def __getFillSize(self, order, bar):
     if self.__volumeLimit is not None:
         maxQuantity = bar.getVolume() * self.__volumeLimit
         if order.getQuantity() > maxQuantity:
             # Partial fills not supported yet.
             ret = None
         else:
             ret = order.getQuantity()
     else:
         ret = order.getQuantity()
     return ret
Exemple #10
0
    def onBars(self, broker_, bars):
        self.__volumeUsed = {}
        self.__volumeLeft = {}

        # Reset volumes.
        for bar in bars.getBars():
            instrument = bar.getInstrument()

            self.__volumeUsed[instrument] = 0.0

            if self.__volumeLimit is not None:
                # For TRADE bars we can use all the volume available.
                if bar.getFrequency() == pyalgotrade.bar.Frequency.TRADE:
                    volumeLeft = bar.getVolume()
                else:
                    volumeLeft = self.__instrumentTraits.round(
                        bar.getVolume() * self.__volumeLimit,
                        bar.getInstrument().symbol,
                        roundDown=True)
                self.__volumeLeft[instrument] = volumeLeft
Exemple #11
0
 def onBars(self, bars):
     for instrument in bars.getInstruments():
         bar = bars[instrument]
         self.info("%s: %s %s %s %s %s %s" % (
             instrument,
             bar.getOpen(),
             bar.getHigh(),
             bar.getLow(),
             bar.getClose(),
             bar.getAdjClose(),
             bar.getVolume(),
         ))
    def addBar(self, instrument, bar, frequency):
        instrument = normalize_instrument(instrument)
        instrumentId = self.__getOrCreateInstrument(instrument)
        timeStamp = dt.datetime_to_timestamp(bar.getDateTime())

        try:
            sql = "insert into bar (instrument_id, frequency, timestamp, open, high, low, close, volume, adj_close) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
            params = [instrumentId, frequency, timeStamp, bar.getOpen(), bar.getHigh(), bar.getLow(), bar.getClose(), bar.getVolume(), bar.getAdjClose()]
            self.__connection.execute(sql, params)
        except sqlite3.IntegrityError:
            sql = "update bar set open = ?, high = ?, low = ?, close = ?, volume = ?, adj_close = ?" \
                " where instrument_id = ? and frequency = ? and timestamp = ?"
            params = [bar.getOpen(), bar.getHigh(), bar.getLow(), bar.getClose(), bar.getVolume(), bar.getAdjClose(), instrumentId, frequency, timeStamp]
            self.__connection.execute(sql, params)
Exemple #13
0
    def onBars(self, bars):
        try:
            symbol = bars.getInstruments()[0]
            bar = bars[symbol]
               
            self.__ticker.append((bar.getDateTime().strftime('%Y-%m-%d %H:%M:%S.%f'), symbol.split('coinigy-')[1], bar.getPrice(), bar.getVolume(), 1 if bar.isBuy() else 2))

            #logging.info('symbol: %r,  frequency: %r, volume: %r' %(symbol, bar.getFrequency(), bar.getVolume())) 

        except Exception as e:
            logging.error('%r', e)
        except:
            logging.error('Exception caught!')
	def adjustBars(self):

		for key, value in self.__barsDict.iteritems():

			basicbars = []
			bars = value
			bars_in_dtrange = [bar for bar in bars if self.__startdate.replace(tzinfo=None) <= bar.getDateTime() <= self.__enddate.replace(tzinfo=None)]
			bars_in_dtrange.sort(key=lambda bar: bar.getDateTime(), reverse=True)

			k = 0
			splitdataList = []
			dividendList = []

			for bar in bars_in_dtrange:
				splitdata = float(bar.getSplit())
				dividend = float(bar.getDividend())
				if splitdata != 1.0:
					splitdataList.append(bar.getSplit())
				if dividend != 0.0:
					adjFactor = (bar.getClose() + bar.getDividend()) / bar.getClose()
					dividendList.append(adjFactor)
				#### Special case.... end date / analysis date nothing to do..
				if (k==0):
					bar = BasicBar(bar.getDateTime(), 
						bar.getOpen() , bar.getHigh(), bar.getLow(), bar.getClose(), bar.getVolume(), bar.getClose(), Frequency.DAY)
					basicbars.append(bar)
				else:
					#### Adjust OHLC & Volume data for split adjustments and dividend adjustments
					Open = bar.getOpen()
					High = bar.getHigh()
					Low  = bar.getLow()
					Close = bar.getClose()
					Volume = bar.getVolume()
					### adjust data for splits
					for split in splitdataList:
						Open = Open / split
						High = High / split
						Low  = Low / split
						Close = Close /split
						Volume = Volume * split

					### adjust data for dividends
					for adjFactor in dividendList:
						Open = Open / adjFactor
						High = High / adjFactor
						Low  = Low / adjFactor
						Close = Close / adjFactor
						Volume = Volume * adjFactor
					bar = BasicBar(bar.getDateTime(), 
						Open , High, Low, Close, Volume, Close, Frequency.DAY)
					basicbars.append(bar)
				k +=1


			DateTimes = []
			OpenSeries = SequenceDataSeries(4000)
			HighSeries = SequenceDataSeries(4000)
			LowSeries =  SequenceDataSeries(4000)
			CloseSeries = SequenceDataSeries(4000)
			VolumeSeries = SequenceDataSeries(4000)
			TypicalSeries = SequenceDataSeries(4000)
			barSeries = BarDataSeries(4000)
			basicbars.sort(key=lambda bar: bar.getDateTime(), reverse=False)
			

			for bar in basicbars:
				DateTimes.append(bar.getDateTime())
				OpenSeries.appendWithDateTime(bar.getDateTime(), bar.getOpen())
				HighSeries.appendWithDateTime(bar.getDateTime(), bar.getHigh())
				LowSeries.appendWithDateTime(bar.getDateTime(), bar.getLow())
				CloseSeries.appendWithDateTime(bar.getDateTime(), bar.getClose())
				VolumeSeries.appendWithDateTime(bar.getDateTime(), bar.getVolume())
				TypicalSeries.appendWithDateTime(bar.getDateTime(), (bar.getClose()+bar.getHigh()+bar.getLow())/3.0)
				barSeries.appendWithDateTime(bar.getDateTime(), bar)


			self.__DateTimes[key+"_adjusted"] = DateTimes
			self.__OpenDataSeries[key+"_adjusted"] = OpenSeries
			self.__HighDataSeries[key+"_adjusted"] = HighSeries
			self.__LowDataSeries[key+"_adjusted"] =  LowSeries
			self.__CloseDataSeries[key+"_adjusted"] = CloseSeries
			self.__VolumeDataSeries[key+"_adjusted"] = VolumeSeries
			self.__TypicalDataSeries[key+"_adjusted"] = TypicalSeries
			self.__barSeries[key+"_adjusted"] = barSeries
Exemple #15
0
 def __addBar(self, symbol, bar, cursor):
     string = ("INSERT INTO data (`symbol`, `date`,`milliseconds`, `open`, `close`, `high`, `low`, `volume`) " + 
               "VALUES ('{0:s}', '{1:%Y-%m-%d %H:%M:%S}', '{4:d}','{2:f}', '{2:f}', '{2:f}', '{2:f}', '{3:f}')")
     queryStr = string.format(symbol, bar.getDateTime(), bar.getClose(), bar.getVolume(), bar.getMilliSeconds())
     cursor.execute(queryStr)