本文整理汇总了Python中pyalgotrade.utils.dt.localize函数的典型用法代码示例。如果您正苦于以下问题:Python localize函数的具体用法?Python localize怎么用?Python localize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了localize函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testResampleNinjaTraderHour
def testResampleNinjaTraderHour(self):
# Resample.
feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
feed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
resampledBarDS = resampled.ResampledBarDataSeries(feed["spy"], barfeed.Frequency.HOUR)
resampledFile = os.path.join(common.get_temp_path(), "hour-nt-spy-minute-2011.csv")
resample.resample_to_csv(feed, barfeed.Frequency.HOUR, resampledFile)
resampledBarDS.pushLast() # Need to manually push the last stot since time didn't change.
# Load the resampled file.
feed = csvfeed.GenericBarFeed(barfeed.Frequency.HOUR, marketsession.USEquities.getTimezone())
feed.addBarsFromCSV("spy", resampledFile)
feed.loadAll()
self.assertEqual(len(feed["spy"]), 340)
self.assertEqual(feed["spy"][0].getDateTime(), dt.localize(datetime.datetime(2011, 1, 3, 9), marketsession.USEquities.getTimezone()))
self.assertEqual(feed["spy"][-1].getDateTime(), dt.localize(datetime.datetime(2011, 2, 1, 1), marketsession.USEquities.getTimezone()))
self.assertEqual(feed["spy"][0].getOpen(), 126.35)
self.assertEqual(feed["spy"][0].getHigh(), 126.45)
self.assertEqual(feed["spy"][0].getLow(), 126.3)
self.assertEqual(feed["spy"][0].getClose(), 126.4)
self.assertEqual(feed["spy"][0].getVolume(), 3397.0)
self.assertEqual(feed["spy"][0].getAdjClose(), None)
self.assertEqual(len(resampledBarDS), len(feed["spy"]))
self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(datetime.datetime(2011, 1, 3, 9)))
self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(datetime.datetime(2011, 2, 1, 1)))
示例2: testResampleNinjaTraderDay
def testResampleNinjaTraderDay(self):
# Resample.
feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
feed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
resampledBarDS = resampled.ResampledBarDataSeries(feed["spy"], barfeed.Frequency.DAY)
resampledFile = os.path.join(common.get_temp_path(), "day-nt-spy-minute-2011.csv")
resample.resample_to_csv(feed, barfeed.Frequency.DAY, resampledFile)
resampledBarDS.pushLast() # Need to manually push the last stot since time didn't change.
# Load the resampled file.
feed = csvfeed.GenericBarFeed(barfeed.Frequency.DAY)
feed.addBarsFromCSV("spy", resampledFile, marketsession.USEquities.getTimezone())
feed.loadAll()
self.assertEqual(len(feed["spy"]), 25)
self.assertEqual(
feed["spy"][0].getDateTime(),
dt.localize(datetime.datetime(2011, 1, 3), marketsession.USEquities.getTimezone()),
)
self.assertEqual(
feed["spy"][-1].getDateTime(),
dt.localize(datetime.datetime(2011, 2, 1), marketsession.USEquities.getTimezone()),
)
self.assertEqual(len(resampledBarDS), len(feed["spy"]))
self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(datetime.datetime(2011, 1, 3)))
self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(datetime.datetime(2011, 2, 1)))
示例3: __init__
def __init__(self, dateTime):
self.__begin = datetime.datetime(dateTime.year, dateTime.month, 1)
# Calculate the ending date.
if dateTime.month == 12:
self.__end = datetime.datetime(dateTime.year + 1, 1, 1)
else:
self.__end = datetime.datetime(dateTime.year, dateTime.month + 1, 1)
if not dt.datetime_is_naive(dateTime):
self.__begin = dt.localize(self.__begin, dateTime.tzinfo)
self.__end = dt.localize(self.__end, dateTime.tzinfo)
示例4: __testHourRangeImpl
def __testHourRangeImpl(self, timezone=None):
freq = bar.Frequency.HOUR
begin = datetime.datetime(2011, 1, 1, 16)
end = datetime.datetime(2011, 1, 1, 17)
if timezone is not None:
begin = dt.localize(begin, timezone)
end = dt.localize(end, timezone)
r = resamplebase.build_range(begin + datetime.timedelta(seconds=120), freq)
self.assertEqual(r.getBeginning(), begin)
for i in range(freq):
self.assertTrue(r.belongs(begin + datetime.timedelta(seconds=i)))
self.assertFalse(r.belongs(begin + datetime.timedelta(seconds=freq+1)))
self.assertEqual(r.getEnding(), end)
示例5: testFeedWithQuandl
def testFeedWithQuandl(self):
class RowFilter(csvfeed.RowFilter):
def includeRow(self, dateTime, values):
return dateTime.year == 2013
feed = csvfeed.Feed("Date", "%Y-%m-%d", maxLen=40, timezone=marketsession.USEquities.timezone)
feed.setRowFilter(RowFilter())
feed.setTimeDelta(datetime.timedelta(hours=23, minutes=59, seconds=59))
feed.addValuesFromCSV(common.get_data_file_path("quandl_gold_2.csv"))
for col in ["USD", "GBP", "EUR"]:
self.assertEqual(len(feed[col]), 0)
dispatcher = observer.Dispatcher()
dispatcher.addSubject(feed)
dispatcher.run()
for col in ["USD", "GBP", "EUR"]:
self.assertEqual(len(feed[col]), 39)
self.assertEqual(feed["USD"][-1], 1333.0)
self.assertEqual(feed["GBP"][-1], 831.203)
self.assertEqual(feed["EUR"][-1], 986.75)
self.assertFalse(dt.datetime_is_naive(feed["USD"].getDateTimes()[-1]))
self.assertEqual(feed["USD"].getDateTimes()[-1], dt.localize(datetime.datetime(2013, 9, 29, 23, 59, 59), marketsession.USEquities.timezone))
示例6: getBars
def getBars(self, instrument, frequency, timezone=None, fromDateTime=None, toDateTime=None):
instrument = normalize_instrument(instrument)
sql = "select bar.timestamp, bar.open, bar.high, bar.low, bar.close, bar.volume, bar.adj_close, bar.frequency" \
" from bar join instrument on (bar.instrument_id = instrument.instrument_id)" \
" where instrument.name = ? and bar.frequency = ?"
args = [instrument, frequency]
if fromDateTime is not None:
sql += " and bar.timestamp >= ?"
args.append(dt.datetime_to_timestamp(fromDateTime))
if toDateTime is not None:
sql += " and bar.timestamp <= ?"
args.append(dt.datetime_to_timestamp(toDateTime))
sql += " order by bar.timestamp asc"
cursor = self.__connection.cursor()
cursor.execute(sql, args)
ret = []
for row in cursor:
dateTime = dt.timestamp_to_datetime(row[0])
if timezone:
dateTime = dt.localize(dateTime, timezone)
ret.append(bar.BasicBar(dateTime, row[1], row[2], row[3], row[4], row[5], row[6], row[7]))
cursor.close()
return ret
示例7: __parseDate
def __parseDate(self, dateString):
datetime_format = "%Y-%m-%d %H:%M:%S"
ret = datetime.datetime.strptime(dateString, datetime_format)
# Localize the datetime if a timezone was given.
if self.__timezone:
ret = dt.localize(ret, self.__timezone)
return ret
示例8: testUnrealized
def testUnrealized(self):
barFeed = self.loadIntradayBarFeed()
strat = TestStrategy(barFeed, 1000)
strat.setExitOnSessionClose(False)
# 3/Jan/2011 205300 - Enter long
# 3/Jan/2011 205400 - entry gets filled at 127.21
# 3/Jan/2011 210000 - last bar
strat.addPosEntry(
dt.localize(datetime.datetime(2011, 1, 3, 20, 53), pytz.utc),
strat.enterShort,
StrategyTestCase.TestInstrument,
1,
True,
)
strat.run()
self.assertTrue(strat.getEnterOkEvents() == 1)
self.assertTrue(strat.getExitOkEvents() == 0)
self.assertTrue(strat.getEnterCanceledEvents() == 0)
self.assertTrue(strat.getExitCanceledEvents() == 0)
self.assertEqual(strat.getActivePosition().getUnrealizedReturn(127.21), 0)
self.assertEqual(strat.getActivePosition().getUnrealizedNetProfit(127.21), 0)
self.assertEqual(round(strat.getActivePosition().getUnrealizedReturn(127.21 / 2), 4), 0.5)
self.assertEqual(round(strat.getActivePosition().getUnrealizedNetProfit(127.21 / 2), 4), 127.21 / 2)
self.assertEqual(strat.getActivePosition().getUnrealizedReturn(127.21 * 2), -1)
self.assertEqual(strat.getActivePosition().getUnrealizedNetProfit(127.21 * 2), -127.21)
示例9: get_slot_datetime
def get_slot_datetime(dateTime, frequency):
ts = dt.datetime_to_timestamp(dateTime)
slot = ts / frequency
slotTs = (slot + 1) * frequency - 1
ret = dt.timestamp_to_datetime(slotTs, False)
if not dt.datetime_is_naive(dateTime):
ret = dt.localize(ret, dateTime.tzinfo)
return ret
示例10: __parseDate
def __parseDate(self, dateString):
ret = parse_date(dateString,self.__frequency)
# Time on Yahoo! Finance CSV files is empty. If told to set one, do it.
if self.__frequency == bar.Frequency.DAY and self.__dailyBarTime is not None:
ret = datetime.datetime.combine(ret, self.__dailyBarTime)
# Localize the datetime if a timezone was given.
if self.__timezone:
ret = dt.localize(ret, self.__timezone)
return ret
示例11: _parseDate
def _parseDate(self, dateString):
ret = datetime.datetime.strptime(dateString, self.__dateTimeFormat)
if self.__dailyBarTime is not None:
ret = datetime.datetime.combine(ret, self.__dailyBarTime)
# Localize the datetime if a timezone was given.
if self.__timezone:
ret = dt.localize(ret, self.__timezone)
return ret
示例12: __parseDate
def __parseDate(self, dateString):
ret = datetime.datetime.strptime(dateString, "%Y-%m-%d")
# Time on Yahoo! Finance CSV files is empty. If told to set one, do it.
if self.__dailyBarTime != None:
ret = datetime.datetime.combine(ret, self.__dailyBarTime)
# Localize the datetime if a timezone was given.
if self.__timezone:
ret = dt.localize(ret, self.__timezone)
return ret
示例13: __parseDate
def __parseDate(self, dateString):
ret = parse_date(dateString)
# Time on Google Finance CSV files is empty. If told to set one, do it.
if self.__dailyBarTime is not None:
ret = datetime.datetime.combine(ret, self.__dailyBarTime)
# Localize the datetime if a timezone was given.
if self.__timezone:
ret = dt.localize(ret, self.__timezone)
return ret
示例14: __parseDate
def __parseDate(self, dateString):
dt_, d_ = parse_date(dateString)
# Time on Yahoo! Finance CSV files is empty. If told to set one, do it.
if self.__dailyBarTime != None:
dt_ = datetime.datetime.combine(dt_, self.__dailyBarTime)
# Localize the datetime if a timezone was given.
if self.__timezone:
dt_ = dt.localize(dt_, self.__timezone)
return dt_, d_
示例15: __parseDate
def __parseDate(self, dateString):
ret = self.parse_date(dateString)
# Time on Yahoo! Finance CSV files is empty. If told to set one, do it.
#mid 下面这个过程将ret带h:m:s的时间弄成无h:m:s,使只能测试日线
if(False):
if self.__dailyBarTime is not None:
ret = datetime.datetime.combine(ret, self.__dailyBarTime)
# Localize the datetime if a timezone was given.
if self.__timezone:
ret = dt.localize(ret, self.__timezone)
return ret