本文整理汇总了Python中strategy_test.datetime_from_date函数的典型用法代码示例。如果您正苦于以下问题:Python datetime_from_date函数的具体用法?Python datetime_from_date怎么用?Python datetime_from_date使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了datetime_from_date函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testDrawDownIGE_SPY_Broker
def testDrawDownIGE_SPY_Broker(self):
# This testcase is based on an example from Ernie Chan's book:
# 'Quantitative Trading: How to Build Your Own Algorithmic Trading Business'
barFeed = yahoofeed.Feed()
barFeed.addBarsFromCSV("ige", common.get_data_file_path("sharpe-ratio-test-ige.csv"))
barFeed.addBarsFromCSV("spy", common.get_data_file_path("sharpe-ratio-test-spy.csv"))
strat = strategy_test.TestStrategy(barFeed, 1000)
strat.getBroker().setUseAdjustedValues(True)
strat.setBrokerOrdersGTC(True)
stratAnalyzer = drawdown.DrawDown()
strat.attachAnalyzer(stratAnalyzer)
# Manually place IGE order to get it filled on the first bar.
order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, "ige", 1, True) # Adj. Close: 42.09
order.setGoodTillCanceled(True)
strat.getBroker().placeOrder(order)
strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, "ige", 1, True) # Adj. Close: 127.64
# Manually place SPY order to get it filled on the first bar.
order = strat.getBroker().createMarketOrder(broker.Order.Action.SELL_SHORT, "spy", 1, True) # Adj. Close: 105.52
order.setGoodTillCanceled(True)
strat.getBroker().placeOrder(order)
strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.getBroker().createMarketOrder, broker.Order.Action.BUY_TO_COVER, "spy", 1, True) # Adj. Close: 147.67
strat.run()
self.assertTrue(round(strat.getBroker().getCash(), 2) == round(1000 + (127.64 - 42.09) + (105.52 - 147.67), 2))
self.assertTrue(strat.getOrderUpdatedEvents() == 4)
self.assertTrue(round(stratAnalyzer.getMaxDrawDown(), 5) == 0.09448)
self.assertTrue(stratAnalyzer.getMaxDrawDownDuration()== 229)
示例2: testSharpeRatioIGE_SPY_Broker
def testSharpeRatioIGE_SPY_Broker(self):
initialCash = 42.09
# This testcase is based on an example from Ernie Chan's book:
# 'Quantitative Trading: How to Build Your Own Algorithmic Trading Business'
bar_feed = yahoofeed.Feed()
bar_feed.add_bars_from_csv("ige", common.get_data_file_path("sharpe-ratio-test-ige.csv"))
bar_feed.add_bars_from_csv("spy", common.get_data_file_path("sharpe-ratio-test-spy.csv"))
strat = strategy_test.TestStrategy(bar_feed, initialCash)
strat.get_broker().set_use_adj_values(True)
strat.setBrokerOrdersGTC(True)
stratAnalyzer = sharpe.SharpeRatio()
strat.attach_analyzer(stratAnalyzer)
# Manually place IGE order to get it filled on the first bar.
order = strat.get_broker().create_market_order(broker.Order.Action.BUY, "ige", 1, True) # Adj. Close: 42.09
order.set_good_until_canceled(True)
strat.get_broker().place_order(order)
# Manually place SPY order to get it filled on the first bar.
order = strat.get_broker().create_market_order(broker.Order.Action.SELL_SHORT, "spy", 1, True) # Adj. Close: 105.52
order.set_good_until_canceled(True)
strat.get_broker().place_order(order)
strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.get_broker().create_market_order, broker.Order.Action.SELL, "ige", 1, True) # Adj. Close: 127.64
strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.get_broker().create_market_order, broker.Order.Action.BUY_TO_COVER, "spy", 1, True) # Adj. Close: 147.67
strat.run()
self.assertTrue(strat.get_order_updated_events() == 4)
self.assertTrue(round(strat.get_broker().get_cash(), 2) == round(initialCash + (127.64 - 42.09) + (105.52 - 147.67), 2))
示例3: testTwoBarReturns_CloseClose
def testTwoBarReturns_CloseClose(self):
initialCash = 15.90
barFeed = yahoofeed.Feed()
barFeed.setBarFilter(
csvfeed.DateRangeFilter(
strategy_test.datetime_from_date(2001, 12, 06), strategy_test.datetime_from_date(2001, 12, 07)
)
)
barFeed.addBarsFromCSV(ReturnsTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv"))
strat = strategy_test.DummyStrategy(barFeed, initialCash)
# 2001-12-06,15.61,16.03,15.50,15.90,66944900,15.55
# 2001-12-07,15.74,15.95,15.55,15.91,42463200,15.56
# Manually place the entry order, to get it filled on the first bar.
order = strat.getBroker().createMarketOrder(
broker.Order.Action.BUY, ReturnsTestCase.TestInstrument, 1, True
) # Close: 15.90
strat.getBroker().placeOrder(order)
strat.addOrder(
strategy_test.datetime_from_date(2001, 12, 06),
strat.getBroker().createMarketOrder,
broker.Order.Action.SELL,
ReturnsTestCase.TestInstrument,
1,
True,
) # Close: 15.91
stratAnalyzer = returns.Returns()
strat.attachAnalyzer(stratAnalyzer)
strat.run()
assert strat.getBroker().getCash() == initialCash + (15.91 - 15.90)
# First day returns: 0
assert stratAnalyzer.getReturns()[0] == 0
# Second day returns: Open vs Prev. day's close
assert stratAnalyzer.getReturns()[1] == (15.91 - 15.90) / 15.90
示例4: testOneBarReturn
def testOneBarReturn(self):
initialCash = 1000
barFeed = yahoofeed.Feed()
barFeed.setBarFilter(
csvfeed.DateRangeFilter(
strategy_test.datetime_from_date(2001, 12, 07), strategy_test.datetime_from_date(2001, 12, 07)
)
)
barFeed.addBarsFromCSV(ReturnsTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv"))
strat = strategy_test.DummyStrategy(barFeed, initialCash)
# 2001-12-07,15.74,15.95,15.55,15.91,42463200,15.56
# Manually place the orders to get them filled on the first (and only) bar.
order = strat.getBroker().createMarketOrder(
broker.Order.Action.BUY, ReturnsTestCase.TestInstrument, 1, False
) # Open: 15.74
strat.getBroker().placeOrder(order)
order = strat.getBroker().createMarketOrder(
broker.Order.Action.SELL, ReturnsTestCase.TestInstrument, 1, True
) # Close: 15.91
strat.getBroker().placeOrder(order)
stratAnalyzer = returns.Returns()
strat.attachAnalyzer(stratAnalyzer)
strat.run()
assert strat.getBroker().getCash() == initialCash + (15.91 - 15.74)
finalValue = 1000 - 15.74 + 15.91
rets = (finalValue - initialCash) / float(initialCash)
self.assertEqual(stratAnalyzer.getReturns()[-1], rets)
示例5: testCumulativeReturn
def testCumulativeReturn(self):
barFeed = yahoofeed.Feed()
barFeed.addBarsFromCSV(ReturnsTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv"))
strat = strategy_test.TestStrategy(barFeed, 1000)
strat.addPosEntry(strategy_test.datetime_from_date(2001, 1, 12), strat.enterLong, ReturnsTestCase.TestInstrument, 1) # 33.06
strat.addPosExit(strategy_test.datetime_from_date(2001, 11, 27), strat.exitPosition) # 14.32
stratAnalyzer = returns.Returns()
strat.attachAnalyzer(stratAnalyzer)
strat.run()
self.assertTrue(round(strat.getBroker().getCash(), 2) == round(1000 + (14.32 - 33.06), 2))
self.assertTrue(round(33.06 * (1 + stratAnalyzer.getCumulativeReturn()), 2) == 14.32)
示例6: testCumulativeReturn
def testCumulativeReturn(self):
initialCash = 33.06
bar_feed = yahoofeed.Feed()
bar_feed.add_bars_from_csv(ReturnsTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv"))
strat = strategy_test.TestStrategy(bar_feed, initialCash)
strat.addPosEntry(strategy_test.datetime_from_date(2001, 1, 12), strat.enter_long, ReturnsTestCase.TestInstrument, 1) # 33.06
strat.addPosExit(strategy_test.datetime_from_date(2001, 11, 27), strat.exit_position) # 14.32
stratAnalyzer = returns.Returns()
strat.attach_analyzer(stratAnalyzer)
strat.run()
self.assertTrue(round(strat.get_broker().get_cash(), 2) == round(initialCash + (14.32 - 33.06), 2))
self.assertTrue(round(33.06 * (1 + stratAnalyzer.get_cumulative_returns()[-1]), 2) == 14.32)
示例7: testIGE_BrokerWithCommission
def testIGE_BrokerWithCommission(self):
commision = 0.5
initialCash = 42.09 + commision
# This testcase is based on an example from Ernie Chan's book:
# 'Quantitative Trading: How to Build Your Own Algorithmic Trading Business'
barFeed = yahoofeed.Feed()
barFeed.addBarsFromCSV("ige", common.get_data_file_path("sharpe-ratio-test-ige.csv"))
strat = strategy_test.TestStrategy(barFeed, initialCash)
strat.getBroker().setCommission(backtesting.FixedPerTrade(commision))
strat.setUseAdjustedValues(True)
strat.setBrokerOrdersGTC(True)
stratAnalyzer = sharpe.SharpeRatio()
strat.attachAnalyzer(stratAnalyzer)
# Disable volume checks to match book results.
strat.getBroker().getFillStrategy().setVolumeLimit(None)
# Manually place the order to get it filled on the first bar.
order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, "ige", 1, True) # Adj. Close: 42.09
order.setGoodTillCanceled(True)
strat.getBroker().placeOrder(order)
strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, "ige", 1, True) # Adj. Close: 127.64
strat.run()
self.assertTrue(round(strat.getBroker().getCash(), 2) == initialCash + (127.64 - 42.09 - commision*2))
self.assertTrue(strat.getOrderUpdatedEvents() == 4)
# The results are slightly different only because I'm taking into account the first bar as well,
# and I'm also adding commissions.
self.assertEqual(round(stratAnalyzer.getSharpeRatio(0.04, True), 6), 0.776443)
示例8: __testIGE_BrokerImpl
def __testIGE_BrokerImpl(self, quantity):
initialCash = 42.09*quantity
# This testcase is based on an example from Ernie Chan's book:
# 'Quantitative Trading: How to Build Your Own Algorithmic Trading Business'
barFeed = yahoofeed.Feed()
barFeed.addBarsFromCSV("ige", common.get_data_file_path("sharpe-ratio-test-ige.csv"))
strat = strategy_test.TestStrategy(barFeed, initialCash)
strat.getBroker().setUseAdjustedValues(True)
strat.setBrokerOrdersGTC(True)
stratAnalyzer = drawdown.DrawDown()
strat.attachAnalyzer(stratAnalyzer)
# Disable volume checks to match book results.
strat.getBroker().getFillStrategy().setVolumeLimit(None)
# Manually place the order to get it filled on the first bar.
order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, "ige", quantity, True) # Adj. Close: 42.09
order.setGoodTillCanceled(True)
strat.getBroker().placeOrder(order)
strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, "ige", quantity, True) # Adj. Close: 127.64
strat.run()
self.assertTrue(round(strat.getBroker().getCash(), 2) == initialCash + (127.64 - 42.09) * quantity)
self.assertTrue(strat.getOrderUpdatedEvents() == 4)
self.assertTrue(round(stratAnalyzer.getMaxDrawDown(), 5) == 0.31178)
self.assertTrue(stratAnalyzer.getLongestDrawDownDuration() == datetime.timedelta(days=623))
示例9: testIGE_BrokerWithCommission
def testIGE_BrokerWithCommission(self):
commision = 0.5
initialCash = 42.09 + commision
# This testcase is based on an example from Ernie Chan's book:
# 'Quantitative Trading: How to Build Your Own Algorithmic Trading Business'
bar_feed = yahoofeed.Feed()
bar_feed.add_bars_from_csv("ige", common.get_data_file_path("sharpe-ratio-test-ige.csv"))
brk = backtesting.Broker(initialCash, bar_feed, backtesting.FixedCommission(commision))
strat = strategy_test.TestStrategy(bar_feed, initialCash, brk)
strat.get_broker().set_use_adj_values(True)
strat.setBrokerOrdersGTC(True)
stratAnalyzer = sharpe.SharpeRatio()
strat.attach_analyzer(stratAnalyzer)
# Manually place the order to get it filled on the first bar.
order = strat.get_broker().create_market_order(broker.Order.Action.BUY, "ige", 1, True) # Adj. Close: 42.09
order.set_good_until_canceled(True)
strat.get_broker().place_order(order)
strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.get_broker().create_market_order, broker.Order.Action.SELL, "ige", 1, True) # Adj. Close: 127.64
strat.run()
self.assertTrue(round(strat.get_broker().get_cash(), 2) == initialCash + (127.64 - 42.09 - commision*2))
self.assertTrue(strat.get_order_updated_events() == 2)
# The results are slightly different only because I'm taking into account the first bar as well,
# and I'm also adding commissions.
self.assertEqual(round(stratAnalyzer.get_sharpe_ratio(0.04, 252, annualized=True), 6), 0.776443)
示例10: testTwoBarReturns_CloseClose
def testTwoBarReturns_CloseClose(self):
barFeed = yahoofeed.Feed()
barFeed.setBarFilter(csvfeed.DateRangeFilter(strategy_test.datetime_from_date(2001, 12, 06), strategy_test.datetime_from_date(2001, 12, 07)))
barFeed.addBarsFromCSV(ReturnsTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv"))
strat = strategy_test.TestStrategy(barFeed, 1000)
# 2001-12-06,15.61,16.03,15.50,15.90,66944900,15.55
# 2001-12-07,15.74,15.95,15.55,15.91,42463200,15.56
# Manually place the entry order, to get it filled on the first bar.
order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, ReturnsTestCase.TestInstrument, 1, True) # Close: 15.90
strat.getBroker().placeOrder(order)
strat.addOrder(strategy_test.datetime_from_date(2001, 12, 06), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, ReturnsTestCase.TestInstrument, 1, True) # Close: 15.91
returnsDS = returns.ReturnsDataSeries(strat)
strat.run()
self.assertTrue(strat.getBroker().getCash() == 1000 + (15.91 - 15.90))
# First day returns: 0
self.assertTrue(returnsDS.getValueAbsolute(0) == 0)
# Second day returns: Open vs Prev. day's close
self.assertTrue(returnsDS.getValueAbsolute(1) == (15.91 - 15.90) / 15.90)
示例11: testTwoBarReturns_CloseOpen
def testTwoBarReturns_CloseOpen(self):
initialCash = 15.9
bar_feed = yahoofeed.Feed()
bar_feed.set_bar_filter(csvfeed.DateRangeFilter(strategy_test.datetime_from_date(2001, 12, 06), strategy_test.datetime_from_date(2001, 12, 07)))
bar_feed.add_bars_from_csv(ReturnsTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv"))
strat = strategy_test.TestStrategy(bar_feed, initialCash)
# 2001-12-06,15.61,16.03,15.50,15.90,66944900,15.55
# 2001-12-07,15.74,15.95,15.55,15.91,42463200,15.56
# Manually place the entry order, to get it filled on the first bar.
order = strat.get_broker().create_market_order(broker.Order.Action.BUY, ReturnsTestCase.TestInstrument, 1, True) # Close: 15.90
strat.get_broker().place_order(order)
strat.addOrder(strategy_test.datetime_from_date(2001, 12, 06), strat.get_broker().create_market_order, broker.Order.Action.SELL, ReturnsTestCase.TestInstrument, 1, False) # Open: 15.74
stratAnalyzer = returns.Returns()
strat.attach_analyzer(stratAnalyzer)
strat.run()
self.assertTrue(strat.get_broker().get_cash() == initialCash + (15.74 - 15.90))
# First day returns: 0
self.assertTrue(stratAnalyzer.get_returns()[0] == 0)
# Second day returns: Open vs Prev. day's close
self.assertTrue(stratAnalyzer.get_returns()[1] == (15.74 - 15.90) / 15.90)
示例12: __testIGE_BrokerImpl
def __testIGE_BrokerImpl(self, quantity):
# This testcase is based on an example from Ernie Chan's book:
# 'Quantitative Trading: How to Build Your Own Algorithmic Trading Business'
barFeed = yahoofeed.Feed()
barFeed.addBarsFromCSV("ige", common.get_data_file_path("sharpe-ratio-test-ige.csv"))
strat = strategy_test.TestStrategy(barFeed, 1000)
strat.getBroker().setUseAdjustedValues(True)
strat.setBrokerOrdersGTC(True)
stratAnalyzer = sharpe.SharpeRatio()
strat.attachAnalyzer(stratAnalyzer)
# Manually place the order to get it filled on the first bar.
order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, "ige", quantity, True) # Adj. Close: 42.09
order.setGoodTillCanceled(True)
strat.getBroker().placeOrder(order)
strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, "ige", quantity, True) # Adj. Close: 127.64
strat.run()
self.assertTrue(round(strat.getBroker().getCash(), 2) == 1000 + (127.64 - 42.09) * quantity)
self.assertTrue(strat.getOrderUpdatedEvents() == 2)
# The results are slightly different different only because I'm taking into account the first bar as well.
self.assertTrue(round(stratAnalyzer.getSharpeRatio(0.04, 252, annualized=True), 4) == 0.7889)
示例13: __testIGE_BrokerImpl
def __testIGE_BrokerImpl(self, quantity):
initialCash = 42.09*quantity
# This testcase is based on an example from Ernie Chan's book:
# 'Quantitative Trading: How to Build Your Own Algorithmic Trading Business'
bar_feed = yahoofeed.Feed()
bar_feed.add_bars_from_csv("ige", common.get_data_file_path("sharpe-ratio-test-ige.csv"))
strat = strategy_test.TestStrategy(bar_feed, initialCash)
strat.get_broker().set_use_adj_values(True)
strat.setBrokerOrdersGTC(True)
stratAnalyzer = drawdown.DrawDown()
strat.attach_analyzer(stratAnalyzer)
# Manually place the order to get it filled on the first bar.
order = strat.get_broker().create_market_order(broker.Order.Action.BUY, "ige", quantity, True) # Adj. Close: 42.09
order.set_good_until_canceled(True)
strat.get_broker().place_order(order)
strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.get_broker().create_market_order, broker.Order.Action.SELL, "ige", quantity, True) # Adj. Close: 127.64
strat.run()
self.assertTrue(round(strat.get_broker().get_cash(), 2) == initialCash + (127.64 - 42.09) * quantity)
self.assertTrue(strat.get_order_updated_events() == 2)
self.assertTrue(round(stratAnalyzer.get_max_draw_down(), 5) == 0.31178)
self.assertTrue(stratAnalyzer.get_longest_draw_down_duration()== 432)
示例14: testIGE_BrokerWithCommission
def testIGE_BrokerWithCommission(self):
commision = 0.5
initialCash = 42.09 + commision
# This testcase is based on an example from Ernie Chan's book:
# 'Quantitative Trading: How to Build Your Own Algorithmic Trading Business'
barFeed = yahoofeed.Feed()
barFeed.addBarsFromCSV("ige", common.get_data_file_path("sharpe-ratio-test-ige.csv"))
brk = backtesting.Broker(initialCash, barFeed, backtesting.FixedCommission(commision))
strat = strategy_test.DummyStrategy(barFeed, initialCash, brk)
strat.getBroker().setUseAdjustedValues(True)
strat.setBrokerOrdersGTC(True)
stratAnalyzer = sortino.SortinoRatio()
strat.attachAnalyzer(stratAnalyzer)
# Manually place the order to get it filled on the first bar.
order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, "ige", 1, True) # Adj. Close: 42.09
order.setGoodTillCanceled(True)
strat.getBroker().placeOrder(order)
strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, "ige", 1, True) # Adj. Close: 127.64
strat.run()
assert round(strat.getBroker().getCash(), 2) == initialCash + (127.64 - 42.09 - commision*2)
assert strat.getOrderUpdatedEvents() == 2
assert round(stratAnalyzer.getSortinoRatio(), 4) == 1.375