def execute_results(self,
                        rootlocalforfilespulled,
                        #onlyoutofthemoney, # onlyoutofthemoney 1 for yes, 0 for no
                        maxvalueatrisk, 
                        maxsellearlyprice, # MaxSellEarlyPrice
                        minbuylaterprice, # MinBuyLaterPrice
                        minpairspreadpercent, # MinimumSpreadPercentage
                        maxpairspreadpercent, # MinimumSpreadPercentage
                        maxbidaskspreadpercentagesell,
                        maxbidaskspreadpercentagebuy,    
                        minopeninterest,
                        earningsdatestring,
                        directorylocaloutput,
                        showresults):
 #       import mytools
        downloaddirectorylocal = rootlocalforfilespulled #mytools.mystrings.appendnormaldateddirectorybasedoncurrenttime15(rootlocalforfilespulled)
        print('-- downloaddirectorylocal set to ' + downloaddirectorylocal)
        
        import readintomemorybuilddictionaryofpairsdictionariesbysymbol        
        o = readintomemorybuilddictionaryofpairsdictionariesbysymbol.read(downloaddirectorylocal,showresults)
        
        dDictionaryOfPairsDictionariesBySymbol = o.DictionaryOfPairsDictionariesBySymbol        
#        dTestValueAndPairTupleSortableByPairSpreadPct = {}
#        dPairsValid = {}
        dCalendarPairs = {}
        dQualifiedPairsBasedOnAllCriteriaProvided = {}
        bContinue = 1   
        for kSymbol,vPairsDictionary in dDictionaryOfPairsDictionariesBySymbol.items():
            dPairs  = vPairsDictionary
            #if showresults == 1:
            print('-- ' + kSymbol + ' building valid pairs... ')
            bContinue == 1            
            for k,ls in dPairs.items():
                earlier = ls[0]
                later = ls[1]
                bContinue == 1
                #-- Make sure EarlierExpirationIsBefore and LaterExpirationIsAfter
                if len(earningsdatestring) > 0:
                    bContinue == 0
                    from datetime import datetime
                    earningsdate = datetime.strptime(earningsdatestring, '%Y-%m-%d')
                    print('earningsdate= ' + str(earningsdate))
                    if earlier.expirationdate <= earningsdate and later.expirationdate > earningsdate:
                        bContinue == 1
                if bContinue == 1:
                    dCalendarPairs[len(dCalendarPairs)] = ls
                    #-- Make sure onlyoutofthemoney is respected
#                    if onlyoutofthemoney == 1:
#                        bContinue == 0
                    if (earlier.optiontype == 'C' and float(earlier.stockprice) < float(earlier.strike)) \
                        or (earlier.optiontype == 'P' and float(earlier.stockprice) > float(earlier.strike)):
                            #print('passed',earlier.optiontype ,earlier.stockprice,earlier.strike)
#                            bContinue == 1
#                    print( bContinue,earlier.optiontype ,earlier.stockprice,earlier.strike)
#                    if bContinue == 1:
                        #-- Set your MaxSellEarlyPrice
                        
                            #-- check bid-ask prices are numeric
                            if earlier.bid.replace('.','',1).isdigit() and later.ask.replace('.','',1).isdigit():
                                if later.bid.replace('.','',1).isdigit() and earlier.ask.replace('.','',1).isdigit():
                                    #-- check earlier bid <= maxsellearlyprice
                                    if float(earlier.bid) <= maxsellearlyprice:
                                        #-- Set your MinBuyLaterPrice    
                                        if float(later.ask) > minbuylaterprice:
                                            if float(earlier.bid) > 0.0 and float(later.ask) > 0.0 and float(later.bid) > 0:                        
                                            #-- Make sure MinimumSpreadPercentage is greater than
                                                if float(earlier.bid)/float(later.ask) > minpairspreadpercent:
                                                    #-- Make sure MaximumSpreadPercentage is less than
                                                    if float(earlier.bid)/float(later.ask) <= maxpairspreadpercent:
                                                        if float(maxvalueatrisk) >= -float(earlier.bid)+float(later.ask):
                                                            if (float(earlier.ask) - float(earlier.bid)) / float(earlier.bid) <= float(maxbidaskspreadpercentagesell):
                                                                if (float(later.ask) - float(later.bid)) / float(later.bid) <= float(maxbidaskspreadpercentagebuy):
                                                                    if int(earlier.openinterest) > minopeninterest and int(later.openinterest) > minopeninterest:
                                                                        dQualifiedPairsBasedOnAllCriteriaProvided[len(dQualifiedPairsBasedOnAllCriteriaProvided)] = ls
