Created
July 2, 2015 14:08
-
-
Save crtradeworks/2783e430c62e9c40e9e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib inline" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"\"\"\"\n", | |
"FX Symbol Selection project\n", | |
"Select FX Symbol for trading based on volatility, liquidity, momentum, etc.\n", | |
"Use Dollar Index as benchmark.\n", | |
"\"\"\"\n", | |
"\n", | |
"# Import Python standard libraries\n", | |
"import csv\n", | |
"import math\n", | |
"from datetime import datetime" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"#################################################\n", | |
"### Part I - read data from csv files as raw data\n", | |
"\n", | |
"# Read data from csv files\n", | |
"EURUSDreader = csv.reader(open('EURUSD.csv'))\n", | |
"GBPUSDreader = csv.reader(open('GBPUSD.csv'))\n", | |
"USDJPYreader = csv.reader(open('USDJPY.csv'))\n", | |
"USDCADreader = csv.reader(open('USDCAD.csv'))\n", | |
"USDCHFreader = csv.reader(open('USDCHF.csv'))\n", | |
"USDSEKreader = csv.reader(open('USDSEK.csv'))\n", | |
"\n", | |
"# 4 functions to process data\n", | |
"def FX(FXreader):\n", | |
"\tFX = []\n", | |
"\tfor line in FXreader:\n", | |
"\t\tFX.append(line)\n", | |
"\treturn FX\n", | |
"\n", | |
"def FX_Time(FX):\n", | |
"\tFX_Time = []\n", | |
"\tfor row in FX:\n", | |
"\t\tFX_Time.append(datetime.strptime((row[0] + \" \" + row[1]), \"%Y.%m.%d %H:%M\"))\n", | |
"\treturn FX_Time\n", | |
"\n", | |
"def FX_Close(FX):\n", | |
"\tFX_Close = []\n", | |
"\tfor row in FX:\n", | |
"\t\tFX_Close.append(float(row[5]))\n", | |
"\treturn FX_Close\n", | |
"\n", | |
"def FX_Volume(FX):\n", | |
"\tFX_Volume = []\n", | |
"\tfor row in FX:\n", | |
"\t\tFX_Volume.append(float(row[6]))\n", | |
"\treturn FX_Volume\n", | |
"\n", | |
"# Call the functions to get Time, Close_price, Volume data series of each FX pair\n", | |
"EURUSD = FX(EURUSDreader)\n", | |
"GBPUSD = FX(GBPUSDreader)\n", | |
"USDJPY = FX(USDJPYreader)\n", | |
"USDCAD = FX(USDCADreader)\n", | |
"USDCHF = FX(USDCHFreader)\n", | |
"USDSEK = FX(USDSEKreader)\n", | |
"\n", | |
"EURUSD_Time = FX_Time(EURUSD)\n", | |
"GBPUSD_Time = FX_Time(GBPUSD)\n", | |
"USDJPY_Time = FX_Time(USDJPY)\n", | |
"USDCAD_Time = FX_Time(USDCAD)\n", | |
"USDCHF_Time = FX_Time(USDCHF)\n", | |
"USDSEK_Time = FX_Time(USDSEK)\n", | |
"\n", | |
"EURUSD_Close = FX_Close(EURUSD)\n", | |
"GBPUSD_Close = FX_Close(GBPUSD)\n", | |
"USDJPY_Close = FX_Close(USDJPY)\n", | |
"USDCAD_Close = FX_Close(USDCAD)\n", | |
"USDCHF_Close = FX_Close(USDCHF)\n", | |
"USDSEK_Close = FX_Close(USDSEK)\n", | |
"\n", | |
"EURUSD_Volume = FX_Volume(EURUSD)\n", | |
"GBPUSD_Volume = FX_Volume(GBPUSD)\n", | |
"USDJPY_Volume = FX_Volume(USDJPY)\n", | |
"USDCAD_Volume = FX_Volume(USDCAD)\n", | |
"USDCHF_Volume = FX_Volume(USDCHF)\n", | |
"USDSEK_Volume = FX_Volume(USDSEK)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"########################################\n", | |
"# Part II - get Dollar Index as Benchmark\n", | |
"\n", | |
"# Get the Dollar_Index\n", | |
"Index_Time = []\n", | |
"for item in USDJPY_Time:\n", | |
"\tif (item in EURUSD_Time) and (item in GBPUSD_Time) and (item in USDCAD_Time) and (item in USDCHF_Time) and (item in USDSEK_Time):\n", | |
"\t\tIndex_Time.append(item)\n", | |
"\n", | |
"\n", | |
"Dollar_Index_ClosePrice = []\n", | |
"for item in Index_Time:\n", | |
"\tEUR = EURUSD_Close[EURUSD_Time.index(item)]\n", | |
"\tGBP = GBPUSD_Close[GBPUSD_Time.index(item)]\n", | |
"\tJPY = USDJPY_Close[USDJPY_Time.index(item)]\n", | |
"\tCAD = USDCAD_Close[USDCAD_Time.index(item)]\n", | |
"\tCHF = USDCHF_Close[USDCHF_Time.index(item)]\n", | |
"\tSEK = USDSEK_Close[USDSEK_Time.index(item)]\n", | |
"\ttemp_element = 50.14348112 * EUR**(-0.576) * JPY**(0.136) * GBP**(-0.119) * CAD**(0.091) * SEK**(0.042) * CHF**(0.036)\n", | |
"\tDollar_Index_ClosePrice.append(temp_element)\n", | |
"\n", | |
"Dollar_Index_Volume = []\n", | |
"for item in Index_Time:\n", | |
"\tEUR = EURUSD_Volume[EURUSD_Time.index(item)]\n", | |
"\tGBP = GBPUSD_Volume[GBPUSD_Time.index(item)]\n", | |
"\tJPY = USDJPY_Volume[USDJPY_Time.index(item)]\n", | |
"\tCAD = USDCAD_Volume[USDCAD_Time.index(item)]\n", | |
"\tCHF = USDCHF_Volume[USDCHF_Time.index(item)]\n", | |
"\tSEK = USDSEK_Volume[USDSEK_Time.index(item)]\n", | |
"\ttemp_element = EUR**(-0.576) * JPY**(0.136) * GBP**(-0.119) * CAD**(0.091) * SEK**(0.042) * CHF**(0.036)\n", | |
"\tDollar_Index_Volume.append(temp_element)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"########################################\n", | |
"# Part III - get FX_Return (closing price percentage change) data series\n", | |
"\n", | |
"# Function of FX_Return\n", | |
"def get_FX_Return(FX_Close):\n", | |
"\t\"\"\"\n", | |
"\tInput data: closing price data series of the FX\n", | |
"\tReturn: closing price change data series of the FX\n", | |
"\t\"\"\"\n", | |
"\tFX_Return = []\n", | |
"\t\n", | |
"\tfor index in (range(len(FX_Close) - 1)):\n", | |
"\t\ttemp = abs(math.log(FX_Close[index + 1]) - math.log(FX_Close[index]))\n", | |
"\t\tFX_Return.append(temp)\n", | |
"\t\n", | |
"\treturn FX_Return" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"########################################\n", | |
"# Part IV - Volatility\n", | |
"\n", | |
"\"\"\"\n", | |
"How volatility guides retail traders:\n", | |
"Risk aversion - prefer FX pairs with low volatility, stable performance with low risk\n", | |
"Risk preference - prefer FX pairs with high volatility, may make sudden millionaire while high risk\n", | |
"\"\"\"\n", | |
"\n", | |
"# Function of FX_Volatility, using Exponential Weighted Moving Average model\n", | |
"def FX_Volatility(FX_Close_raw, FX_Time, starting_time, ending_time):\n", | |
"\t\"\"\"\n", | |
"\tFor production:\n", | |
"\tusers should choose only starting_time and the name of FX symbol, \n", | |
"\tthe ending_time should always be the latest time in our market database,\n", | |
"\twe do the data processing(to generate FX_Close_raw and FX_Time) for users.\n", | |
"\n", | |
"\t4 input data:\n", | |
"\t1-dimension array/list/vector of FX_Close_price series\n", | |
"\t1-dimension array/list/vector of FX_Time series\n", | |
"\ta time data of starting_time\n", | |
"\ta time data of ending_time\n", | |
"\t\"\"\"\n", | |
"\t\n", | |
"\t# Get the Close_price data within a period from starting_time to ending_time\n", | |
"\tindex_1 = FX_Time.index(starting_time)\n", | |
"\tindex_2 = FX_Time.index(ending_time) + 1\n", | |
"\tFX_Close = FX_Close_raw[index_1 : index_2]\n", | |
"\n", | |
"\t# Get the Return_series of the FX symbol\n", | |
"\tFX_Return = get_FX_Return(FX_Close)\n", | |
"\n", | |
"\t# Calculate EWMA model and get the volatility\n", | |
"\tLAMBDA = 0.94\n", | |
"\tVolatility = []\n", | |
"\tVolatility.append(FX_Return[0])\n", | |
"\n", | |
"\tfor i in range(len(FX_Return)):\n", | |
"\t\ttemp_Volatility = math.sqrt(LAMBDA * Volatility[i] ** 2 + (1 - LAMBDA) * FX_Return[i] ** 2)\n", | |
"\t\tVolatility.append(temp_Volatility)\n", | |
"\n", | |
"\t# import matplotlib.pyplot as plt\n", | |
"\t# time = FX_Time[index_1 : index_2]\n", | |
"\t# plt.plot(time, Volatility)\n", | |
"\n", | |
"\t# The returned value is the last value of the volatility series\n", | |
"\treturn Volatility[len(Volatility) - 1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"########################################\n", | |
"# Part V - Liquidity\n", | |
"\n", | |
"\"\"\"\n", | |
"How liquidity guides retail traders:\n", | |
"\n", | |
"\"\"\"\n", | |
"\n", | |
"def FX_Liquidity(FX_Close_raw, FX_Volume_raw, FX_Time, starting_time, ending_time):\n", | |
"\t\"\"\"\n", | |
"\tThe same as FX_Volatility :)\n", | |
"\t\"\"\"\n", | |
"\n", | |
"\t# Get the Close_price data within a period from starting_time to ending_time\n", | |
"\tindex_1 = FX_Time.index(starting_time)\n", | |
"\tindex_2 = FX_Time.index(ending_time) + 1\n", | |
"\tFX_Close = FX_Close_raw[index_1 : index_2]\n", | |
"\tFX_Volume = FX_Volume_raw[index_1 : index_2]\n", | |
"\n", | |
"\t# Get the absolute value of Return_series of the FX symbol\n", | |
"\tFX_Return = get_FX_Return(FX_Close)\n", | |
"\n", | |
"\t# Calculate liquidity\n", | |
"\tLiquidity = 0.0\n", | |
"\tfor i in range(len(FX_Return)):\n", | |
"\t\tLiquidity += FX_Return[i] / FX_Volume[i+1]\n", | |
"\n", | |
"\treturn Liquidity" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.000953732576007\n", | |
"91.8201186094\n", | |
"\n", | |
"['FX pair ', 'Volatility', 'Liquidity']\n", | |
"['EURUSD: ', 1.195790928621961, 0.0035493317347093296]\n", | |
"['GBPUSD: ', 1.2575349936397355, 0.00219394492243788]\n", | |
"['USDJPY: ', 0.8344798291117231, 0.0033466999799656554]\n", | |
"['USDCAD: ', 1.2940480442538815, 0.0041707638560959884]\n", | |
"['USDCHF: ', 2.418970052091221, 0.0034447129042055515]\n", | |
"['USDSEK: ', 2.4387256180025756, 0.0049629575755860455]\n" | |
] | |
} | |
], | |
"source": [ | |
"# Test module\n", | |
"EURUSD_volatility = FX_Volatility(EURUSD_Close, EURUSD_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 01, 07, 0))\n", | |
"GBPUSD_volatility = FX_Volatility(GBPUSD_Close, GBPUSD_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 23, 23, 0))\n", | |
"USDJPY_volatility = FX_Volatility(USDJPY_Close, USDJPY_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 23, 23, 0))\n", | |
"USDCAD_volatility = FX_Volatility(USDCAD_Close, USDCAD_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 23, 23, 0))\n", | |
"USDCHF_volatility = FX_Volatility(USDCHF_Close, USDCHF_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 23, 23, 0))\n", | |
"USDSEK_volatility = FX_Volatility(USDSEK_Close, USDSEK_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 23, 23, 0))\n", | |
"\n", | |
"EURUSD_liquidity = FX_Liquidity(EURUSD_Close, EURUSD_Volume, EURUSD_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 01, 07, 0))\n", | |
"GBPUSD_liquidity = FX_Liquidity(GBPUSD_Close, GBPUSD_Volume, GBPUSD_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 01, 07, 0))\n", | |
"USDJPY_liquidity = FX_Liquidity(USDJPY_Close, USDJPY_Volume, USDJPY_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 01, 07, 0))\n", | |
"USDCAD_liquidity = FX_Liquidity(USDCAD_Close, USDCAD_Volume, USDCAD_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 01, 07, 0))\n", | |
"USDCHF_liquidity = FX_Liquidity(USDCHF_Close, USDCHF_Volume, USDCHF_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 01, 07, 0))\n", | |
"USDSEK_liquidity = FX_Liquidity(USDSEK_Close, USDSEK_Volume, USDSEK_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 01, 07, 0))\n", | |
"\n", | |
"Benchmark_volatility = FX_Volatility(Dollar_Index_ClosePrice, Index_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 01, 07, 0))\n", | |
"Benchmark_liquidity = FX_Liquidity(Dollar_Index_ClosePrice, Dollar_Index_Volume, Index_Time, datetime(2014, 01, 01, 23, 0), datetime(2015, 04, 01, 07, 0))\n", | |
"print Benchmark_volatility\n", | |
"print Benchmark_liquidity\n", | |
"print \n", | |
"\n", | |
"board = [[\"FX pair \", \"Volatility\", \"Liquidity\"],\n", | |
"\t\t [\"EURUSD: \", EURUSD_volatility / Benchmark_volatility, EURUSD_liquidity],\n", | |
" [\"GBPUSD: \", GBPUSD_volatility / Benchmark_volatility, GBPUSD_liquidity],\n", | |
" [\"USDJPY: \", USDJPY_volatility / Benchmark_volatility, USDJPY_liquidity],\n", | |
" [\"USDCAD: \", USDCAD_volatility / Benchmark_volatility, USDCAD_liquidity],\n", | |
" [\"USDCHF: \", USDCHF_volatility / Benchmark_volatility, USDCHF_liquidity],\n", | |
" [\"USDSEK: \", USDSEK_volatility / Benchmark_volatility, USDSEK_liquidity]]\n", | |
"\n", | |
"for row in board:\n", | |
"\tprint row" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment