我有一些与买卖比特币有关的清单。 一个是(买入或卖出)价格,另一个是相关日期。 当我绘制在不同时间段与不同时间段内从购买/出售中赚取(或损失)的总资金时,结果是“震荡”-并非我所期望的。而且我认为我的逻辑可能是错误的
我的原始输入列表如下:
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....]
基于移动平均交叉(基于https://stackoverflow.com/a/14884058/2089889计算),我将以发生交叉的日期/价格买卖比特币。
我想弄清楚(这种方法到今天为止能给我带来多少收益)与(几天前我开始采用这种方法的时间)
但是
我遇到麻烦的是,生成的图确实很不连贯。首先,我认为这是因为我的买入多于卖出(反之亦然),所以我尝试考虑这一点。但这仍然很不稳定。 注意,以下代码在循环for days_ago in reversed(range(0,approach_started_days_ago)):
中被调用,因此每次执行以下代码时,它都应该吐出如果我 days_ago开始使用该方法会赚多少钱 em>(我称为 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
答案 0 :(得分:0)
我认为在“银行”中的金额就是我已售出的金额-我已购买的金额
但是,如果第一个跨界交易是一个卖出交易,我就不愿计算在内(我假设我做的第一个交易将是一个买入交易。
然后,如果我最后一笔交易是买入的(对我的银行不利),我会将今天的价格计入我的“银行”中
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