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

R Machine Learning simple example
Updated: Views / Replies:1,093 / 1
Created: by Big Mike Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 100,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • 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.

-- Big Mike, Site Administrator

Thread Tools Search this Thread

R Machine Learning simple example

  #1 (permalink)
Site Administrator
Manta, Ecuador
Trading Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
Big Mike's Avatar
Posts: 47,181 since Jun 2009
Thanks: 30,064 given, 88,009 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:
  #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 > > > > R Machine Learning simple example

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

futures io is celebrating 10-years w/ over $18,000 in prizes!

Right now

Similar Threads
Thread Thread Starter Forum Replies Last Post
Machine Learning/AI discussion (Generic) NJAMC The Elite Circle 78 February 26th, 2018 02:34 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

automatically, average, conditional, data, dow, ema, est, indicator, indicators, information, list, machine learning, matlab, matlab trading, moving average, python, python trading, quantmod, r project, r trading, short, split, standard deviation, strategy, trading, training, values, variable

All times are GMT -4. The time now is 10:46 AM. (this page content is cached, log in for real-time version)

Copyright © 2019 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432 WhatsApp Business, info@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 2019-06-17 in 0.20 seconds with 14 queries on phoenix