绘制(一段时间内的离散总和)与(时间段)产生不连续的图表

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

我有一些与买卖比特币相关的清单。 一个是价格(买入或卖出),另一个是相关日期。 当我绘制我的 buying/selling 在不同时间长度与那些不同时间长度内赚取(或损失)的总资金时,结果是 'choppy' - 这不是我的预期。而且我认为我的逻辑可能是错误的

我的原始输入列表如下所示:

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....]

根据移动平均线交叉(根据 计算)我将在发生交叉的 date/price 处买卖比特币。

我想绘制(截至今天,这种方法本可以让我赚多少钱)与(几天前我开始使用这种方法)相比如何

但是

我遇到了麻烦,因为生成的图表真的很不稳定。首先,我认为这是因为我的买入多于卖出(反之亦然),所以我试图解释这一点。但它仍然波涛汹涌。 注意 以下代码在循环中调用 for days_ago in reversed(range(0,approach_started_days_ago)): 因此每次执行以下代码时,它应该吐出如果我开始使用该方法会赚多少钱 days_ago(我称之为bank),波涛汹涌的情节是days_ago对比 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

我认为我在 'bank' 中的金额是我卖出的金额 - 我买入的金额

但是,如果第一个交叉是卖出,我不想算(我假设我做的第一个 tx 是买入。

然后如果我做的最后一笔交易是买入(对我的银行不利),我会将今天的价格计入我的 '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