Calculating Indicators for Stock Data Forecasting

In the previous posts was shown how to download stock data into Google spreadsheet using perl script. Here we will look how to add other data indicators based on downloaded stock data prices and volume. The use for stock data prediction of such indicators based on stock data that is usually is available for downloading can be viewed in data mining papers [1], [2] More links and some notes can be found at input-for-stock-data-prediction. Here is the link to stock data analysis perl module with the source code to calculate simple moving average, force index indicator, momentum and some other. In the near future will be added calculations for other indicators.

And here an example how the functions from this module can be used in any perl program:

use warnings;
use stock_data_analysis;
for($i=0; $i<10; $i++) { $data[$i]=$i; $vol[$i]=2; } for($i=0; $i<10; $i++) { print $data[$i]; print "\n"; } @diff_data= stock_data_analysis::do_diff(@data); for($i=0; $i<10; $i++) { print $diff_data[$i]; print "\n"; } print “\n”; print “mean=”; print stock_data_analysis::get_mean(@data); print “\n\n”; @a=stock_data_analysis::sma(4, @data); for($i=0; $i<10; $i++) { print $a[$i]; print "\n"; } print “\n\n”; @a=stock_data_analysis::get_FI(1, \@data, \@vol); for($i=0; $i<10; $i++) { print $a[$i]; print "\n"; } Thus now we can add above indicators to downloaded stock price data in Google spreadsheet. References 1. Financial Stock Market Forecast using Data Mining Techniques K. Senthamarai Kannan, P. Sailapathi Sekar, M.Mohamed Sathik and P. Arumugam, IMECS 2010 2. The Comparison of Methods Artificial Neural Network with Linear Regression Using Specific Variables for Prediction Stock Price in Tehran Stock Exchange, Reza Gharoie Ahangar, Mahmood Yahyazadehfar, Hassan Pournaghshband, (IJCSIS) International Journal of Computer Science and Information Security, Vol. 7, No. 2, February 2010



Calculating Indicators for Stock Data Forecasting

In the previous posts was shown how to download stock data into Google spreadsheet using perl script. Here we will look how to add other data indicators based on downloaded stock data prices and volume. The use for stock data prediction of such indicators based on stock data that is usually is available for downloading can be viewed in data mining papers [1], [2] More links and some notes can be found at input-for-stock-data-prediction. Here is the link to stock data analysis perl module with the source code to calculate simple moving average, force index indicator, momentum and some other. In the near future will be added calculations for many others indicators. And here an example how the functions from this module can be used in any perl program: use warnings; use stock_data_analysis; for($i=0; $i<10; $i++) { $data[$i]=$i; $vol[$i]=2; } for($i=0; $i<10; $i++) { print $data[$i]; print "\n"; } @diff_data= stock_data_analysis::do_diff(@data); for($i=0; $i<10; $i++) { print $diff_data[$i]; print "\n"; } print "\n"; print "mean="; print stock_data_analysis::get_mean(@data); print "\n\n"; @a=stock_data_analysis::sma(4, @data); for($i=0; $i<10; $i++) { print $a[$i]; print "\n"; } print "\n\n"; @a=stock_data_analysis::get_FI(1, \@data, \@vol); for($i=0; $i<10; $i++) { print $a[$i]; print "\n"; } Thus now we can add above indicators to downloaded stock price data in Google spreadsheet. References 1. Financial Stock Market Forecast using Data Mining Techniques K. Senthamarai Kannan, P. Sailapathi Sekar, M.Mohamed Sathik and P. Arumugam, IMECS 2010 2. The Comparison of Methods Artificial Neural Network with Linear Regression Using Specific Variables for Prediction Stock Price in Tehran Stock Exchange, Reza Gharoie Ahangar, Mahmood Yahyazadehfar, Hassan Pournaghshband, (IJCSIS) International Journal of Computer Science and Information Security, Vol. 7, No. 2, February 2010



Input for Stock Data Prediction Algorithms

What can be used for input to stock data prediction system? In this post we will consider some indicator that often are used for stock data forecasting. The links to information about indicators and how to calculate will be also provided.

Moving averages are often used in technical analysis. A few examples: Simple Moving Average (SMA) – to calculate SMA for period of n days we take sum of prices for the n days going back and divide by n. Then move to next day and do it again.

Exponential Moving Average (EMA) – with this moving average the recent data are getting more weight than other. With SMA all data points are getting the same weight.

Here is the indicator that uses prices and volume: Force Index. In two words it is showing how strong is the current trend, how likely it will continue or change.[1] Force index for 1 period can be calculated as

Force Index(1) = (Close_Price (current) – Close_Price (prev)) x Volume

The list of indicators with the information how they are calculated can be found at [2]. They are divided in 3 groups, price based, volume based and breadth indicators. Breadth indicators are based on statistics derived from the broad market. Some papers on stock data forecasting also describe very well the list of variables that were chosen for input.

If we look we can find that there are many different choices but it can improve the quality of forecast accuracy.[3],[4] Not only price , volume or other market statistics can be used for stock market forecasting. Textual web data can be also used but it requires web/text mining processing. You can find some example at [5]

References

1.How to Use Force Index
2.Technical analysis From Wikipedia, the free encyclopedia
3. The Comparison of Methods Artificial Neural Network with Linear Regression Using Specific Variables for Prediction Stock Price in Tehran Stock Exchange Reza Gharoie Ahangar,Mahmood Yahyazadehfar,Hassan Pournaghshband (IJCSIS) International Journal of Computer Science and Information Security, Vol. 7, No. 2, February 2010
4.Financial Stock Market Forecast using Data Mining Techniques K. Senthamarai Kannan, P. Sailapathi Sekar, M.Mohamed Sathik and P. Arumugam, IMECS 2010, hong Kong
5.Daily Stock Market Forecast from Textual Web Data B. Wuthrich, V. Cho, S. Leung, D. Permunetilleke, K. Sankaran, J. Zhang, W. Lam, The Hong Kong University of Science and Technology
6. Technical Indicators and Overlays From StockCharts.com – ChartSchool 7.Financial Stock Market Forecast using Data Mining Techniques K. Senthamarai Kannan, P. Sailapathi Sekar, M.Mohamed Sathik and P. Arumugam



Swing Stock Trading

Swing stock trading means buying and selling stocks many times when the stock prices are at or near their bottom or top position. Some details and more information can be found at [1],[2].

The simple and well known rule – buy low and sell high can be applied here too. But how do we detect when it is low and when it is high?

Historical data and computer simulation can be the answer. We can pick the strategy and then evaluate it using past data. By changing some parameters and running simulation on the past data we can also improve the strategy.

For example the simple rule – buy when one day stock price decrease is more than some number (delta) and sell when the increase is more than delta can be run on previous stock data with the different delta.

References – External Links

1. Swing trading From Wikipedia, the free encyclopedia
2.Swing Stock Trading – Simple Time And Price Predictions Strategy



Adding Stock Market Data to a Google Spreadsheet with a Perl Script

In the previous post it was shown how to download stock data data prices into Google spreadsheet using perl script.
This post will extend this script by considering situation when we just want to add the latest data to the existing spreadsheet with the data downloaded previously. For example first time we can download data for the last 3 month and then going forward in the future each day we will need only to add data for the previous day or days that are not loaded yet. This will significantly save time on upgrading process.

Below is segment of code that is used for adding new data to the existing spreadsheet with previously downloaded data. We call this mode = “ad”. In this mode we find blank row and then put values for the record that is matching to blank row number.

For this to work we need always have the same start date for our downloading data set.
if ($mode eq “ad”)
{ for($i=2; $icell({col => 1, row > $i});
$v=$cell->input_value;
if ($v eq “”)
{ for($j=1; $jcell({col => $j, row => $i});
$cell->input_value($data[$i-2][$j-1]);
} } }
exit; }
# end of if $mode = “ad”

Here is the whole script. To run it first time you need disable the line $mode=”ad”; by putting # in front of $ sign.
Then remove # so it will run in the addition of data mode.
Don’t forget also put your email, password and document / spreadsheet title names.
use Finance::QuoteHist;
#if first time download data into empty sheet $mode =””;
#if adding data to the sheet with previously downloaded data $mode=”ad”;
#disable this line if first time download data
$q = Finance::QuoteHist->
new ( symbols => [qw(IBM UPS AMZN)], start_date => ‘8/18/2013’, end_date => ‘today’, );
$count=0;
foreach $row ($q->quotes()) { ($symbol, $date, $open, $high, $low, $close, $volume) = @$row;
$date =~ s/\//-/g;
print “$symbol, $date, $open, $high, $low, $close, $volume \n”; $data[$count][0]=$symbol;
$data[$count][1]=$date;
$data[$count][2]=$open;
$data[$count][3]=$high;
$data[$count][4]=$low;
$data[$count][5]=$close;
$data[$count][6]=$volume;
$count=$count+1; }
use Net::Google::Spreadsheets;
my $service = Net::Google::Spreadsheets->new( username => ‘youremail@company.com’, password => ‘xxxxxxx’ );
my @spreadsheets = $service->spreadsheets();
my $spreadsheet = $service->spreadsheet( { title => ‘titlename’ } );
my $worksheet1 = $spreadsheet->worksheet( { title => ‘Sheet_name’ } );
if ($mode eq “ad”) { for($i=2; $icell({col => 1, row => $i}); $v=$cell->input_value;
if ($v eq “”) { for($j=1; $jcell({col => $j, row => $i}); $cell->
input_value($data[$i-2][$j-1]); } } }
exit; }
# if $mode = “ad”
$cell = $worksheet1->
cell({col => 1, row => 1});
$cell->input_value(“symbol”);
$cell = $worksheet1->cell({col => 2, row => 1});
$cell->input_value(“date”);
$cell = $worksheet1->cell({col => 3, row => 1});
$cell->input_value(“open”);
$cell = $worksheet1->
cell({col =>4, row => 1});
$cell->input_value(“high”);
$cell = $worksheet1->cell({col => 5, row => 1});
$cell->input_value(“low”);
$cell = $worksheet1->
cell({col => 6, row => 1});
$cell->input_value(“close”);
$cell = $worksheet1->cell({col =>7, row =>; 1});
$cell->input_value(“volume”);
for($i=2; $i { for($j=1; $jcell({col => $j, row => $i});
$cell->
input_value($data[$i-2][$j-1]);
} }
Now we can use this script for downloading and updating data on regular basis.