#                                            dTestValueAndPairTupleSortableByPairSpreadPct[len(dTestValueAndPairTupleSortableByPairSpreadPct)] = [float(earlier.bid)/float(later.ask),ls]
                                            #dPairsValid[len(dPairsValid)] = ls
#                                            if showresults == 1:
#                                                print(str(len(dTestValueAndPairTupleSortableByPairSpreadPct)) + ' valid pairs...')
#        print(str(len(dCalendarPairs)) + ' qualified pairs based on straddling expiration date')
#        print('sorting results...')            
#        from collections import OrderedDict
#        dOrdered = OrderedDict(sorted(dTestValueAndPairTupleSortableByPairSpreadPct.items(), key=lambda t: t[1][0]))
        self.DictionaryOfAllCalendarPairs = dCalendarPairs
        self.DictionaryOfFilteredCalendarPairs = dQualifiedPairsBasedOnAllCriteriaProvided
    def execute_results(self,
                        rootlocalforfilespulled,
                        #onlyoutofthemoney, # onlyoutofthemoney 1 for yes, 0 for no
                        maxvalueatrisk, 
                        maxsellearlyprice, # MaxSellEarlyPrice
                        minbuylaterprice, # MinBuyLaterPrice
                        minspreadpercent, # MinimumSpreadPercentage
                        maxspreadpercent, # MinimumSpreadPercentage
                        earningsdatestring,
                        directorylocaloutput,
                        showresults):
 #       import mytools
        downloaddirectorylocal = rootlocalforfilespulled #mytools.mystrings.appendnormaldateddirectorybasedoncurrenttime15(rootlocalforfilespulled)
        print('downloaddirectorylocal set to ' + downloaddirectorylocal + ' within class buildpairsdictionarywithcriteriafromdirectoryofpulledfiles.py')
        
        import readintomemorybuilddictionaryofpairsdictionariesbysymbol        
        o = readintomemorybuilddictionaryofpairsdictionariesbysymbol.read(downloaddirectorylocal,showresults)
        
        dDictionaryOfPairsDictionariesBySymbol = o.DictionaryOfPairsDictionariesBySymbol
        
#        dTestValueAndPairTupleSortableByPairSpreadPct = {}
#        dPairsValid = {}
        

        
        dCalendarPairs = {}
        dQualifiedPairsBasedOnAllCriteriaProvided = {}
        bContinue = 1   
        for kSymbol,vPairsDictionary in dDictionaryOfPairsDictionariesBySymbol.items():
            dPairs  = vPairsDictionary
            #if showresults == 1:
            print('building valid pairs... ' + kSymbol)
            bContinue == 1            
            for k,ls in dPairs.items():
                earlier = ls[0]
                later = ls[1]
                bContinue == 1
                #-- Make sure EarlierExpirationIsBefore and LaterExpirationIsAfter
                if len(earningsdatestring) > 0:
                    bContinue == 0
                    from datetime import datetime
                    earningsdate = datetime.strptime(earningsdatestring, '%Y-%m-%d')
                    print('earningsdate= ' + str(earningsdate))
                    if earlier.expirationdate <= earningsdate and later.expirationdate > earningsdate:
                        bContinue == 1
                if bContinue == 1:
                    dCalendarPairs[len(dCalendarPairs)] = ls
                    #-- Make sure onlyoutofthemoney is respected
#                    if onlyoutofthemoney == 1:
#                        bContinue == 0
                    if (earlier.optiontype == 'C' 
                        and float(earlier.stockprice) < float(earlier.strike)) 
                        or 
                        (earlier.optiontype == 'P' 
                        and float(earlier.stockprice) > float(earlier.strike)):
                            #print('passed',earlier.optiontype ,earlier.stockprice,earlier.strike)
