Skip to content

Commit

Permalink
warning fixing and bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonwang371 committed Sep 12, 2021
1 parent 8a0c5e7 commit b6ecd88
Show file tree
Hide file tree
Showing 21 changed files with 486 additions and 485 deletions.
2 changes: 1 addition & 1 deletion pyalgotrade/feed/memfeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class MemFeed(feed.BaseFeed):
def __init__(self, maxLen=None):
super(MemFeed, self).__init__(maxLen, False)
super(MemFeed, self).__init__(maxLen)

self.__values = []
self.__nextIdx = 0
Expand Down
3 changes: 2 additions & 1 deletion pyalgotrade/utils/csvutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def next(self):


def download_csv(url, url_params=None, content_type="text/csv"):
response = requests.get(url, params=url_params)
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',}
response = requests.get(url, params=url_params, headers=headers)

response.raise_for_status()
response_content_type = response.headers['content-type']
Expand Down
46 changes: 23 additions & 23 deletions testcases/bar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,30 @@ def testInvalidConstruction(self):

def testTypicalPrice(self):
b = bar.BasicBar(datetime.datetime.now(), 2, 3, 1, 2.1, 10, 5, bar.Frequency.DAY)
self.assertEquals(b.getTypicalPrice(), (3 + 1 + 2.1) / 3)
self.assertEqual(b.getTypicalPrice(), (3 + 1 + 2.1) / 3)

def testGetPrice(self):
b = bar.BasicBar(datetime.datetime.now(), 2, 3, 1, 2.1, 10, 5, bar.Frequency.DAY)
self.assertEquals(b.getPrice(), b.getClose())
self.assertEqual(b.getPrice(), b.getClose())
b.setUseAdjustedValue(True)
self.assertEquals(b.getPrice(), b.getAdjClose())
self.assertEqual(b.getPrice(), b.getAdjClose())

def testPickle(self):
b1 = bar.BasicBar(datetime.datetime.now(), 2, 3, 1, 2.1, 10, 5, bar.Frequency.DAY)
b2 = cPickle.loads(cPickle.dumps(b1))
self.assertEquals(b1.getDateTime(), b2.getDateTime())
self.assertEquals(b1.getOpen(), b2.getOpen())
self.assertEquals(b1.getHigh(), b2.getHigh())
self.assertEquals(b1.getLow(), b2.getLow())
self.assertEquals(b1.getClose(), b2.getClose())
self.assertEquals(b1.getVolume(), b2.getVolume())
self.assertEquals(b1.getAdjClose(), b2.getAdjClose())
self.assertEquals(b1.getFrequency(), b2.getFrequency())
self.assertEquals(b1.getPrice(), b2.getPrice())
self.assertEquals(b1.getOpen(True), b2.getOpen(True))
self.assertEquals(b1.getHigh(True), b2.getHigh(True))
self.assertEquals(b1.getLow(True), b2.getLow(True))
self.assertEquals(b1.getClose(True), b2.getClose(True))
self.assertEqual(b1.getDateTime(), b2.getDateTime())
self.assertEqual(b1.getOpen(), b2.getOpen())
self.assertEqual(b1.getHigh(), b2.getHigh())
self.assertEqual(b1.getLow(), b2.getLow())
self.assertEqual(b1.getClose(), b2.getClose())
self.assertEqual(b1.getVolume(), b2.getVolume())
self.assertEqual(b1.getAdjClose(), b2.getAdjClose())
self.assertEqual(b1.getFrequency(), b2.getFrequency())
self.assertEqual(b1.getPrice(), b2.getPrice())
self.assertEqual(b1.getOpen(True), b2.getOpen(True))
self.assertEqual(b1.getHigh(True), b2.getHigh(True))
self.assertEqual(b1.getLow(True), b2.getLow(True))
self.assertEqual(b1.getClose(True), b2.getClose(True))

