目录

(MCM/ICM)比特币和黄金组合投资策略的策略代码部分

目录

前言

美赛论文对应策略代码部分,吐槽一句美赛居然不收代码也是离谱。

论文配套的代码均为本人编写,运行无碍。

感谢组员组长的配合论文撰写。

正文

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import numpy as np
import pandas as pd
import re,math
import matplotlib.pyplot as plt


np.set_printoptions(suppress=True)
B = pd.read_csv(r'B.csv') # B
H = pd.read_csv(r'H.csv') # H
Times = pd.read_csv(r'022.csv') # Time
B = B.set_index("Unnamed: 0")
H = H.set_index("Unnamed: 0")
Times = Times.set_index("Unnamed: 0")
# 先向前取值填充,再先后取值填充
BH = pd.merge(H.iloc[:,0:2], B.iloc[:,0:2], how='outer',on='0').sort_values('0',ascending=True)
BH = pd.merge(BH, H.iloc[:,0:3:2], how="left", on=["0"])
BH = pd.merge(BH, B.iloc[:,0:3:2], how="left", on=["0"]).fillna(0)
1
H

0 1 2
Unnamed: 0
0 76 -1 1145.90
1 154 1 1281.85
2 171 -1 1257.40
3 233 1 1282.30
4 247 -1 1333.10
5 308 1 1291.85
6 324 -1 1264.55
7 532 1 1223.00
8 551 -1 1203.25
9 608 1 1312.40
10 628 -1 1285.85
11 685 1 1280.95
12 703 -1 1431.40
13 760 1 1503.10
14 781 -1 1490.60
15 839 1 1567.85
16 856 -1 1578.25
17 912 1 1682.05
18 931 -1 1737.95
19 988 1 2031.15
20 1008 -1 1928.45
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
m = 10000
h = 0
b = 0
p = 0
q = 0
control_list = []
for i in range(len(Times)):
    x1 = 0
    x2 = 0
    j = 0
    t = 0
    if i <= 9:
        control_list.append([i,0,m,h,j,b,t,x1,x2])
        continue
    else:
        if i not in list(np.array(BH['0'])):
            control_list.append([i,0,m,h,j,b,t,x1,x2])
            continue
    if np.array(BH[BH['0'].isin([str(i)])]['1_x'])[0] != 0: # H
#         if np.array(BH[BH['0'].isin([str(i)])]['2_y'])[0] == 0: # B
        if np.array(BH[BH['0'].isin([str(i)])]['1_x'])[0] < 0: # 买
            j = np.array(H[H['0'].isin([i])]['2'])[0]
            print(j)
            x1 = np.array(Times.iloc[[i]]['0'])[0]
            x2 = np.array(Times.iloc[[i]]['1'])[0]
            p = m*x1#(x1/(x1+x2))
            q = m*x2#(x2/(x1+x2))
            h = (p-0.01*p)/j
            p = 0
            m = p + q
            control_list.append([i,11,m,h,j,b,0,x1,x2])
#             print(m)
        if np.array(BH[BH['0'].isin([str(i)])]['1_x'])[0] > 0: # 卖
            j = np.array(H[H['0'].isin([i])]['2'])[0]
            x1 = np.array(Times.iloc[[i]]['0'])[0]
            x2 = np.array(Times.iloc[[i]]['1'])[0]
            p = m*x1
            q = m*x2
            m = h*j-h*j*0.01+p+q
            h = 0
            control_list.append([i,-11,m,h,j,b,0,x1,x2])
#             print(m)
            
#             if np.array(BH[BH['0'].isin([str(i)])]['2_x'])[0] == 0: # H
    if np.array(BH[BH['0'].isin([str(i)])]['1_y'])[0] != 0: # B
        if np.array(BH[BH['0'].isin([str(i)])]['1_y'])[0] < 0: # 买
            t = np.array(B[B['0'].isin([i])]['2'])[0]
            x1 = np.array(Times.iloc[[i]]['0'])[0]
            x2 = np.array(Times.iloc[[i]]['1'])[0]
            p = m*x1#(x1/(x1+x2))
            q = m*x2#(x2/(x1+x2))
            b = (q-0.02*q)/t
            q = 0
            m = p + q
            control_list.append([i,22,m,h,0,b,t,x1,x2])
#             print(m)
        elif np.array(BH[BH['0'].isin([str(i)])]['1_y'])[0] > 0: # 卖
            t = np.array(B[B['0'].isin([i])]['2'])[0]
            x1 = np.array(Times.iloc[[i]]['0'])[0]
            x2 = np.array(Times.iloc[[i]]['1'])[0]
            p = m*x1
            q = m*x2
            m = b*t-b*t*0.02+p+q
            b = 0
            control_list.append([i,-22,m,h,0,b,t,x1,x2])
#             print(m)

m
1145.9
1257.4
1333.1
1264.55
1203.25
1285.85
1431.4
1490.6
1578.25
1737.95
1928.45





2.935705996261112e-06
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
m = 10000
h = 0
b = 0
p = 0
q = 0
control_list = []
for i in range(len(Times)):
    x1 = 0
    x2 = 0
    j = 0
    t = 0
    if i <= 9:
        control_list.append([i,0,m,h,j,b,t,x1,x2])
        continue
    else:
        if i not in list(np.array(BH['0'])):
            control_list.append([i,0,m,h,j,b,t,x1,x2])
            continue
    if np.array(BH[BH['0'].isin([str(i)])]['1_x'])[0] != 0: # H
#         if np.array(BH[BH['0'].isin([str(i)])]['2_y'])[0] == 0: # B
        if np.array(BH[BH['0'].isin([str(i)])]['1_x'])[0] < 0: # 买
            j = np.array(H[H['0'].isin([i])]['2'])[0]
            x1 = np.array(Times.iloc[[i]]['0'])[0]
            x2 = np.array(Times.iloc[[i]]['1'])[0]
            p = m*x1#(x1/(x1+x2))
            q = m*x2#(x2/(x1+x2))
            h = (p-0.01*p)/j
            p = 0
            m = p + q
            control_list.append([i,11,m,h,j,b,t,x1,x2])
#             print(m)
        if np.array(BH[BH['0'].isin([str(i)])]['1_x'])[0] > 0: # 卖
            j = np.array(H[H['0'].isin([i])]['2'])[0]
            x1 = np.array(Times.iloc[[i]]['0'])[0]
            x2 = np.array(Times.iloc[[i]]['1'])[0]
            p = m*x1
            q = m*x2
            m = h*j-h*j*0.01+p+q
            h = 0
            control_list.append([i,-11,m,h,j,b,t,x1,x2])
#             print(m)
            
#             if np.array(BH[BH['0'].isin([str(i)])]['2_x'])[0] == 0: # H
    if np.array(BH[BH['0'].isin([str(i)])]['1_y'])[0] != 0: # B
        if np.array(BH[BH['0'].isin([str(i)])]['1_y'])[0] < 0: # 买
            t = np.array(B[B['0'].isin([i])]['2'])[0]
            x1 = np.array(Times.iloc[[i]]['0'])[0]
            x2 = np.array(Times.iloc[[i]]['1'])[0]
            p = m*x1#(x1/(x1+x2))
            q = m*x2#(x2/(x1+x2))
            b = (q-0.02*q)/t
            q = 0
            m = p + q
            control_list.append([i,22,m,h,j,b,t,x1,x2])
#             print(m)
        elif np.array(BH[BH['0'].isin([str(i)])]['1_y'])[0] > 0: # 卖
            t = np.array(B[B['0'].isin([i])]['2'])[0]
            x1 = np.array(Times.iloc[[i]]['0'])[0]
            x2 = np.array(Times.iloc[[i]]['1'])[0]
            p = m*x1
            q = m*x2
            m = b*t-b*t*0.02+p+q
            b = 0
            control_list.append([i,-22,m,h,j,b,t,x1,x2])
#             print(m)

m
1
2
data_data = pd.DataFrame(control_list,columns=["day","process","USD","H","Hprice","B","Bprice","Hx1","Bx2"])#.to_csv("Process.csv")
data_data

day process USD H Hprice B Bprice Hx1 Bx2
0 0 0 10000.000000 0.000000 0.0 0.0 0.0 0.0 0.0
1 1 0 10000.000000 0.000000 0.0 0.0 0.0 0.0 0.0
2 2 0 10000.000000 0.000000 0.0 0.0 0.0 0.0 0.0
3 3 0 10000.000000 0.000000 0.0 0.0 0.0 0.0 0.0
4 4 0 10000.000000 0.000000 0.0 0.0 0.0 0.0 0.0
... ... ... ... ... ... ... ... ... ...
1820 1819 0 0.000003 5.729175 0.0 0.0 0.0 0.0 0.0
1821 1820 0 0.000003 5.729175 0.0 0.0 0.0 0.0 0.0
1822 1821 0 0.000003 5.729175 0.0 0.0 0.0 0.0 0.0
1823 1822 0 0.000003 5.729175 0.0 0.0 0.0 0.0 0.0
1824 1823 0 0.000003 5.729175 0.0 0.0 0.0 0.0 0.0

1825 rows × 9 columns

1
2
3
4
5
6
7
ttp = []
price = pd.read_csv(r'price.csv', index_col=["Unnamed: 0"])
ttp.append(np.array(data_data["USD"])+np.array(data_data["H"])*np.array(price["USD (PM)"])[1:]+np.array(data_data["H"])*np.array(price["USD (PM)"])[1:])
ttp.append(np.array(data_data["H"])*np.array(price["USD (PM)"])[1:])
ttp.append(np.array(data_data["B"])*np.array(price["Value"])[1:])
# ttp.append(price["Value"])
# ttp.append(price["USD (PM)"])
1
pd.DataFrame(ttp).T

0 1 2
0 10000.000000 0.000000 0.0
1 10000.000000 0.000000 0.0
2 10000.000000 0.000000 0.0
3 10000.000000 0.000000 0.0
4 10000.000000 0.000000 0.0
... ... ... ...
1820 20872.528860 10436.264429 0.0
1821 20649.663969 10324.831983 0.0
1822 20464.611630 10232.305813 0.0
1823 20490.392915 10245.196456 0.0
1824 20563.153432 10281.576715 0.0

1825 rows × 3 columns