Skip to content
Snippets Groups Projects
Commit d2415902 authored by Recolic K's avatar Recolic K
Browse files

fix issues

parent ae8d4a86
No related branches found
No related tags found
No related merge requests found
......@@ -2,14 +2,7 @@ import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import pickle
# [(Snapshot_Time, [(buy_price1, amount1), ...] , [(sell_price1, amount1), ...]), ...]
# [(1620457034392, [(56000, 0.01), (55900, 1), (55700, 30), ...] , [(57000, 0.01), (57100, 1), ...] ), (1620457034394, [...]), ...]
# The snapshots should has almost identical time-interval. Good for LSTM.
# Time axis: [history, older, newer, ..., latest]
realtime_shortterm_dataset_depth = []
realtime_shortterm_dataset_depth_size = 1024
import pickle, numpy, math
# [(Trade_Time, PRICE, AMOUNT), ...]
# [(1620457034392, 56000, 0.5), (1620457034394, 56001, 0.05), ...]
......@@ -18,6 +11,13 @@ realtime_shortterm_dataset_depth_size = 1024
realtime_shortterm_dataset_aggtrade = []
realtime_shortterm_dataset_aggtrade_size = 1024*1024
# [(Snapshot_Time, [(buy_price1, amount1), ...] , [(sell_price1, amount1), ...]), ...]
# [(1620457034392, [(56000, 0.01), (55900, 1), (55700, 30), ...] , [(57000, 0.01), (57100, 1), ...] ), (1620457034394, [...]), ...]
# The snapshots should has almost identical time-interval. Good for LSTM.
# Time axis: [history, older, newer, ..., latest]
realtime_shortterm_dataset_depth = []
realtime_shortterm_dataset_depth_size = 1024
# The trading thread would not start working, before finish analysing longterm dataset.
# Time-interval for longterm dataset is 1 minute.
longterm_dataset = []
......@@ -35,7 +35,7 @@ def load_realtime_dataset_on_start():
class LSTM_Shortterm_Predictor(nn.Module):
def __init__(self, input_dim):
super(LSTM_Shortterm_Predictor, self).__init__()
self.lstm_idim = 16
self.lstm_idim = 10 # this is the length of depth_to_impulsive_score_vector()
self.lstm_odim = 128
# The input would be a tuple containing complex information.
......@@ -51,7 +51,7 @@ class LSTM_Shortterm_Predictor(nn.Module):
self.out = nn.Linear(self.lstm_odim, 1)
def forward(self, sample_seq):
input_seq = sample_seq.view(len(sample_seq), 1, lstm_idim)
input_seq = sample_seq.view(len(sample_seq), 1, self.lstm_idim)
lstm_in = self.serializer(input_seq)
lstm_out, _ = self.lstm(lstm_in)
predict_shortterm_trend = self.out(torch.tanh(lstm_out[-1:]))
......@@ -82,18 +82,33 @@ def learn_once(depth_seq, trend_answer_score):
i = torch.tensor(input_seq, dtype=torch.float)
o = model(i)
loss = torch.square(o - trend_answer_score)
loss = abs(o - trend_answer_score)
loss.backward(retain_graph=True)
optimizer.step()
return loss.to_list()[0]
return float(loss)
load_realtime_dataset_on_start()
for i in range(300):
print("DEBUG: l=", realtime_shortterm_dataset_depth[i+256])
answer = realtime_shortterm_dataset_depth[i+256][1][0][0] - realtime_shortterm_dataset_depth[i][1][0][0]
losses = []
for i in range(2000):
answer = numpy.average([realtime_shortterm_dataset_depth[i+di][1][0][0] for di in (256,512,768,1024,2000,5000)]) - realtime_shortterm_dataset_depth[i][1][0][0]
print("answer=", answer)
loss = learn_once(realtime_shortterm_dataset_depth[i], answer)
loss = learn_once(realtime_shortterm_dataset_depth[i:i+128], answer)
print("Loss=", loss)
losses += [loss]
#####################
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
x = numpy.arange(len(losses))
colors = cm.rainbow(np.linspace(0, 1, 10))
for index, y in enumerate(losses):
plt.scatter(index, y, color=colors[1])
#for x, y in guess_xy:
# plt.scatter(x, y, color=colors[6])
plt.show()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment