Example #1
0
def test_calculate_investment_value__max():
    wallet = Wallet(0.01, 3)  # min recomended investment = 300
    wallet.money = 10000
    max_fraction = 5
    expected_result = 2000

    result = calculate_investment_value(wallet, max_fraction)

    assert expected_result == result
Example #2
0
def test_calculate_investment_value__invest_all():
    wallet = Wallet(0.01, 3)  # min recomended investment = 300
    wallet.money = 2000
    wallet.buy('CCC', 20, 50)  # cost = 1010
    max_fraction = 2
    expected_result = 990

    result = calculate_investment_value(wallet, max_fraction)

    assert expected_result == result
        def simulation(*args, **kwargs):
            wallet_history = pd.DataFrame(columns=['Date', 'Wallet state'])
            stocks_to_buy = []
            stocks_to_sell = []

            for day in time_range:
                day_str = day.strftime('%Y-%m-%d')

                # Buy selected day before. Loop over list, order can be important here.
                # Strategy can sort relevant stocks - high priority first.
                for tck in stocks_to_buy:
                    if not wallet.get_volume_of_stocks(tck):
                        price = traded_stocks[tck].ohlc['Open'].get(day, None)
                        if price:
                            total = calculate_investment_value(
                                wallet, max_positions)
                            total = total - wallet(
                                total)  # needs some money to pay commission
                            volume = math.floor(total / price)
                            if volume > 0:
                                wallet.buy(tck, volume, price)
                                print(
                                    info_str(day_str, 'B', tck, volume, price))
                                # make sure to not sell it the same day
                                if tck in stocks_to_sell:
                                    stocks_to_sell.remove(tck)

                # Sell selected day before. List to set, we dont care here about the order.
                # Set will remove duplicates.
                for tck in set(stocks_to_sell):
                    if wallet.get_volume_of_stocks(tck):
                        price = traded_stocks[tck].ohlc['Open'].get(day, None)
                        if price:
                            print_color = determine_print_color_from_prices(
                                price,
                                wallet.get_purchase_price_of_stocks(tck))
                            volume = wallet.sell_all(tck, price)
                            print_color(
                                info_str(day_str, 'S', tck, volume, price))

                # call decorated function - strategy function
                stocks_to_buy, stocks_to_sell = func(
                    day=day, traded_stocks=traded_stocks, *args, **kwargs)

                # if auto trading is active, check if price of any stock in the wallet crossed
                # take proffit or stop loss price; if yes then sell it immediately
                if auto_trading:
                    for tck in wallet.list_stocks().copy():

                        price_max = traded_stocks[tck].ohlc['High'].get(
                            day, None)
                        if price:
                            wallet.update_price(tck, price_max)
                        if wallet.change(tck) > take_profit:
                            price = wallet.get_purchase_price_of_stocks(
                                tck) * (1 + take_profit
                                        )  # selling it immediately
                            price = round(price, 2)
                            volume = wallet.sell_all(tck, price)
                            print_green(
                                info_str(day_str, 'TP', tck, volume, price))
                            continue

                        price_min = traded_stocks[tck].ohlc['Low'].get(
                            day, None)
                        if price:
                            wallet.update_price(tck, price_min)
                        if wallet.change(tck) < -stop_loss:
                            price = wallet.get_purchase_price_of_stocks(
                                tck) * (1 - stop_loss
                                        )  # selling it immediately
                            price = round(price, 2)
                            volume = wallet.sell_all(tck, price)
                            print_red(
                                info_str(day_str, 'SL', tck, volume, price))

                for tck in wallet.list_stocks():
                    # update the price to the closing price and calculate relative price change
                    ohlc = traded_stocks[tck].ohlc
                    price = ohlc['Close'].get(day, None)
                    if price:
                        wallet.update_price(tck, price)

                    # if auto trading is not active, then take profit / stop loss the next day
                    if not auto_trading:

                        # take profit the next day
                        if take_profit and wallet.change(tck) > take_profit:
                            stocks_to_sell.append(tck)

                        # stop loss the next day - price below purchase price
                        if stop_loss and wallet.change(tck) < -stop_loss:
                            stocks_to_sell.append(tck)

                # save history of the wallet
                wallet_history = wallet_history.append(
                    {
                        'Date': day,
                        'Wallet state': wallet.total_value
                    },
                    ignore_index=True)

            return wallet_history