def RunAStrategy(self):
        theStartDate = datetime.datetime.strptime(self.StartDate, '%Y-%m-%d')
        theEndDate = datetime.datetime.strptime(self.EndDate, '%Y-%m-%d')
        currentDate = theStartDate
        while currentDate <= theEndDate:
            if len(self.CurrentList) < self.StockNumber:
                BuyRes = self.GetBuyList(currentDate.strftime('%Y-%m-%d'))
                for anitem in BuyRes:
                    self.BuyAStock(currentDate.strftime('%Y-%m-%d'), anitem[0],
                                   anitem[1])
                    if len(self.CurrentList) == self.StockNumber:
                        break
            removed = []
            for i in range(len(self.CurrentList)):
                asymbol = self.CurrentList.keys()[i]
                [SellOrNot,
                 SellPrice] = self.SellNow(asymbol,
                                           currentDate.strftime('%Y-%m-%d'))
                if SellOrNot:
                    self.SellAStock(asymbol, SellPrice,
                                    currentDate.strftime('%Y-%m-%d'))
                    removed.append(asymbol)
            for anitem in removed:
                del self.CurrentList[anitem]
            currentDate += timedelta(days=1)

        thecurrentdata = MyPredictDB.GetAllData(self.EndDate, 1)
        totalmoney = 0
        for anitem in self.CurrentList:
            thedata = self.CurrentList[anitem]
            totalnum = 0
            for adata in thedata:
                totalnum += adata[0]
            print('We are holding ' + str(totalnum) + ' ' + anitem)
            print('The current price is ' + str(thecurrentdata[anitem][2][0]))
            themoney = totalnum * thecurrentdata[anitem][2][0]
            totalmoney += themoney
        print('we have remain cash:' + str(self.RemainMoney))
        print('we have total money:' + str(self.RemainMoney + totalmoney))
        print('Percent is ' +
              str((self.RemainMoney + totalmoney) / self.InitMoney))
示例#2
0
def PrepareLearningData(StockFile):
    alist = PrepareData.GetBigCompany(StockFile)
    #alist = ['aapl','aap','orcl','f','azo']
    AllData = MyPredictDB.GetAllDataFromAList(alist, '2018-02-22', 600)
    GetPicturesAndLabels(AllData,KnownDays=KnownDays,PredictDays=10,StepDays=20,UpThreshold=0.05)
示例#3
0
def PreparePredictData(StockFile,TheDate):
    '''
    @param TheDate: 2018-02-02
    '''
    ###
    #   delete the old predict files first
    ###
    thedir = './data/Stock/predict/'
    for root, subdir, files in os.walk(thedir):
        for afile in files:
            os.remove(os.path.join(thedir,afile))
            
    alist = PrepareData.GetBigCompany(StockFile)
    #alist = ['aapl']
    AllData = MyPredictDB.GetAllDataFromAList(alist, TheDate, KnownDays+1)
    plt = myplt()
    convert = mdates.strpdate2num('%Y-%m-%d %H:%M:%S')
    '''
    '''
    # get all data
    
    #BigLists = ['AAPL']
    #print BigLists
    #AllData = MyPredictDB.GetAllDataFromAList(BigLists, '2017-07-21', 400)
    TotalSymbolNumber = len(AllData)
    CurrentSymbolNumber = 0
    for asymbol in AllData:
        print(asymbol+'\t'+str(CurrentSymbolNumber)+'/'+str(TotalSymbolNumber))
        TheData = AllData[asymbol]
        dates = []
        for anitem in TheData[0]:
            dates.append(convert(anitem.strftime('%Y-%m-%d %H:%M:%S')))
        dates.reverse()
        TheData[0].reverse()
        TheData[1].reverse()
        TheData[2].reverse()
        TheData[3].reverse()
        TheData[4].reverse()
        TheData[5].reverse()
        
        TheData[1] = _fix_zero_data(TheData[1])
        TheData[2] = _fix_zero_data(TheData[2])
        TheData[3] = _fix_zero_data(TheData[3])
        TheData[4] = _fix_zero_data(TheData[4])
        
        i = 0
        TotalNum = len(dates)
        f=open(os.path.join(thedir,asymbol+'.txt'),'w')
        while i+KnownDays<TotalNum:
            KnownDates = dates[i:i+KnownDays]
            KnownOpen = TheData[1][i:i+KnownDays]
            KnownClose = TheData[2][i:i+KnownDays]
            KnownHigh = TheData[3][i:i+KnownDays]
            KnownLow = TheData[4][i:i+KnownDays]
            KnownVolume = TheData[5][i:i+KnownDays]
            savename = os.path.join(thedir,asymbol+'_'+TheData[0][i+KnownDays-1].strftime('%Y-%m-%d')+'.png')
            plt.drawData(KnownDates, KnownOpen, KnownClose,KnownHigh,KnownLow,KnownVolume,True,savename)
            lastClose = KnownClose[-1]
            
            if lastClose==0.0:
                print ('Something is wrong')
                print (asymbol)
                print (KnownClose)
                print (KnownDates)
                return
            f.write(asymbol+'_'+TheData[0][i+KnownDays-1].strftime('%Y-%m-%d')+':::1:::0.0\n')
            i+=1
        f.close()
        CurrentSymbolNumber+=1
