Search code examples
pythonnumpynumerical-methods

Plotting (discrete sum over time period) vs. (time period) yields graph with discontinuities


I have some lists related to buying and selling bitcoin. One is the price (of a buy or sell) and the other is an associated date. When I plot the total money made (or lost) from my buying/selling over different lengths of time vs. those different lengths of time, the result is 'choppy' - not what I expected. And I think my logic might be wrong

My raw input lists look like:

dates=['2013-05-12 00:00:00', '2013-05-13 00:00:00', '2013-05-14 00:00:00', ....]

prices=[114.713, 117.18, 114.5, 114.156,...]

#simple moving average of prices calced over a short period
sma_short_list = [None, None, None, None, 115.2098, 116.8872, 118.2272, 119.42739999999999, 121.11219999999999, 122.59219999999998....]

#simple moving average of prices calced over a longer period
sma_long_list = [...None, None, None, None, 115.2098, 116.8872, 118.2272, 119.42739999999999, 121.11219999999999, 122.59219999999998....]

Based on the moving average cross-overs (which were calculated based on https://stackoverflow.com/a/14884058/2089889) I will either buy or sell the bitcoin at the date/price where crossover occurred.

I wanted to plot how (much money this approach would have made me as of today) vs. (days ago that I started this approach)

BUT

I am having trouble in that the resulting graph is really choppy. First I thought this was because I have one more buy than sell (or vis-versa) so I tried to account for that. But it was still choppy. NOTE the following code is called in a loop for days_ago in reversed(range(0,approach_started_days_ago)): so each time the following code executes it should spit out how much money that approach would have made had I started it days_ago (I call this bank), and the choppy plot is the days_ago vs. bank

dates = data_dict[file]['dates']
prices = data_dict[file]['prices']
sma_short_list = data_dict[file]['sma'][str(sma_short)]
sma_long_list = data_dict[file]['sma'][str(sma_long)]

prev_diff=0
bank = 0.0
buy_amt, sell_amt = 0.0,0.0
buys,sells, amt, first_tx_amt, last_tx_amt=0,0,0, 0, 0
start, finish = len(dates)-days_ago,len(dates)
for j in range(start, finish):
    diff = sma_short_list[j]-sma_long_list[j]
    amt=prices[j]

    #If a crossover of the moving averages occured
    if diff*prev_diff<0:
        if first_tx_amt==0:
            first_tx_amt = amt
        #BUY
        if diff>=0 and prev_diff<=0:
            buys+=1
            bank = bank - amt
            #buy_amt = buy_amt+amt
            #print('BUY ON %s (PRICE %s)'%(dates[j], prices[j]))
        #SELL
        elif diff<=0 and prev_diff>=0:
            sells+=1
            bank = bank + amt
            #sell_amt = sell_amt + amt
            #print('SELL ON %s (PRICE %s)'%(dates[j], prices[j]))
    prev_diff=diff

last_tx_amt=amt
#if buys > sells, subtract last
if buys > sells:
    bank = bank + amt
elif sells < buys:
    bank = bank - amt

#THIS IS RELATED TO SOME OTHER APPROACH I TRIED
#a = (buy_amt) / buys if buys else 0
#b = (sell_amt) / sells if sells else 0
#diff_of_sum_of_avg_tx_amts = a - b

start_date = datetime.now()-timedelta(days=days_ago)

return bank, start_date

Solution

  • I reasoned that my amount in the 'bank' would be the amount I have sold - the amount I have bought

    But, if the first crossover was a sell I don't want to count that (I am going to assume that the first tx I make will be a buy.

    Then if the last tx I make is a buy (negative to my bank), I will count today's price into my 'bank'

    if last_tx_type=='buy':
        sell_amt=sell_amt+prices[len(prices)-1] #add the current amount to the sell amount if the last purchase you made is a buy
    if sell_first==True:
        sell_amt = sell_amt - first_tx_amt #if the first thing you did was sell, you do not want to add this to money made b/c it was with apriori money
    
    bank = sell_amt-buy_amt