def Run(self, budget, symbol, quotes, start, end): correct_quote = self._correctQuote(symbol, quotes, start) average_rate = ((correct_quote.high + correct_quote.low) / 2) t = Trades() integralAmount = int(budget / average_rate) t.buy(Trade(start, integralAmount, symbol, average_rate)) if(integralAmount * average_rate != budget): t.buy(Trade(start, -integralAmount * average_rate + budget, "cash", 1.0)) return t
def Run(self, budget, symbol, quotes, start, end): repeat = int(budget / self._amount) t = Trades() for i in range(repeat): time = start + i * self._spacing correct_quote = self._correctQuote(symbol, quotes, time) average_rate = ((correct_quote.high + correct_quote.low) / 2) integralAmount = int(self._amount / average_rate) t.buy(Trade(time, integralAmount, symbol, average_rate)) if (integralAmount * average_rate != self._amount): t.buy( Trade(time, -integralAmount * average_rate + self._amount, "cash", 1.0)) return t
def test_increasingrate_doublebuy(): t = Trades() t.buy(Trade(0.0, 1, "x", 1.0)) t.buy(Trade(1.0, 1, "x", 2.0)) assert t.value_of_this_stock(1.5, "x", 2.0) == 4.0
def test_value_of_this_stock(): t = Trades() t.buy(Trade(0.0, 1, "x", 2.0)) assert t.value_of_this_stock(1.0, "x", 2.1) == 2.1
def test_expensebeforelastTrade(): t = Trades() t.buy(Trade(0.0, 1, "S", 1.0)) t.buy(Trade(1.0, 1, "S", 1.0)) assert 1.0 == t.expense(0.1)
def test_buyOne_expenseIsOne(): t = Trades() t.buy(Trade(0.0, 1, "S", 1.0)) assert 1.0 == t.expense(0.1)
def test_buyTwodifferent_expenseTwo(): t = Trades() t.buy(Trade(0.0, 1, "S", 1.0)) t.buy(Trade(0.0, 1, "D", 1.0)) assert 2.0 == t.expense(1)
def test_buyOnesellOne_sameDate(): t = Trades() t.buy(Trade(0.0, 1, "S", 1.0)) t.sell(Trade(0.0, -1, "S", 1.0)) assert 0.0 == t.expense(1) + t.revenue(1)
def test_buyOneSellOne_revenueIsminusOne(): t = Trades() t.buy(Trade(0.0, 1, "S", 1.0)) t.sell(Trade(1.0, -1, "S", 1.0)) assert -1.0 == t.revenue(2)
def test_buyOneSellOne_expenseIsOne(): t = Trades() t.buy(Trade(0.0, 1, "S", 1.0)) t.sell(Trade(1.0, -1, "S", 1.0)) assert 1.0 == t.expense(2)
def test_buyAtLeastOne(): t = Trades() with pytest.raises(Exception): t.buy(Trade(0.0, 0, "S", 1.0))
def test_buyOne_expenseIsZeroBeforeBuy(): t = Trades() t.buy(Trade(0.0, 1, "S", 1.0)) assert 0.0 == t.expense(date=-1)
def test_possible_profit_of_this_stock(): t = Trades() t.buy(Trade(0.0, 1, "x", 1.0)) t.buy(Trade(1.0, 1, "x", 2.0)) assert t.possible_profit_of_this_stock(1.5, "x", 2.0) == 1.0