zipline icon indicating copy to clipboard operation
zipline copied to clipboard

How to buy and sell "fractional shares" for Bitcoin backtest?

Open basil-chatha-zz opened this issue 5 years ago • 2 comments

Hey everyone,

Intro: I'm trying to backtest a strategy using a minutely csv file of BTC data with the following fields: open, close, high, low, volume. Unfortunately, I'm having the following problem and don't see a way around it.

Background: Basically, I'm starting a backtest with $10,000. My strategy loses money for a little while, but then at some point it stops buying and selling completely because the price of BTC gets above however much total cash I have on hand so it can't buy at least one whole Bitcoin.

Question: Is there a good way to have Zipline buy and sell "fractional shares" so that this doesn't happen? I want it to buy as much bitcoin as I have cash on hand (maybe using a concept like $1 contracts?)

basil-chatha-zz avatar Jul 01 '20 18:07 basil-chatha-zz

Convert the price feed so you are modelling in satoshi?

phatcher avatar Aug 01 '20 11:08 phatcher

As @phatcher said, you can simulate your test on a smaller scale, e.g. on Satoshi level (1e8).

I can think of two methods:

  1. Increase your capital to the base of 1e8, and leave the input as is. This way you can analyse the result in Satoshi, but you need to correct for the final portfolio value and any other factors that are dependent on the capital base.
  2. Scale the input to Satoshi or any other level and change the handle_data method to either order on Satoshi level or based on your portfolio percentage using order_target_percent method.

NOTE: Zipline rounds the inputs to 3 decimal points. So re-scaling to Satoshi turns prices that are lower than 5000 to NaN (not considering rounding errors for higher prices). My suggestion is to either use 1e5 for Bitcoin or log-scale.

myradmehdizadeh avatar Jun 04 '21 04:06 myradmehdizadeh