Created
May 9, 2018 03:17
-
-
Save SathishN/0720813c550010f0ace0ace870339d7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#IF YOU FOUND THIS USEFUL, Please Donate some Bitcoin .... 1FWt366i5PdrxCC6ydyhD8iywUHQ2C7BWC | |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import numpy as np | |
import pandas as pd | |
from subprocess import check_output | |
from keras.layers.core import Dense, Activation, Dropout | |
from keras.layers.recurrent import LSTM | |
from keras.layers.advanced_activations import LeakyReLU, PReLU | |
from keras.models import Sequential | |
from sklearn.cross_validation import train_test_split | |
import math | |
from sklearn.metrics import mean_squared_error | |
from sklearn.preprocessing import MinMaxScaler | |
import matplotlib.pyplot as plt | |
from numpy import newaxis | |
from keras import backend as K | |
import math | |
import os | |
import requests | |
import json | |
import time | |
import random | |
import uuid | |
#ALL EPICS | |
epic_ids = ["CS.D.AUDUSD.TODAY.IP", "CS.D.EURCHF.TODAY.IP", "CS.D.EURGBP.TODAY.IP", "CS.D.EURJPY.TODAY.IP", "CS.D.EURUSD.TODAY.IP", "CS.D.GBPEUR.TODAY.IP", "CS.D.GBPUSD.TODAY.IP", "CS.D.USDCAD.TODAY.IP", "CS.D.USDCHF.TODAY.IP", "CS.D.USDJPY.TODAY.IP", "CS.D.CADCHF.TODAY.IP", "CS.D.CADJPY.TODAY.IP", "CS.D.CHFJPY.TODAY.IP", "CS.D.EURCAD.TODAY.IP", "CS.D.EURSGD.TODAY.IP", "CS.D.EURZAR.TODAY.IP", "CS.D.GBPCAD.TODAY.IP", "CS.D.GBPCHF.TODAY.IP", "CS.D.GBPJPY.TODAY.IP", "CS.D.GBPSGD.TODAY.IP", "CS.D.GBPZAR.TODAY.IP", "CS.D.MXNJPY.TODAY.IP", "CS.D.NOKJPY.TODAY.IP", "CS.D.PLNJPY.TODAY.IP", "CS.D.SEKJPY.TODAY.IP", "CS.D.SGDJPY.TODAY.IP", "CS.D.USDSGD.TODAY.IP", "CS.D.USDZAR.TODAY.IP", "CS.D.AUDCAD.TODAY.IP", "CS.D.AUDCHF.TODAY.IP", "CS.D.AUDEUR.TODAY.IP", "CS.D.AUDGBP.TODAY.IP", "CS.D.AUDJPY.TODAY.IP", "CS.D.AUDNZD.TODAY.IP", "CS.D.AUDSGD.TODAY.IP", "CS.D.EURAUD.TODAY.IP", "CS.D.EURNZD.TODAY.IP", "CS.D.GBPAUD.TODAY.IP", "CS.D.GBPNZD.TODAY.IP", "CS.D.NZDAUD.TODAY.IP", "CS.D.NZDCAD.TODAY.IP", "CS.D.NZDCHF.TODAY.IP", "CS.D.NZDEUR.TODAY.IP", "CS.D.NZDGBP.TODAY.IP", "CS.D.NZDJPY.TODAY.IP", "CS.D.NZDUSD.TODAY.IP", "CS.D.CHFHUF.TODAY.IP", "CS.D.EURCZK.TODAY.IP", "CS.D.EURHUF.TODAY.IP", "CS.D.EURILS.TODAY.IP", "CS.D.EURMXN.TODAY.IP", "CS.D.EURPLN.TODAY.IP", "CS.D.EURTRY.TODAY.IP", "CS.D.GBPCZK.TODAY.IP", "CS.D.GBPHUF.TODAY.IP", "CS.D.GBPILS.TODAY.IP", "CS.D.GBPMXN.TODAY.IP", "CS.D.GBPPLN.TODAY.IP", "CS.D.GBPTRY.TODAY.IP", "CS.D.TRYJPY.TODAY.IP", "CS.D.USDCZK.TODAY.IP", "CS.D.USDHUF.TODAY.IP", "CS.D.USDILS.TODAY.IP", "CS.D.USDMXN.TODAY.IP", "CS.D.USDPLN.TODAY.IP", "CS.D.USDTRY.TODAY.IP", "CS.D.CADNOK.TODAY.IP", "CS.D.CHFNOK.TODAY.IP", "CS.D.EURDKK.TODAY.IP", "CS.D.EURNOK.TODAY.IP", "CS.D.EURSEK.TODAY.IP", "CS.D.GBPDKK.TODAY.IP", "CS.D.GBPNOK.TODAY.IP", "CS.D.GBPSEK.TODAY.IP", "CS.D.NOKSEK.TODAY.IP", "CS.D.USDDKK.TODAY.IP", "CS.D.USDNOK.TODAY.IP", "CS.D.USDSEK.TODAY.IP", "CS.D.AUDCNH.TODAY.IP", "CS.D.CADCNH.TODAY.IP", "CS.D.CNHJPY.TODAY.IP", "CS.D.BRLJPY.TODAY.IP", "CS.D.GBPINR.TODAY.IP", "CS.D.USDBRL.TODAY.IP", "CS.D.USDIDR.TODAY.IP", "CS.D.USDINR.TODAY.IP", "CS.D.USDKRW.TODAY.IP", "CS.D.USDMYR.TODAY.IP", "CS.D.USDPHP.TODAY.IP", "CS.D.USDTWD.TODAY.IP", "CS.D.EURCNH.TODAY.IP", "CS.D.sp_EURRUB.TODAY.IP", "CS.D.GBPCNH.TODAY.IP", "CS.D.NZDCNH.TODAY.IP", "CS.D.USDCNH.TODAY.IP", "CS.D.sp_USDRUB.TODAY.IP"] | |
#ALL EPICS | |
#TESTING | |
decent_epics = ['CS.D.AUDUSD.TODAY.IP', 'CS.D.EURGBP.TODAY.IP', 'CS.D.EURJPY.TODAY.IP', 'CS.D.EURUSD.TODAY.IP', 'CS.D.GBPUSD.TODAY.IP', 'CS.D.USDCAD.TODAY.IP', 'CS.D.USDCHF.TODAY.IP', 'CS.D.USDJPY.TODAY.IP', 'CS.D.AUDEUR.TODAY.IP', 'CS.D.AUDGBP.TODAY.IP', 'CS.D.AUDJPY.TODAY.IP', 'CS.D.EURAUD.TODAY.IP', 'CS.D.GBPAUD.TODAY.IP'] | |
#TESTING | |
# TIME_FRAMES = ["MINUTE/1", "MINUTE_2/1", "MINUTE_3/1", "MINUTE_5/1", "MINUTE_10/1", | |
# "MINUTE_15/1", "MINUTE_30/1", "HOUR/1", "HOUR_2/1", "HOUR_3/1", "HOUR_4/1", | |
# "DAY/1", "WEEK/1", "MONTH/1"] | |
TIME_FRAMES = ["MINUTE_15/16"] | |
####################################################################### | |
# REAL_OR_NO_REAL = 'https://demo-api.ig.com/gateway/deal' | |
# API_ENDPOINT = "https://demo-api.ig.com/gateway/deal/session" | |
# API_KEY = '****' | |
# API_KEY = '****' | |
# ############################################################# | |
# #API_KEY = '****' #<- DO NOT USE!! | |
# data = {"identifier":"****","password": "****"} | |
####################################################################### | |
# FOR REAL.... | |
####################################################################### | |
####################################################################### | |
####################################################################### | |
REAL_OR_NO_REAL = 'https://api.ig.com/gateway/deal' | |
API_ENDPOINT = "https://api.ig.com/gateway/deal/session" | |
API_KEY = '*****' | |
#################################################### | |
#API_KEY = '****' #<- DO NOT USE | |
data = {"identifier":"****","password": "****"} | |
headers = {'Content-Type':'application/json; charset=utf-8', | |
'Accept':'application/json; charset=utf-8', | |
'X-IG-API-KEY':API_KEY, | |
'Version':'2' | |
} | |
r = requests.post(API_ENDPOINT, data=json.dumps(data), headers=headers) | |
headers_json = dict(r.headers) | |
CST_token = headers_json["CST"] | |
print (R"CST : " + CST_token) | |
x_sec_token = headers_json["X-SECURITY-TOKEN"] | |
print (R"X-SECURITY-TOKEN : " + x_sec_token) | |
#GET ACCOUNTS | |
base_url = REAL_OR_NO_REAL + '/accounts' | |
authenticated_headers = {'Content-Type':'application/json; charset=utf-8', | |
'Accept':'application/json; charset=utf-8', | |
'X-IG-API-KEY':API_KEY, | |
'CST':CST_token, | |
'X-SECURITY-TOKEN':x_sec_token} | |
auth_r = requests.get(base_url, headers=authenticated_headers) | |
d = json.loads(auth_r.text) | |
# print ("-----------------DEBUG-----------------") | |
# print ("#################DEBUG#################") | |
# print(auth_r.status_code) | |
# print(auth_r.reason) | |
# print (auth_r.text) | |
# print ("-----------------DEBUG-----------------") | |
# print ("#################DEBUG#################") | |
for i in d['accounts']: | |
if str(i['accountType']) == "SPREADBET": | |
print ("Spreadbet Account ID is : " + str(i['accountId'])) | |
spreadbet_acc_id = str(i['accountId']) | |
#SET SPREAD BET ACCOUNT AS DEFAULT | |
base_url = REAL_OR_NO_REAL + '/session' | |
data = {"accountId":spreadbet_acc_id,"defaultAccount": "True"} | |
auth_r = requests.put(base_url, data=json.dumps(data), headers=authenticated_headers) | |
# print ("-----------------DEBUG-----------------") | |
# print ("#################DEBUG#################") | |
# print(auth_r.status_code) | |
# print(auth_r.reason) | |
# print (auth_r.text) | |
# print ("-----------------DEBUG-----------------") | |
# print ("#################DEBUG#################") | |
#ERROR about account ID been the same, Ignore! | |
################################################################################### | |
##########################END OF LOGIN CODE######################################## | |
##########################END OF LOGIN CODE######################################## | |
##########################END OF LOGIN CODE######################################## | |
##########################END OF LOGIN CODE######################################## | |
################################################################################### | |
#PROGRAMMABLE VALUES | |
#SET INITIAL VARIABLES, Hacky for now! | |
orderType_value = "MARKET" | |
size_value = "1" | |
expiry_value = "DFB" | |
guaranteedStop_value = True | |
currencyCode_value = "GBP" | |
forceOpen_value = True | |
stopDistance_value = "0" #Make this a global variable for ease! | |
real_trader_multiplier = 0.6 | |
#fix random seed for reproducibility | |
np.random.seed(7) | |
#prices to use | |
close = [] | |
def humanize_time(secs): | |
mins, secs = divmod(secs, 60) | |
hours, mins = divmod(mins, 60) | |
return '%02d:%02d:%02d' % (hours, mins, secs) | |
def lowest_spread_epic(): | |
spreads_and_epics = [] | |
i_count = 0 | |
pick_from_epics = [] | |
for epic_id in epic_ids: | |
tmp_lst = [] | |
base_url = REAL_OR_NO_REAL + '/markets/'+ epic_id | |
auth_r = requests.get(base_url, headers=authenticated_headers) | |
d = json.loads(auth_r.text) | |
# print(auth_r.status_code) | |
# print(auth_r.reason) | |
# print (auth_r.text) | |
# print (epic_id) | |
try: | |
i_count = i_count + 1 | |
if epic_id.find('MXN') != -1: | |
print ("!!DEBUG!!...skipping, FOUND MXN in..." + str(epic_id)) | |
elif epic_id.find('SEK') != -1: | |
print ("!!DEBUG!!...skipping, FOUND SEK in..." + str(epic_id)) | |
elif epic_id.find('NOK') != -1: | |
print ("!!DEBUG!!...skipping, FOUND NOK in..." + str(epic_id)) | |
elif epic_id.find('CNH') != -1: | |
print ("!!DEBUG!!...skipping, FOUND CNH in..." + str(epic_id)) | |
else: | |
bid_price = d['snapshot']['bid'] | |
ask_price = d['snapshot']['offer'] | |
spread = float(bid_price) - float(ask_price) | |
if float(spread) > -2: | |
tmp_lst.append(epic_id) | |
pick_from_epics.append(epic_id) | |
# print ("bid : " + str(bid_price)) | |
# print ("ask : " + str(ask_price)) | |
# print ("-------------------------") | |
# print ("spread : " + str(spread)) | |
# print ("-------------------------") | |
tmp_lst.append(spread) | |
spreads_and_epics.append(tmp_lst) | |
print ("!!DEBUG!!...FOUND GOOD EPIC..." + str(i_count) + "/" + str(len(epic_ids))) | |
time.sleep(1) | |
else: | |
print ("!!DEBUG!!...skipping, NO GOOD EPIC....Checking next epic spreads..." + str(i_count) + "/" + str(len(epic_ids))) | |
time.sleep(1) | |
except Exception: | |
pass | |
#sorted_list = sorted(spreads_and_epics, key=operator.itemgetter(1)) | |
# for i in range(len(sorted_list)): | |
# print(sorted_list[i]) | |
#print (max(sorted_list, key=lambda x: x[1])) | |
#lowest_epic = max(sorted_list, key=lambda x: x[1]) | |
#return (str(lowest_epic[0])) | |
#random.shuffle(pick_from_epics) | |
#return(str(random.choice(pick_from_epics))) | |
return pick_from_epics #Return a list of epics with decent spread, Then choose from later. | |
def calculate_stop_loss(d): | |
price_ranges = [] | |
closing_prices = [] | |
first_time_round_loop = True | |
TR_prices = [] | |
price_compare = "bid" | |
for i in d['prices']: | |
if first_time_round_loop == True: | |
#First time round loop cannot get previous | |
closePrice = i['closePrice'][price_compare] | |
closing_prices.append(closePrice) | |
high_price = i['highPrice'][price_compare] | |
low_price = i['lowPrice'][price_compare] | |
price_range = float(high_price - closePrice) | |
price_ranges.append(price_range) | |
first_time_round_loop = False | |
else: | |
prev_close = closing_prices[-1] | |
############################### | |
closePrice = i['closePrice'][price_compare] | |
closing_prices.append(closePrice) | |
high_price = i['highPrice'][price_compare] | |
low_price = i['lowPrice'][price_compare] | |
price_range = float(high_price - closePrice) | |
price_ranges.append(price_range) | |
TR = max(high_price-low_price, abs(high_price-prev_close), abs(low_price-prev_close)) | |
#print (TR) | |
TR_prices.append(TR) | |
return str(int(float(max(TR_prices)))) | |
def are_we_going_to_trade(pred_ict_price, epic_id): | |
base_url = REAL_OR_NO_REAL + '/markets/'+ epic_id | |
auth_r = requests.get(base_url, headers=authenticated_headers) | |
d = json.loads(auth_r.text) | |
MARKET_ID = d['instrument']['marketId'] | |
CURRENT_PRICE_bid = float(d['snapshot']['bid']) | |
high_DAY = float(d['snapshot']['high']) | |
low_DAY = float(d['snapshot']['low']) | |
netChange = float(d['snapshot']['netChange']) | |
day_RANGE = float(high_DAY) - float(low_DAY) | |
if day_RANGE < 0: | |
day_RANGE = day_RANGE * -1 #Make positive | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
print ("!!DEBUG!!...day_RANGE is ..." + str(day_RANGE)) | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
# stopDistance_value = str(int(netChange)) | |
# stopDistance_value = str(int(day_RANGE)) | |
#stopDistance_value = str(int(netChange) + int(day_RANGE)) | |
price_diff = float(CURRENT_PRICE_bid) - float(pred_ict_price) | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
print ("!!DEBUG!!...price difference is ..." + str(price_diff)) | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
if float(price_diff) > 0: | |
print ("!!DEBUG!!...SELL") | |
DIRECTION_TO_TRADE = "SELL" | |
limitDistance_value = str(int(float(price_diff * real_trader_multiplier))) | |
else: | |
print ("!!DEBUG!!...BUY") | |
DIRECTION_TO_TRADE = "BUY" | |
limitDistance_value = str(int(float(price_diff * real_trader_multiplier * -1))) | |
if int(limitDistance_value) < 1 or int(stopDistance_value) < 1: | |
print ("!!DEBUG!!...Bailing Ooooot, Limit Distance/Stop Loss Below 1, Risk/Reward Not worth a trade") | |
return None #Really don't bother! | |
############################################################################################################### | |
print ("Order will be a " + str(DIRECTION_TO_TRADE) + " Order, With a limit of: " + str(limitDistance_value)) | |
print ("stopDistance_value for " + str(epic_id) + " will bet set at " + str(stopDistance_value)) | |
############################################################################################################### | |
#MAKE AN ORDER | |
base_url = REAL_OR_NO_REAL + '/positions/otc' | |
data = {"direction":DIRECTION_TO_TRADE,"epic": epic_id, "limitDistance":limitDistance_value, "orderType":orderType_value, "size":size_value,"expiry":expiry_value,"guaranteedStop":guaranteedStop_value,"currencyCode":currencyCode_value,"forceOpen":forceOpen_value,"stopDistance":stopDistance_value} | |
r = requests.post(base_url, data=json.dumps(data), headers=authenticated_headers) | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
print (r.status_code) | |
print (r.reason) | |
print (r.text) | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
d = json.loads(r.text) | |
deal_ref = d['dealReference'] | |
time.sleep(1) | |
#CONFIRM MARKET ORDER | |
base_url = REAL_OR_NO_REAL + '/confirms/'+ deal_ref | |
auth_r = requests.get(base_url, headers=authenticated_headers) | |
d = json.loads(auth_r.text) | |
DEAL_ID = d['dealId'] | |
print("DEAL ID : " + str(d['dealId'])) | |
print(d['dealStatus']) | |
print(d['reason']) | |
if str(d['reason']) != "SUCCESS": | |
print ("!!DEBUG!!...!!some thing occurred ERROR!!") | |
time.sleep(360) #TO DO :- Remove this and treat like a normal error...Eventually! But for now acts as very basic rate limiting | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
print ("!!DEBUG!! Resuming...") | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
else: | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
print ("!!DEBUG!!...Yay, ORDER OPEN") | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
def create_dataset(dataset, look_back=1): | |
dataX, dataY = [], [] | |
for i in range(len(dataset)-look_back-1): | |
dataX.append(dataset[i:(i+look_back), 0]) | |
dataY.append(dataset[i + look_back, 0]) | |
return np.array(dataX), np.array(dataY) | |
def plot_results_multiple(predicted_data, true_data,length): | |
print ("!!DEBUG!!...graphing disabled in Linux") | |
#plt.plot(scaler.inverse_transform(true_data.reshape(-1, 1))[length:],'k') | |
#plt.plot(scaler.inverse_transform(np.array(predicted_data).reshape(-1, 1))[length:], 'k') | |
#plt.show() | |
def predict_sequences_multiple(model, firstValue,length): | |
prediction_seqs = [] | |
curr_frame = firstValue | |
for i in range(length): | |
predicted = [] | |
#print(model.predict(curr_frame[newaxis,:,:])) | |
predicted.append(model.predict(curr_frame[newaxis,:,:])[0,0]) | |
curr_frame = curr_frame[0:] | |
curr_frame = np.insert(curr_frame[0:], i+1, predicted[-1], axis=0) | |
prediction_seqs.append(predicted[-1]) | |
return prediction_seqs | |
if __name__ == '__main__': | |
rudimentary_loop_counter = 0 | |
while True: | |
K.clear_session() #Hopefully, Speed things up later! | |
####################Refresh epics###################### | |
####################Refresh epics###################### | |
####################Refresh epics###################### | |
rudimentary_loop_counter = rudimentary_loop_counter + 1 | |
if rudimentary_loop_counter > 30: | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
print ("!!DEBUG!!...Refreshing epic list...") | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
rudimentary_loop_counter = 0 | |
decent_epics = lowest_spread_epic() | |
print (decent_epics) | |
####################Refresh epics###################### | |
####################Refresh epics###################### | |
####################Refresh epics###################### | |
random.shuffle(decent_epics) | |
epic_id = random.choice(decent_epics) | |
print ("!!DEBUG!!...chosen epic is ..." + str(epic_id)) | |
time.sleep(3) #Generic Rate Limit for IG Index API.... | |
close = [] #MUST MUST CLEAR OUT THIS EACH TIME ROUND LOOP!!! | |
for item in TIME_FRAMES: | |
base_url = REAL_OR_NO_REAL + "/prices/" + epic_id + "/" + item | |
# Price resolution (MINUTE, MINUTE_2, MINUTE_3, MINUTE_5, MINUTE_10, MINUTE_15, MINUTE_30, HOUR, HOUR_2, HOUR_3, HOUR_4, DAY, WEEK, MONTH) | |
auth_r = requests.get(base_url, headers=authenticated_headers) | |
d = json.loads(auth_r.text) | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
print(auth_r.status_code) | |
print(auth_r.reason) | |
# print (auth_r.text) | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
for i in d['prices']: | |
#snapshotTime = i['snapshotTime'] #NOT IN USE CURRENTLY | |
if i['closePrice']['bid'] is not None: | |
closePrice = i['closePrice']['bid'] | |
close.append(closePrice) | |
# if i['highPrice']['bid'] is not None: | |
# highPrice = i['highPrice']['bid'] | |
# close.append(highPrice) | |
# if i['lowPrice']['bid'] is not None: | |
# lowPrice = i['lowPrice']['bid'] | |
# close.append(lowPrice) | |
base_url = REAL_OR_NO_REAL + '/prices/'+ epic_id + '/HOUR_4/4' | |
# Price resolution (MINUTE, MINUTE_2, MINUTE_3, MINUTE_5, MINUTE_10, MINUTE_15, MINUTE_30, HOUR, HOUR_2, HOUR_3, HOUR_4, DAY, WEEK, MONTH) | |
auth_r = requests.get(base_url, headers=authenticated_headers) | |
d = json.loads(auth_r.text) | |
stopDistance_value = calculate_stop_loss(d) | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
print ("!!DEBUG ... " + str(stopDistance_value)) | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
#load the dataset | |
prices_dataset = pd.DataFrame(close) | |
prices_dataset = prices_dataset.fillna(method='pad') | |
prices_dataset = prices_dataset.values | |
prices_dataset = prices_dataset.astype('float64') | |
################################################################################### | |
##########################END OF DATA CODE######################################### | |
##########################END OF DATA CODE######################################### | |
##########################END OF DATA CODE######################################### | |
##########################END OF DATA CODE######################################### | |
################################################################################### | |
scaler = MinMaxScaler(feature_range=(0, 1)) | |
prices_dataset = scaler.fit_transform(prices_dataset) | |
train_size = int(len(prices_dataset) * 0.75) | |
test_size = len(prices_dataset) - train_size | |
train = prices_dataset[0:train_size,:] | |
test = prices_dataset[train_size:len(prices_dataset),:] | |
# reshape into X=t and Y=t+1 | |
#safer to manually code it than use train_split library | |
look_back = 1 | |
trainX, trainY = create_dataset(train, look_back) | |
testX, testY = create_dataset(test, look_back) | |
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) | |
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1])) | |
#Now lets contruct LSTM model, which works better than RNN in this case | |
model = Sequential() | |
model.add(LSTM(return_sequences=True, input_shape=(None, 1), units=50, use_bias = False)) | |
model.add(Dropout(0.2)) | |
model.add(LSTM(100,return_sequences=False)) | |
model.add(Dense(1, activation = None, use_bias = False)) | |
model.add(LeakyReLU(alpha=0.1)) | |
start = time.time() | |
model.compile(loss = 'mse', optimizer = 'adam', metrics = ['accuracy']) | |
print ('compilation time : ', time.time() - start) | |
model.fit(trainX, trainY, batch_size=1, epochs=400, validation_split=0.05) | |
predict_length=1 | |
predictions = predict_sequences_multiple(model, testX[-1], predict_length) | |
print("#######################PREDICTIONS############################") | |
print("#######################PREDICTIONS############################") | |
print("#######################PREDICTIONS############################") | |
print("#######################PREDICTIONS############################") | |
print(scaler.inverse_transform(np.array(predictions).reshape(-1, 1))) | |
plot_results_multiple(predictions, testY, predict_length) | |
print("#######################PREDICTIONS############################") | |
print("#######################PREDICTIONS############################") | |
print("#######################PREDICTIONS############################") | |
print("#######################PREDICTIONS############################") | |
# create and fit the LSTM network | |
model = Sequential() | |
model.add(LSTM(4, input_shape=(look_back, 1),use_bias = False)) | |
model.add(Dense(1, activation = None, use_bias = False)) | |
model.add(Dropout(0.2)) | |
model.add(LeakyReLU(alpha=0.1)) | |
start = time.time() | |
model.compile(loss = 'mse', optimizer = 'adam', metrics = ['accuracy']) | |
print ('compilation time : ', time.time() - start) | |
model.fit(trainX, trainY, epochs=400, batch_size=1, validation_split=0.05) | |
# make predictions | |
trainPredict = model.predict(trainX) | |
testPredict = model.predict(testX) | |
# invert predictions and targets to unscaled | |
trainPredict = scaler.inverse_transform(trainPredict) | |
trainY = scaler.inverse_transform([trainY]) | |
testPredict = scaler.inverse_transform(testPredict) | |
testY = scaler.inverse_transform([testY]) | |
# calculate root mean squared error | |
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0])) | |
print('Train Score: %.2f RMSE' % (trainScore)) | |
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0])) | |
print('Test Score: %.2f RMSE' % (testScore)) | |
# shift predictions of training data for plotting | |
trainPredictPlot = np.empty_like(prices_dataset) | |
trainPredictPlot[:, :] = np.nan | |
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict | |
# shift predictions of test data for plotting | |
testPredictPlot = np.empty_like(prices_dataset) | |
testPredictPlot[:, :] = np.nan | |
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(prices_dataset)-1, :] = testPredict | |
futurePredict = scaler.inverse_transform(np.array(predictions).reshape(-1, 1)) | |
# plot baseline and predictions | |
# plt.plot(scaler.inverse_transform(prices_dataset), 'b--') | |
# plt.plot([None for _ in range(prices_dataset.shape[0])] + [x for x in futurePredict], color='red', linestyle='--', linewidth=2, marker='s') | |
# plt.plot(trainPredictPlot, 'g') | |
# plt.plot(testPredictPlot, 'r') | |
# plt.grid(True) | |
# graph_file_name = str(uuid.uuid4()) + ".png" | |
# plt.title(str(graph_file_name) + " " + "(" + str(epic_id) + ")") | |
# plt.savefig(str(graph_file_name)) | |
# #plt.show() | |
# plt.clf() | |
pred_ict_price = np.amax(futurePredict) | |
are_we_going_to_trade(pred_ict_price, epic_id) | |
############################################## | |
############################################## | |
############################################## | |
remaining_allowance = d['allowance']['remainingAllowance'] | |
reset_time = humanize_time(int(d['allowance']['allowanceExpiry'])) | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
print ("Remaining API Calls left: " + str(remaining_allowance)) | |
print ("Time to API Key reset: " + str(reset_time)) | |
print ("-----------------DEBUG-----------------") | |
print ("#################DEBUG#################") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment