7

Click here to load reader

Stackpirt 20170222

Embed Size (px)

Citation preview

Page 1: Stackpirt 20170222

# Artificial Intelligence Stock Portfolio.# Connect to stock data API.# http://dev.markitondemand.com/MODApis/# http://dev.markitondemand.com/MODApis/Api/v2/Lookup/jsonp?input=NFLX# Don't use your API key in this script, provide sample data in this script instead.# access api in python tutorial.# http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python# https://www.quantshare.com/sa-43-10-ways-to-download-historical-stock-quotes-data-for-free# https://ichart.finance.yahoo.com/table.csv?d=6&e=1&f=2009&g=d&a=7&b=19&c=2004%20&ignore=.csv&s=NFLX# Google: yahoo stock download settings, stock data downloads# Maybe download data instead of retrieving it via an API. This will allow the script to perform faster.# https://www.google.com/finance/historical?output=csv&q=NFLX# https://www.google.co.uk/finance/company_news?q=NFLX&output=rss

# Trading period: 1 month. Result: the stock price rises after 1 day. Hypothesis: positive press release the day before.

# http://stackoverflow.com/questions/16283799/how-to-read-a-csv-file-from-a-url-with-python# urllib urlopen text mode# urllib.request.urlretrieve is used to store the resource in a temporary location.# https://docs.python.org/3.4/howto/urllib2.html# Google: python xml find containing node# Google: python elementtree basics# https://docs.python.org/3.4/library/xml.etree.elementtree.html# http://stackoverflow.com/questions/9672448/urllib-combined-together-with-elementtree# http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elementtree# http://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python

# Add comments between code."""http://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops-in-pythonNeed a dictionary to store results for each keyword.Add a storage facility for found results, e.g. whether "buy" triggers a fall or rise in price on that day.Copy and paste an example of the data.

The purpose of Stackpirt is money for other projects.Study big changes in price in a short period of time.Search for existing trading algorithms; open source trading algorithms.Study steady rises in stock prices, a familiar name, and well-received marketing campaigns.Cityfalcon.Stock data API.Possibly collaborate with someone you know.Use historical data to make predictions for e.g. last year, check whether there was a rise last year.Venture capitalist funding: need a return of at least 20% a year.Quantopian.Write an algorithm connected to a stock data API, and trade manually using the algorithm information at Capital One, or another stock broker with low minimum deposit.Write Stackpirt using Python for portfolio.Use Python Django to create a web interface.Store predictions and results to add to database. The database should contain

Page 2: Stackpirt 20170222

both news and stock prices.Nested dictionaries.Use a stock news API that shows historical news.Save news RSS feed in local database.The program learns when it is right or wrong.Neural network to make decisions. Think of how you would construct it. Cells: keyword success, each keyword, how frequently the keywords appear, whether stock goes up or down. Neural network learning mode: search for big increase or decrease in stock price, with consistency of direction according to one keyword.Use json to draw a neural network.Neural network: whether news feed contains good news or bad news. Input layer of neurons, output layer, processing layer. The output is trained, and the exact neural network is built dynamically.Search directory of open access journals for neural network papers.Each neuron is connected to all of the neurons in the next layer, and each neuron is responsible for a specific task in analysis.Output = function (matrix of weighted inputs + bias of neuron) .Trace a path through the neural network for each "yes" output.Scan annual reports.Learn from an error, based on how big the error was.Neural network input neurons: number of columns. Neural network output neurons: number of classes. Hidden layer neurons: mean of the input and output, in most cases only one hidden layer is required.Advantages of increasing hidden layers in a neural network.Separate out the hidden layers, so each hidden layer has a function and a fixed output.Artificial evolution: of keywords chosen, and their position. Also of technical analysis.Neural network to determine recent trends in buying and selling, as people are affected by procedural drift.Write some code for the neural network.Training the neural network: Matrix for input neuron's output = 1, Rand() to fill in the rest.Take a combination of states and determine values for your neural network.Take every word, and see whether the stock price goes down or up, use this to train the neural network.Neural network to have parallel layers, which each have their own function, and simultaneously feed into a summary layer. Check each layer to see that the output makes sense.The neural network must cover every possible state, so the input must be sufficiently complicated.

Check that python examples actually work as intended, and that they work for a simple example of stock price situations.