def testNoAdjClose(self):
b = bar.BasicBar(datetime.datetime.now(), 2, 3, 1, 2.1, 10, None, bar.Frequency.DAY)
Expand Down Expand Up @@ -97,11 +97,11 @@ def testBasic(self):
b1 = bar.BasicBar(dt, 1, 1, 1, 1, 10, 1, bar.Frequency.DAY)
b2 = bar.BasicBar(dt, 2, 2, 2, 2, 10, 2, bar.Frequency.DAY)
bars = bar.Bars({"a": b1, "b": b2})
self.assertEquals(bars["a"].getClose(), 1)
self.assertEquals(bars["b"].getClose(), 2)
self.assertEqual(bars["a"].getClose(), 1)
self.assertEqual(bars["b"].getClose(), 2)
self.assertTrue("a" in bars)
self.assertEquals(bars.items(), [("a", b1), ("b", b2)])
self.assertEquals(bars.keys(), ["a", "b"])
self.assertEquals(bars.getInstruments(), ["a", "b"])
self.assertEquals(bars.getDateTime(), dt)
self.assertEquals(bars.getBar("a").getClose(), 1)
self.assertEqual(bars.items(), [("a", b1), ("b", b2)])
self.assertEqual(bars.keys(), ["a", "b"])
self.assertEqual(bars.getInstruments(), ["a", "b"])
self.assertEqual(bars.getDateTime(), dt)
self.assertEqual(bars.getBar("a").getClose(), 1)
14 changes: 7 additions & 7 deletions testcases/barfeed_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ def check_base_barfeed(testCase, barFeed, barsHaveAdjClose):

def callback(dateTime, bars):
called["called"] = True
testCase.assertEquals(barFeed.getCurrentDateTime(), dateTime)
testCase.assertEqual(barFeed.getCurrentDateTime(), dateTime)

testCase.assertEquals(barFeed.getCurrentDateTime(), None)
testCase.assertEquals(barFeed.barsHaveAdjClose(), barsHaveAdjClose)
testCase.assertEqual(barFeed.getCurrentDateTime(), None)
testCase.assertEqual(barFeed.barsHaveAdjClose(), barsHaveAdjClose)
if not barsHaveAdjClose:
with testCase.assertRaisesRegexp(Exception, "The barfeed doesn't support adjusted close values.*"):
with testCase.assertRaisesRegex(Exception, "The barfeed doesn't support adjusted close values.*"):
barFeed.setUseAdjustedValues(True)

d = dispatcher.Dispatcher()
d.addSubject(barFeed)
barFeed.getNewValuesEvent().subscribe(callback)
d.run()

testCase.assertEquals(called["called"], True)
testCase.assertEqual(called["called"], True)


class OptimizerBarFeedTestCase(common.TestCase):
Expand All @@ -56,7 +56,7 @@ def testDateTimesNotInOrder(self):
bar.Bars({"orcl": bar.BasicBar(datetime.datetime(2001, 1, 1), 1, 1, 1, 1, 1, 1, bar.Frequency.DAY)}),
]
f = barfeed.OptimizerBarFeed(bar.Frequency.DAY, ["orcl"], bars)
with self.assertRaisesRegexp(Exception, "Bar date times are not in order.*"):
with self. assertRaisesRegex(Exception, "Bar date times are not in order.*"):
for dt, b, freq in f:
pass

Expand All @@ -78,7 +78,7 @@ def testBaseBarFeedNoAdjClose(self):

def testEmtpy(self):
barFeed = barfeed.OptimizerBarFeed(bar.Frequency.DAY, ["orcl"], [])
self.assertEquals(barFeed.barsHaveAdjClose(), False)
self.assertEqual(barFeed.barsHaveAdjClose(), False)


class CommonTestCase(common.TestCase):
Expand Down
140 changes: 70 additions & 70 deletions testcases/bitstamp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ def __onOrderBookUpdate(self, orderBookUpdate):
class InstrumentTraitsTestCase(tc_common.TestCase):
def testInstrumentTraits(self):
traits = common.BTCTraits()
self.assertEquals(traits.roundQuantity(0), 0)
self.assertEquals(traits.roundQuantity(1), 1)
self.assertEquals(traits.roundQuantity(1.1 + 1.1 + 1.1), 3.3)
self.assertEquals(traits.roundQuantity(1.1 + 1.1 + 1.1 - 3.3), 0)
self.assertEquals(traits.roundQuantity(0.00441376), 0.00441376)
self.assertEquals(traits.roundQuantity(0.004413764), 0.00441376)
self.assertEqual(traits.roundQuantity(0), 0)
self.assertEqual(traits.roundQuantity(1), 1)
self.assertEqual(traits.roundQuantity(1.1 + 1.1 + 1.1), 3.3)
self.assertEqual(traits.roundQuantity(1.1 + 1.1 + 1.1 - 3.3), 0)
self.assertEqual(traits.roundQuantity(0.00441376), 0.00441376)
self.assertEqual(traits.roundQuantity(0.004413764), 0.00441376)


