# Ape in (buy the latest token)#

This is an example automated trading strategy how to buy in to all tthe latest tokens.

This can act as a real strategy, but not recommended.

• This strategy is based on QSTrader backtesting engine integration.

• The trading universe is all DEX trading pairs. To speed up the simulation in this notebook, we limit the trading strategy to a tokens that meet the certain criteria. Note that you should not do this for the real backtesting, as this induces survivorship bias.

• The trading strategy is a multi-asset strategy that rebalances the portfolio daily.

• For each day, the strategy checks newly available tokens that have come to the markets.

• When the tokens cross the liquidity threshold (have enough liquidity) the strategy buys those tokens, by selling the tokens from the previous day and equally balancing the generated cash in hand across all the tokens of the day.

• The strategy sells the tokens on the following day - the sell signal is 1 day hold.

• There is a high cash buffer, as the strategy is deemed to make a lot of unsuccessful picks.

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

• The backtest simulation takes some minutes to run. We display the progress using an interactive tqdm progress bar.

## Creating a dataset client#

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

[1]:

try:
except ImportError:
import site
site.main()

client = Client.create_jupyter_client()

Started Capitalgram in Jupyter notebook environment, configuration is stored in /Users/mikkoohtamaa/.capitalgram


## Strategy and backtesting parameters#

Here we define all parameters that affect the backtest outcome.

[2]:

import pandas as pd

# The starting date of the backtest
# Note: At the moment, due to QsTrader internal limitation,
# we define this as NYSE UTC trading hours
start = pd.Timestamp('2020-10-01 14:30:00')

# The ending date of the backtest
end = pd.Timestamp('2021-01-08 23:59:00')

### Tearsheet chart#

Tearsheet displays the portfolio profit and risk over the time.

[10]:

from qstrader.statistics.tearsheet import TearsheetStatistics

tearsheet = TearsheetStatistics(
strategy_equity=strategy_backtest.get_equity_curve(),
title=f'Ape in the latest'
)

tearsheet.plot_results()


Show the distribution of won and lost trades as a histogram.

[11]:

from matplotlib.figure import Figure

expanded_timeline, _ = expand_timeline(exchange_universe, pair_universe, timeline)



The timeline displays individual trades the strategy made. This is good for figuring out some really stupid trades the algorithm might have made.

[ ]:

from tradingstrategy.analysis.tradeanalyzer import expand_timeline

# Generate raw timeline of position open and close events

# Expand timeline with human-readable exchange and pair symbols
expanded_timeline, apply_styles = expand_timeline(exchange_universe, pair_universe, timeline)

# Do not truncate the row output
with pd.option_context("display.max_row", None):
display(apply_styles(expanded_timeline))


## Conclusion#

An ape in strategy with a parameters chosen with Stetson-Harrison method is not profitable.

However, this is an interactive notebook. Click Launch binder button at the top, edit this strategy live and try to come up with better parameters.