我是python的新手,尝试将一个项目从MT4中收集到数据,通过tensorflow计算预测并发送回结果。如果您希望我可以附加套接字服务器文件的链接以运行metatrader端。从MT4发送数据就可以了,信息就在那里。 张量流的基本计算是可以的。 问题是,我不知道如何发送回数据并关闭套接字。 谢谢。
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import socket
import ast
import json
mpl.rcParams['figure.figsize'] = (8, 6)
mpl.rcParams['axes.grid'] = False
TRAIN_SPLIT = 500
tf.random.set_seed(13)
def univariate_data(dataset, start_index, end_index, history_size, target_size):
data = []
labels = []
start_index = start_index + history_size
if end_index is None:
end_index = len(dataset) - target_size
for i in range(start_index, end_index):
indices = range(i-history_size, i)
# Reshape data from (history_size,) to (history_size, 1)
data.append(np.reshape(dataset[indices], (history_size, 1)))
labels.append(dataset[i+target_size])
return np.array(data), np.array(labels)
def train_test_model(msg=''):
msg = msg.replace('true', 'True')
msg = ast.literal_eval(msg)
print(type(msg))
if (type(msg) == dict):
input_data = msg
else:
return "BAD JSON!!"
uni_data = pd.DataFrame(input_data['Data'])# make pandas dataframe
uni_data.index = input_data['Time']
uni_data = uni_data.astype('float64')# convert string to float
uni_data = uni_data.values
uni_train_mean = uni_data[:TRAIN_SPLIT].mean()
uni_train_std = uni_data[:TRAIN_SPLIT].std()
uni_data = (uni_data - uni_train_mean) / uni_train_std
univariate_past_history = 20 #int(input_data['TestingPart'] / 100 * len(data)) #
univariate_future_target = 0 #len(data) - testSize
x_train_uni, y_train_uni = univariate_data(uni_data, 0, TRAIN_SPLIT,
univariate_past_history,
univariate_future_target)
responseJSON = {}
responseJSON['Pred'] = str(y_train_uni[0])
return json.dumps(responseJSON)
class socketserver:
def __init__(self, address='', port=9090):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.address = address
self.port = port
self.sock.bind((self.address, self.port))
self.cummdata = ''
def recvmsg(self):
self.sock.listen(1)
self.conn, self.addr = self.sock.accept()
print('connected to', self.addr)
self.cummdata = ''
while True:
data = self.conn.recv(10000000)
self.cummdata += data.decode("utf-8")
if not data:
break
self.conn.send(bytes(train_test_model(self.cummdata), "utf-8"))
return self.cummdata
def __del__(self):
self.sock.close()
serv = socketserver('127.0.0.1', 9090)
print('Socket Created at {}. Waiting for client..'.format(serv.sock.getsockname()))
while True:
msg = serv.recvmsg()
答案 0 :(得分:0)
它运行正常还是serv.recvmsg()
产生运行时错误?我在recvmsg
中看到一些缩进错误。您打算让它看起来像这样吗?
def recvmsg(self):
self.sock.listen(1)
self.conn, self.addr = self.sock.accept()
print('connected to', self.addr)
self.cummdata = ''
while True:
data = self.conn.recv(10000000)
self.cummdata += data.decode("utf-8")
if not data:
break
self.conn.send(bytes(train_test_model(self.cummdata), "utf-8"))
return self.cummdata