https://colab.research.google.com/assets/colab-badge.svg

Entropy Monkey (flip the coin)

This is an example strategy how to randomly go all-in to a new token every day.

This is not a real strategy, but an example of different facilities available for the trade: how to construct a trading pair universe, how to integrate with Backtrader, how to check price, how to buy, how to close position.

  • The trading universe is SushiSwap pairs on Ethereum mainnet with USD 500k+ all-time buy volume.

  • The strategy picks randomly a tokens that have 200k USD volume for the last 7 days.

  • This is a simplified example strategy that ignores available liquidity and loss of trade balance due to slippage, currency conversions, etc.

  • This is not a Monte-Carlo simulation. The outcome of the strategy depends purely on which seed number you choose to initialise the random number generator. Thus, it not meaningful for any real-world comparison.

Creating trading universe

First let’s import libraries and initialise our dataset client.

[7]:
try:
    import tradingstrategy
except ImportError:
    !pip install tradingstrategy
    import site
    site.main()

from tradingstrategy.client import Client

client = Client.create_jupyter_client()

Started Trading Strategy in Jupyter notebook environment, configuration is stored in /Users/moo/.tradingstrategy

Let’s create a pair universe for Sushi. See full example. We will create a dataset of 4h candles that trade on Sushiswap on Ethereum.

[8]:
import pandas as pd
from tradingstrategy.chain import ChainId
from tradingstrategy.pair import PandasPairUniverse

# Decompress the pair dataset to Python map
columnar_pair_table = client.fetch_pair_universe()

# Exchange map data is so small it does not need any decompression
exchange_universe = client.fetch_exchange_universe()

# Convert PyArrow table to Pandas format to continue working on it
all_pairs_dataframe = columnar_pair_table.to_pandas()

# Filter down to pairs that only trade on Sushiswap
# and also have (had) some meaningful volume in some point of their life
sushi_swap = exchange_universe.get_by_name_and_chain(ChainId.ethereum, "sushiswap")
sushi_pairs: pd.DataFrame = all_pairs_dataframe.loc[
    (all_pairs_dataframe['exchange_id'] == sushi_swap.exchange_id) &  # Trades on Sushi
    (all_pairs_dataframe['buy_volume_all_time'] > 500_000)  # 500k min buys
]

# Create a Python set of pair ids
wanted_pair_ids = sushi_pairs["pair_id"]

# Make the trading pair data easily accessible
pair_universe = PandasPairUniverse(sushi_pairs)

print(f"Sushiswap on Ethereum has {len(pair_universe.get_all_pair_ids())} legit trading pairs")
Sushiswap on Ethereum has 403 legit trading pairs

Get daily candles and filter them against our wanted pair set.

[9]:
import datetime

from tradingstrategy.timebucket import TimeBucket
from tradingstrategy.candle import GroupedCandleUniverse
from tradingstrategy.pair import PandasPairUniverse
from tradingstrategy.frameworks.backtrader import prepare_candles_for_backtrader

# Get daily candles as Pandas DataFrame
all_candles = client.fetch_all_candles(TimeBucket.d1).to_pandas()
sushi_candles: pd.DataFrame = all_candles.loc[all_candles["pair_id"].isin(wanted_pair_ids)]

sushi_candles = prepare_candles_for_backtrader(sushi_candles)

# We limit candles to a specific date range to make this notebook deterministic
start = datetime.datetime(2020, 10, 1)
end = datetime.datetime(2021, 6, 1)

sushi_candles = sushi_candles[(sushi_candles.index >= start) & (sushi_candles.index <= end)]

# Group candles by the trading pair ticker
sushi_tickers = GroupedCandleUniverse(sushi_candles)

print(f"Candles to feed to the strategy, all pairs is {len(sushi_candles)}")
Candles to feed to the strategy, all pairs is 32990

Creating the strategy

See the Backtrader quickstart tutorial.

Cumulative volume indicator

We create a Backtrader indicator based on PeriodN indicator how much cumulative trade volume the trading pair has seen in the past. We use this indicator to filter out trading pairs that seem to be dead, as no trading happening, and thus not subject to our random entry.

More information about Backtrader custom indicators.

[10]:
import math
from random import Random

import backtrader as bt
from backtrader.indicators import PeriodN


class PastTradeVolumeIndicator(PeriodN):
    """Indicates whether the trading pair has reached certain volume for the last N days.

    Based on indicator base class that takes period (days) as an input.
    """

    lines = ('cum_volume',)

    params = (('period', 7),)

    def next(self):
        # This indicator is feed with volume line.
        # We simply take the sum of the daily volumes based on the period (number of days)
        datasum = math.fsum(self.data.get(size=self.p.period))
        self.lines.cum_volume[0] = datasum

Coinflip strategy core

By using the indicator from the above here is our strategy.

[11]:
from tradingstrategy.frameworks.backtrader import CapitalgramFeed

