Decision Trees and Neural Network

After finishing the courses, I have a question regarding how will one implement his/her strategy for paper trading with end of the day data of stocks/indices?



Example : I created a Decision Tree Classifier code to predict the trend of a stock for the next day. The next day stock either goes up or down, how can I feed the actual outcome to further teach the machine?



If this can not be done with Decision Trees, can it be done with RNN models where we can store their memory as a file?



An example related to this will be appreciated because that most important thing which was not explained explicity on both the courses.

Hi Abinash,

 

Once you have created a model that you want to save, you can save the model in a json format and use it later. Please check the code below to see how to do it. This code has been updated on the course content as well.
# Serialize the model to JSON
# to_json() is an inbuilt function of Keras models for saving models
model_json = model.to_json()
# Write this model to a ".json" file
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# Serialize the weights of neurons to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
Whenever you want to load this saved model, you can use the model_from_json function from keras with the following code
# Use the model_from_json function to load the previously saved model 
from keras.models import model_from_json
# Load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# Load weights of neurons for the model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")

For decision trees and other sklearn models, you can save the model using pickle library

To save using pickle, please using the following code:
import pickle
filehandler = open(b"model.sav","wb")
pickle.dump(model,filehandler)

Load the model later using the below code:
file = open("model.sav",'rb')
model= pickle.load(file)


Once you load the pre-built model, you can use it live trading. To learn how to do paper trading 
on Interactive Broker's demo account please check this course: 
https://quantra.quantinsti.com/course/Automated-Trading-IBridgePY-Interactive-Brokers-Platform

Thank you.