Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Das Skript ändert eine Dukascopy csv-File in MESZ + Sommerzeitumstellung
- und nimmt Wochenenddaten heraus.
- Die Dukascopydaten müssen im folgenden Format heruntergeladen werden:
- Time,Open,High,Low,Close,Volume
- 2014/08/01 12:15:00,137.909,137.91,137.895,137.901,167.48
- 2014/08/01 12:16:00,137.901,137.915,137.899,137.902,124.4
- Die Variable 'Asset' muss jedesmal per Hand entsprechend dem Asset
- eingegeben werden.
- Die Ausgabefile erhält den gleichen Namen mit dem Präfix 'MEZ_DST_'
- '''
- import os
- import csv
- import datetime
- import tkFileDialog
- # Namen der Assetklasse hier eingeben!!
- Asset = 'NZDUSD'
- # Function which shifts date & time by h hours
- def shiftdata(h):
- global newRow, myDateTime
- myDateTime = myDateTime + datetime.timedelta(hours=h)
- newRow[1] = myDateTime.strftime('%Y%m%d')
- newRow[2] = myDateTime.strftime('%H%M%S')
- # Function which returns True if date is within summertime
- def summertime(myDateTime):
- year = myDateTime.strftime('%Y')
- MarchSwitchDay = datetime.datetime.strptime(year+'0401', '%Y%m%d')
- OctSwitchDay = datetime.datetime.strptime(year+'1101', '%Y%m%d')
- # Calculate Switchingday in March (last Sunday of March)
- while 1:
- MarchSwitchDay -= datetime.timedelta(days=1)
- if MarchSwitchDay.weekday() == 6:
- break
- # Calculate Switchingday in October (last Sunday of October)
- while 1:
- OctSwitchDay -= datetime.timedelta(days=1)
- if OctSwitchDay.weekday() == 6:
- break
- # Return True if current date is within summer period
- return MarchSwitchDay <= myDateTime < OctSwitchDay
- # Get csv-file via dialog and create output filename and path
- sourceFile = tkFileDialog.askopenfilename()
- myPath, fileName = os.path.split(sourceFile)
- outFile = os.path.join(myPath, "MEZ_DST_"+fileName)
- try:
- print "Please wait..."
- with open(sourceFile, 'rb') as inputFile, open(outFile, 'wb') as\
- outputFile:
- inputReader = csv.reader(inputFile)
- myFileWriter = csv.writer(outputFile)
- next(inputReader) # Skip header
- header = ['<TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,\
- <CLOSE>,<VOL>']
- myFileWriter.writerow(header) # Write own header instead
- rowCount = 0 # Begin to count rows
- for row in inputReader:
- rowCount += 1
- newRow = row[:]
- #Change columns into proper MT4 format
- newRow.insert(0, Asset)
- del newRow[1:2]
- newRow.insert(1, row[0].split()[0].replace("/", ""))
- newRow.insert(2, row[0].split()[1].replace(":", ""))
- # Select Date&Time for further calculations
- dateString = newRow[1] + newRow[2]
- # Check if date & time format are valid (strptime will produce a
- # ValueError if not) and convert into datetime object
- myDateTime = datetime.datetime.strptime(dateString, '%Y%m%d%H%M%S')
- # Shift data
- if summertime(myDateTime):
- shiftdata(2)
- else: shiftdata(1)
- # Skip weekend data
- if myDateTime.weekday() in (5, 6):
- continue
- # Write shifted row into Ouputfile
- myFileWriter.writerow(newRow)
- print "Everything went fine!"
- except ValueError:
- print newRow
- print "Fehler in Zeile {}: {} {} entspricht nicht der notwendigen \
- Formatierung!".format(rowCount, newRow[1], newRow[2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement