本文整理汇总了Python中pyalgotrade.utils.dt.as_utc函数的典型用法代码示例。如果您正苦于以下问题:Python as_utc函数的具体用法?Python as_utc怎么用?Python as_utc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了as_utc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testResampleMtGoxDay
def testResampleMtGoxDay(self):
# Resample.
feed = mtgoxfeed.CSVTradeFeed()
feed.addBarsFromCSV(common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv"))
resampledBarDS = resampled.ResampledBarDataSeries(feed["BTC"], barfeed.Frequency.DAY)
resampledFile = os.path.join(common.get_temp_path(), "day-mgtox-usd-2013-01-01.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("BTC", resampledFile)
feed.loadAll()
self.assertEqual(len(feed["BTC"]), 1)
self.assertEqual(feed["BTC"][0].getDateTime(), datetime.datetime(2013, 1, 1))
self.assertEqual(feed["BTC"][0].getOpen(), 13.51001)
self.assertEqual(feed["BTC"][0].getHigh(), 13.56)
self.assertEqual(feed["BTC"][0].getLow(), 13.16123)
self.assertEqual(feed["BTC"][0].getClose(), 13.30413)
self.assertEqual(feed["BTC"][0].getVolume(), 28168.9114596)
self.assertEqual(feed["BTC"][0].getAdjClose(), 13.30413)
self.assertEqual(len(resampledBarDS), len(feed["BTC"]))
self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(feed["BTC"][0].getDateTime()))
self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(feed["BTC"][-1].getDateTime()))
self.assertEqual(resampledBarDS[0].getOpen(), feed["BTC"][0].getOpen())
self.assertEqual(resampledBarDS[0].getHigh(), feed["BTC"][0].getHigh())
self.assertEqual(resampledBarDS[0].getLow(), feed["BTC"][0].getLow())
self.assertEqual(resampledBarDS[0].getClose(), feed["BTC"][0].getClose())
self.assertEqual(round(resampledBarDS[0].getVolume(), 5), round(feed["BTC"][0].getVolume(), 5))
self.assertEqual(resampledBarDS[0].getAdjClose(), feed["BTC"][0].getAdjClose())
示例2: 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)))
示例3: load_intraday_bars
def load_intraday_bars():
global feed
print "Loading bars from file"
feed = ninjatraderfeed.Feed(barfeed.Frequency.MINUTE)
feed.setBarFilter(csvfeed.DateRangeFilter(dt.as_utc(datetime.datetime(2008, 1, 1)), dt.as_utc(datetime.datetime(2008, 12, 31))))
feed.addBarsFromCSV(instrument, "/Users/gabo/Downloads/etf-quotes/SPY.Last.txt")
示例4: 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)))
示例5: testMarketTimes
def testMarketTimes(self):
# 9:30 in GMT-5
dateTime = dt.as_utc(datetime.datetime(2013, 1, 1, 9+5, 30))
self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "XNYS")), datetime.datetime(2013, 1, 1, 9, 30))
self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "XASE")), datetime.datetime(2013, 1, 1, 9, 30))
self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "XNAS")), datetime.datetime(2013, 1, 1, 9, 30))
self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "XNYS")), datetime.datetime(2013, 1, 1, 9, 30))
# 8:00 in GMT
dateTime = dt.as_utc(datetime.datetime(2013, 1, 1, 8))
self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "CHIX")), datetime.datetime(2013, 1, 1, 8))
# From Apr~Oct CHIX is GMT+1
dateTime = dt.as_utc(datetime.datetime(2013, 4, 1, 8))
self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "CHIX")), datetime.datetime(2013, 4, 1, 9))
示例6: testLoadFilterFromAndTo
def testLoadFilterFromAndTo(self):
feed = barfeed.CSVTradeFeed()
feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"), "bitstampUSD", fromDateTime=dt.as_utc(datetime.datetime(2012, 5, 29)), toDateTime=datetime.datetime(2012, 5, 31))
loaded = [(dateTime, bars) for dateTime, bars in feed]
self.assertEquals(len(loaded), 579)
self.assertEquals(loaded[0][0], dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
self.assertEquals(loaded[0][1]["bitstampUSD"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
self.assertEquals(loaded[0][1]["bitstampUSD"].getClose(), 5.07)
self.assertEquals(loaded[0][1]["bitstampUSD"].getVolume(), 1.39081288)
self.assertEquals(loaded[-1][0], dt.as_utc(datetime.datetime(2012, 5, 30, 23, 49, 21)))
self.assertEquals(loaded[-1][1]["bitstampUSD"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 30, 23, 49, 21)))
self.assertEquals(loaded[-1][1]["bitstampUSD"].getClose(), 5.14)
self.assertEquals(loaded[-1][1]["bitstampUSD"].getVolume(), 20)
示例7: testLoadFilterFrom
def testLoadFilterFrom(self):
feed = barfeed.CSVTradeFeed()
feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"), "bitstampUSD", fromDateTime=dt.as_utc(datetime.datetime(2012, 5, 29)))
loaded = [(dateTime, bars) for dateTime, bars in feed]
self.assertEquals(len(loaded), 646)
self.assertEquals(loaded[0][0], dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
self.assertEquals(loaded[0][1]["bitstampUSD"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
self.assertEquals(loaded[0][1]["bitstampUSD"].getClose(), 5.07)
self.assertEquals(loaded[0][1]["bitstampUSD"].getVolume(), 1.39081288)
self.assertEquals(loaded[-1][0], dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
self.assertEquals(loaded[-1][1]["bitstampUSD"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
self.assertEquals(loaded[-1][1]["bitstampUSD"].getClose(), 5.1)
self.assertEquals(loaded[-1][1]["bitstampUSD"].getVolume(), 0.39215686)
示例8: testLoadNoFilter
def testLoadNoFilter(self):
feed = barfeed.CSVTradeFeed()
feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"))
loaded = [(dateTime, bars) for dateTime, bars in feed]
self.assertEquals(len(loaded), 9999)
self.assertEquals(loaded[0][0], dt.as_utc(datetime.datetime(2011, 9, 13, 13, 53, 36)))
self.assertEquals(loaded[0][1]["BTC"].getDateTime(), dt.as_utc(datetime.datetime(2011, 9, 13, 13, 53, 36)))
self.assertEquals(loaded[0][1]["BTC"].getClose(), 5.8)
self.assertEquals(loaded[0][1]["BTC"].getVolume(), 1.0)
self.assertEquals(loaded[-1][0], dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
self.assertEquals(loaded[-1][1]["BTC"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
self.assertEquals(loaded[-1][1]["BTC"].getClose(), 5.1)
self.assertEquals(loaded[-1][1]["BTC"].getVolume(), 0.39215686)
示例9: testResampleMtGoxHour
def testResampleMtGoxHour(self):
# Resample.
feed = mtgoxfeed.CSVTradeFeed()
feed.addBarsFromCSV(common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv"))
resampledBarDS = resampled.ResampledBarDataSeries(feed["BTC"], barfeed.Frequency.HOUR)
resample.resample_to_csv(feed, barfeed.Frequency.HOUR, "hour-mgtox-usd-2013-01-01.csv")
resampledBarDS.pushLast() # Need to manually push the last stot since time didn't change.
# Load the resampled file.
feed = csvfeed.GenericBarFeed(barfeed.Frequency.HOUR)
feed.addBarsFromCSV("BTC", "hour-mgtox-usd-2013-01-01.csv")
feed.loadAll()
self.assertEqual(len(feed["BTC"]), 24)
self.assertEqual(feed["BTC"][0].getDateTime(), datetime.datetime(2013, 01, 01, 00, 59, 59))
self.assertEqual(feed["BTC"][-1].getDateTime(), datetime.datetime(2013, 01, 01, 23, 59, 59))
self.assertEqual(len(resampledBarDS), len(feed["BTC"]))
self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(feed["BTC"][0].getDateTime()))
self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(feed["BTC"][-1].getDateTime()))
示例10: testResampleMtGoxMinute
def testResampleMtGoxMinute(self):
# Resample.
feed = mtgoxfeed.CSVTradeFeed()
feed.addBarsFromCSV(common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv"))
resampledBarDS = resampled.ResampledBarDataSeries(feed["BTC"], barfeed.Frequency.MINUTE)
resampledFile = os.path.join(common.get_temp_path(), "minute-mgtox-usd-2013-01-01.csv")
resample.resample_to_csv(feed, barfeed.Frequency.MINUTE, resampledFile)
resampledBarDS.pushLast() # Need to manually push the last stot since time didn't change.
# Load the resampled file.
feed = csvfeed.GenericBarFeed(barfeed.Frequency.MINUTE)
feed.addBarsFromCSV("BTC", resampledFile)
feed.loadAll()
self.assertEqual(len(feed["BTC"]), 537)
self.assertEqual(feed["BTC"][0].getDateTime(), datetime.datetime(2013, 1, 1, 0, 4))
self.assertEqual(feed["BTC"][-1].getDateTime(), datetime.datetime(2013, 1, 1, 23, 58))
self.assertEqual(len(resampledBarDS), len(feed["BTC"]))
self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(feed["BTC"][0].getDateTime()))
self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(feed["BTC"][-1].getDateTime()))
示例11: testBuildBar
def testBuildBar(self):
# This is the response to http://globalrealtime.xignite.com/v3/xGlobalRealTime.json/GetBar?Identifier=RIOl.CHIX&IdentifierType=Symbol&EndTime=3/19/2014%2010:00:00&Precision=Minutes&Period=5
response = """{
"Outcome": "Success",
"Message": null,
"Identity": "Request",
"Delay": 0.0330687,
"Bar": {
"StartDate": "3/19/2014",
"StartTime": "9:55:00 AM",
"EndDate": "3/19/2014",
"EndTime": "10:00:00 AM",
"UTCOffset": 0,
"Open": 31.71,
"High": 31.71,
"Low": 31.68,
"Close": 31.69,
"Volume": 2966,
"Trades": 19,
"TWAP": 31.6929,
"VWAP": 31.693
},
"Security": {
"CIK": "0000863064",
"CUSIP": null,
"Symbol": "RIOl.CHIX",
"ISIN": null,
"Valoren": "402589",
"Name": "Rio Tinto PLC",
"Market": "CHI-X EUROPE LIMITED.",
"MarketIdentificationCode": "CHIX",
"MostLiquidExchange": false,
"CategoryOrIndustry": "IndustrialMetalsAndMinerals"
}
}"""
responseDict = json.loads(response)
bar = barfeed.build_bar(responseDict["Bar"], "RIOl.CHIX", 60*5)
self.assertEqual(bar.getOpen(), 31.71)
self.assertEqual(bar.getHigh(), 31.71)
self.assertEqual(bar.getLow(), 31.68)
self.assertEqual(bar.getClose(), 31.69)
self.assertEqual(bar.getVolume(), 2966)
self.assertEqual(bar.getDateTime(), dt.as_utc(datetime.datetime(2014, 3, 19, 9, 55)))
示例12: build_order_from_open_order
def build_order_from_open_order(openOrder, instrumentTraits):
#order_id = openOrder.order.m_permId #we use the TWS id for the order rather than our id - not sure this is a good idea but its apparently consistent across sessions - https://www.interactivebrokers.com/en/software/api/apiguide/java/order.htm
order_id = openOrder.order.m_orderId
#doesn't seem to be a useable date/time for orders so going to use current time
order_time = dt.as_utc(datetime.datetime.now())
order_type = openOrder.order.m_orderType #stop, limit, stoplimit, market
order_action = openOrder.order.m_action
order_amount = openOrder.order.m_totalQuantity
order_limprice = openOrder.order.m_lmtPrice
order_auxprice = openOrder.order.m_auxPrice
contract_symbol = openOrder.contract.m_symbol
if order_action == 'BUY':
action = broker.Order.Action.BUY
elif order_action == 'SELL':
action = broker.Order.Action.SELL
elif order_action == 'SSHORT':
action = broker.Order.Action.SELL_SHORT
else:
raise Exception("Invalid order action")
if order_type == 'LMT': #Limit
ret = broker.LimitOrder(action, contract_symbol, order_limprice, order_amount, instrumentTraits)
elif order_type == 'MKT': #Market
ret = broker.MarketOrder(action, contract_symbol, order_amount, False, instrumentTraits)
elif order_type == 'MOC': #Market On Close
ret = broker.MarketOrder(action, contract_symbol, order_amount, True, instrumentTraits)
elif order_type == 'STP': #Stop order
ret = broker.StopOrder(action, contract_symbol, order_auxprice, order_amount, instrumentTraits)
elif order_type == 'STP LMT':
ret = broker.StopLimitOrder(action, contract_symbol, order_auxprice, order_limprice, order_amount, instrumentTraits)
else:
#Totally possible if you use pyalgotrade and TWS to manage the same account which is not really a good idea
raise Exception("Unsupported order type - %s" % order_type)
ret.setSubmitted(order_id, order_time)
ret.setState(broker.Order.State.ACCEPTED)
return ret
示例13: parse_datetime
def parse_datetime(dateTime):
try:
ret = datetime.datetime.strptime(dateTime, "%Y-%m-%d %H:%M:%S")
except ValueError:
ret = datetime.datetime.strptime(dateTime, "%Y-%m-%d %H:%M:%S.%f")
return dt.as_utc(ret)
示例14: utcnow
def utcnow():
return dt.as_utc(datetime.datetime.utcnow())
示例15: to_utc_if_naive
def to_utc_if_naive(dateTime):
if dateTime is not None and dt.datetime_is_naive(dateTime):
dateTime = dt.as_utc(dateTime)
return dateTime