Exemple #1
0
    def historicalData(self, reqId: int, bar: BarData):
        """Q: Does tEng[] want a bar class or a bar dict?
           A: When he goes to the bar, he's looking for dict. Get it?!? HAHAHAHAHAHAHA"""
        eTime = human2epoch(bar.date)
        if eTime > self.latestEpoch[reqId]:
            self.latestEpoch[reqId] = eTime
            setattr(bar, 'epochTime', eTime)
            dateObj = epoch2human(eTime)
            dateStr = str(dateObj)
            print("Adding to mongo: ", self.tickers[reqId], "     ", dateStr)

            oneBar = {
                "secType": "STK",
                "open": bar.open,
                "close": bar.close,
                "high": bar.high,
                "low": bar.low,
                "epoch": eTime,
                "dateStr": dateStr[:8],
                "year": dateObj.year,
                "month": dateObj.month,
                "day": dateObj.day,
                "hour": dateObj.hour,
                "minute": dateObj.minute,
                "second": dateObj.second,
                "ticker": self.tickers[reqId],
                "barLen": "15 min"
            }

            # Send to mongo:
            self.posts.insert_one(oneBar)
    def historicalData(self, reqId: int, bar: BarData):
        """Q: Does tEng[] want a bar class or a bar dict?
           A: When he goes to the bar, he's looking for dict. Get it?!? HAHAHAHAHAHAHA"""
        eTime = human2epoch(bar.date)
        setattr(bar, 'epochTime', eTime)
        self.dailyEpochs[reqId].append(eTime)
        self.dailyDates[reqId].append(bar.date)
        self.dailyBars[reqId].append(bar)
        self.dailyHighs[reqId].append(bar.high)
        self.dailyLows[reqId].append(bar.low)
        self.dailyCloses[reqId].append(bar.close)
        self.dailyOpens[reqId].append(bar.open)

        dateObj = epoch2human(eTime)
        dateStr = str(dateObj)

        oneBar = {
            "secType": "STK",
            "open": bar.open,
            "close": bar.close,
            "high": bar.high,
            "low": bar.low,
            "epoch": eTime,
            "dateStr": dateStr[:8],
            "year": dateObj.year,
            "month": dateObj.month,
            "day": dateObj.day,
            "hour": dateObj.hour,
            "minute": dateObj.minute,
            "second": dateObj.second,
            "ticker": self.tickers[reqId],
            "barLen": "15 mins"
        }

        self.posts.insert_one(oneBar)
    def historicalData(self, reqId: int, bar: BarData):
        """Q: Does tEng[] want a bar class or a bar dict?
           A: When he goes to the bar, he's looking for dict. Get it?!? HAHAHAHAHAHAHA"""
        ticker = self.tickers[reqId]
        print("Historical Data: ", ticker, "    ", bar.close)
        eTime = human2epoch(bar.date)
        dateObj = epoch2human(eTime)
        dateStr = str(dateObj)

        oneBar = {
            "secType": "OPT",
            "open": bar.open,
            "close": bar.close,
            "high": bar.high,
            "low": bar.low,
            "epoch": eTime,
            "dateStr": dateStr[:8],
            "year": dateObj.year,
            "month": dateObj.month,
            "day": dateObj.day,
            "hour": dateObj.hour,
            "minute": dateObj.minute,
            "second": dateObj.second,
            "barLen": "15 mins",
            "ticker": ticker,
            "strike": self.optContracts[ticker]["strike"],
            "expiration": self.optContracts[ticker]["expiration"],
            "right": self.optContracts[ticker]["right"],
            "exchange": self.optContracts[ticker]["exchange"]
        }

        self.posts.insert_one(oneBar)
    def realtimeBar(self,
                    reqId: TickerId,
                    eTime: int,
                    open: float,
                    high: float,
                    low: float,
                    close: float,
                    volume: int,
                    wap: float,
                    count: int,
                    testMode=False):
        super().realtimeBar(reqId, time, open, high, low, close, volume, wap,
                            count)
        ticker = self.tickers[reqId]
        print("RT Data: ", ticker, "    ", close, "     ", self.rights[reqId])
        dateObj = epoch2human(eTime)
        dateStr = str(dateObj)

        oneBar = {
            "secType": "OPT",
            "open": open,
            "close": close,
            "high": high,
            "low": low,
            "epoch": eTime,
            "dateStr": dateStr[:8],
            "year": dateObj.year,
            "month": dateObj.month,
            "day": dateObj.day,
            "hour": dateObj.hour,
            "minute": dateObj.minute,
            "second": dateObj.second,
            "barLen": "15 mins",
            "ticker": ticker,
            "strike": self.optContracts[ticker]["strike"],
            "expiration": self.optContracts[ticker]["expiration"],
            "right": self.rights[reqId],
            "exchange": self.optContracts[ticker]["exchange"]
        }

        self.posts.insert_one(oneBar)