The ideal artificial intelligence must have some way of finding new criteria to judge news reports on.The ideal artificial intelligence would receive some human data as to whether they think a stock price is going to rise or fall, then find its own way of interpreting the reasons as to why the decisions were made.Each stage of the processing by the artificial intelligence should be neat and tidy.The final version of Stackpirt should not be made public, as in trading you should do the opposite of what everyone else is doing.Stackpirt: determine entry and exit points for trade.

Write a neural network in English.Markov chain statistics: fill in gaps in stock prices and news items, adjust input only.A neural network that searches and selects inputs, e.g. different words can be different inputs. Then training gives weight to different inputs.Train each individual neuron, so that the whole process is neat. Each neuron is a matrix.How does a neural network add: convert the two numbers into 1s, add all the 1s

Page 3: Stackpirt 20170222

together.Logic neurons, like logic gates.Sum neurons which sum the inputs.Self-growing neural network.

2017/08/26.New mathematics for neural networks. Formula for number of possible outputs relative to inputs, depending on number of neurons and their connections. Matrix manipulation required. For neurons with a fixed number of inputs and outputs. Find a method of dealing with when a neuron of different input and output is inserted. Do layer by layer of neurons. Does not matter about the exact position of the odd neuron, as you are only interested in the total number of inputs and outputs. Total possible inputs and outputs for each neuron. The output is the combination of neurons in the final layer that fire. The possible path taken by the signal to produce one combination of output in the final layer. The probability of each combination in the final layer's output if a certain input combination was made. Solve this problem layer by layer. Is there a faster method than this. The matrix would look like: [layer] [input neuron 1, input 1, input 1] [output of 1].[1][0 0 1][1]|1||1 0 1||0|[2][1 1 0][1][layer] [neuron] [inputs to produce a positive output][1][1][0 0 1]|1||2||1 0 1|[2][1][1 1 0]Diagram: first neuron with three inputs e.g. 001, and output linking to neuron 3. Neuron 2 has inputs 101 and outputs to neuron 3. Neuron 3 has 2 inputs.Only the paths with 1 matter, it is like the 0 inputs don't exist, so long as all the 1s for the neuron are activated.Input matrix: [neuron] [inputs tried][1][1 1 0][2][1 0 1]Subtract the inputs tried from inputs to produce a positive output.Outcome matrix:[1][1][-1 -1 1][1][2][0 0 0] This neuron will fire.For the next layer 2, get the number of the neuron whose |0|+|0|+|0|=0 and put 1 to produce an input tried.Input matrix:[2][1][0 1 0]Output matrix:[2][1][1 0 0]|1|+|0|+|0|=1, therefore no fire. Now if the inputs are summed, they are processed. You can add an extra column to distinguish between logic, inputs tried and outcome.To calculate probabilities, use conditional probabilities.[0.8 0.5 0.2] <- input tried.[1 1 0] <- logic.Outcome is 0.8*1*0.5*1=0.4 . There appears to be a simpler way of doing the matrix manipulations.Matrix 1. Logic.[1][1][0 0 1]|1||2||1 0 1|[2][1][1 1 0]Matrix 2. Inputs tried.[1][1 1 0][2][1 0 1]Matrix 3. Output.[2][1][0 0 0]Is it possible that if matrix 2 is subtracted from matrix 1, and if it is at 0, an output will ensue? No, because the logic of the third row, [2][1] counts.The first part of the matrix e.g. [2], is layer, the second part of the matrix e.g. [1], is the neuron.

Page 4: Stackpirt 20170222

Output is the last row of inputs tried minus logic.Matrix 2. Extend inputs tried, put Matrix 3 with Matrix 2.[1][1][1 1 0]|1||2||1 0 1|[2][1][0 1 0]

A matrix can be [layer] [neuron number] [links to which neurons in the previous layer] .See whether selective input equals selective output. Need multiple outputs from a single neuron. May also need multiple valid combinations of inputs for a neuron.Calculate the number of possible inputs and outputs of brain neurons.Let the output matrix go at the end of the inputs tried matrix.

---Stackpirt.

Python example neural network.The trading interval is a week.

Use in portfolio of contributing web developers.Start off as open source. Then as it grows, make it private.Ask for funding (money which will not need to be returned). Then, as the project starts to bring in money, find lenders and investors.