class BacktestingTestCase(tc_common.TestCase):
Expand All @@ -244,10 +244,10 @@ def onBars(self, bars):
brk = broker.BacktestingBroker(100, barFeed)
strat = TestStrategy(barFeed, brk)
strat.run()
self.assertEquals(strat.pos.getShares(), 1)
self.assertEquals(strat.pos.entryActive(), False)
self.assertEquals(strat.pos.isOpen(), True)
self.assertEquals(strat.pos.getEntryOrder().getAvgFillPrice(), 5.83)
self.assertEqual(strat.pos.getShares(), 1)
self.assertEqual(strat.pos.entryActive(), False)
self.assertEqual(strat.pos.isOpen(), True)
self.assertEqual(strat.pos.getEntryOrder().getAvgFillPrice(), 5.83)

def testMinTrade(self):
class TestStrategy(strategy.BaseStrategy):
Expand All @@ -263,7 +263,7 @@ def onBars(self, bars):
barFeed.addBarsFromCSV(tc_common.get_data_file_path("bitstampUSD.csv"))
brk = broker.BacktestingBroker(100, barFeed)
strat = TestStrategy(barFeed, brk)
with self.assertRaisesRegexp(Exception, "Trade must be >= 5"):
with self. assertRaisesRegex(Exception, "Trade must be >= 5"):
strat.run()


Expand All @@ -290,9 +290,9 @@ def onBars(self, bars):
strat.run()

