Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 5.19 KB  |  hits: 15  |  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. #Global Vars for modification
  2. enterdays=20
  3. closedays=10
  4.  
  5. #Main Global Vars
  6. import win32com.client,datetime,os
  7. conn=None
  8.  
  9. def connect():
  10.     global conn
  11.     conn=win32com.client.Dispatch("DSAPI_Client.clientAPI")
  12.     api=conn.CreateClientAPI()
  13.     if api<0:
  14.         print(api)
  15.     return api
  16.  
  17. def forexSignal():
  18.     global conn
  19.     QBid1, QAsk1, QHi1, QLow1, ErrNumber1=range(5) #Never used but need to be declared for GetQuote
  20.     files=['marketslist.txt','shareslist.txt']
  21.     for marketList in files[:-1]:
  22.         signals=[]
  23.         try:
  24.             today=datetime.datetime.today()
  25.             past=today-datetime.timedelta(days=40) #Use 40 because weekends are not written to the file, being safe(could be optimized)
  26.             fromdate=conn.ConvertDateToDouble(past.date())
  27.             todate=conn.ConvertDateToDouble(today.date())
  28.             with open(marketList,'r') as market:
  29.                 for marketLine in market:
  30.                     #Check if weird data
  31.                     if len(marketLine.split())>1:
  32.                         with open('Errors.txt','a') as logFile:
  33.                             logFile.write(marketList,' ',marketLine)
  34.                             continue
  35.                     elif marketLine.startswith('#'):
  36.                         continue
  37.                     marketLine=marketLine.replace('\n','')
  38.                     folder='MarketData' if marketList=='marketslist.txt' else 'SharesData'
  39.                     path=os.path.join(os.getcwd(),folder,marketLine.replace('/','')+".txt")
  40.                     result1=1        
  41.                     conn.AddInstr(marketLine)
  42.                     result=conn.GetChartData(marketLine, "24", path, fromdate, todate,result1)
  43.                     if result[1]==-1:
  44.                         with open('Errors.txt','a') as logFile:
  45.                             logFile.write("File: "+marketList+" Market: "+marketLine+" not added or doesn't exist.\n")
  46.                         break
  47.                        
  48.                     #Read individual market file and grab the high and the low for last 'enterdays'
  49.                     high=-1
  50.                     low=99999
  51.                     try:
  52.                         with open(path,'r') as dataFile:
  53.                             days=1
  54.                             for line in dataFile:
  55.                                 line=line.strip()
  56.                                 #Check the line to make sure it's in the correct format
  57.                                 if line=='' or line.startswith(',') or line.find("00:00:00")!=-1:
  58.                                     continue
  59.                                 #process data
  60.                                 data=line.split(',')
  61.                                 if float(data[2])>high:
  62.                                     high=float(data[2])
  63.                                 if float(data[3])<low:
  64.                                     low=float(data[3])
  65.                                 #only grab data for 'enterdays'
  66.                                 if days==enterdays:
  67.                                     break
  68.                                 else:
  69.                                     days+=1
  70.                     except IOError as e:
  71.                         print('Either the file for',marketLine,'doesn\'t exist or there wasn\'t enough\
  72.                               data in the file')
  73.                        
  74.                     #Get current market price              
  75.                     quote=conn.GetQuote (marketLine, QBid1, QAsk1, QHi1, QLow1, ErrNumber1)                    
  76.                     shortdist,longdist=0,0
  77.                     #Calc number of decimals in quote
  78.                     temp=str(high).split('.')
  79.                     placeholder=len(temp[1])
  80.                     #Convert to integers
  81.                     temphigh=high*(10**placeholder)
  82.                     templow=low*(10**placeholder)
  83.                     QBid1=quote[0]*(10**placeholder)
  84.                     QAsk1=quote[1]*(10**placeholder)
  85.                     #Calc distances
  86.                     shortdist=int(QAsk1-templow) #Short            
  87.                     longdist=int(temphigh-QBid1) #Long
  88.                     #Add to list for sorting later, convert to strings
  89.                     signals.append([marketLine,str(high),str(low),str(longdist),str(shortdist),longdist if longdist<shortdist else shortdist])
  90.  
  91.             #Sort the signals list
  92.             signals.sort(key=lambda student: student[5])
  93.             signalFileName='marketSignal.txt' if marketList=='marketslist.txt' else 'sharesSignal.txt'
  94.             with open(signalFileName,'a') as signalFile:
  95.                 for x in signals:
  96.                     signalFile.write(x[0]+' High: '+x[1]+' Low: '+x[2]+' Long: '+x[3]+' Short: '+x[4]+'\n')
  97.         except IOError as e:
  98.             print(marketList,'not found.')
  99.  
  100.  
  101. try:
  102.     error=connect()
  103.     if error<0:
  104.         raise Exception
  105.     #Clear all files
  106.     with open('Errors.txt','w') as logFile:
  107.         logFile.flush()
  108.     with open('marketSignal.txt','w') as marketSignalFile:
  109.         marketSignalFile.flush()
  110.     with open('sharesSignal.txt','w') as sharesSignalFile:
  111.         sharesSignalFile.flush()
  112.     forexSignal()
  113. finally:
  114.     conn.DestroyClientAPI()