R Machine Learning simple example - Matlab, R project and Python | futures.io
futures.io futures trading

Go Back   futures.io

> Futures Trading, News, Charts and Platforms > Platforms and Indicators > Matlab, R project and Python

R Machine Learning simple example
Started:July 23rd, 2014 (01:16 PM) by Big Mike Views / Replies:559 / 1
Last Reply:July 23rd, 2014 (01:16 PM) Attachments:0

Welcome to futures.io.

Welcome, Guest!

This forum was established to help traders (especially futures traders) by openly sharing indicators, strategies, methods, trading journals and discussing the psychology of trading.

We are fundamentally different than most other trading forums:
  • We work extremely hard to keep things positive on our forums.
  • We do not tolerate rude behavior, trolling, or vendor advertising in posts.
  • We firmly believe in openness and encourage sharing. The holy grail is within you, it is not something tangible you can download.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community. It's free and simple, and we will never resell your private information.

-- Big Mike

Thread Tools Search this Thread

R Machine Learning simple example

Old July 23rd, 2014, 01:16 PM   #1 (permalink)
Site Administrator
Manta, Ecuador
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
Big Mike's Avatar
Posts: 45,447 since Jun 2009
Thanks: 28,848 given, 79,605 received

R Machine Learning simple example



Step-By-Step Example in R

Now we will go through a very simple example in R. We will use the day of the week to predict whether todayís price of Apple stock will close up or down.
First, letís make sure we have all the libraries we need installed and loaded.

#Allows us to import the data we need
#Makes it easier to work with the dates
#Gives us access to the NaÔve Bayes classifier

Next, letís get all the data we will need.
startDate = as.Date("2012-01-01")
# The beginning of the date range we want to look at
endDate = as.Date("2014-01-01")
# The end of the date range we want to look at
getSymbols("AAPL", src = "yahoo", from = startDate, to = endDate)
# Retrieving Appleís daily OHLCV from Yahoo Finance
Now that we have all the data we will need, letís get our indicator, the day of the week.
DayofWeek<-wday(AAPL, label=TRUE)
#Find the day of the week

And what we are trying to predict, whether the dayís price will close up or down, and create the final data set.
PriceChange<- Cl(AAPL) - Op(AAPL)
#Find the difference between the close price and open price
#Convert to a binary classification. (In our data set, there are no bars with an exactly 0 price change so, for simplicity sake, we will not address bars that had the same open and close price.)
#Create our data set

Finally, we are ready to use the NaÔve Bayes classifier.
#The input, or independent variable (DataSet,1]), and what we are trying to predict, the dependent variable (DataSet[,2]).

Congratulations! We have now used a machine-learning algorithm to analyze Apple stock. Now, letís dive into the results.

NaÔve Bayes Output from R

This shows us the probability of a price increase or decrease over our entire data set (known as the prior probabilities). We can see there is a slight bearish bias, but not much.

NaÔve Bayes Output from R
This shows the conditional probabilities (Given that it is a certain day of the week, what is the probability that the price will close up or down.) These are much lower than 50% due the fact they are scaled down by the prior probabilities of the outcomes (probability of price increase over the entire data set).

We are able to see that this this not a very good model, i.e. it does not return very high probabilities. However, we can see that you are generally better off going long in the beginning of the week and short towards the end of the week.

Improving the Model

Obviously you are going to want a slightly more sophisticated strategy than just looking at the day of the week. Letís now add a moving average cross to our model. (You can get some more information on adding other indicators, or features, to your model here.)
I prefer using exponential moving averages, so letís look at a 5-period and 10-period exponential moving average (EMA) cross.

First, we need to calculate the EMAs:

EMA5<-EMA(Op(AAPL),n = 5)
#We are calculating a 5-period EMA off the open price
EMA10<-EMA(Op(AAPL),n = 10)
#Then the 10-period EMA, also off the open price
Then calculate the cross
EMACross <- EMA5 - EMA10
#Positive values correspond to the 5-period EMA being above the 10-period EMA

And rounding the values to 2 decimal places. This is important because if there is an instance that the NaÔve Bayes has never seen, it will automatically calculate the probability at 0%. For example, if we were looking at the EMA cross to 6 decimal places and it found a very high probability of a downward price movement when the difference was $2.349181 and then was presented with a new data point that had the difference as $2.349182, it would calculate a 0% probability leading to a price increase or decrease. By rounding to 2 decimal places, we greatly mitigate this risk as a large enough dataset it should have seen most values of the indicator. This is an important limitation to remember when building your own models.

Letís create a new dataset and split it into a training and test set so we are able to see how well our model does over new data
DataSet2<-data.frame(DayofWeek,EMACross, Class)
#We need to remove the instances where the 10-period moving average is still being calculated
#We will use ⅔ of the data to train the model
#And ⅓ to test it on unseen data

Now to build the model:
NaÔve Bayes Output from R
The Conditional Probability of the EMA Cross, a numeric variable, shows the mean value for each case ([,1]), and the standard deviation ([,2]). We can see that the mean difference between the 5-period EMA and 10-period EMA for long and short trades was $0.54 and -$0.24, respectively.
And test it over new data:

NaÔve Bayes Output from R
We can see overall it got 79 out of 164, or 48%, correct. It had a fairly large downward bias, predicting 95, or 58%, cases as ďDOWNĒ.
While these are not great results, this should give you all the information you need to build your own machine-learning based strategy.

In the next part of our series, we will go over how you can actually use this model to improve your own trading.


Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
The following 5 users say Thank You to Big Mike for this post:

Old July 23rd, 2014, 01:16 PM   #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.



futures.io > Futures Trading, News, Charts and Platforms > Platforms and Indicators > Matlab, R project and Python > R Machine Learning simple example

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Upcoming Webinars and Events (4:30PM ET unless noted)

NinjaTrader 8: Programming Profitable Trading Edges w/Scott Hodson

Elite only

Anthony Drager: Executing on Intermarket Correlations & Order Flow, Part 2

Elite only

Adam Grimes: Five critically important keys to professional trading

Elite only

Machine Learning Concepts w/FIO member NJAMC

Elite only

MarketDelta Cloud Platform: Announcing new mobile features

Dec 1

NinjaTrader 8: Features and Enhancements

Dec 6

Similar Threads
Thread Thread Starter Forum Replies Last Post
Machine Learning/AI discussion (Generic) NJAMC The Elite Circle 70 August 22nd, 2016 01:35 PM
Webinar: Intro to Machine Learning by NJAMC Big Mike Elite Automated Trading 53 December 20th, 2015 06:23 PM
How I made $500k with machine learning and HFT NJAMC Elite Automated Trading 22 October 17th, 2014 06:45 PM
AUTOPOST: NJAMC Machine Learning Autoposts NJAMC Elite Trading Journals 52 December 27th, 2013 11:08 PM
R Machine Learning Big Mike Matlab, R project and Python 3 December 5th, 2013 12:46 AM

All times are GMT -4. The time now is 03:46 PM.

Copyright © 2016 by futures.io. All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts

Page generated 2016-10-28 in 0.08 seconds with 19 queries on phoenix via your IP