self.assertTrue(strat.pos.isOpen())
self.assertEquals(round(strat.pos.getShares(), 3), 0.3)
self.assertEquals(len(strat.posExecutionInfo), 1)
self.assertEquals(strat.pos.getEntryOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
self.assertEqual(round(strat.pos.getShares(), 3), 0.3)
self.assertEqual(len(strat.posExecutionInfo), 1)
self.assertEqual(strat.pos.getEntryOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())

def testBuyAndSellWithPartialFill1(self):

Expand Down Expand Up @@ -320,10 +320,10 @@ def onBars(self, bars):
strat.run()

self.assertTrue(strat.pos.isOpen())
self.assertEquals(round(strat.pos.getShares(), 3), 0.1)
self.assertEquals(len(strat.posExecutionInfo), 1)
self.assertEquals(strat.pos.getEntryOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
self.assertEquals(strat.pos.getExitOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
self.assertEqual(round(strat.pos.getShares(), 3), 0.1)
self.assertEqual(len(strat.posExecutionInfo), 1)
self.assertEqual(strat.pos.getEntryOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
self.assertEqual(strat.pos.getExitOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())

def testBuyAndSellWithPartialFill2(self):

Expand Down Expand Up @@ -352,10 +352,10 @@ def onBars(self, bars):
strat.run()

self.assertFalse(strat.pos.isOpen())
self.assertEquals(strat.pos.getShares(), 0)
self.assertEquals(len(strat.posExecutionInfo), 2)
self.assertEquals(strat.pos.getEntryOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
self.assertEquals(strat.pos.getExitOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
self.assertEqual(strat.pos.getShares(), 0)
self.assertEqual(len(strat.posExecutionInfo), 2)
self.assertEqual(strat.pos.getEntryOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
self.assertEqual(strat.pos.getExitOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())

def testRoundingBugWithTrades(self):
# Unless proper rounding is in place 0.01 - 0.00441376 - 0.00445547 - 0.00113077 == 6.50521303491e-19
Expand Down Expand Up @@ -383,19 +383,19 @@ def onBars(self, bars):
strat = Strategy(barFeed, brk)
strat.run()

self.assertEquals(brk.getShares("BTC"), 0)
self.assertEquals(strat.pos.getEntryOrder().getAvgFillPrice(), 1000)
self.assertEquals(strat.pos.getExitOrder().getAvgFillPrice(), 1000)
self.assertEquals(strat.pos.getEntryOrder().getFilled(), 0.01)
self.assertEquals(strat.pos.getExitOrder().getFilled(), 0.01)
self.assertEquals(strat.pos.getEntryOrder().getRemaining(), 0)
self.assertEquals(strat.pos.getExitOrder().getRemaining(), 0)
self.assertEquals(strat.pos.getEntryOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
self.assertEquals(strat.pos.getExitOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
self.assertEqual(brk.getShares("BTC"), 0)
self.assertEqual(strat.pos.getEntryOrder().getAvgFillPrice(), 1000)
self.assertEqual(strat.pos.getExitOrder().getAvgFillPrice(), 1000)
self.assertEqual(strat.pos.getEntryOrder().getFilled(), 0.01)
self.assertEqual(strat.pos.getExitOrder().getFilled(), 0.01)
self.assertEqual(strat.pos.getEntryOrder().getRemaining(), 0)
self.assertEqual(strat.pos.getExitOrder().getRemaining(), 0)
self.assertEqual(strat.pos.getEntryOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())
self.assertEqual(strat.pos.getExitOrder().getSubmitDateTime().date(), wsclient.get_current_datetime().date())

self.assertFalse(strat.pos.isOpen())
self.assertEquals(len(strat.posExecutionInfo), 2)
self.assertEquals(strat.pos.getShares(), 0.0)
self.assertEqual(len(strat.posExecutionInfo), 2)
self.assertEqual(strat.pos.getShares(), 0.0)

def testInvalidOrders(self):
barFeed = TestingLiveTradeFeed()
Expand Down Expand Up @@ -433,9 +433,9 @@ def onBars(self, bars):
strat = Strategy(barFeed, brk)
strat.run()

self.assertEquals(strat.errors, 4)
self.assertEquals(brk.getShares("BTC"), 0)
self.assertEquals(brk.getCash(), 0)
self.assertEqual(strat.errors, 4)
self.assertEqual(brk.getShares("BTC"), 0)
self.assertEqual(brk.getCash(), 0)

def testRanOutOfCash(self):
class Strategy(TestStrategy):
Expand All @@ -458,9 +458,9 @@ def onBars(self, bars):
strat = Strategy(barFeed, brk)
strat.run()

self.assertEquals(strat.errors, 2)
self.assertEquals(brk.getShares("BTC"), 0.1)
self.assertEquals(brk.getCash(), 0)
self.assertEqual(strat.errors, 2)
self.assertEqual(brk.getShares("BTC"), 0.1)
self.assertEqual(brk.getCash(), 0)

def testSellWithoutBTC(self):
class Strategy(TestStrategy):
Expand All @@ -482,9 +482,9 @@ def onBars(self, bars):
strat = Strategy(barFeed, brk)
strat.run()

self.assertEquals(strat.errors, 2)
self.assertEquals(brk.getShares("BTC"), 0)
self.assertEquals(brk.getCash(), 0)
self.assertEqual(strat.errors, 2)
self.assertEqual(brk.getShares("BTC"), 0)
self.assertEqual(brk.getCash(), 0)

def testRanOutOfCoins(self):
class Strategy(TestStrategy):
Expand Down Expand Up @@ -512,9 +512,9 @@ def onBars(self, bars):
strat = Strategy(barFeed, brk)
strat.run()

self.assertEquals(strat.errors, 1)
self.assertEquals(brk.getShares("BTC"), 0)
self.assertEquals(brk.getCash(), 10)
self.assertEqual(strat.errors, 1)
self.assertEqual(brk.getShares("BTC"), 0)
self.assertEqual(brk.getCash(), 10)


class LiveTradingTestCase(tc_common.TestCase):
Expand Down Expand Up @@ -544,15 +544,15 @@ def onBars(self, bars):
strat = Strategy(barFeed, brk)
strat.run()

self.assertEquals(len(strat.orderExecutionInfo), 2)
self.assertEquals(strat.orderExecutionInfo[0].getPrice(), 578.79)
self.assertEquals(strat.orderExecutionInfo[0].getQuantity(), 0.04557395)
self.assertEquals(strat.orderExecutionInfo[0].getCommission(), 0.14)
self.assertEquals(strat.orderExecutionInfo[0].getDateTime().date(), datetime.datetime.now().date())
self.assertEquals(strat.orderExecutionInfo[1].getPrice(), 567.21)
self.assertEquals(strat.orderExecutionInfo[1].getQuantity(), 0.04601436)
self.assertEquals(strat.orderExecutionInfo[1].getCommission(), 0.14)
self.assertEquals(strat.orderExecutionInfo[1].getDateTime().date(), datetime.datetime.now().date())
self.assertEqual(len(strat.orderExecutionInfo), 2)
self.assertEqual(strat.orderExecutionInfo[0].getPrice(), 578.79)
self.assertEqual(strat.orderExecutionInfo[0].getQuantity(), 0.04557395)
self.assertEqual(strat.orderExecutionInfo[0].getCommission(), 0.14)
self.assertEqual(strat.orderExecutionInfo[0].getDateTime().date(), datetime.datetime.now().date())
self.assertEqual(strat.orderExecutionInfo[1].getPrice(), 567.21)
self.assertEqual(strat.orderExecutionInfo[1].getQuantity(), 0.04601436)
self.assertEqual(strat.orderExecutionInfo[1].getCommission(), 0.14)
self.assertEqual(strat.orderExecutionInfo[1].getDateTime().date(), datetime.datetime.now().date())

def testCancelOrder(self):
class Strategy(TestStrategy):
Expand All @@ -577,11 +577,11 @@ def onBars(self, bars):
strat = Strategy(barFeed, brk)
strat.run()

self.assertEquals(brk.getShares("BTC"), 0)
self.assertEquals(brk.getCash(), 0)
self.assertEquals(len(strat.orderExecutionInfo), 1)
self.assertEquals(strat.orderExecutionInfo[0], None)
self.assertEquals(len(strat.ordersUpdated), 1)
self.assertEqual(brk.getShares("BTC"), 0)
self.assertEqual(brk.getCash(), 0)
self.assertEqual(len(strat.orderExecutionInfo), 1)
self.assertEqual(strat.orderExecutionInfo[0], None)
self.assertEqual(len(strat.ordersUpdated), 1)
self.assertTrue(strat.ordersUpdated[0].isCanceled())

def testBuyAndSell(self):
Expand Down Expand Up @@ -621,17 +621,17 @@ def onBars(self, bars):
self.assertTrue(strat.buyOrder.isPartiallyFilled())
self.assertTrue(strat.sellOrder.isFilled())
# 2 events for each order: 1 for accepted, 1 for fill.
self.assertEquals(len(strat.orderExecutionInfo), 4)
self.assertEquals(strat.orderExecutionInfo[0], None)
self.assertEquals(strat.orderExecutionInfo[1].getPrice(), 10)
self.assertEquals(strat.orderExecutionInfo[1].getQuantity(), 0.5)
self.assertEquals(strat.orderExecutionInfo[1].getCommission(), 0.01)
self.assertEquals(strat.orderExecutionInfo[1].getDateTime().date(), datetime.datetime.now().date())
self.assertEquals(strat.orderExecutionInfo[2], None)
self.assertEquals(strat.orderExecutionInfo[3].getPrice(), 10)
self.assertEquals(strat.orderExecutionInfo[3].getQuantity(), 0.5)
self.assertEquals(strat.orderExecutionInfo[3].getCommission(), 0.01)
self.assertEquals(strat.orderExecutionInfo[3].getDateTime().date(), datetime.datetime.now().date())
self.assertEqual(len(strat.orderExecutionInfo), 4)
self.assertEqual(strat.orderExecutionInfo[0], None)
self.assertEqual(strat.orderExecutionInfo[1].getPrice(), 10)
self.assertEqual(strat.orderExecutionInfo[1].getQuantity(), 0.5)
self.assertEqual(strat.orderExecutionInfo[1].getCommission(), 0.01)
self.assertEqual(strat.orderExecutionInfo[1].getDateTime().date(), datetime.datetime.now().date())
self.assertEqual(strat.orderExecutionInfo[2], None)
self.assertEqual(strat.orderExecutionInfo[3].getPrice(), 10)
self.assertEqual(strat.orderExecutionInfo[3].getQuantity(), 0.5)
self.assertEqual(strat.orderExecutionInfo[3].getCommission(), 0.01)
self.assertEqual(strat.orderExecutionInfo[3].getDateTime().date(), datetime.datetime.now().date())


class WebSocketTestCase(tc_common.TestCase):
Expand Down
Loading

0 comments on commit b6ecd88

Please sign in to comment.