class EntropyMonkey(bt.Strategy):
    """A strategy that picks a new token to go all-in every day."""

    def __init__(self, pair_universe: PandasPairUniverse, seed: int):
        #: Allows us to print human-readable pair information
        self.pair_universe = pair_universe

        #: Initialize (somewhat) determininistic random number generator
        self.dice = Random(seed)

        #: We operate on daily candles.
        #: At each tick, we process to the next candle
        self.day = 0

        #: Cumulative volume indicator for each of the data feed
        self.indicators = {}
        pair: CapitalgramFeed
        for pair in self.datas:
            self.indicators[pair] = PastTradeVolumeIndicator(pair.lines.volume)

        # How much USD volume token needs to have in order to be eligible for a pick
        self.cumulative_volume_threshold = 200_000

        # How many times we try to pick a token pair to buy
        # before giving up (at early days there might not be enough volume)
        self.pick_attempts = 100

        # If our balance goes below this considering giving up
        self.cash_balance_death_threshold = 100

    def next(self):
        """Tick the strategy.

        Because we are using daily candles, tick will run once per each day.
        """

        # Advance to the next day
        self.day += 1

        # Pick a new token to buy
        for i in range(self.pick_attempts):
            random_pair: CapitalgramFeed = self.dice.choice(self.datas)
            pair_info = random_pair.pair_info
            cum_volume_indicator = self.indicators[random_pair]
            volume = cum_volume_indicator.lines.cum_volume[0]
            if volume > self.cumulative_volume_threshold and random_pair.close[0] > 0:
                break
        else:
            print(f"On day #{self.day} did not find any token to buy")
            return

        # Sell any existing token we have
        for ticker in self.datas:
             if self.getposition(ticker).size > 0:
                # Too verbose
                # print(f"On day #{self.day}, selling existing position of {pair_info.base_token_symbol} - {pair_info.quote_token_symbol}")
                self.close(ticker)

        # Buy in with all money we have.
        # We are not really worried about order size quantisation in crypto.
        cash = self.broker.get_cash()

        if cash < self.cash_balance_death_threshold:
            # We are busted
            return

        # Buy using the daily candle closing price as the rate
        price = random_pair.close[0]
        assert price > 0, "Pair had zero price "
        size = cash / price

        # Sell the existing position
        print(f"On day #{self.day} we are buying {pair_info.base_token_symbol} - {pair_info.quote_token_symbol} that has 7 days vol of {volume:,.0f} USD. Buy in at price of {price:.4f} USD, cash at hand {cash:,.2f} USD")

        self.buy(random_pair, size=size, exectype=bt.Order.Market)

Feeding the strategy

Feed our trade data from Sushiswap to the Backtrader strategy.

[12]:
from tradingstrategy.frameworks.backtrader import add_dataframes_as_feeds

# Create a cerebro entity
cerebro = bt.Cerebro(stdstats=False)

# Add a strategy
cerebro.addstrategy(EntropyMonkey, pair_universe=pair_universe, seed=0x1000)

# Pass all Sushi pairs to the data fees to the strategy
# noinspection JupyterKernel
feeds = [df for pair_id, df in sushi_tickers.get_all_pairs()]
add_dataframes_as_feeds(
    cerebro,
    pair_universe,
    feeds,
    start,
    end,
    TimeBucket.d1)

Running the strategy

We are adding some observers that tell us how well the strategy performs.

[13]:
cerebro.addobserver(bt.observers.Value)
cerebro.addanalyzer(bt.analyzers.SharpeRatio, riskfreerate=0.0)
cerebro.addanalyzer(bt.analyzers.Returns)
cerebro.addanalyzer(bt.analyzers.DrawDown)

# Run over everything
print('Starting portfolio value: %.2f USD' % cerebro.broker.getvalue())
results = cerebro.run()
print('Ending portfolio value: %.2f USD' % cerebro.broker.getvalue())

