def testFormatPrintDateFromIntComponentsTimeDayMonthFourDigitsYearHourMinute( self): day = 17 month = 11 year = 2018 hour = 18 minute = 27 timezone = LOCAL_TIME_ZONE dateTimeFormat = 'DD/MM/YY HH:mm' dateDMY, dateHM = DateTimeUtil.formatPrintDateTimeFromIntComponents( day, month, year, hour, minute, timezone, dateTimeFormat) self.assertEqual('17/11/18', dateDMY) self.assertEqual('18:27', dateHM)
def testFormatPrintDateFromIntComponentsTimeDayMonthTwoDigitsYearHourMinute( self): day = 7 month = 9 year = 18 hour = 8 minute = 7 timezone = LOCAL_TIME_ZONE dateTimeFormat = 'DD/MM/YY HH:mm' dateDMY, dateHM = DateTimeUtil.formatPrintDateTimeFromIntComponents( day, month, year, hour, minute, timezone, dateTimeFormat) self.assertEqual('07/09/18', dateDMY) self.assertEqual('08:07', dateHM)
def testFormatPrintDateFromRealTimeIntComponents(self): day = 0 month = 0 year = 0 hour = 0 minute = 0 timezone = LOCAL_TIME_ZONE dateTimeFormat = 'DD/MM/YY HH:mm' dateDMY, dateHM = DateTimeUtil.formatPrintDateTimeFromIntComponents( day, month, year, hour, minute, timezone, dateTimeFormat) now = DateTimeUtil.localNow(LOCAL_TIME_ZONE) nowYearStr, nowMonthStr, nowDayStr, nowHourStr, nowMinuteStr = UtilityForTest.getFormattedDateTimeComponentsForArrowDateTimeObj( now) self.assertEqual('{}/{}/{}'.format(nowDayStr, nowMonthStr, nowYearStr), dateDMY) self.assertEqual('{}:{}'.format(nowHourStr, nowMinuteStr), dateHM)
def _computeOptionFiatAmount(self, resultData, fiat, fiatExchange, crypto, unit, cryptoUnitExchange, year, month, day, hour, minute, dateTimeFormat, localTz): """ Compute the optionFiatAmount according to the passed parms and put the result in the passed resultData. :param resultData: :param fiat: :param fiatExchange: :param crypto: :param unit: :param cryptoUnitExchange: used in case the -f fiat symbol equals either the crypto or the unit symbol :return: a ResultData in which price value info has been added. """ if fiatExchange == None: if fiat == unit or \ fiat == crypto: fiatExchange = cryptoUnitExchange else: fiatExchange = 'CCCAGG' if fiat == unit: fiatConversionRate = 1 return self._calculateAndStoreFiatData(fiat, fiatConversionRate, fiatExchange, resultData) elif fiat == crypto and fiatExchange == cryptoUnitExchange: # memorizing previously obtained crypto/unit price cryptoUnitPrice = resultData.getValue(resultData.RESULT_KEY_PRICE) fiatResultData = self._getPrice(unit, fiat, fiatExchange, year, month, day, hour, minute, dateTimeFormat, localTz) if fiatResultData.noError(): # indicates that unit/fiat pair is supported by the fiatExchange which equals # the cryptoUnitExchange fiatConversionRate = cryptoUnitPrice else: # indicates that fiat/unit pair is supported by the fiatExchange which equals # the cryptoUnitExchange and that the fiatConversionRate is the inverse of # cryptoUnitPrice # Example: # Request: eth usd 19/02/18 kraken -v0.3821277eth -feth # Unit = USD, Fiat = ETH # Result: 0.3821277 ETH/359.44459973 USD/0.3821277 ETH on Kraken: 19/02/18 00:00C 940.64 1 # Computation: 940.64 ETH/USD * 0.00106311 USD/ETH = 1 ETH/ETH # USD/ETH = 1 / ETH/USD fiatConversionRate = 1 / cryptoUnitPrice return self._calculateAndStoreFiatData(fiat, fiatConversionRate, fiatExchange, resultData) fiatResultData = self._getPrice(unit, fiat, fiatExchange, year, month, day, hour, minute, dateTimeFormat, localTz) if fiatResultData.noError(): fiatConversionRate = fiatResultData.getValue(resultData.RESULT_KEY_PRICE) return self._calculateAndStoreFiatData(fiat, fiatConversionRate, fiatExchange, resultData) else: # since fiat/unit is not supported by the exchange, we try to request the unit/fiat inverted rate fiatResultData = self._getPrice(fiat, unit, fiatExchange, year, month, day, hour, minute, dateTimeFormat, localTz) if fiatResultData.noError(): fiatConversionRate = 1 / fiatResultData.getValue(resultData.RESULT_KEY_PRICE) return self._calculateAndStoreFiatData(fiat, fiatConversionRate, fiatExchange, resultData) else: errorMsg = fiatResultData.getErrorMessage() dateDMY, dateHM = DateTimeUtil.formatPrintDateTimeFromIntComponents(day, month, year, hour, minute, localTz, dateTimeFormat) if 'market does not exist for this coin pair' in errorMsg: errorMsg = 'PROVIDER ERROR - fiat option coin pair {}/{} or {}/{} not supported by exchange {} on date {} {}'.format(fiat, unit, unit, fiat, fiatExchange, dateDMY, dateHM) else: # making the error msg specific to the fiat option errorMsg = errorMsg.replace('Requesting', 'Requesting fiat option coin pair {}/{} or'.format(unit, fiat)) errorMsg += ' on date {} {}'.format(dateDMY, dateHM) fiatResultData.setError(errorMsg) return fiatResultData
def _getPrice(self, currency, targetCurrency, exchange, year, month, day, hour, minute, dateTimeFormat, localTz, optionPriceAmount=None, optionPriceSaveFlag=None): ''' Returns the price of 1 unit of currency in targetCurrency. Ex: currency == btc, targetCurrency == usd --> returned price: 1 btc == 10000 usd. :param currency: :param targetCurrency: :param exchange: :param year: :param month: :param day: :param hour: :param minute: :param dateTimeFormat: :param localTz: :param optionPriceAmount: :param optionPriceSaveFlag: used to improve option price warning in case set in conjunction with RT request. :seqdiag_return ResultData :return: ''' if (day + month + year) == 0: # when the user specifies 0 for the date, this means current price is asked and # date components are set to zero ! resultData = self.priceRequester.getCurrentPrice(currency, targetCurrency, exchange) if resultData.noError(): # adding date time info if no error returned timeStamp = resultData.getValue(ResultData.RESULT_KEY_PRICE_TIME_STAMP) requestedPriceArrowLocalDateTime = DateTimeUtil.timeStampToArrowLocalDate(timeStamp, localTz) requestedDateTimeStr = requestedPriceArrowLocalDateTime.format(dateTimeFormat) resultData.setValue(ResultData.RESULT_KEY_PRICE_DATE_TIME_STRING, requestedDateTimeStr) if optionPriceAmount: # option price not compatible with RT request ! if optionPriceSaveFlag: optionStrForWarning = '-ps' else: optionStrForWarning = '-p' resultData.setWarning(ResultData.WARNING_TYPE_OPTION_PRICE, "WARNING - option {}{} is not compatible with real time request. {} option ignored.".format( optionStrForWarning, optionPriceAmount, optionStrForWarning)) else: # getting historical price, either histo day or histo minute timeStampLocal = DateTimeUtil.dateTimeComponentsToTimeStamp(day, month, year, hour, minute, 0, localTz) timeStampUtcNoHHMM = DateTimeUtil.dateTimeComponentsToTimeStamp(day, month, year, 0, 0, 0, 'UTC') resultData = self.priceRequester.getHistoricalPriceAtUTCTimeStamp(currency, targetCurrency, timeStampLocal, localTz, timeStampUtcNoHHMM, exchange) if resultData.noError(): # adding date time info if no error returned if resultData.getValue(ResultData.RESULT_KEY_PRICE_TYPE) == ResultData.PRICE_TYPE_HISTO_MINUTE: # histominute price returned requestedPriceArrowLocalDateTime = DateTimeUtil.timeStampToArrowLocalDate(timeStampLocal, localTz) requestedDateTimeStr = requestedPriceArrowLocalDateTime.format(dateTimeFormat) elif optionPriceAmount: # here, the -p price option is active and the transaction date with minute # precision must be set in the returned ResultData requestedPriceArrowLocalDateTime = DateTimeUtil.timeStampToArrowLocalDate(timeStampLocal, localTz) requestedDateTimeStr = requestedPriceArrowLocalDateTime.format(dateTimeFormat) else: # histoday price returned requestedPriceArrowUtcDateTime = DateTimeUtil.timeStampToArrowLocalDate(timeStampUtcNoHHMM, 'UTC') requestedDateTimeStr = requestedPriceArrowUtcDateTime.format(dateTimeFormat) resultData.setValue(ResultData.RESULT_KEY_PRICE_DATE_TIME_STRING, requestedDateTimeStr) price = resultData.getValue(ResultData.RESULT_KEY_PRICE) if price == 0: dateDMY, dateHM = DateTimeUtil.formatPrintDateTimeFromIntComponents(day, month, year, hour, minute, localTz, dateTimeFormat) resultData.setError('PROVIDER ERROR - Requesting {}/{} price for date {} {} on exchange {} returned invalid value 0'.format(currency, targetCurrency, dateDMY, dateHM, exchange)) return resultData