Exemple #5
0
 def historicalDataEnd(self, reqId: int, start: str, end: str):
     if self.closes[reqId] is not None:
         dateObj = epoch2human(self.epochs[reqId])
         dateStr = str(dateObj)
         oneBar = {
             "close"     :   self.closes[reqId],
             "epoch"     :   self.epochs[reqId],
             "dateStr"   :   dateStr[:8],
             "year"      :   dateObj.year,
             "month"     :   dateObj.month,
             "day"       :   dateObj.day,
             "hour"      :   dateObj.hour,
             "minute"    :   dateObj.minute,
             "second"    :   dateObj.second,
             "ticker"    :   self.tickers[reqId],
             "barLen"    :   "close only"
         }
         self.posts.insert_one(oneBar)
     self.historicalDone[reqId] = True
     self.requestNextTicker()
     if self.allDailyHistoricalDone():
         self.done = True
 def update(self, epoch, price):
     if self.enableChase:
         if self.priceChaser(price=price):
             self.exitReason = "Price Chase Is Over"
             self.exitTime = epoch
             return True
     if self.enablePoop:
         if self.poopDetector(epoch=epoch, price=price):
             self.exitReason = "Poop Detected"
             self.exitTime = epoch
             return True
     if self.enableEndOfDayExit:
         humanThing = epoch2human(epoch)
         if self.endOfDayExit(hour=humanThing.hour, minute=humanThing.minute):
             self.exitReason = "Time to go home"
             self.exitTime = epoch
             return True
     if self.enableMaxPositionTime:
         if self.timeLimiter(now=epoch):
             self.exitReason = "Arrested for Loitering"
             self.exitTime = epoch
             return True
     return False
    def realtimeBar(self,
                    reqId: TickerId,
                    eTime: int,
                    open: float,
                    high: float,
                    low: float,
                    close: float,
                    volume: int,
                    wap: float,
                    count: int,
                    testMode=False):
        super().realtimeBar(reqId, time, open, high, low, close, volume, wap,
                            count)

        dateObj = epoch2human(eTime)
        dateStr = str(dateObj)

        oneBar = {
            "open": open,
            "close": close,
            "high": high,
            "low": low,
            "epoch": eTime,
            "dateStr": dateStr[:8],
            "year": dateObj.year,
            "month": dateObj.month,
            "day": dateObj.day,
            "hour": dateObj.hour,
            "minute": dateObj.minute,
            "second": dateObj.second,
            "trigNow": None,
            "ticker": self.tickers[reqId],
            "barLen": "5 sec",
            "volume": volume,
            "count": count
        }
        self.posts.insert_one(oneBar)
    def historicalData(self, reqId: int, bar: BarData):
        """Q: Does tEng[] want a bar class or a bar dict?
           A: When he goes to the bar, he's looking for dict. Get it?!? HAHAHAHAHAHAHA"""
        eTime = human2epoch(bar.date)
        setattr(bar, 'epochTime', eTime)
        self.dailyEpochs[reqId].append(eTime)
        self.dailyDates[reqId].append(bar.date)
        self.dailyBars[reqId].append(bar)
        self.dailyHighs[reqId].append(bar.high)
        self.dailyLows[reqId].append(bar.low)
        self.dailyCloses[reqId].append(bar.close)
        self.dailyOpens[reqId].append(bar.open)

        dateObj = epoch2human(eTime)
        dateStr = str(dateObj)

        oneBar = {
            "thingType": "bar",
            "secType": "STK",
            "open": bar.open,
            "close": bar.close,
            "high": bar.high,
            "low": bar.low,
            "epoch": eTime,
            "dateStr": dateStr[:8],
            "year": dateObj.year,
            "month": dateObj.month,
            "day": dateObj.day,
            "hour": dateObj.hour,
            "minute": dateObj.minute,
            "second": dateObj.second,
            "trigNow": False,
            "ticker": self.tickers[reqId],
            "barLen": "1 day"
        }

        if len(self.dailyCloses[reqId]) > 10:
            # Calculate 10 day MA
            self.ma10[reqId].append(
                makeTrend(self.dailyHighs[reqId][-9:],
                          self.dailyLows[reqId][-9:]))
            self.ma10Times[reqId].append(eTime)

        if len(self.dailyCloses[reqId]) > 21:
            # Calculate 20 day MA
            self.ma20[reqId].append(
                makeTrend(self.dailyHighs[reqId][-21:],
                          self.dailyLows[reqId][-21:],
                          tol=0.02))
            self.ma20Times[reqId].append(eTime)

        if len(self.dailyCloses[reqId]) > 20:
            # Calculate %K:
            self.kVec[reqId].append(
                kLite(self.dailyHighs[reqId][-20:],
                      self.dailyLows[reqId][-20:], bar.close))
            self.kTimes[reqId].append(eTime)

        if len(self.kVec[reqId]) > 3:
            # Calculate %D:
            self.dVec[reqId].append(dLite(self.kVec[reqId][-3:]))
            self.dTimes[reqId].append(eTime)

        self.entrySig(reqId, eTime)

        if len(self.entryTimes[reqId]
               ) > 0 and self.entryTimes[reqId][-1] == eTime:
            oneBar['trigNow'] = True
            oneBar["trigDirection"] = self.entryVec[reqId][-1]

        # Send to Mongo:
        if ENABLE_REDUNDANCY_PREVENTION:
            if len(REDUNDANCY_PREVENTION_TERMS) > 0:
                redunPreventionDic = {}
                if 'ticker' in REDUNDANCY_PREVENTION_TERMS:
                    redunPreventionDic["ticker"] = self.tickers[reqId]
                if "epoch" in REDUNDANCY_PREVENTION_TERMS:
                    redunPreventionDic["epoch"] = eTime
                # Insert other if statements here.
                if self.posts.find(redunPreventionDic).count() == 0:
                    self.posts.insert_one(oneBar)
                else:
                    # If results are returned, do not add this bar again.
                    pass
            else:
                self.posts.insert_one(oneBar)
        else:
            self.posts.insert_one(oneBar)