示例#4
0
    def __init__(self,
                 InitMoney,
                 StartDate,
                 EndDate,
                 AllTimeHighPeriod=200,
                 AllTimeLowPeriod=10,
                 StockList=[],
                 StockNumber=3):
        '''
        @param AllTimeHighPeriod: one year all time high or one month all time high? the unit is days
        @param DownPercent: must >0 and <1 
        '''
        SimulationBaseClass.__init__(self, InitMoney, StartDate, EndDate,
                                     StockNumber)
        self.AllTimeLowPeriod = AllTimeLowPeriod
        self.AllList = StockList
        # Get all data
        theStartDate = datetime.datetime.strptime(self.StartDate,
                                                  '%Y-%m-%d').date()
        theEndDate = datetime.datetime.strptime(self.EndDate,
                                                '%Y-%m-%d').date()
        totalPeriod = (theEndDate - theStartDate).days
        print("Getting all data")
        self.AllData = MyPredictDB.GetAllDataFromAList(
            self.AllList, EndDate, AllTimeHighPeriod + totalPeriod)
        print("Calculating all time highs")
        self.AllTimeHigh = {}
        self.AllTimeLow = {}
        RemoveData = []
        for anitem in self.AllData:
            # get all time high
            alltimehigh = {}
            alltimelow = {}
            TheStockData = self.AllData[anitem]
            allClosePrices = []
            allDates = []
            # the index of start date in the array
            # the front, the near date. the end, the previous date
            startIndex = -1
            i = 0
            allClosePrices = TheStockData[2]
            allDates = TheStockData[0]
            for i in range(len(allDates)):
                if theStartDate == allDates[i]:
                    startIndex = i
                    break
            if startIndex == -1:
                #print("the start date is not in the database")
                RemoveData.append(anitem)
                continue

            try:
                for i in range(startIndex + 1):
                    lastIndex = min(i + 1 + AllTimeHighPeriod,
                                    len(allClosePrices))
                    lastLowIndex = min(i + 1 + self.AllTimeLowPeriod,
                                       len(allClosePrices))
                    #print lastIndex
                    #print i
                    #print allClosePrices
                    if allClosePrices[i] > max(
                            allClosePrices[i + 1:lastIndex]):
                        alltimehigh[allDates[i].strftime('%Y-%m-%d')] = True
                    else:
                        alltimehigh[allDates[i].strftime('%Y-%m-%d')] = False

                    if allClosePrices[i] < min(
                            allClosePrices[i + 1:lastLowIndex]):
                        alltimelow[allDates[i].strftime('%Y-%m-%d')] = True
                    else:
                        alltimelow[allDates[i].strftime('%Y-%m-%d')] = False

                od = collections.OrderedDict(
                    sorted(alltimehigh.items(), reverse=True))
                self.AllTimeHigh[anitem] = od

                odlow = collections.OrderedDict(
                    sorted(alltimelow.items(), reverse=True))
                self.AllTimeLow[anitem] = odlow

            except:
                if anitem in self.AllTimeHigh:
                    del self.AllTimeHigh[anitem]
                if anitem in self.AllTimeLow:
                    del self.AllTimeLow[anitem]
                RemoveData.append(anitem)

        for anitem in RemoveData:
            del self.AllData[anitem]
 def __init__(self,InitMoney,StartDate,EndDate,AllTimeHighPeriod=200,AllTimeLowPeriod =30,VibrationPeriod = 10,\
              StockList = [],StockNumber = 3, Leverage=5,VibrationRatio=3.5, BeTesting=False):
     '''
     @param AllTimeHighPeriod: one year all time high or one month all time high? the unit is days
     @param DownPercent: must >0 and <1 
     '''
     SimulationBaseClass.__init__(self, InitMoney, StartDate, EndDate,StockNumber)
     self.VibrationPeriod = VibrationPeriod
     self.VibrationRatio = VibrationRatio
     self.Leverage = Leverage
     self.RemainMoney = self.InitMoney*self.Leverage
     self.TrueMoney = InitMoney
     self.Margin = 0.0
     self.Commission = 15
     self.AllList = StockList
     
     self.TrueMoneyList = {}
     # Get all data
     theStartDate = datetime.datetime.strptime(self.StartDate,'%Y-%m-%d').date()
     theEndDate = datetime.datetime.strptime(self.EndDate,'%Y-%m-%d').date()
     totalPeriod = (theEndDate-theStartDate).days
     self.SPYData = []
     print("Getting all data")
     if not BeTesting:
         self.AllData = MyPredictDB.GetAllDataFromAList(self.AllList,EndDate,AllTimeHighPeriod+totalPeriod)
     else:
         self.SPYData = GetSPXInfo()
         self.AllData = {}
         ReadStockHistoryFromCSV(self.StartDate,self.EndDate,AllTimeHighPeriod,self.AllData)
     print("Calculating all time highs")
     self.AllTimeHigh = {}
     self.AllTimeLow = {}
     self.AllTimeLowPeriod = AllTimeLowPeriod
     self.Vibration = {}
     RemoveData = []
     for anitem in self.AllData:
         # get all time high
         alltimehigh = {}
         alltimelow = {}
         AVibration = {}
         AMeanVibration = {}
         ADayVibration = {}
         TheStockData = self.AllData[anitem]
         allClosePrices = []
         allDates = []
         # the index of start date in the array
         # the front, the near date. the end, the previous date
         startIndex = -1
         i = 0
         allOpenPrices = TheStockData[1]
         allClosePrices = TheStockData[2]
         allHighPrices = TheStockData[3]
         allLowPrices = TheStockData[4]
         ###
         #sometimes there is 0 in the data, this is an error. We fix it using a simple method.
         self._fixZeroValues(allOpenPrices)
         self._fixZeroValues(allClosePrices)
         self._fixZeroValues(allHighPrices)
         self._fixZeroValues(allLowPrices)
                     
         ###
         #allVibration = numpy.abs(numpy.array(allClosePrices)-numpy.array(allOpenPrices))
         PreClosePrices = allClosePrices[1:]
         PreClosePrices.append(0.0)
         HMinPreClose = numpy.array(allHighPrices)-numpy.array(PreClosePrices)
         PreCloseMinL = numpy.array(PreClosePrices) - numpy.array(allLowPrices)
         HighMinLow = numpy.array(allHighPrices)-numpy.array(allLowPrices)
         allVibration = []
         for viIndex in range(len(HMinPreClose)):
             allVibration.append(numpy.max([HMinPreClose[viIndex],PreCloseMinL[viIndex],HighMinLow[viIndex]]))
         
         allDates = TheStockData[0]
         for i in range(len(allDates)):
             if theStartDate==allDates[i]:
                 startIndex = i
                 break
         if startIndex==-1:
             #print("the start date is not in the database")
             RemoveData.append(anitem)
             continue
         
         try:
             for i in range(startIndex+1):
                 lastIndex = min(i+1+AllTimeHighPeriod,len(allClosePrices))
                 lastVibrationIndex = min(i+1+self.VibrationPeriod,len(allClosePrices))
                 lastLowIndex = min(i+1+self.AllTimeLowPeriod,len(allClosePrices))
                 #print lastIndex
                 #print i
                 #print allClosePrices
                 currentStringFormatDate = allDates[i].strftime('%Y-%m-%d')
                 if allClosePrices[i]>max(allClosePrices[i+1:lastIndex]):
                     alltimehigh[currentStringFormatDate]=True
                 else:
                     alltimehigh[currentStringFormatDate]=False
                 
                 #calculate vibration
                 AMeanVibration[currentStringFormatDate] = numpy.mean(allVibration[i+1:lastVibrationIndex])
                 #ADayVibration[currentStringFormatDate] = allHighPrices[i]-allLowPrices[i]
                 ADayVibration[currentStringFormatDate] = numpy.max([allHighPrices[i]-allLowPrices[i],allHighPrices[i]-allClosePrices[i+1],allClosePrices[i+1]-allLowPrices[i]])
                 if ADayVibration[currentStringFormatDate]>self.VibrationRatio*AMeanVibration[currentStringFormatDate] and allClosePrices[i]<allOpenPrices[i]:
                     AVibration[currentStringFormatDate] = True
                 else:
                     AVibration[currentStringFormatDate] = False
                 
                 if allClosePrices[i]<min(allClosePrices[i+1:lastLowIndex]):
                     alltimelow[allDates[i].strftime('%Y-%m-%d')] = True
                 else:
                     alltimelow[allDates[i].strftime('%Y-%m-%d')] = False
                     
             od = collections.OrderedDict(sorted(alltimehigh.items(),reverse=True))
             self.AllTimeHigh[anitem] = od
             
             odVibration = collections.OrderedDict(sorted(AVibration.items(),reverse=True))
             odlowMean = collections.OrderedDict(sorted(AMeanVibration.items(),reverse=True))
             odlowADay = collections.OrderedDict(sorted(ADayVibration.items(),reverse=True))
             self.Vibration[anitem] = [odVibration,odlowMean,odlowADay]
             
             odlow = collections.OrderedDict(sorted(alltimelow.items(),reverse=True))
             self.AllTimeLow[anitem] = odlow
         except:
             if anitem in self.AllTimeHigh:
                 del self.AllTimeHigh[anitem]
             if anitem in self.Vibration:
                 del self.Vibration[anitem]
             if anitem in self.AllTimeLow:
                 del self.AllTimeLow[anitem]
             RemoveData.append(anitem)
             
     for anitem in RemoveData:
         del self.AllData[anitem]