strategy: EntropyMonkey = results[0]
Starting portfolio value: 10000.00 USD
On day #1 we are buying COMP - WETH that has 7 days vol of 5,400,299 USD. Buy in at price of 109.9777 USD, cash at hand 10,000.00 USD
On day #2 we are buying SNX - WETH that has 7 days vol of 8,709,391 USD. Buy in at price of 3.9839 USD, cash at hand 10,000.00 USD
On day #3 we are buying LEND - WETH that has 7 days vol of 17,990,044 USD. Buy in at price of 0.4978 USD, cash at hand 10,000.00 USD
On day #4 we are buying WETH - USDT that has 7 days vol of 23,605,109 USD. Buy in at price of 372.3140 USD, cash at hand 10,000.00 USD
On day #5 we are buying LIGHT - WETH that has 7 days vol of 1,862,103 USD. Buy in at price of 69.6684 USD, cash at hand 10,000.00 USD
On day #6 we are buying AMPL - WETH that has 7 days vol of 1,455,346 USD. Buy in at price of 0.8215 USD, cash at hand 114.63 USD
On day #7 we are buying CRO - WETH that has 7 days vol of 266,073 USD. Buy in at price of 0.1398 USD, cash at hand 11,318.23 USD
On day #8 we are buying yyDAI+yUSDC+yUSDT+yTUSD - WETH that has 7 days vol of 919,214 USD. Buy in at price of 1.1496 USD, cash at hand 124.60 USD
On day #9 we are buying <unknown> - WETH that has 7 days vol of 617,970 USD. Buy in at price of 561.2661 USD, cash at hand 11,594.49 USD
On day #10 we are buying TBTC - WBTC that has 7 days vol of 2,053,673 USD. Buy in at price of 11351.1523 USD, cash at hand 233.20 USD
On day #11 we are buying SRM - WETH that has 7 days vol of 2,111,464 USD. Buy in at price of 1.2654 USD, cash at hand 11,148.52 USD
On day #12 we are buying PICKLE - WETH that has 7 days vol of 451,133 USD. Buy in at price of 25.9618 USD, cash at hand 158.64 USD
On day #13 we are buying wNXM - WETH that has 7 days vol of 1,331,864 USD. Buy in at price of 29.1289 USD, cash at hand 11,310.22 USD
On day #14 we are buying UFT - WETH that has 7 days vol of 397,239 USD. Buy in at price of 0.1427 USD, cash at hand 215.33 USD
On day #15 we are buying SUSHI - WETH that has 7 days vol of 13,072,720 USD. Buy in at price of 0.6658 USD, cash at hand 9,814.78 USD
On day #16 we are buying SNX - WETH that has 7 days vol of 8,982,246 USD. Buy in at price of 3.8750 USD, cash at hand 244.24 USD
On day #17 we are buying BAND - WETH that has 7 days vol of 2,358,723 USD. Buy in at price of 5.9144 USD, cash at hand 9,817.15 USD
On day #18 we are buying LEND - WETH that has 7 days vol of 7,716,760 USD. Buy in at price of 0.3933 USD, cash at hand 223.89 USD
On day #19 we are buying WETH - DAI that has 7 days vol of 8,722,620 USD. Buy in at price of 405.2222 USD, cash at hand 9,856.24 USD
On day #20 we are buying WETH - USDT that has 7 days vol of 9,489,875 USD. Buy in at price of 394.0026 USD, cash at hand 274.42 USD
On day #21 we are buying TBTC - WBTC that has 7 days vol of 948,846 USD. Buy in at price of 13640.4814 USD, cash at hand 9,477.97 USD
On day #22 we are buying AMPL - WETH that has 7 days vol of 2,729,667 USD. Buy in at price of 1.1007 USD, cash at hand 306.04 USD
On day #23 we are buying TBTC - WBTC that has 7 days vol of 716,855 USD. Buy in at price of 13550.8828 USD, cash at hand 9,146.95 USD
On day #24 we are buying wNXM - WETH that has 7 days vol of 1,533,596 USD. Buy in at price of 21.1529 USD, cash at hand 592.49 USD
On day #25 we are buying UFT - WETH that has 7 days vol of 209,297 USD. Buy in at price of 0.3898 USD, cash at hand 9,146.04 USD
On day #26 we are buying RARI - WETH that has 7 days vol of 401,226 USD. Buy in at price of 2.1613 USD, cash at hand 796.64 USD
On day #27 we are buying RARI - WETH that has 7 days vol of 398,079 USD. Buy in at price of 1.6235 USD, cash at hand 8,543.97 USD
On day #28 we are buying COMP - WETH that has 7 days vol of 7,128,922 USD. Buy in at price of 87.0330 USD, cash at hand 740.84 USD
On day #29 we are buying SUSHI - WETH that has 7 days vol of 13,251,453 USD. Buy in at price of 0.4905 USD, cash at hand 8,482.36 USD
On day #30 we are buying COMP - WETH that has 7 days vol of 5,102,920 USD. Buy in at price of 91.0242 USD, cash at hand 719.89 USD
On day #31 we are buying RARI - WETH that has 7 days vol of 288,789 USD. Buy in at price of 2.0089 USD, cash at hand 8,905.05 USD
On day #32 we are buying yyDAI+yUSDC+yUSDT+yTUSD - WETH that has 7 days vol of 686,925 USD. Buy in at price of 1.1631 USD, cash at hand 767.77 USD
On day #33 we are buying WETH - sUSD that has 7 days vol of 3,674,238 USD. Buy in at price of 455.0316 USD, cash at hand 9,636.82 USD
On day #34 we are buying LEND - WETH that has 7 days vol of 2,374,965 USD. Buy in at price of 0.5456 USD, cash at hand 773.20 USD
On day #35 we are buying wNXM - WETH that has 7 days vol of 4,931,892 USD. Buy in at price of 22.5197 USD, cash at hand 9,357.46 USD
On day #36 we are buying HEGIC - WETH that has 7 days vol of 227,730 USD. Buy in at price of 0.2023 USD, cash at hand 867.75 USD
On day #37 we are buying YAM - WETH that has 7 days vol of 622,838 USD. Buy in at price of 0.9366 USD, cash at hand 9,590.91 USD
On day #38 we are buying TBTC - WBTC that has 7 days vol of 335,755 USD. Buy in at price of 16280.7822 USD, cash at hand 896.59 USD
On day #39 we are buying WETH - DAI that has 7 days vol of 9,364,480 USD. Buy in at price of 460.9188 USD, cash at hand 11,079.20 USD
On day #40 we are buying WETH - USDC that has 7 days vol of 13,603,756 USD. Buy in at price of 450.4316 USD, cash at hand 964.85 USD
On day #41 we are buying AXS - WETH that has 7 days vol of 1,517,226 USD. Buy in at price of 0.5586 USD, cash at hand 10,693.68 USD
On day #42 we are buying SEEN - WETH that has 7 days vol of 351,925 USD. Buy in at price of 1.0126 USD, cash at hand 1,082.07 USD
On day #43 we are buying AMPL - WETH that has 7 days vol of 1,940,827 USD. Buy in at price of 0.9613 USD, cash at hand 9,737.74 USD
On day #44 we are buying WETH - USDT that has 7 days vol of 39,334,478 USD. Buy in at price of 471.9820 USD, cash at hand 928.45 USD
On day #45 we are buying yyDAI+yUSDC+yUSDT+yTUSD - WETH that has 7 days vol of 346,464 USD. Buy in at price of 1.1731 USD, cash at hand 9,503.59 USD
On day #46 we are buying WETH - sUSD that has 7 days vol of 4,692,459 USD. Buy in at price of 548.8455 USD, cash at hand 802.17 USD
On day #47 we are buying REN - WETH that has 7 days vol of 4,182,203 USD. Buy in at price of 0.3597 USD, cash at hand 9,742.77 USD
On day #48 we are buying WETH - WBTC that has 7 days vol of 67,803,144 USD. Buy in at price of 608.4514 USD, cash at hand 896.52 USD
On day #49 we are buying COMP - WETH that has 7 days vol of 9,637,975 USD. Buy in at price of 126.7125 USD, cash at hand 9,839.57 USD
On day #50 we are buying UNI - WETH that has 7 days vol of 8,753,038 USD. Buy in at price of 3.6966 USD, cash at hand 821.35 USD
On day #51 we are buying AMPL - WETH that has 7 days vol of 2,193,949 USD. Buy in at price of 1.2649 USD, cash at hand 9,493.68 USD
On day #52 we are buying LINK - WETH that has 7 days vol of 19,413,982 USD. Buy in at price of 12.5245 USD, cash at hand 738.14 USD
On day #53 we are buying SEEN - WETH that has 7 days vol of 439,981 USD. Buy in at price of 1.3127 USD, cash at hand 8,813.36 USD
On day #54 we are buying wNXM - WETH that has 7 days vol of 606,202 USD. Buy in at price of 21.8029 USD, cash at hand 798.53 USD
On day #55 we are buying CRD - WETH that has 7 days vol of 1,229,871 USD. Buy in at price of 0.0447 USD, cash at hand 9,037.47 USD
On day #56 we are buying WETH - USDT that has 7 days vol of 79,755,403 USD. Buy in at price of 586.0411 USD, cash at hand 936.26 USD
On day #57 we are buying WETH - DAI that has 7 days vol of 102,226,164 USD. Buy in at price of 597.6522 USD, cash at hand 8,648.36 USD
On day #58 we are buying WETH - USDT that has 7 days vol of 61,645,656 USD. Buy in at price of 616.2513 USD, cash at hand 951.68 USD
On day #59 we are buying REN - WETH that has 7 days vol of 1,713,223 USD. Buy in at price of 0.3220 USD, cash at hand 8,880.05 USD
On day #60 we are buying SEEN - WETH that has 7 days vol of 1,288,464 USD. Buy in at price of 2.1556 USD, cash at hand 891.31 USD
On day #61 we are buying AMPL - WETH that has 7 days vol of 2,739,591 USD. Buy in at price of 1.3129 USD, cash at hand 9,148.18 USD
On day #62 we are buying SUSHI - WETH that has 7 days vol of 157,903,508 USD. Buy in at price of 2.4117 USD, cash at hand 909.88 USD
On day #63 we are buying SNX - WETH that has 7 days vol of 16,239,436 USD. Buy in at price of 4.6683 USD, cash at hand 9,355.43 USD
On day #64 we are buying SNX - WETH that has 7 days vol of 14,952,161 USD. Buy in at price of 4.8822 USD, cash at hand 830.93 USD
On day #65 we are buying YAM - WETH that has 7 days vol of 7,638,346 USD. Buy in at price of 0.9095 USD, cash at hand 9,763.32 USD
On day #66 we are buying BAND - WETH that has 7 days vol of 5,758,024 USD. Buy in at price of 6.0444 USD, cash at hand 735.35 USD
On day #67 we are buying AAVE - WETH that has 7 days vol of 22,602,682 USD. Buy in at price of 78.0414 USD, cash at hand 10,235.58 USD
On day #68 we are buying LINA - WETH that has 7 days vol of 361,627 USD. Buy in at price of 0.0105 USD, cash at hand 715.70 USD
On day #69 we are buying LINK - WETH that has 7 days vol of 13,397,030 USD. Buy in at price of 12.9242 USD, cash at hand 10,932.89 USD
On day #70 we are buying WETH - USDC that has 7 days vol of 52,233,024 USD. Buy in at price of 590.7690 USD, cash at hand 662.51 USD
On day #71 we are buying BAND - WETH that has 7 days vol of 3,978,207 USD. Buy in at price of 6.7850 USD, cash at hand 10,773.32 USD
On day #72 we are buying YAM - WETH that has 7 days vol of 1,911,766 USD. Buy in at price of 1.0491 USD, cash at hand 696.02 USD
On day #73 we are buying UMA - WETH that has 7 days vol of 5,775,214 USD. Buy in at price of 8.4532 USD, cash at hand 10,365.01 USD
On day #74 we are buying COVER - WETH that has 7 days vol of 21,733,151 USD. Buy in at price of 959.2275 USD, cash at hand 677.53 USD
On day #75 we are buying SFI - WETH that has 7 days vol of 3,294,228 USD. Buy in at price of 371.3159 USD, cash at hand 9,833.65 USD
On day #76 we are buying SUSHI - WETH that has 7 days vol of 117,249,710 USD. Buy in at price of 2.5040 USD, cash at hand 623.71 USD
On day #77 we are buying OMG - WETH that has 7 days vol of 533,173 USD. Buy in at price of 2.9902 USD, cash at hand 9,303.71 USD
On day #78 we are buying WETH - USDT that has 7 days vol of 75,412,849 USD. Buy in at price of 584.0107 USD, cash at hand 548.46 USD
On day #79 we are buying OMG - WETH that has 7 days vol of 782,007 USD. Buy in at price of 2.5760 USD, cash at hand 7,154.72 USD
On day #80 we are buying SUSHI - WETH that has 7 days vol of 155,415,591 USD. Buy in at price of 2.5350 USD, cash at hand 565.99 USD
On day #81 we are buying UNI - WETH that has 7 days vol of 5,094,339 USD. Buy in at price of 3.3960 USD, cash at hand 7,202.65 USD
On day #82 we are buying DSD - USDC that has 7 days vol of 3,771,661 USD. Buy in at price of 0.6021 USD, cash at hand 513.95 USD
On day #83 we are buying AMPL - WETH that has 7 days vol of 801,520 USD. Buy in at price of 0.9695 USD, cash at hand 7,891.48 USD
On day #84 we are buying REN - WETH that has 7 days vol of 5,558,624 USD. Buy in at price of 0.3587 USD, cash at hand 415.73 USD
On day #85 we are buying MIC - USDT that has 7 days vol of 16,434,736 USD. Buy in at price of 203.6477 USD, cash at hand 8,097.23 USD
On day #86 we are buying BOR - WETH that has 7 days vol of 470,972 USD. Buy in at price of 264.6402 USD, cash at hand 392.95 USD
On day #87 we are buying COMP - WETH that has 7 days vol of 9,846,665 USD. Buy in at price of 146.1777 USD, cash at hand 5,955.53 USD
On day #88 we are buying ALPHA - WETH that has 7 days vol of 1,122,281 USD. Buy in at price of 0.2390 USD, cash at hand 374.08 USD
On day #89 we are buying LEND - WETH that has 7 days vol of 409,415 USD. Buy in at price of 0.9247 USD, cash at hand 5,536.49 USD
On day #90 we are buying WETH - DAI that has 7 days vol of 208,183,736 USD. Buy in at price of 1042.2900 USD, cash at hand 315.05 USD
On day #91 we are buying DPI - WETH that has 7 days vol of 262,862 USD. Buy in at price of 162.7457 USD, cash at hand 6,995.91 USD
On day #92 we are buying LINK - WETH that has 7 days vol of 64,455,975 USD. Buy in at price of 17.3434 USD, cash at hand 506.97 USD
On day #93 we are buying <unknown> - WETH that has 7 days vol of 6,479,966 USD. Buy in at price of 1061.7201 USD, cash at hand 7,328.39 USD
On day #94 we are buying CRETH2 - WETH that has 7 days vol of 344,585 USD. Buy in at price of 1041.2147 USD, cash at hand 526.38 USD
On day #95 we are buying WETH - WBTC that has 7 days vol of 448,671,224 USD. Buy in at price of 1283.3453 USD, cash at hand 7,197.73 USD
On day #96 we are buying ESD - WETH that has 7 days vol of 10,236,316 USD. Buy in at price of 0.6711 USD, cash at hand 579.18 USD
On day #97 we are buying SFI - WETH that has 7 days vol of 3,436,461 USD. Buy in at price of 344.4769 USD, cash at hand 7,052.66 USD
On day #98 we are buying MIS - USDT that has 7 days vol of 257,734,917 USD. Buy in at price of 225.1981 USD, cash at hand 582.42 USD
On day #99 we are buying WETH - WBTC that has 7 days vol of 380,539,782 USD. Buy in at price of 1124.7678 USD, cash at hand 8,090.71 USD
On day #100 we are buying ALPHA - WETH that has 7 days vol of 4,862,726 USD. Buy in at price of 0.4394 USD, cash at hand 431.26 USD
On day #101 we are buying JRT - WETH that has 7 days vol of 421,087 USD. Buy in at price of 0.0931 USD, cash at hand 8,807.44 USD
On day #102 we are buying AMPL - WETH that has 7 days vol of 1,053,219 USD. Buy in at price of 0.8866 USD, cash at hand 219.69 USD
On day #103 we are buying CRD - WETH that has 7 days vol of 352,108 USD. Buy in at price of 0.0487 USD, cash at hand 9,809.17 USD
On day #104 we are buying ARCH - WETH that has 7 days vol of 2,526,705 USD. Buy in at price of 2.0956 USD, cash at hand 213.20 USD
On day #105 we are buying ARCH - WETH that has 7 days vol of 2,543,070 USD. Buy in at price of 1.6128 USD, cash at hand 10,263.85 USD
On day #106 we are buying yyDAI+yUSDC+yUSDT+yTUSD - WETH that has 7 days vol of 5,907,280 USD. Buy in at price of 1.2223 USD, cash at hand 299.99 USD
On day #107 we are buying BADGER - WBTC that has 7 days vol of 24,049,872 USD. Buy in at price of 12.1956 USD, cash at hand 13,578.58 USD
On day #108 we are buying <unknown> - WETH that has 7 days vol of 5,896,021 USD. Buy in at price of 1398.3127 USD, cash at hand 526.17 USD
On day #109 we are buying LINA - WETH that has 7 days vol of 1,318,035 USD. Buy in at price of 0.0217 USD, cash at hand 14,048.49 USD
On day #110 we are buying LINK - WETH that has 7 days vol of 92,345,078 USD. Buy in at price of 24.6690 USD, cash at hand 436.14 USD
On day #111 we are buying OMG - WETH that has 7 days vol of 2,469,984 USD. Buy in at price of 3.5707 USD, cash at hand 17,410.54 USD
On day #112 we are buying ARMOR - WBTC that has 7 days vol of 1,600,450 USD. Buy in at price of 0.8849 USD, cash at hand 620.71 USD
On day #113 we are buying AAVE - WETH that has 7 days vol of 122,414,332 USD. Buy in at price of 283.5727 USD, cash at hand 16,402.50 USD
On day #114 we are buying DUSD - WETH that has 7 days vol of 398,026 USD. Buy in at price of 0.9871 USD, cash at hand 236.92 USD
On day #115 we are buying SRM - WETH that has 7 days vol of 4,175,255 USD. Buy in at price of 1.8933 USD, cash at hand 17,246.70 USD
On day #116 we are buying LPT - WETH that has 7 days vol of 214,351 USD. Buy in at price of 2.8818 USD, cash at hand 175.51 USD
On day #117 we are buying TRU - WETH that has 7 days vol of 386,144 USD. Buy in at price of 0.2438 USD, cash at hand 18,894.35 USD
On day #118 we are buying MIC - USDT that has 7 days vol of 23,345,475 USD. Buy in at price of 0.3763 USD, cash at hand 510.19 USD
On day #119 we are buying aETHc - WETH that has 7 days vol of 370,526 USD. Buy in at price of 1452.2032 USD, cash at hand 18,315.62 USD
On day #120 we are buying ESD - WETH that has 7 days vol of 5,522,614 USD. Buy in at price of 0.2927 USD, cash at hand 18,926.32 USD
On day #122 we are buying LON - USDT that has 7 days vol of 78,741,967 USD. Buy in at price of 7.5452 USD, cash at hand 15,776.10 USD
On day #124 we are buying ANY - WETH that has 7 days vol of 708,694 USD. Buy in at price of 1.0938 USD, cash at hand 15,544.21 USD
On day #125 we are buying ASSY - WETH that has 7 days vol of 500,528 USD. Buy in at price of 1.7408 USD, cash at hand 15,544.21 USD
On day #126 we are buying DEFI+L - WETH that has 7 days vol of 278,602 USD. Buy in at price of 4.6194 USD, cash at hand 319.52 USD
On day #127 we are buying SRM - WETH that has 7 days vol of 9,757,682 USD. Buy in at price of 4.2580 USD, cash at hand 16,202.06 USD
On day #128 we are buying AMP - WETH that has 7 days vol of 3,073,093 USD. Buy in at price of 0.0324 USD, cash at hand 598.95 USD
On day #129 we are buying RGT - WETH that has 7 days vol of 31,210,263 USD. Buy in at price of 12.7081 USD, cash at hand 15,666.72 USD
On day #130 we are buying PNK - WETH that has 7 days vol of 702,125 USD. Buy in at price of 0.0806 USD, cash at hand 470.04 USD
On day #131 we are buying BADGER - WBTC that has 7 days vol of 71,749,743 USD. Buy in at price of 66.1520 USD, cash at hand 17,186.34 USD
On day #132 we are buying YFI - WETH that has 7 days vol of 256,729,616 USD. Buy in at price of 40663.3672 USD, cash at hand 458.35 USD
On day #133 we are buying STAKE - WETH that has 7 days vol of 1,734,588 USD. Buy in at price of 21.3184 USD, cash at hand 17,012.87 USD
On day #134 we are buying AMP - WETH that has 7 days vol of 8,232,783 USD. Buy in at price of 0.0315 USD, cash at hand 243.94 USD
On day #135 we are buying TORN - WETH that has 7 days vol of 9,404,703 USD. Buy in at price of 290.0152 USD, cash at hand 16,149.78 USD
On day #137 we are buying DERI - USDT that has 7 days vol of 2,695,153 USD. Buy in at price of 0.4460 USD, cash at hand 17,644.99 USD
On day #138 we are buying Mars - USDT that has 7 days vol of 834,393 USD. Buy in at price of 0.1212 USD, cash at hand 17,644.99 USD
On day #140 we are buying LEND - WETH that has 7 days vol of 484,579 USD. Buy in at price of 3.5762 USD, cash at hand 18,661.06 USD
On day #141 we are buying TORN - WETH that has 7 days vol of 8,773,454 USD. Buy in at price of 299.9341 USD, cash at hand 18,661.06 USD
On day #142 we are buying INDEX - WETH that has 7 days vol of 1,352,226 USD. Buy in at price of 17.7906 USD, cash at hand 18,661.06 USD
On day #143 we are buying DSD - USDC that has 7 days vol of 3,079,488 USD. Buy in at price of 0.0722 USD, cash at hand 18,661.06 USD
On day #144 we are buying TORN - WETH that has 7 days vol of 4,920,146 USD. Buy in at price of 251.2940 USD, cash at hand 18,661.06 USD
On day #145 we are buying ARMOR - WBTC that has 7 days vol of 222,904 USD. Buy in at price of 0.6613 USD, cash at hand 998.46 USD
On day #146 we are buying AMPL - WETH that has 7 days vol of 2,506,015 USD. Buy in at price of 0.8512 USD, cash at hand 15,229.49 USD
On day #147 we are buying Mars - USDT that has 7 days vol of 375,033 USD. Buy in at price of 0.0843 USD, cash at hand 828.23 USD
On day #148 we are buying PUNK - WETH that has 7 days vol of 539,757 USD. Buy in at price of 145963.0781 USD, cash at hand 15,162.82 USD
On day #149 we are buying ZRX - WETH that has 7 days vol of 1,745,859 USD. Buy in at price of 1.3879 USD, cash at hand 924.97 USD
On day #150 we are buying BAND - WETH that has 7 days vol of 4,628,116 USD. Buy in at price of 12.4757 USD, cash at hand 13,414.93 USD
On day #151 we are buying yveCRV-DAO - WETH that has 7 days vol of 3,705,201 USD. Buy in at price of 1.9097 USD, cash at hand 799.93 USD
On day #152 we are buying XFT - WETH that has 7 days vol of 209,525 USD. Buy in at price of 5.0198 USD, cash at hand 13,742.00 USD
On day #153 we are buying WETH - USDC that has 7 days vol of 300,419,972 USD. Buy in at price of 1837.4292 USD, cash at hand 129.78 USD
On day #154 we are buying xSUSHI - WETH that has 7 days vol of 2,313,329 USD. Buy in at price of 21.7480 USD, cash at hand 14,511.87 USD
On day #155 we are buying ANT - WETH that has 7 days vol of 928,448 USD. Buy in at price of 5.2133 USD, cash at hand 14,644.18 USD
On day #157 we are buying DEFI+L - WETH that has 7 days vol of 213,796 USD. Buy in at price of 4.9719 USD, cash at hand 14,279.92 USD
On day #158 we are buying PREMIA - WETH that has 7 days vol of 3,042,327 USD. Buy in at price of 1.8013 USD, cash at hand 650.35 USD
On day #159 we are buying XFT - WETH that has 7 days vol of 383,769 USD. Buy in at price of 4.4271 USD, cash at hand 15,335.28 USD
On day #160 we are buying BAO - USDC that has 7 days vol of 417,436 USD. Buy in at price of 0.0010 USD, cash at hand 776.87 USD
On day #161 we are buying PICKLE - WETH that has 7 days vol of 214,689 USD. Buy in at price of 11.9028 USD, cash at hand 14,394.08 USD
On day #162 we are buying TUSD - WETH that has 7 days vol of 686,443 USD. Buy in at price of 0.9900 USD, cash at hand 822.09 USD
On day #163 we are buying XFT - WETH that has 7 days vol of 271,633 USD. Buy in at price of 4.6455 USD, cash at hand 15,261.76 USD
On day #164 we are buying ARMOR - WETH that has 7 days vol of 967,997 USD. Buy in at price of 0.6023 USD, cash at hand 976.72 USD
On day #165 we are buying YFI - WETH that has 7 days vol of 36,576,099 USD. Buy in at price of 35022.5859 USD, cash at hand 15,356.69 USD
On day #166 we are buying MIS - USDT that has 7 days vol of 257,088 USD. Buy in at price of 3.9923 USD, cash at hand 852.69 USD
On day #167 we are buying YETI - WETH that has 7 days vol of 365,037 USD. Buy in at price of 1.7970 USD, cash at hand 15,465.17 USD
On day #168 we are buying SUPER - WETH that has 7 days vol of 510,596 USD. Buy in at price of 2.9928 USD, cash at hand 641.14 USD
On day #169 we are buying DPI - WETH that has 7 days vol of 1,575,540 USD. Buy in at price of 367.0734 USD, cash at hand 14,327.63 USD
On day #170 we are buying WETH - WBTC that has 7 days vol of 220,031,596 USD. Buy in at price of 1585.1584 USD, cash at hand 821.82 USD
On day #171 we are buying ESD - WETH that has 7 days vol of 2,132,348 USD. Buy in at price of 0.1647 USD, cash at hand 14,961.89 USD
On day #172 we are buying HEGIC - WETH that has 7 days vol of 2,775,316 USD. Buy in at price of 0.2521 USD, cash at hand 852.36 USD
On day #173 we are buying ALCX - WETH that has 7 days vol of 97,205,524 USD. Buy in at price of 1371.3940 USD, cash at hand 15,102.78 USD
On day #174 we are buying BCP - WETH that has 7 days vol of 306,781 USD. Buy in at price of 3.3347 USD, cash at hand 783.80 USD
On day #175 we are buying WOO - USDC that has 7 days vol of 3,246,586 USD. Buy in at price of 0.5567 USD, cash at hand 16,253.13 USD
On day #176 we are buying BAO - USDC that has 7 days vol of 294,705 USD. Buy in at price of 0.0008 USD, cash at hand 112.40 USD
On day #177 we are buying PUNK - WETH that has 7 days vol of 329,877 USD. Buy in at price of 116831.6094 USD, cash at hand 14,605.13 USD
On day #178 we are buying UNI - WETH that has 7 days vol of 13,238,274 USD. Buy in at price of 30.1942 USD, cash at hand 14,726.73 USD
On day #179 we are buying DERI - USDT that has 7 days vol of 1,693,651 USD. Buy in at price of 0.2579 USD, cash at hand 14,726.73 USD
On day #181 we are buying TRU - WETH that has 7 days vol of 1,929,794 USD. Buy in at price of 0.4979 USD, cash at hand 14,490.60 USD
On day #182 we are buying MM - WETH that has 7 days vol of 256,930 USD. Buy in at price of 11.4083 USD, cash at hand 14,490.60 USD
On day #184 we are buying POOL - WETH that has 7 days vol of 907,055 USD. Buy in at price of 22.1397 USD, cash at hand 15,506.80 USD
On day #185 we are buying ONX - WETH that has 7 days vol of 887,777 USD. Buy in at price of 2.3588 USD, cash at hand 15,506.80 USD
On day #186 we are buying buidl - WETH that has 7 days vol of 430,670 USD. Buy in at price of 12.1108 USD, cash at hand 15,506.80 USD
On day #187 we are buying RUNE - WETH that has 7 days vol of 26,909,950 USD. Buy in at price of 11.1586 USD, cash at hand 15,506.80 USD
On day #188 we are buying BANK - WETH that has 7 days vol of 11,815,679 USD. Buy in at price of 491.4038 USD, cash at hand 181.74 USD
On day #189 we are buying INJ - WETH that has 7 days vol of 1,950,318 USD. Buy in at price of 13.8807 USD, cash at hand 16,984.72 USD
On day #191 we are buying ICHI - WETH that has 7 days vol of 2,545,567 USD. Buy in at price of 14.7462 USD, cash at hand 16,281.02 USD
On day #192 we are buying RAY - WETH that has 7 days vol of 3,435,762 USD. Buy in at price of 10.4797 USD, cash at hand 16,281.02 USD
On day #193 we are buying DERI - USDT that has 7 days vol of 3,236,833 USD. Buy in at price of 0.3868 USD, cash at hand 16,281.02 USD
On day #194 we are buying CHI - WETH that has 7 days vol of 916,805 USD. Buy in at price of 5.7927 USD, cash at hand 233.80 USD
On day #195 we are buying OGN - WETH that has 7 days vol of 11,466,825 USD. Buy in at price of 1.6751 USD, cash at hand 11,449.57 USD
On day #196 we are buying TORN - WETH that has 7 days vol of 2,972,833 USD. Buy in at price of 134.1028 USD, cash at hand 418.41 USD
On day #197 we are buying DERI - USDT that has 7 days vol of 4,580,433 USD. Buy in at price of 0.3383 USD, cash at hand 11,860.62 USD
On day #198 we are buying REVV - WETH that has 7 days vol of 254,939 USD. Buy in at price of 0.2260 USD, cash at hand 813.86 USD
On day #199 we are buying DOGES - WETH that has 7 days vol of 1,507,081 USD. Buy in at price of 165.0839 USD, cash at hand 8,810.14 USD
On day #200 we are buying OCC - USDC that has 7 days vol of 211,131 USD. Buy in at price of 9.3675 USD, cash at hand 959.75 USD
On day #201 we are buying FARM - WETH that has 7 days vol of 1,058,453 USD. Buy in at price of 198.5730 USD, cash at hand 10,061.90 USD
On day #202 we are buying POOL - WETH that has 7 days vol of 850,055 USD. Buy in at price of 17.0397 USD, cash at hand 484.58 USD
On day #203 we are buying WETH - USDC that has 7 days vol of 669,241,776 USD. Buy in at price of 2665.1069 USD, cash at hand 11,117.17 USD
On day #204 we are buying CHI - WETH that has 7 days vol of 2,007,851 USD. Buy in at price of 3.0865 USD, cash at hand 513.36 USD
On day #205 we are buying COVER - WETH that has 7 days vol of 3,871,960 USD. Buy in at price of 732.4871 USD, cash at hand 11,404.51 USD
On day #206 we are buying LON - USDT that has 7 days vol of 12,765,435 USD. Buy in at price of 5.5566 USD, cash at hand 601.96 USD
On day #207 we are buying POOL - WETH that has 7 days vol of 787,169 USD. Buy in at price of 19.5196 USD, cash at hand 10,766.32 USD
On day #208 we are buying UMA - WETH that has 7 days vol of 9,674,478 USD. Buy in at price of 25.5682 USD, cash at hand 329.23 USD
On day #209 we are buying YLD - WETH that has 7 days vol of 3,033,467 USD. Buy in at price of 0.6984 USD, cash at hand 10,322.00 USD
On day #210 we are buying MOVE - WETH that has 7 days vol of 869,405 USD. Buy in at price of 0.8306 USD, cash at hand 615.16 USD
On day #211 we are buying PENDLE - WETH that has 7 days vol of 19,143,311 USD. Buy in at price of 2.2800 USD, cash at hand 10,086.09 USD
On day #212 we are buying WETH - DAI that has 7 days vol of 438,052,920 USD. Buy in at price of 3483.4888 USD, cash at hand 571.51 USD
On day #213 we are buying API3 - WETH that has 7 days vol of 3,460,115 USD. Buy in at price of 6.9760 USD, cash at hand 8,317.70 USD
On day #214 we are buying BAO - WETH that has 7 days vol of 2,813,241 USD. Buy in at price of 0.0009 USD, cash at hand 487.72 USD
On day #215 we are buying ALEPH - WETH that has 7 days vol of 618,067 USD. Buy in at price of 0.4804 USD, cash at hand 8,601.01 USD
On day #216 we are buying SUPER - WETH that has 7 days vol of 786,299 USD. Buy in at price of 2.0255 USD, cash at hand 371.81 USD
On day #217 we are buying OPIUM - WETH that has 7 days vol of 2,428,789 USD. Buy in at price of 6.7330 USD, cash at hand 7,739.71 USD
On day #218 we are buying PUNK - WETH that has 7 days vol of 491,400 USD. Buy in at price of 202085.1562 USD, cash at hand 112.59 USD
On day #219 we are buying DNT - WETH that has 7 days vol of 564,709 USD. Buy in at price of 0.2645 USD, cash at hand 7,475.89 USD
On day #221 we are buying NU - WETH that has 7 days vol of 669,434 USD. Buy in at price of 0.4072 USD, cash at hand 8,150.58 USD
On day #223 we are buying TORN - WETH that has 7 days vol of 685,107 USD. Buy in at price of 83.7844 USD, cash at hand 7,507.80 USD
On day #224 we are buying TCAP - WETH that has 7 days vol of 3,105,609 USD. Buy in at price of 303.6930 USD, cash at hand 102.18 USD
On day #225 we are buying OPIUM - WETH that has 7 days vol of 675,085 USD. Buy in at price of 3.8621 USD, cash at hand 7,079.82 USD
On day #226 we are buying LDO - WETH that has 7 days vol of 27,893,748 USD. Buy in at price of 3.1897 USD, cash at hand 604.26 USD
On day #227 we are buying PUNK - WETH that has 7 days vol of 568,638 USD. Buy in at price of 156940.5938 USD, cash at hand 7,861.65 USD
On day #228 we are buying PICKLE - WETH that has 7 days vol of 1,319,083 USD. Buy in at price of 12.2774 USD, cash at hand 1,265.77 USD
On day #229 we are buying CRETH2 - WETH that has 7 days vol of 697,136 USD. Buy in at price of 1880.8766 USD, cash at hand 6,624.06 USD
On day #230 we are buying UMA - WETH that has 7 days vol of 15,350,889 USD. Buy in at price of 14.1989 USD, cash at hand 813.12 USD
On day #231 we are buying BDI - WETH that has 7 days vol of 11,600,164 USD. Buy in at price of 383.6694 USD, cash at hand 8,168.01 USD
On day #232 we are buying BAO - WETH that has 7 days vol of 1,261,000 USD. Buy in at price of 0.0006 USD, cash at hand 659.23 USD
On day #233 we are buying DFX - WETH that has 7 days vol of 274,469 USD. Buy in at price of 1.3592 USD, cash at hand 8,556.94 USD
On day #234 we are buying <unknown> - WETH that has 7 days vol of 816,772 USD. Buy in at price of 0.1925 USD, cash at hand 1,057.22 USD
On day #235 we are buying OPIUM - WETH that has 7 days vol of 568,540 USD. Buy in at price of 3.0634 USD, cash at hand 7,695.65 USD
On day #236 we are buying OCC - USDC that has 7 days vol of 407,773 USD. Buy in at price of 13.3949 USD, cash at hand 1,145.95 USD
On day #237 we are buying AXS - WETH that has 7 days vol of 3,655,639 USD. Buy in at price of 5.0570 USD, cash at hand 8,158.90 USD
On day #238 we are buying UBXT - WETH that has 7 days vol of 724,579 USD. Buy in at price of 0.0421 USD, cash at hand 1,215.12 USD
Ending portfolio value: 8976.33 USD

Analysing the portfolio results

[14]:
print(f"Backtest range {start.date()} - {end.date()}")
print(f"Sharpe: {results[0].analyzers.sharperatio.get_analysis()['sharperatio']:.3f}")
print(f"Norm. Annual Return: {results[0].analyzers.returns.get_analysis()['rnorm100']:.2f}%")
print(f"Max Drawdown: {results[0].analyzers.drawdown.get_analysis()['max']['drawdown']:.2f}%")
Backtest range 2020-10-01 - 2021-06-01
Sharpe: 0.060
Norm. Annual Return: -10.55%
Max Drawdown: 63.21%

Plotting the portfolio value

Below is an example chart how the portfolio value moves over the time.

[15]:
from matplotlib.figure import Figure

figs = cerebro.plot(iplot=False)

fig: Figure = figs[0][0]

fig
[15]:
../../_images/programming_algorithms_entropy-monkey_18_0.png

That’s all this time. Onwards.