Single neuron network. It has 6 combinations of inputs matching 6 outputs.1, 2, 3.A, B, C, D, E, F."""

# Python 3.6

import urllib.requestimport csvimport codecs

from xml.etree import ElementTree

site = urllib.request.urlopen("https://www.google.co.uk/finance/company_news?q=NFLX&output=rss")html = site.read().decode('utf-8')xml = ElementTree.fromstring(html)

# This section lists dates under each of the keywords.# xml[0] is everything under <rss>. "./item" is item, which is a child of the top-level. https://docs.python.org/3.6/library/xml.etree.elementtree.html# relevant_dates is a dictionary, with list entries. See:# http://stackoverflow.com/questions/3199171/append-multiple-values-for-one-key-in-python-dictionary# https://docs.python.org/3/library/stdtypes.htmlnews_keyword_list = ["buy", "sell"]# {'buy': ['Fri, 03 Mar 2017 13:07:30 GMT'], 'sell': ['Fri, 03 Mar 2017 13:07:30 GMT']}relevant_dates = {}for keyword_1 in news_keyword_list:

print ("keyword_1")print (keyword_1)site =

urllib.request.urlopen("https://www.google.co.uk/finance/company_news?q=NFLX&output=rss")

html = site.read().decode('utf-8')xml = ElementTree.fromstring(html)relevant_dates[keyword_1] = []for item_node in xml[0].findall("./item"):

Page 5: Stackpirt 20170222

# if item_node[0].text.find(keyword_1) != -1# if item_node[4].text.find(keyword_1) != -1:# case_insensitive_news = item_node[4].text.casefoldcase_insensitive_news = item_node[4].text.lower()if case_insensitive_news.find(keyword_1) != -1:

date_found = item_node[3].textprint ("The date found is:")print (date_found)relevant_dates[keyword_1].append(date_found)print ("The list of dates is:")print (relevant_dates)

# This section lists formatted dates under each of the keywords.formatted_relevant_dates_with_keyword = {}# relevant_dates example: Fri, 03 Mar 2017 13:07:30 GMTfor keyword_2 in list(relevant_dates.keys()):

formatted_relevant_dates_with_keyword[keyword_2] = []for dates_in_each_keyword_2 in relevant_dates[keyword_2]:

day_number = dates_in_each_keyword_2[5:-22]print (day_number)if day_number[:1] == "0":

day_number_truncated = day_number[1:]else:

day_number_truncated = day_numbermonth = dates_in_each_keyword_2[8:-18]year = dates_in_each_keyword_2[14:-13]formatted_relevant_date = day_number_truncated + "-" + month + "-" +

yearprint (formatted_relevant_date)

formatted_relevant_dates_with_keyword[keyword_2].append(formatted_relevant_date)# {'buy': ['3-Mar-17'], 'sell': ['3-Mar-17']}print (formatted_relevant_dates_with_keyword)

# This section loops through the contents of formatted_relevant_dates_with_keyword and sees whether the price went up or down during the day.for entry_3 in list(formatted_relevant_dates_with_keyword.keys()):

print ("entry_3")print (entry_3)for dates_in_each_keyword_3 in

formatted_relevant_dates_with_keyword[entry_3]:print ("dates_in_each_keyword_3")print (dates_in_each_keyword_3)response_3 =

urllib.request.urlopen('https://www.google.com/finance/historical?output=csv&q=NFLX')

output_3 = csv.reader(codecs.iterdecode(response_3, 'utf-8'))for row_3 in output_3:

if row_3[0] == dates_in_each_keyword_3:print (row_3)change_in_price = float(row_3[4]) - float(row_3[1])if change_in_price > 0:

print ("Price went up.")elif change_in_price < 0:

print ("Price went down.")else:

print ("Price stayed the same.")

# This section saves the results of the price going up or down in relation to the keyword presence in the news, and based on this, gives a probability that the next occurrence of the keyword will result in a rise (the 1- probability is the probability that the keyword will result in a fall).

Page 6: Stackpirt 20170222

"""1. Create new CSV file."""# This section is about the neural network.# Rank the outputs if possible, so it is easier to predict the correct answers without training."""One neuron neural network.Training:1. If input is on, output is off.2. If input is off, output is off.https://www.researchgate.net/post/Is_it_possible_to_train_a_neural_network_with_missing_data2"""

Page 7: Stackpirt 20170222

"""1. Create new CSV file."""# This section is about the neural network.# Rank the outputs if possible, so it is easier to predict the correct answers without training."""One neuron neural network.Training:1. If input is on, output is off.2. If input is off, output is off.https://www.researchgate.net/post/Is_it_possible_to_train_a_neural_network_with_missing_data2"""