Итак, код обучения и прогнозирования нейросетью рыночных котировок на 5 минут.
import sqlite3 as sql
from scipy.stats import logistic
import math
import numpy as np
import numpy.random as rnd
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPRegressor
sdata =[]
sql1= "select ticker, date, open, high, low, close, vol \
from Hist_1m where ticker_id=1 order by Date;"
con=sql.connect('C:/Users/ubase/Documents/StockDB/StockDB21.sqlite')
cur=con.cursor()
cur.execute(sql1)
sdata=cur.fetchall()
con.commit()
con.close()
Ldata = len(sdata)
N = 8000 # Количество сделок
ld = 5 #Продолжительность сделки
NNinterval = 20 # Количество входов NN
# Генерация случайных чисел
rng = rnd.default_rng()
rm=rng.integers(0, Ldata, N )
class Candle:
tr = 0
dt = 1
o = 2
h = 3
l = 4
c = 5
v = 6
cl = Candle
DataC =[sdata[i][cl.c] for i in range(0,Ldata)]
# sigmoid линейность до 0.5
def sigmoidnorm(x, alfa = 0.9, xmin = -1.3, xmax = 1.3):
return (xmax - xmin)*((1 / (1 + math.exp(-x*2.0*alfa))) - 1.0) + xmax
x = [0.002 * i - 3 for i in range(0,3000)]
y = [sigmoidnorm(x[i]) for i in range(len(x))]
plt.plot(x,y)
plt.grid()
plt.show()
# формируем сделки.
def DealsGenL(rm,ld):
#Lm = len(rm)
ix = []
x = []
pr = []
for i in range(0,N):
if rm[i] + ld < Ldata and rm[i] - NNinterval - 1 > 0:
delta = (sdata[rm[i]+ld][cl.c] - sdata[rm[i]][cl.c])/sdata[rm[i]+ld][cl.c]*100
x0 = [sigmoidnorm((sdata[rm[i] - j][cl.c] - sdata[rm[i]][cl.c])/sdata[rm[i]][cl.c]*100) \
for j in range(0, NNinterval)]
ix.append(rm[i])
x.append(x0)
pr.append(delta)
return ix, x, pr
Ix, X, Pr = DealsGenL(rm,ld)
Ib = 0
Ie = 100
plt.plot(X)
plt.legend()
plt.grid()
plt.show()
plt.plot(Pr, label = 'Prof')
plt.legend()
plt.grid()
plt.show()
regr = MLPRegressor(hidden_layer_sizes = [30,20,15,10,5], \
max_iter=500, activation = 'tanh')
regr.fit(X, Pr)
Out = regr.predict(X)
plt.plot(Pr, Out, '.')
plt.grid()
plt.show()
И вот результат прогнозирования:
(
Читать дальше )