#                            bContinue == 1
#                    print( bContinue,earlier.optiontype ,earlier.stockprice,earlier.strike)
#                    if bContinue == 1:
                        #-- Set your MaxSellEarlyPrice
                        if earlier.bid.replace('.','',1).isdigit() and float(earlier.bid) <= maxsellearlyprice:
                            #-- Set your MinBuyLaterPrice    
                            if later.ask.replace('.','',1).isdigit() and float(later.ask) > minbuylaterprice:
                                #-- Make sure MinimumSpreadPercentage is greater than
                                if float(earlier.bid)/float(later.ask) > minspreadpercent:
                                    #-- Make sure MaximumSpreadPercentage is less than
                                    if float(earlier.bid)/float(later.ask) <= maxspreadpercent:
                                        if float(maxvalueatrisk) >= -float(earlier.bid)+float(later.ask):
                                            dQualifiedPairsBasedOnAllCriteriaProvided[len(dQualifiedPairsBasedOnAllCriteriaProvided)] = ls
    def execute_results(self,pathfilelocalsymbols,pathfilelocalexpirations,rootlocalforfilespulled,earningsdatestring,directorylocaloutput,showresults):
        import mytools
        downloaddirectorylocal = rootlocalforfilespulled #mytools.mystrings.appendnormaldateddirectorybasedoncurrenttime15(rootlocalforfilespulled)
        print('downloaddirectorylocal=' + downloaddirectorylocal)
        
        import readintomemorybuilddictionaryofpairsdictionariesbysymbol        
        o = readintomemorybuilddictionaryofpairsdictionariesbysymbol.read(downloaddirectorylocal)
        
        dDictionaryOfPairsDictionariesBySymbol = o.DictionaryOfPairsDictionariesBySymbol
        
        dPairsCalculated = {}
        dPairsValid = {}
        
        from datetime import datetime
        earningsdate = datetime.strptime(earningsdatestring, '%Y-%m-%d')
        print('earningsdate= ' + str(earningsdate))
        
        dQualifiedPairsBasedOnDate = {}
        for kSymbol,vPairsDictionary in dDictionaryOfPairsDictionariesBySymbol.items():
            dPairs  = vPairsDictionary
            #if showresults == 1:
            print('building valid pairs... ' + kSymbol)
                
            for k,ls in dPairs.items():
                earlier = ls[0]
                later = ls[1]
                #print(earlier.expirationdate,earningsdate)
                if earlier.expirationdate < earningsdate and later.expirationdate > earningsdate:
                    dQualifiedPairsBasedOnDate[len(dQualifiedPairsBasedOnDate)] = ls
                    if earlier.bid.replace('.','',1).isdigit(): #and float(earlier.bid) >= 0.25:
                        #if float(earlier.bid) <= 4.0:
                            if (earlier.optiontype == 'C' and float(earlier.stockprice) < float(earlier.strike)) or (earlier.optiontype == 'P' and float(earlier.stockprice) > float(earlier.strike)): 
                                #------------------------------------------
                                #-- Set your Percentage cutoff here
                                #------------------------------------------
                                if later.ask.replace('.','',1).isdigit() and float(later.ask) > 0.0:
                                    if float(earlier.bid)/float(later.ask) > 0.0:
                                        #if float(earlier.bid)/float(later.ask) <= 1.0:
                                        dPairsCalculated[len(dPairsCalculated)] = float(earlier.bid)/float(later.ask)
                                        dPairsValid[len(dPairsValid)] = ls
                                        if showresults == 1:
                                            print(str(len(dPairsCalculated)) + ' valid pairs...')
        print(str(len(dQualifiedPairsBasedOnDate)) + ' qualified pairs based on straddling expiration date')
        print('sorting results...')            
        from collections import OrderedDict
        dOrdered = OrderedDict(sorted(dPairsCalculated.items(), key=lambda t: t[1]))
        print(str(len(dOrdered))+ ' items in Ordered dictionary')
        outputlines = {}
        
        #if showresults == 1:
        print('putting results into printable dictionary ' + str(len(dOrdered)) + ' lines')
        from datetime import datetime 
        for k1,v1 in dOrdered.items():
            #ls = list(dPairs.keys())[list(dPairs.values()).index(k1)]
            ls = dPairsValid.get(k1)
            earlier = ls[0]
            later = ls[1]
            outputlines[len(outputlines)]=earlier.optionsymbol + ',' + \
                later.optionsymbol + ',' + \
                "'" + str(earlier.strike) + '/' + str(later.strike) + ',' + \
                later.optiontype + ',' + \
                str(earlier.expirationdate-datetime.today()) + ',' + \
                str(later.expirationdate-earlier.expirationdate) + ',' + \
                str(float(earlier.strike) - float(later.strike)) + ',' + \
                str(later.stockprice) + ',' + \
                str(earlier.bid) + ',' + \
                str(later.ask) + ',' + '{percent:.0%}'.format(percent=float(earlier.bid)/float(later.ask)) + ',' + \
                str(float(later.ask)-float(earlier.bid)) + ',' + \
                str(later.inthemoney)
            
        mytools.general.make_sure_path_exists(directorylocaloutput)
        
        datetime14 = mytools.mystrings.ConvertDatetime14()
        print(datetime14)
        outputfilepath = directorylocaloutput + '\\calendarspreads ' + datetime14 + '.csv'
        #if showresults == 1:
        print('printing results to ' + outputfilepath)
            
        with open(outputfilepath, 'w') as f:
            for outputline in outputlines.values():
                f.write(outputline+'\n')

        #if showresults == 1:
        print('Finished executing calendarspreadslive...')
        self.OutputFilePathString = outputfilepath
    def execute_results(self,pathfilelocalsymbols,pathfilelocalexpirations,rootlocalforfilespulled,
                        #onlyoutofthemoney, # onlyoutofthemoney 1 for yes, 0 for no
                        maxvalueatrisk, 
                        maxsellearlyprice, # MaxSellEarlyPrice
                        minbuylaterprice, # MinBuyLaterPrice
                        minspreadpercent, # MinimumSpreadPercentage
                        maxspreadpercent, # MinimumSpreadPercentage
                        earningsdatestring,
                        directorylocaloutput,
                        showresults):
        import mytools
        downloaddirectorylocal = rootlocalforfilespulled #mytools.mystrings.appendnormaldateddirectorybasedoncurrenttime15(rootlocalforfilespulled)
        print('downloaddirectorylocal=' + downloaddirectorylocal)
        
        import readintomemorybuilddictionaryofpairsdictionariesbysymbol        
        o = readintomemorybuilddictionaryofpairsdictionariesbysymbol.read(downloaddirectorylocal)
        
        dDictionaryOfPairsDictionariesBySymbol = o.DictionaryOfPairsDictionariesBySymbol
        
        dPairsCalculated = {}
        dPairsValid = {}
        

        
        dQualifiedPairsBasedOnDate = {}
        bContinue = 1   
        for kSymbol,vPairsDictionary in dDictionaryOfPairsDictionariesBySymbol.items():
            dPairs  = vPairsDictionary
            #if showresults == 1:
            print('building valid pairs... ' + kSymbol)
            bContinue == 1            
            for k,ls in dPairs.items():
                earlier = ls[0]
                later = ls[1]
                bContinue == 1
                #-- Make sure EarlierExpirationIsBefore and LaterExpirationIsAfter
                if len(earningsdatestring) > 0:
                    bContinue == 0
                    from datetime import datetime
                    earningsdate = datetime.strptime(earningsdatestring, '%Y-%m-%d')
                    print('earningsdate= ' + str(earningsdate))
                    if earlier.expirationdate <= earningsdate and later.expirationdate > earningsdate:
                        bContinue == 1
                if bContinue == 1:
                    dQualifiedPairsBasedOnDate[len(dQualifiedPairsBasedOnDate)] = ls
                    #-- Make sure onlyoutofthemoney is respected
