我有以下代码:
import pandas as pd
import numpy as np
results = pd.DataFrame()
BTC = pd.read_csv('BTC_USDT_Range100.csv', index_col=0)
BTC['Standard Returns'] = np.log(BTC['Close'] / BTC['Close'].shift())
for a in range(5, 250):
BTC['SMA1'] = BTC['Close'].rolling(a).mean()
BTC['SMA2'] = BTC['Close'].rolling(300).mean()
BTC['Position'] = np.where(BTC['SMA1'] > BTC['SMA2'], 1, 0)
BTC.dropna(inplace = True)
BTC.index = np.arange(0, len(BTC))
BTC['Strategy'] = BTC['Position'].shift(1)*BTC['Standard Returns']
print(a, BTC['Strategy'].sum())
但结果似乎并不如预期。例如,运行上面的代码,在 a = 171 之后所有的 BTC['Strategy'].sum() 值都为零,但是如果你在 for 循环中运行代码块只为值 a=172 它给出 0.71 .
随着循环的进行,这些值似乎在减少,直到它们“基本上为零”,所以我觉得我在这个 DataFrame 上迭代是错误的,它没有“重置”for中的 DataFrame 的列正确循环。
编辑:作为示例,我在这里提供 BTC.head(50):
Timestamp Open High Low Close Volume
0 1577836800000 7195.24 7196.25 7178.64 7179.78 95.509133
1 1577837100000 7179.76 7191.77 7178.20 7191.07 59.365225
2 1577837400000 7193.15 7193.53 7180.24 7180.97 48.068510
3 1577837700000 7180.97 7186.40 7177.35 7178.29 32.192929
4 1577838000000 7177.71 7182.46 7175.47 7176.96 49.027397
5 1577838300000 7177.59 7185.56 7176.11 7178.45 47.022328
6 1577838600000 7178.19 7185.44 7177.54 7180.68 35.109830
7 1577838900000 7180.96 7182.53 7176.23 7177.53 24.863496
8 1577839200000 7177.14 7182.45 7176.34 7179.56 23.514132
9 1577839500000 7179.35 7182.99 7179.35 7182.94 21.358850
10 1577839800000 7182.94 7183.98 7175.51 7179.03 42.910971
11 1577840100000 7178.65 7181.75 7175.46 7177.02 32.872100
12 1577840400000 7176.47 7185.86 7175.71 7183.29 30.163076
13 1577840700000 7183.55 7194.04 7182.82 7189.62 44.803327
14 1577841000000 7189.63 7194.04 7188.62 7190.86 28.554119
15 1577841300000 7190.46 7194.27 7189.23 7194.06 24.216078
16 1577841600000 7193.02 7198.00 7190.79 7192.39 34.107575
17 1577841900000 7193.01 7217.00 7191.98 7212.10 193.219399
18 1577842200000 7212.10 7230.00 7211.32 7218.83 273.468070
19 1577842500000 7219.11 7221.95 7210.53 7216.00 60.977766
20 1577842800000 7216.02 7219.11 7207.17 7211.87 52.801416
21 1577843100000 7212.34 7212.67 7205.01 7205.08 47.987055
22 1577843400000 7205.08 7211.77 7204.28 7209.35 54.491792
23 1577843700000 7209.75 7216.41 7207.39 7216.27 38.262930
24 1577844000000 7215.52 7223.37 7214.09 7217.25 62.727497
25 1577844300000 7217.21 7223.80 7214.27 7217.18 40.379270
26 1577844600000 7217.73 7219.03 7211.41 7211.97 30.653886
27 1577844900000 7212.87 7225.00 7212.15 7224.20 32.415222
28 1577845200000 7224.20 7230.00 7218.97 7229.32 36.710927
29 1577845500000 7229.50 7238.88 7224.91 7228.09 106.131355
30 1577845800000 7227.12 7236.07 7226.79 7234.76 56.688336
31 1577846100000 7233.83 7235.83 7228.08 7232.21 73.997180
32 1577846400000 7232.75 7237.99 7229.65 7237.63 42.633819
33 1577846700000 7237.42 7237.81 7231.93 7236.13 38.429914
34 1577847000000 7237.21 7244.20 7232.20 7243.65 89.949170
35 1577847300000 7243.07 7244.87 7238.00 7242.85 44.440233
36 1577847600000 7242.66 7245.00 7230.25 7235.33 87.259123
37 1577847900000 7235.18 7235.66 7220.10 7224.98 108.385003
38 1577848200000 7224.98 7229.70 7223.43 7228.50 75.378899
39 1577848500000 7227.43 7235.50 7227.19 7235.50 76.264249
40 1577848800000 7234.11 7237.41 7232.87 7235.87 33.335067
41 1577849100000 7235.53 7238.77 7231.23 7237.15 59.789927
42 1577849400000 7237.82 7244.33 7237.16 7243.77 60.043500
43 1577849700000 7242.40 7242.48 7226.00 7228.85 81.881714
44 1577850000000 7228.75 7231.99 7220.23 7221.27 61.273717
45 1577850300000 7221.27 7227.89 7220.00 7227.50 38.460440
46 1577850600000 7228.18 7228.18 7223.21 7226.06 45.602554
47 1577850900000 7225.58 7230.78 7223.61 7225.01 56.050674
48 1577851200000 7225.00 7229.72 7223.93 7228.58 64.682238
49 1577851500000 7228.09 7229.88 7224.61 7228.14 36.798454
并且可以更改代码以产生问题:
BTC['Standard Returns'] = np.log(BTC['Close'] / BTC['Close'].shift())
for a in range(5, 15):
BTC['SMA1'] = BTC['Close'].rolling(a).mean()
BTC['SMA2'] = BTC['Close'].rolling(20).mean()
BTC['Position'] = np.where(BTC['SMA1'] > BTC['SMA2'], 1, 0)
BTC.dropna(inplace = True)
BTC.index = np.arange(0, len(BTC))
BTC['Strategy'] = BTC['Position'].shift(1)*BTC['Standard Returns']
print(a, BTC['Strategy'].sum())
然后在 a=7 后返回全零。