Advertisement
Guest User

Untitled

a guest
Nov 10th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. '''
  2. Das Skript ändert eine Dukascopy csv-File in MESZ + Sommerzeitumstellung
  3. und nimmt Wochenenddaten heraus.
  4.  
  5. Die Dukascopydaten müssen im folgenden Format heruntergeladen werden:
  6. Time,Open,High,Low,Close,Volume
  7. 2014/08/01 12:15:00,137.909,137.91,137.895,137.901,167.48
  8. 2014/08/01 12:16:00,137.901,137.915,137.899,137.902,124.4
  9.  
  10. Die Variable 'Asset' muss jedesmal per Hand entsprechend dem Asset
  11. eingegeben werden.
  12.  
  13. Die Ausgabefile erhält den gleichen Namen mit dem Präfix 'MEZ_DST_'
  14. '''
  15.  
  16. import os
  17. import csv
  18. import datetime
  19. import tkFileDialog
  20.  
  21. # Namen der Assetklasse hier eingeben!!
  22. Asset = 'NZDUSD'
  23.  
  24.  
  25. # Function which shifts date & time by h hours
  26. def shiftdata(h):
  27.     global newRow, myDateTime
  28.     myDateTime = myDateTime + datetime.timedelta(hours=h)
  29.     newRow[1] = myDateTime.strftime('%Y%m%d')
  30.     newRow[2] = myDateTime.strftime('%H%M%S')
  31.  
  32.  
  33. # Function which returns True if date is within summertime
  34. def summertime(myDateTime):
  35.     year = myDateTime.strftime('%Y')
  36.     MarchSwitchDay = datetime.datetime.strptime(year+'0401', '%Y%m%d')
  37.     OctSwitchDay = datetime.datetime.strptime(year+'1101', '%Y%m%d')
  38.     # Calculate Switchingday in March (last Sunday of March)
  39.     while 1:
  40.         MarchSwitchDay -= datetime.timedelta(days=1)
  41.         if MarchSwitchDay.weekday() == 6:
  42.             break
  43.     # Calculate Switchingday in October (last Sunday of October)
  44.     while 1:
  45.         OctSwitchDay -= datetime.timedelta(days=1)
  46.         if OctSwitchDay.weekday() == 6:
  47.             break
  48.     # Return True if current date is within summer period
  49.     return MarchSwitchDay <= myDateTime < OctSwitchDay
  50.  
  51.  
  52. # Get csv-file via dialog and create output filename and path
  53. sourceFile = tkFileDialog.askopenfilename()
  54. myPath, fileName = os.path.split(sourceFile)
  55. outFile = os.path.join(myPath, "MEZ_DST_"+fileName)
  56.  
  57. try:
  58.     print "Please wait..."
  59.  
  60.     with open(sourceFile, 'rb') as inputFile, open(outFile, 'wb') as\
  61.          outputFile:
  62.  
  63.         inputReader = csv.reader(inputFile)
  64.         myFileWriter = csv.writer(outputFile)
  65.  
  66.         next(inputReader) # Skip header
  67.         header = ['<TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,\
  68. <CLOSE>,<VOL>']
  69.         myFileWriter.writerow(header) # Write own header instead
  70.  
  71.         rowCount = 0 # Begin to count rows
  72.          
  73.         for row in inputReader:
  74.             rowCount += 1
  75.             newRow = row[:]
  76.  
  77.             #Change columns into proper MT4 format
  78.             newRow.insert(0, Asset)
  79.             del newRow[1:2]
  80.             newRow.insert(1, row[0].split()[0].replace("/", ""))
  81.             newRow.insert(2, row[0].split()[1].replace(":", ""))
  82.  
  83.             # Select Date&Time for further calculations
  84.             dateString = newRow[1] + newRow[2]
  85.  
  86.             # Check if date & time format are valid (strptime will produce a
  87.             # ValueError if not) and convert into datetime object
  88.             myDateTime = datetime.datetime.strptime(dateString, '%Y%m%d%H%M%S')
  89.  
  90.             # Shift data
  91.             if summertime(myDateTime):
  92.                 shiftdata(2)
  93.             else: shiftdata(1)
  94.            
  95.             # Skip weekend data
  96.             if myDateTime.weekday() in (5, 6):
  97.                 continue
  98.  
  99.             # Write shifted row into Ouputfile
  100.             myFileWriter.writerow(newRow)
  101.  
  102.         print "Everything went fine!"
  103.  
  104. except ValueError:
  105.     print newRow
  106.     print "Fehler in Zeile {}: {} {} entspricht nicht der notwendigen \
  107. Formatierung!".format(rowCount, newRow[1], newRow[2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement