-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cointegracao_daytrade_win_TESTE.py
61 lines (49 loc) · 1.45 KB
/
Cointegracao_daytrade_win_TESTE.py
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
import pandas as pd
import statsmodels.api as sm
import statistics
import matplotlib.pyplot as plt
from scipy.stats import zscore as zs
import statsmodels.tsa.stattools as ts
pathh = r'C:\Users\Joao\AppData\Roaming\MetaQuotes\Terminal\FB9A56D617EDDDFE29EE54EBEFFE96C1\MQL5\Files\daytrade_win\\'
nomeArq = 'Robo_win_acoes_PERIOD_M10_close_500Candles'
base = pd.read_csv(pathh + nomeArq + '.csv', sep=';')
atvA = 'CMIG4'
atvB = 'WINV20'
period = 350
_df, _adf = .1, '10%'
desvio = 2
yi, xi = 0, 0
for i in range(base.shape[1]-1):
yn = base.iloc[:,i].name
xn = base.iloc[:,i].name
if yn == atvA:
yi = i
if xn == atvB:
xi = i
period = period*-1
y = base.iloc[period:,yi].values
y = zs(y)
x = base.iloc[period:,xi].values
x = zs(x)
ynn = base.iloc[:,yi].name
xnn = base.iloc[:,xi].name
x = sm.add_constant(x)
model = sm.OLS(y, x).fit()
adf = ts.adfuller(model.resid, 1)
std = statistics.stdev(model.resid)
if model.resid[-1] > desvio*std or model.resid[-1] < desvio*std*-1:
print('Pode Operar!')
print(ynn, xnn)
if adf[1] < _df:
print('DF: Stationary')
if adf[0] < adf[4][_adf]:
print('ADF: Stationary')
print('df: {} / adf: {}' .format(_df, _adf))
plt.plot(model.resid)
plt.ylabel('Residual')
plt.axhline(0, color='black',label='mean')
plt.axhline(desvio*std, color='red', linestyle='--', linewidth=2)
plt.axhline(-desvio*std, color='green', linestyle='--', linewidth=2)
plt.xlabel('')
plt.margins(0.1)
plt.show()