Advertisement
Guest User

Untitled

a guest
Jan 30th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import re, sys, os
  4. root = re.sub("src.*?$", "src",os.getcwd())
  5. sys.path.append(root)
  6. from sql.common import excecSqlWithColumns as excecute
  7.  
  8. def write_line(min_data, f):
  9. f.write(','.join([min_data["symbol"],
  10. str(min_data["datetime"]),
  11. str(min_data["open"]),
  12. str(max(min_data["high"])),
  13. str(min(min_data["low"])),
  14. str(min_data["close"])])
  15. +os.linesep)
  16.  
  17. def generate_min5_csv():
  18. generate_min_csv(min_length=5, based_on= '')
  19.  
  20. def generate_min10_csv():
  21. generate_min_csv(min_length=10, based_on='_min5')
  22.  
  23. def generate_min30_csv():
  24. generate_min_csv(min_length=30, based_on='_min10')
  25.  
  26. def generate_min60_csv():
  27. generate_min_csv(min_length=60, based_on='_min30')
  28.  
  29. def generate_min_csv(min_length, based_on):
  30. sql = "select * from USDJPY{based_on} order by datetime asc".format(based_on=based_on)
  31. min_data = {"mod":min_length}
  32. targetDir=root+"/"'out'
  33.  
  34. if not os.path.exists(targetDir):
  35. os.mkdir(targetDir)
  36.  
  37. f = open(targetDir+"/min{}.csv".format(min_length), "w")
  38.  
  39. for ohlc in excecute(sql):
  40. if ohlc['datetime'].minute % min_data["mod"] == 0:
  41. #write previous mindata into csv file
  42. if min_data.has_key("open"):
  43. min_data["symbol"] = ohlc["symbol"]
  44. write_line(min_data,f)
  45. #prep for next mindata
  46. #set new datetime
  47. min_data.update({"datetime":ohlc["datetime"]})
  48. #set new open price
  49. min_data.update({"open":ohlc["open"]})
  50. #initialize high and low price
  51. min_data.update({"high":[ohlc["high"]], "low":[ohlc["low"]]})
  52. else:
  53. min_data.update({"close": ohlc["close"]})
  54. min_data["high"].append(ohlc["high"])
  55. min_data["low"].append(ohlc["low"])
  56. f.close()
  57. mk_yml("USDJPY", 'min{}'.format(min_length))
  58.  
  59.  
  60. def mk_yml(symbol, candle_name):
  61. with open(root+'/'+symbol+'_'+candle_name+'.yml', 'w') as f:
  62. body = """in:
  63. type: file
  64. path_prefix: "out/candle_name"
  65. parser:
  66. charset: UTF-8
  67. newline: CRLF
  68. type: csv
  69. delimiter: ','
  70. skip_header_lines: 0
  71. trim_if_not_quoted: false
  72. allow_extra_columns: false
  73. allow_optional_columns: false
  74. quote: '"'
  75. escape: '"'
  76. columns:
  77. - {name: symbol, type: string}
  78. - {name: datetime, type: timestamp, format: '%Y-%m-%d %H:%M:%S', timezone: '+0900'}
  79. - {name: open, type: double}
  80. - {name: high, type: double}
  81. - {name: low, type: double}
  82. - {name: close, type: double}
  83. out:
  84. type: mysql
  85. host: localhost
  86. user: root
  87. password: ""
  88. database: finance
  89. table: Symbol_candle_name
  90. mode: replace
  91. """.replace("Symbol",symbol).replace("candle_name", candle_name)
  92. f.write(body)
  93.  
  94. if __name__ == '__main__':
  95. # generate_min5_csv()
  96. # generate_min10_csv()
  97. # generate_min30_csv()
  98. generate_min60_csv()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement