Capswan Natural Language Backtesting

1. Introduction

a. Trading System

  • Every successful trader needs a trading system – a decision making process which will yield long term positive expected value and profitable aggregate outcome in the market.
  • A trading system usually consists of a set of clearly defined, data driven rules based on the price action and indicators, which the trader follows - when to buy, when to sell, and when to exit.
  • There are also secondary considerations to the system, such as which assets to trade, what timeframe to focus on, money management, and risk reward ratio.
  • Capswan aims to guide traders through the process of developing their own profitable trading system personalized to match their trading style.

b. Backtesting

  • Trading involves a significant degree of uncertainty. Strategy that looks like a winner today may not work tomorrow or over a longer time period.
  • Before the trader invests a significant amount of capital or starts trading full time, they need to first ensure they have an edge.
  • It is important to test strategies against real world data to determine whether their rules provide positive expected value and to further develop and adjust them.
  • If this was done manually, it would require a significant amount of time and effort.
  • Capswan aims to provide traders with an easy way of testing their trading systems against historical data and reviewing the results.

c. Natural Language

  • While some traders are capable of developing strategies directly in a programming language of their choice, most do not come from a software development background.
  • Capswan aims to give traders the option to define and fine-tune their strategies in the form of human readable sentences, as if they were describing them to a fellow trader.

2. Signals

a. Signal Types

Each Capswan strategy may include up to 3 trading signals:

  • Buy Signal "Buy when 50 RSI below 20"
  • Sell Signal "Sell when 50 RSI above 80"
  • Exit Signal "Exit when 50 RSI between 40 and 60"
  • We could combine these three into one direction agnostic mean reversion strategy: "Buy when 50 RSI below 20, sell when 50 RSI above 80, exit when 50 RSI between 40 and 60."
  • If we had a bullish bias, our strategy could be just: "Buy when 50 RSI below 20, exit when 50 RSI above 40."
  • We could also omit the exit signal altogether and rely on default Stop Loss and Take Profit orders to close our trades for us (more on Stop Loss and Take Profit configuration in section 3g). "Buy when 50 RSI below 20."

b. Indicators

  • Indicators are the basic building blocks of a strategy. The goal is to provide the same functionality and indicator behavior as leading trading platforms on the market to match the users' expectations.
  • Indicators typically require the user to specify period before indicator name ("20 SMA"), some may also require them to specify value after indicator name ("20 Fibo 62%"). For some indicators such as MACD the period is implied and not required.

We currently support the following indicators:

  • Simple Moving Average / SMA / MA "Buy when [price] above 20 SMA"
  • Exponential Moving Average / EMA "Buy when [price] above 20 EMA"
  • Relative Strength Index / RSI "Buy when 20 RSI above 50"
  • Average True Range / ATR "Buy when 20 ATR above 50"
  • MACD "Buy when MACD above 50"
  • Velocity / delta / change (first derivative) "Buy when [price] velocity above 1%" "Buy when 20 RSI velocity above 1%"
  • Acceleration / roc / rate of change (second derivative) "Buy when [price] acceleration above 1%" "Buy when 20 RSI acceleration above 1%"
  • Percentile "Buy when [price] is in the 90th percentile" "Buy when 20 MA is in the 90th percentile"
  • Momentum "Buy when 20 Momentum above 50"
  • Stochastic Oscillator / stoch "Buy when 20 Stochastic Oscillator above 50"
  • Standard Deviation / stdev / sdev "Buy when 20 Standard Deviation above 50"
  • Directional Movement Index / DMI "Buy when 20 DMI above 50"
  • Average Directional Index / ADX "Buy when 20 ADX above 50"
  • Bollinger Bands / BB / bands "Buy when [price] above 20 BB"
  • Fibonacci Retracement / Fibo "Buy when [price] below 50 Fibonacci 62%"

c. Relations

  • Relations specify behavior of one indicator, interaction between one indicator and price, or interaction between two indicators.

We currently support the following relations:

  • Above "Buy when [price] above 50 MA" "Buy when 20 MA above 50 MA"
  • Below "Buy when [price] below 50 MA" "Buy when 20 MA below 50 MA"
  • Cross Above "Buy when [price] cross above 50 MA" "Buy when 20 MA cross above 50 MA"
  • Cross Below "Buy when [price] cross below 50 MA" "Buy when 20 MA cross below 50 MA"
  • Trend Up "Buy when [price] is trending up" "Buy when 20 RSI is trending up" Trend Down "Buy when [price] is trending down" "Buy when 20 RSI is trending down"
  • Bounce "Buy when [price] bounces off 20 MA" "Buy when 20 MA bounces off 50 MA"
  • Reverse Up "Buy when [price] reverses up" "Buy when 20 MA reverses up"
  • Reverse Down "Buy when [price] reverses down" "Buy when 20 MA reverses down"
  • Between "Buy when 20 RSI between 40 and 60"

d. Statement Chaining

  • It is possible to chain multiple conditions in one signal using a comma (,) or "and".

  • All of the conditions have to be fulfilled for the signal to trigger.

  • This would be a valid buy signal: "Buy when 50 RSI above 60, MA 50 above MA 100, and ATR 20 below 80th percentile"

3. Backtest Configuration

a. Order Type

  • There are 3 order types (the default is Market Order):
    • Market Order immediately opens a new trade at the current candle Open price.
    • Limit Order creates a pending trade at the current candle Open price minus the gap for buy orders or plus the gap for sell orders.
    • Stop Order creates a pending trade at the current candle Open price plus the gap for buy orders or minus the gap for sell orders.
    • Stop Order and Limit Order gap values can be either relative in % or absolute in $. Spread (see f. Spread) is charged for Market Orders and Stop Orders.

Below are sample configurations.

  • Market Order "Buy when 50 MA above 100 MA"
  • Limit Order "Buy limit 1% when 50 MA above 100 MA" "Buy limit $100 when 50 MA above 100 MA"
  • Stop Order "Buy stop 1% when 50 MA above 100 MA" "Buy stop $100 when 50 MA above 100 MA"

b. Asset

  • The default asset is Bitcoin.
  • We currently offer USD pairs for over 300 cryptocurrencies, as well as a selection of NFT, Forex, Commodity, Index, ETF, and Stock Assets with typical data depth of 3 years for one minute timeframe.
  • Asset for strategy backtest can be specified such as: "Buy ETH when 50 MA above 100 MA"
  • Up to date supported asset list can be found at: Currently Supported Assets.

c. Timeframe

  • The default Timeframe is one minute. We currently support one minute, one hour, and one day Timeframes.
  • Timeframe for strategy backtest can be specified such as: "Buy when 50 MA above 100 MA on hourly"

d. Initial Balance

  • Default account balance is $10,000.
  • Initial Balance can be specified such as: "Buy when 50 MA above 100 MA balance $100k"

e. Trade Size

  • Default Trade size is 100% of the current account balance.
  • Trade size can be either relative in % or absolute in $.
  • Trade size can be larger than the account balance, this implies the account is using leverage.
  • Trade size can be specified such as: "Buy when 50 MA above 100 MA size $100" or "Buy when 50 MA above 100 MA size 500%"

f. Spread

  • Default Spread is 0.1%.
  • Spread is the cost of opening a trade.
  • We do not have historical data for both the bid and ask sides of the book so we can not provide real value of the spread at the time the trade was opened.
  • The user can define spread as they wish, and they should estimate and include other expenses incurred at trade open such as commission, slippage, or currency conversion fee.
  • Spread can be specified such as: "Buy when 50 MA above 100 MA spread 0.01%"

g. Risk and Reward

  • Default Take Profit is 2% and default Stop Loss is 1%. They can be defined either as absolute $ values or relative % values.
  • Stop Loss and Take Profit can be specified such as: "Buy when 50 MA above 100 MA take profit 3% stop loss 2%"

h. Optional Params

  • Additional backtest configuration parameters can be specified:
    • Max Trades (default 1) is the maximum count of trades opened at once. If the signal fires again while the first trade is still open, another trade will be opened up to the Max Trades number. "Buy when 50 MA above 100 MA max trades 3"
    • Cooldown (default 0 = not active) is the waiting period in candles between opening new trades. If the signal fires multiple times in a short succession, it will be ignored and won't open another trade until the cooldown period runs out. "Buy when 50 MA above 100 MA max trades 3 cooldown 30"
    • Timeout (default 0 = not active) is the period in candles after which each trade is automatically closed. Timeout can be used to ensure short term scalp signals do not leave long running trades. "Buy when 50 MA above 100 MA timeout 120"
    • All backtest options can be combined, this would be a valid strategy prompt: "Buy BTC on the daily when 50 MA above 100 MA with $100k balance, $10k size, spread 0.05% max trades 3 cooldown 3 take profit 3%."

4. Interface

a. Strategy Input

Strategy can be inputted either through the Capswan web app or using the Capswan#7499 Discord bot and @CapswanApp Twitter bot with the "cs backtest" command. strategyinput.png strategyinput2.png

b. Strategy List

  • Previously created strategies can be viewed on the strategy list screen. strategylist.png

c. Strategy Detail

  • Detail of each strategy, including the currently applied backtest configuration can be reviewed on the strategy detail screen.
  • Backtests are executed in batches of 6000 candles going backward in time and user can request additional batches one by one.
  • Analytical data such as Correlation, Beta, Sharpe, and Sortino ratios as well as aggregate PnL for all backtest batches are provided to evaluate strategy performance with the currently selected parameters.


d. Backtest Configuration

  • Backtest Configuration for each strategy can be fine tuned by clicking the Edit button.


e. Backtest List

  • Previously used backtest configurations can be browsed and re-applied.
  • This will display all the backtest data batches already executed with that configuration without the need to re-run them.
  • Configurations are shared between all Strategies.


f. Backtest Detail

  • Detail of each Backtest data batch can be viewed separately.
  • It includes configuration that was used for this batch, statistics, charts of PnL, price, and any indicators that were used, as well as a detailed list of executed trades. backtestdetail1.png backtestdetail2.png backtestdetail3.png

g. Bot Response

  • In response to the "cs backtest" command, the bot provides a summary response with a link to the strategy result detail in the Capswan web app.
  • On Discord, this response can be shared with the channel.


5. Future Development

The following features are currently either under active development or on the road map:

  • Cross assets (ETHBTC, SPXBTC, …)
  • Strategy Optimization – "Buy BTC when X MA above Y MA"
  • Multi asset strategies
  • Multi strategy backtests
  • Running strategies live to make Capswan Predictions
  • Running strategies live to call external webhooks
  • Unlimited backtest batch size for paid tier users (6000 for free tier)
  • Unlimited data depth for paid tier users (1 year max for free tier)