Você está na página 1de 12

L IB _ MYS QL UD F_ TA

Checkout README in the source distribution for the most up to date version of this document. Implements technical analysis functions as MySQL UDFs. Currently implemented functions are:

TA_SMA TA_EMA TA_RSI TA_TR (True Range) TA_SUM (Running sum, as opposed to aggregate sum provided by mysql) TA_PREVIOUS (Returns a result N periods ago)

Other indicators which can be derived from these functions include: MACD Bollinger Bands Possibly others

IN S TA LL IN G
Compile the library I have only tried building this on Linux with MySQL 5.1 . If you manage to build it on other systems please let me know. You need the following to build this library:

gcc or some other C compiler mysql development packages

Just run make and it should build a shared library lib_mysqludf_ta.so. You might need to edit the Makefile to tell the compiler where to find the MySQL development header files. Install the shared library As make install This simply copies the library to /usr/lib/mysql/plugin and restarts mysql. Again, you might need to edit the Makefile if your MySQL plugin directory is different. Register the UDFs with MySQL The provided installdb SQL script will register all functions with mysql server:
mysql -u root -p < installdb

root,

run:

Basic test From the MySQL prompt try the following simple test
SELECT ta_ema(10.0, 1);

This should return 10.0 More information For more information on building mysql udf libraries:

http://dev.mysql.com/doc/refman/5.1/en/udf-compiling.html http://dev.mysql.com/doc/refman/5.1/en/adding-udf.html http://rpbouman.blogspot.com/2007/09/creating-mysql-udfs-with-microsoft.html

API
To try the examples below import the provided sampledb.sql into an existing MySQL database.
mysqlimport somedb < sampledb.sql

ta_ema ta_rsi ta_sma ta_sum ta_tr ta_previous ta_max ta_min

ta_ema - Exponential moving average ta_ema (float data, int period)

Arguments

data - The data to average period - Running period to calculate for

Example
To calculate a 50 period EMA of closing prices:
SELECT datetime, ta_ema(close, 50) FROM EURUSD_86400;

sma - Simple Moving Average ( aka Running average ) ta_sma (float data, int period)

Arguments

data - The data to average period - Running period to calculate for

Example
To calculate a 50 period SMA of closing prices:
SELECT datetime, ta_sma(close, 50) FROM EURUSD_86400;

rsi - Relative Strength Index ta_rsi (float data, int period)

Arguments

data - The data to average period - Running period to calculate for

Example

To calculate a 14 period RSI of closing prices:


SELECT datetime, ta_rsi(close, 14) FROM EURUSD_86400;

To calculate a 10 period ema of ta_rsi(14):


SELECT datetime, ta_ema(ta_rsi(close, 14), 10) FROM EURUSD_86400;

tr - Calculate True Range ta_tr (float high, float low, float close)

Arguments

high - The Highest price for the period low - The Lowest price for the period close - The Closing price for the period

Example
To calculate True Range
SELECT datetime, ta_tr(high, low, close) FROM EURUSD_86400;

ta_sum - Running Sum ( as opposed to aggregate sum ) ta_sum (float data, int period)

Arguments

data - The data to average period - Running period to calculate for

Example
This function is useful for calculating some indicators, such as Bollinger Bands. To calculate a 50 running sum of closing prices:
SELECT datetime, ta_sum(close, 50) FROM EURUSD_86400;

To calculate the upper limit Bollinger Band of 2 standard deviations over a 21 period sma:
SELECT datetime, ta_sma(close,21) + 2*SQRT(ta_sum(POW(close - ta_sma(close, 21), 2), 21)/21) FROM EURUSD_86400;

To calculate the lower limit Bollinger Band of 2 standard deviations over a 21 period sma:
SELECT datetime, ta_sma(close,21) - 2*SQRT(ta_sum(POW(close - ta_sma(close, 21), 2), 21)/21) FROM EURUSD_86400;

ta_max - Running Max ( as opposed to aggregate max ) ta_max (float data, int period)

Arguments

data - The data to search the maximum value period - Running period to calculate for

Example
To return the maximum close over the last 50 periods:
SELECT datetime, ta_max(close, 50) FROM EURUSD_86400;

ta_min - Running Min ( as opposed to aggregate min ) ta_min (float data, int period)

Arguments

data - The data to search the minimum value period - Running period to calculate for

Example
To return the minimum close over the last 50 periods:
SELECT datetime, ta_min(close, 50) FROM EURUSD_86400;

ta_previous - Return results N periods ago ta_previous (float data, int period)

Arguments

data - The data to lookback into period - Number of periods to look back into

Example
See if today's close is greater than yesterday's close
SELECT datetime, close > ta_previous(close,1) FROM EURUSD_86400;

O TH ER DER IV E D IN D IC ATOR S
macd - Moving Average Convergence / Divergence MACD is defined as the difference between two emas
SELECT datetime, ta_ema(close,12) - ta_ema(close,26) AS MACD FROM EURUSD_86400;

The MACD signal line is defined as an EMA of MACD


SELECT datetime, ta_ema(ema(close,12) - ta_ema(close,26), 9) AS MACD_SIGNAL FROM EURUSD_86400;

US IN G WITH IN T EG ER DATA IN S TE A D OF F LOATS


Financial data tends to be stored as floats, however sometimes it might be useful to run these functions with integer data. The quickest way to achieve this is to use MySQL CAST:
SELECT ta_ema(CAST(integer_data_field AS DECIMAL(65), 14) FROM TABLE;

C R EATIN G N EW FUN C TI ON S
The easiest way might be to copy one of the existing .c files to a different name and modify its implementation. The provided Makefile will pick up new .c files with no modification.

C ON TR IB UTIN G

The

development

repository

for

this

project

is

hosted

at

github: http://github.com/joaocosta/lib_mysqludf_ta

README

DESCRIPTION: Implements technical analysis functions as MySQL UDFs. Currently implemented functions are: TA_SMA TA_EMA TA_RSI TA_TR (True Range) TA_SUM (Running sum, as opposed to aggregate sum provided by mysql) TA_PREVIOUS Other indicators which can be derived from those functions include: MACD Bollinger Bands ADX Possibly others

AVAILABILITY: Source repository at: http://github.com/joaocosta/lib_mysqludf_ta

COMPILING: Windows: A binary dll is shipped with this release. The following article describes how to compile MySQL UDFs in Windows: http://rpbouman.blogspot.com/2007/09/creating-mysqludfs-with-microsoft.html

Linux: You need the following to build this library: - gcc or some other C compiler - mysql development packages (eg: "yum install mysqldevel" in Fedora) Just run make and it should build a shared library lib_mysqludf_ta.so. You might need to edit the Makefile to tell the compiler where to find the MySQL development header files. For more information on compiling MySQL UDFs: http://dev.mysql.com/doc/refman/5.1/en/udfcompiling.html http://dev.mysql.com/doc/refman/5.1/en/addingudf.html

INSTALLING: You need to copy the binary library to the MySQL plugin directory. To find out what the MySQL plugin directory is run this in MySQL prompt: show variables like 'plugin_dir';

Install the UDFs From the MySQL prompt: Windows CREATE FUNCTION ta_ema RETURNS REAL SONAME 'lib_mysqludf_ta.dll'; CREATE FUNCTION ta_rsi RETURNS REAL SONAME 'lib_mysqludf_ta.dll'; CREATE FUNCTION ta_sma RETURNS REAL SONAME 'lib_mysqludf_ta.dll';

CREATE FUNCTION ta_sum RETURNS REAL SONAME 'lib_mysqludf_ta.dll'; CREATE FUNCTION ta_tr RETURNS REAL SONAME 'lib_mysqludf_ta.dll'; CREATE FUNCTION ta_max RETURNS REAL SONAME 'lib_mysqludf_ta.dll'; CREATE FUNCTION ta_min RETURNS REAL SONAME 'lib_mysqludf_ta.dll'; CREATE FUNCTION ta_previous RETURNS REAL SONAME 'lib_mysqludf_ta.dll'; Linux CREATE FUNCTION ta_ema RETURNS REAL SONAME 'lib_mysqludf_ta.so'; CREATE FUNCTION ta_rsi RETURNS REAL SONAME 'lib_mysqludf_ta.so'; CREATE FUNCTION ta_sma RETURNS REAL SONAME 'lib_mysqludf_ta.so'; CREATE FUNCTION ta_sum RETURNS REAL SONAME 'lib_mysqludf_ta.so'; CREATE FUNCTION ta_tr RETURNS REAL SONAME 'lib_mysqludf_ta.so'; CREATE FUNCTION ta_max RETURNS REAL SONAME 'lib_mysqludf_ta.so'; CREATE FUNCTION ta_min RETURNS REAL SONAME 'lib_mysqludf_ta.so'; CREATE FUNCTION ta_previous RETURNS REAL SONAME 'lib_mysqludf_ta.so';

FUNCTIONS: To try the examples below import the provided sampledb.sql into a MySQL database. mysqladmin -u root create lib_ta mysql -u root lib_ta < sampledb.sql mysql -u root lib_ta ----------------------------------------------------------------------------------------------------ta_ema - Exponential moving average ta_ema( float data, int period )

data - The data to average period - Running period to calculate for Example: To calculate a 50 period EMA of closing prices: SELECT datetime, ta_ema(close, 50) FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T; ----------------------------------------------------------------------------------------------------ta_sma - Simple Moving Average ( aka Running average ) ta_sma( float data, int period ) data - The data to average period - Running period to calculate for Example: To calculate a 50 period SMA of closing prices: SELECT datetime, ta_sma(close, 50) FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T; ----------------------------------------------------------------------------------------------------ta_rsi - Relative Strength Index ta_rsi( float data, int period ) data - The data to calculate rsi for period - Running period to calculate for Example: To calculate a 14 period RSI of closing prices:

SELECT datetime, ta_rsi(close, 14) FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T; To calculate a 10 period ta_ema of ta_rsi(14): SELECT datetime, ta_ema(ta_rsi(close, 14), 10) FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T; ----------------------------------------------------------------------------------------------------ta_sum - Running Sum ( as opposed to aggregate sum ) This function is useful for calculating some indicators, such as Bollinger Bands. ta_sum( float data, int period ) data - The data to average period - Running period to calculate for Example: To calculate a 50 running sum of closing prices: SELECT datetime, ta_sum(close, 50) FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T; To calculate the upper limit Bollinger Band of 2 standard deviations over a 21 period sma: SELECT datetime, ( ta_sma(close,21) + 2*SQRT(ta_sum(POW(close - ta_sma(close, 21), 2), 21)/21) ) AS `BOL_UP` FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T; To calculate the lower limit Bollinger Band of 2 standard deviations over a 21 period sma: SELECT datetime, ( ta_sma(close,21) 2*SQRT(ta_sum(POW(close - ta_sma(close, 21), 2), 21)/21) ) AS `BOL_DOWN`

FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T;

----------------------------------------------------------------------------------------------------ta_tr - Calculate True Range ta_tr( float high, float low, float close ) high low close - The Highest price for the period - The Lowest price for the period - The Closing price of the period

Example: To calculate True Range SELECT datetime, ta_tr(high, low, close) FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T; ----------------------------------------------------------------------------------------------------ta_previous - Calculate True Range ta_previous( float data, int period ) data - The data to lookback into period - Number of periods to look back into

Example: See if today's close is greater than yesterday's close SELECT datetime, close > ta_previous(close,1)

FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T; ----------------------------------------------------------------------------------------------------ta_max - Running Max ta_max( float data, int period ) data - The data to search the maximum for period - Running period to calculate for

Example: To return the maximum close over the last 50 periods: SELECT datetime, ta_max(close, 50) FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T; ----------------------------------------------------------------------------------------------------ta_min - Running Min ta_min( float data, int period ) data - The data to search the minimum for period - Running period to calculate for

Example: To return the minimum close over the last 50 periods: SELECT datetime, ta_min(close, 50) FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T; ----------------------------------------------------------------------------------------------------macd - Moving Average Convergence / Divergence

MACD is defined as the difference between two emas SELECT datetime, ta_ema(close,12) - ta_ema(close,26) AS `MACD` FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T; The MACD signal line is defined as an EMA of MACD SELECT datetime, ta_ema(ta_ema(close,12) ta_ema(close,26), 9) AS `MACD_SIGNAL` FROM ( SELECT * FROM EURUSD_86400 ORDER BY datetime ASC ) AS T;

----------------------------------------------------------------------------------------------------USING WITH INTEGER DATA INSTEAD OF FLOATS: Financial data tends to be stored as floats, however sometimes it might be useful to run these functions with integer data. The quickest way to achieve this is to use MySQL CAST: SELECT ta_ema(CAST(integer_data_field AS DECIMAL(65), 14) FROM TABLE;

CREATING NEW FUNCTIONS: The easiest way might be to copy one of the existing .c files to a different name and modify its implementation. The provided Makefile will pick up new .c files.

Você também pode gostar