#                    if onlyoutofthemoney == 1:
#                        bContinue == 0
                    if (earlier.optiontype == 'C' and float(earlier.stockprice) < float(earlier.strike)) or (earlier.optiontype == 'P' and float(earlier.stockprice) > float(earlier.strike)):
                            #print('passed',earlier.optiontype ,earlier.stockprice,earlier.strike)
#                            bContinue == 1
#                    print( bContinue,earlier.optiontype ,earlier.stockprice,earlier.strike)
#                    if bContinue == 1:
                        #-- Set your MaxSellEarlyPrice
                        if earlier.bid.replace('.','',1).isdigit() and float(earlier.bid) <= maxsellearlyprice:
                            #-- Set your MinBuyLaterPrice    
                            if later.ask.replace('.','',1).isdigit() and float(later.ask) > minbuylaterprice:
                                #-- Make sure MinimumSpreadPercentage is greater than
                                if float(earlier.bid)/float(later.ask) > minspreadpercent:
                                    #-- Make sure MaximumSpreadPercentage is less than
                                    if float(earlier.bid)/float(later.ask) <= maxspreadpercent:
                                        if float(maxvalueatrisk) >= -float(earlier.bid)+float(later.ask):
                                            dPairsCalculated[len(dPairsCalculated)] = float(earlier.bid)/float(later.ask)
                                            dPairsValid[len(dPairsValid)] = ls
                                            if showresults == 1:
                                                print(str(len(dPairsCalculated)) + ' valid pairs...')
        print(str(len(dQualifiedPairsBasedOnDate)) + ' qualified pairs based on straddling expiration date')
        print('sorting results...')            
        from collections import OrderedDict
        dOrdered = OrderedDict(sorted(dPairsCalculated.items(), key=lambda t: t[1]))
        print(str(len(dOrdered))+ ' items in Ordered dictionary')
        outputlines = {}
        outputlines[len(outputlines)]='earlier.optionsymbol' + ',' + \
                'later.optionsymbol' + ',' + \
                'earlier.strike' + '/' + 'later.strike' + ',' + \
                'later.optiontype' + ',' + \
                'earlier.expirationdate-datetime.today()' + ',' + 'remainder' + ',' + \
                'later.expirationdate-earlier.expirationdate' + ',' + 'remainder' + ',' + \
                'earlier.strike - later.strike' + ',' + \
                'later.stockprice' + ',' + \
                'earlier.bid' + ',' + \
                'later.ask' + ',' + 'percent earlier.bid/later.ask' + ',' + \
                'later.ask-earlier.bid' + ',' + \
                'later.inthemoney'
        #if showresults == 1:
        print('putting results into printable dictionary ' + str(len(dOrdered)) + ' lines')
        from datetime import datetime 
        for k1,v1 in dOrdered.items():
            #ls = list(dPairs.keys())[list(dPairs.values()).index(k1)]
            ls = dPairsValid.get(k1)
            earlier = ls[0]
            later = ls[1]
            outputlines[len(outputlines)]=earlier.optionsymbol + ',' + \
                later.optionsymbol + ',' + \
                "'" + str(earlier.strike) + '/' + str(later.strike) + ',' + \
                later.optiontype + ',' + \
                str(earlier.expirationdate-datetime.today()) + ',' + \
                str(later.expirationdate-earlier.expirationdate) + ',' + \
                str(float(earlier.strike) - float(later.strike)) + ',' + \
                str(later.stockprice) + ',' + \
                str(earlier.bid) + ',' + \
                str(later.ask) + ',' + '{percent:.0%}'.format(percent=float(earlier.bid)/float(later.ask)) + ',' + \
                str(float(later.ask)-float(earlier.bid)) + ',' + \
                str(later.inthemoney)
            
        mytools.general.make_sure_path_exists(directorylocaloutput)
        
        datetime14 = mytools.mystrings.ConvertDatetime14()
        print(datetime14)
        outputfilepath = directorylocaloutput + '\\calendarspreads ' + datetime14 + '.csv'
        #if showresults == 1:
        print('printing results to ' + outputfilepath)
            
        with open(outputfilepath, 'w') as f:
            for outputline in outputlines.values():
                f.write(outputline+'\n')

        #if showresults == 1:
        print('Finished executing calendarspreadslive...')
        self.OutputFilePathString = outputfilepath
# -*- coding: utf-8 -*-
"""
Created on Sun Feb  1 18:30:53 2015

@author: jmalinchak
"""

import readintomemorybuilddictionaryofpairsdictionariesbysymbol
o = readintomemorybuilddictionaryofpairsdictionariesbysymbol.read('C:\\Documents and Settings\\jmalinchak\\My Documents\\My Python\\Active\\py\\downloads\\2015-02-04\\09\\45\\YHOO',1)
print(o.DictionaryOfSymbols)