Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import re, sys, os
- root = re.sub("src.*?$", "src",os.getcwd())
- sys.path.append(root)
- from sql.common import excecSqlWithColumns as excecute
- def write_line(min_data, f):
- f.write(','.join([min_data["symbol"],
- str(min_data["datetime"]),
- str(min_data["open"]),
- str(max(min_data["high"])),
- str(min(min_data["low"])),
- str(min_data["close"])])
- +os.linesep)
- def generate_min5_csv():
- generate_min_csv(min_length=5, based_on= '')
- def generate_min10_csv():
- generate_min_csv(min_length=10, based_on='_min5')
- def generate_min30_csv():
- generate_min_csv(min_length=30, based_on='_min10')
- def generate_min60_csv():
- generate_min_csv(min_length=60, based_on='_min30')
- def generate_min_csv(min_length, based_on):
- sql = "select * from USDJPY{based_on} order by datetime asc".format(based_on=based_on)
- min_data = {"mod":min_length}
- targetDir=root+"/"'out'
- if not os.path.exists(targetDir):
- os.mkdir(targetDir)
- f = open(targetDir+"/min{}.csv".format(min_length), "w")
- for ohlc in excecute(sql):
- if ohlc['datetime'].minute % min_data["mod"] == 0:
- #write previous mindata into csv file
- if min_data.has_key("open"):
- min_data["symbol"] = ohlc["symbol"]
- write_line(min_data,f)
- #prep for next mindata
- #set new datetime
- min_data.update({"datetime":ohlc["datetime"]})
- #set new open price
- min_data.update({"open":ohlc["open"]})
- #initialize high and low price
- min_data.update({"high":[ohlc["high"]], "low":[ohlc["low"]]})
- else:
- min_data.update({"close": ohlc["close"]})
- min_data["high"].append(ohlc["high"])
- min_data["low"].append(ohlc["low"])
- f.close()
- mk_yml("USDJPY", 'min{}'.format(min_length))
- def mk_yml(symbol, candle_name):
- with open(root+'/'+symbol+'_'+candle_name+'.yml', 'w') as f:
- body = """in:
- type: file
- path_prefix: "out/candle_name"
- parser:
- charset: UTF-8
- newline: CRLF
- type: csv
- delimiter: ','
- skip_header_lines: 0
- trim_if_not_quoted: false
- allow_extra_columns: false
- allow_optional_columns: false
- quote: '"'
- escape: '"'
- columns:
- - {name: symbol, type: string}
- - {name: datetime, type: timestamp, format: '%Y-%m-%d %H:%M:%S', timezone: '+0900'}
- - {name: open, type: double}
- - {name: high, type: double}
- - {name: low, type: double}
- - {name: close, type: double}
- out:
- type: mysql
- host: localhost
- user: root
- password: ""
- database: finance
- table: Symbol_candle_name
- mode: replace
- """.replace("Symbol",symbol).replace("candle_name", candle_name)
- f.write(body)
- if __name__ == '__main__':
- # generate_min5_csv()
- # generate_min10_csv()
- # generate_min30_csv()
- generate_min60_csv()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement