- #Global Vars for modification
- enterdays=20
- closedays=10
- #Main Global Vars
- import win32com.client,datetime,os
- conn=None
- def connect():
- global conn
- conn=win32com.client.Dispatch("DSAPI_Client.clientAPI")
- api=conn.CreateClientAPI()
- if api<0:
- print(api)
- return api
- def forexSignal():
- global conn
- QBid1, QAsk1, QHi1, QLow1, ErrNumber1=range(5) #Never used but need to be declared for GetQuote
- files=['marketslist.txt','shareslist.txt']
- for marketList in files[:-1]:
- signals=[]
- try:
- today=datetime.datetime.today()
- past=today-datetime.timedelta(days=40) #Use 40 because weekends are not written to the file, being safe(could be optimized)
- fromdate=conn.ConvertDateToDouble(past.date())
- todate=conn.ConvertDateToDouble(today.date())
- with open(marketList,'r') as market:
- for marketLine in market:
- #Check if weird data
- if len(marketLine.split())>1:
- with open('Errors.txt','a') as logFile:
- logFile.write(marketList,' ',marketLine)
- continue
- elif marketLine.startswith('#'):
- continue
- marketLine=marketLine.replace('\n','')
- folder='MarketData' if marketList=='marketslist.txt' else 'SharesData'
- path=os.path.join(os.getcwd(),folder,marketLine.replace('/','')+".txt")
- result1=1
- conn.AddInstr(marketLine)
- result=conn.GetChartData(marketLine, "24", path, fromdate, todate,result1)
- if result[1]==-1:
- with open('Errors.txt','a') as logFile:
- logFile.write("File: "+marketList+" Market: "+marketLine+" not added or doesn't exist.\n")
- break
- #Read individual market file and grab the high and the low for last 'enterdays'
- high=-1
- low=99999
- try:
- with open(path,'r') as dataFile:
- days=1
- for line in dataFile:
- line=line.strip()
- #Check the line to make sure it's in the correct format
- if line=='' or line.startswith(',') or line.find("00:00:00")!=-1:
- continue
- #process data
- data=line.split(',')
- if float(data[2])>high:
- high=float(data[2])
- if float(data[3])<low:
- low=float(data[3])
- #only grab data for 'enterdays'
- if days==enterdays:
- break
- else:
- days+=1
- except IOError as e:
- print('Either the file for',marketLine,'doesn\'t exist or there wasn\'t enough\
- data in the file')
- #Get current market price
- quote=conn.GetQuote (marketLine, QBid1, QAsk1, QHi1, QLow1, ErrNumber1)
- shortdist,longdist=0,0
- #Calc number of decimals in quote
- temp=str(high).split('.')
- placeholder=len(temp[1])
- #Convert to integers
- temphigh=high*(10**placeholder)
- templow=low*(10**placeholder)
- QBid1=quote[0]*(10**placeholder)
- QAsk1=quote[1]*(10**placeholder)
- #Calc distances
- shortdist=int(QAsk1-templow) #Short
- longdist=int(temphigh-QBid1) #Long
- #Add to list for sorting later, convert to strings
- signals.append([marketLine,str(high),str(low),str(longdist),str(shortdist),longdist if longdist<shortdist else shortdist])
- #Sort the signals list
- signals.sort(key=lambda student: student[5])
- signalFileName='marketSignal.txt' if marketList=='marketslist.txt' else 'sharesSignal.txt'
- with open(signalFileName,'a') as signalFile:
- for x in signals:
- signalFile.write(x[0]+' High: '+x[1]+' Low: '+x[2]+' Long: '+x[3]+' Short: '+x[4]+'\n')
- except IOError as e:
- print(marketList,'not found.')
- try:
- error=connect()
- if error<0:
- raise Exception
- #Clear all files
- with open('Errors.txt','w') as logFile:
- logFile.flush()
- with open('marketSignal.txt','w') as marketSignalFile:
- marketSignalFile.flush()
- with open('sharesSignal.txt','w') as sharesSignalFile:
- sharesSignalFile.flush()
- forexSignal()
- finally:
- conn.DestroyClientAPI()