Want more features on Pastebin? Sign Up, it's FREE!

From tick by tick data to candlestick

By: Femto_Trader on Sep 9th, 2012  |  syntax: Python  |  size: 1.47 KB  |  views: 188  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2.  
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from matplotlib.finance import candlestick
  7. from datetime import *
  8.  
  9. def conv_str_to_datetime(x):
  10.     return(datetime.strptime(x, '%Y%m%d %H:%M:%S.%f'))
  11.  
  12. df = pd.read_csv('test_EURUSD/EURUSD-2012-07.csv', names=['Symbol', 'Date_Time', 'Bid', 'Ask'], index_col=1, parse_dates=True)
  13.  
  14. PipPosition = 4
  15. df['Spread'] = (df['Ask'] - df['Bid']) * 10**PipPosition
  16.  
  17. print(df)
  18.  
  19. print("="*10)
  20.  
  21. print(df.ix[0])
  22.  
  23. print("Bid={0}".format(df['Bid'].mean()))
  24. print("Ask={0}".format(df['Ask'].mean()))
  25. print("Spread={0}".format(df['Spread'].mean()))
  26.  
  27. grouped = df.groupby('Symbol')
  28.  
  29. ask =  grouped['Ask'].resample('5Min', how='ohlc')
  30.  
  31. bid = grouped['Bid'].resample('5Min', how='ohlc')
  32.  
  33. df2 = pd.concat([ask, bid], axis=1, keys=['Ask', 'Bid'])
  34.  
  35. print(df2)
  36.  
  37. #Date = df2.index.get_level_values(1).astype(object)
  38. #Date = pd.to_datetime(df2.index.get_level_values(1))
  39. Date = range(len(df2))
  40. #ToFix : Date !
  41. Open = df2['Bid']['open'].values
  42. Close = df2['Bid']['close'].values
  43. High = df2['Bid']['high'].values
  44. Low = df2['Bid']['low'].values
  45. Volume = np.zeros(len(df2))
  46.  
  47. DOCHLV = zip(Date, Open, Close, High, Low, Volume)
  48.  
  49. fig = plt.figure()
  50. fig.subplots_adjust(bottom=0.1)
  51. ax = fig.add_subplot(211)
  52. df['Bid'].plot()
  53. plt.title("Price graph")
  54. ax = fig.add_subplot(212)
  55. plt.title("Candlestick chart")
  56. candlestick(ax, DOCHLV, width=0.6, colorup='g', colordown='r', alpha=1.0)
  57. plt.show()
clone this paste RAW Paste Data