Ejemplo n.º 1
0
    def initialize_Function(self):
        self.log.notset(__name__ + '::initialize_Function')
        self.log.info('IBridgePy version %s' % (str(self.versionNumber), ))
        self.qData = QDataClass(self)
        self.request_data(ReqData.reqPositions(), ReqData.reqCurrentTime(),
                          ReqData.reqIds())
        self.request_data(ReqData.reqAccountUpdates(True, self.accountCode),
                          ReqData.reqAllOpenOrders())

        self.initialize_quantopian(
            self.context)  # function name was passed in.

        self.log.info('####    Starting to initialize trader    ####')
        if type(self.accountCode) == type(''):
            self.display_all()
        else:
            for acctCode in self.accountCode:
                self.display_all(acctCode)
        self.log.info('####    Initialize trader COMPLETED    ####')
        if self.before_trading_start_quantopian != None:
            self.schedule_function(
                func=self.before_trading_start_quantopian,
                time_rule=time_rules.market_open(minutes=-10),
                calendar=calendars.US_EQUITIES)
        tmp = self.get_datetime()
        self.check_date_rules(tmp)
        self.stime_previous = tmp
Ejemplo n.º 2
0
    def initialize_Function(self):
        self.log.notset(__name__ + '::initialize_Function')
        self.log.info('IBridgePy version %s' % (self.versionNumber, ))
        self.log.info('fileName = %s' % (self.fileName, ))
        self.qData = QDataClass(self)
        self.request_data(ReqData.reqIds())
        self.request_data(ReqData.reqCurrentTime())
        if self.multiAccountFlag:
            self.request_data(ReqData.reqAccountSummary(),
                              ReqData.reqAllOpenOrders(),
                              ReqData.reqPositions())
        else:
            self.request_data(
                ReqData.reqAccountUpdates(True, self.accountCode),
                ReqData.reqAllOpenOrders(), ReqData.reqPositions())

        self.log.debug(
            __name__ +
            '::initialize_Function::start to run customers init function')
        self.initialize_quantopian(
            self.context)  # function name was passed in.

        self.log.info('####    Starting to initialize trader    ####')
        if self.multiAccountFlag:
            for acctCode in self.accountCode:
                self.display_all(acctCode)
        else:
            self.display_all()
        self.log.info('####    Initialize trader COMPLETED    ####')
Ejemplo n.º 3
0
    def get_contract_details(self,
                             secType,
                             symbol,
                             field,
                             currency='USD',
                             exchange='',
                             primaryExchange='',
                             expiry='',
                             strike=0.0,
                             right='',
                             multiplier='',
                             conId=0,
                             localSymbol=''):
        security = self.superSymbol(secType=secType,
                                    symbol=symbol,
                                    currency=currency,
                                    exchange=exchange,
                                    primaryExchange=primaryExchange,
                                    expiry=expiry,
                                    strike=strike,
                                    right=right,
                                    multiplier=multiplier,
                                    conId=conId,
                                    localSymbol=localSymbol,
                                    addToQdata=False)

        self.request_data(ReqData.reqContractDetails(security))

        # Request contractDetails will not be mixed with other request so that only one result will be returned.
        if len(self.end_check_list_result) == 1:
            for ct in self.end_check_list_result:
                return self._extract_contractDetails(
                    self.end_check_list_result[ct], field)
    def initialize_Function(self):
        self.log.notset(__name__+'::initialize_Function')
        self.log.info('IBridgePy version %s' %(self.versionNumber,))
        self.log.info('fileName = %s' %(self.fileName,))
        self.qData=QDataClass(self)
        self.request_data(ReqData.reqCurrentTime(), ReqData.reqIds())
        self.request_data(ReqData.reqAccountUpdates(True, self.accountCode),
                          ReqData.reqAllOpenOrders(),
                          ReqData.reqPositions())

        self.log.debug(__name__+'::initialize_Function::start to run customers init function')                 
        self.initialize_quantopian(self.context) # function name was passed in.
        self.log.info('####    Starting to initialize trader    ####')  
        if type(self.accountCode)==type(''):            
            self.display_all()
        else:
            for acctCode in self.accountCode:
                self.display_all(acctCode)
        self.log.info('####    Initialize trader COMPLETED    ####') 
         
        # In order to trigger check_date_rule in marketManagerBase.py
        # self.stime_previous.date != timeNow.date
        # So, minus 1 day here.
        self.stime_previous = self.get_datetime() - dt.timedelta(days=1)
Ejemplo n.º 5
0
    def IBridgePyPlaceOrder(self,
                            an_order,
                            accountCode='default',
                            followUpWaiver=False):
        self.log.debug(__name__ + '::IBridgePyPlaceOrder')
        if not self.validateAccountCode(accountCode):
            return

        an_order.order.account = self.adjust_accountCode(accountCode)
        an_order.orderId = self.nextId
        an_order.created = self.get_datetime()
        an_order.stop = an_order.order.auxPrice
        an_order.limit = an_order.order.lmtPrice
        an_order.amount = an_order.order.totalQuantity
        an_order.status = 'PreSubmitted'

        # in the test_run mode, self.nextId may change after self.placeOrder, so, record it at first place
        orderId = self.nextId
        self.nextId = self.nextId + 1
        self.PORTFOLIO.orderStatusBook[orderId] = an_order
        self.request_data(
            ReqData.placeOrder(orderId, an_order.contract, an_order.order,
                               followUpWaiver))
        return orderId
Ejemplo n.º 6
0
 def modify_order(self,
                  orderId,
                  newQuantity=None,
                  newLimitPrice=None,
                  newStopPrice=None,
                  newTif=None,
                  newOrderRef=None):
     self.log.debug(__name__ + '::modify_order: orderId = %s' % (orderId, ))
     if self.PORTFOLIO.orderStatusBook[orderId].status in [
             OrderStatus.PRESUBMITTED, OrderStatus.SUBMITTED
     ]:
         an_order = self.PORTFOLIO.orderStatusBook[orderId]
         an_order.status = OrderStatus.PRESUBMITTED
         if newQuantity is not None:
             # amount is the same number as order.totalQuantity. Keep it because Quantopian has it.
             an_order.amount = newQuantity
             an_order.order.totalQuantity = newQuantity
         if newLimitPrice is not None:
             an_order.limit = newLimitPrice
             an_order.order.lmtPrice = newLimitPrice
         if newStopPrice is not None:
             an_order.stop = newStopPrice
             an_order.order.auxPrice = newStopPrice
         if newTif is not None:
             an_order.order.tif = newTif
         if newOrderRef is not None:
             an_order.order.orderRef = newOrderRef
         self.request_data(
             ReqData.placeOrder(orderId, an_order.contract, an_order.order,
                                FollowUpRequest.DO_NOT_FOLLOW_UP))
     else:
         self.log.error(
             __name__ +
             '::modify_order: Cannot modify order. orderId = %s' %
             (orderId, ))
         exit()
Ejemplo n.º 7
0
 def get_scanner_results(self, **kwargs):
     #        numberOfRows=-1, instrument='', locationCode='', scanCode='', abovePrice=0.0,
     #        belowPrice=0.0, aboveVolume=0, marketCapAbove=0.0, marketCapBelow=0.0, moodyRatingAbove='',
     #        moodyRatingBelow='', spRatingAbove='', spRatingBelow='', maturityDateAbove='', maturityDateBelow='',
     #        couponRateAbove=0.0, couponRateBelow=0.0, excludeConvertible=0, averageOptionVolumeAbove=0,
     #        scannerSettingPairs='', stockTypeFilter=''
     tagList = [
         'numberOfRows', 'instrument', 'locationCode', 'scanCode',
         'abovePrice', 'belowPrice', 'aboveVolume', 'marketCapAbove',
         'marketCapBelow', 'moodyRatingAbove', 'moodyRatingBelow',
         'spRatingAbove', 'spRatingBelow', 'maturityDateAbove',
         'maturityDateBelow', 'couponRateAbove', 'couponRateBelow',
         'excludeConvertible', 'averageOptionVolumeAbove',
         'scannerSettingPairs', 'stockTypeFilter'
     ]
     subscription = IBCpp.ScannerSubscription()
     for ct in kwargs:
         if ct in tagList:
             setattr(subscription, ct, kwargs[ct])
     self.request_data(ReqData.reqScannerSubscription(subscription))
     reqId = int(self.end_check_list[self.end_check_list['reqType'] ==
                                     'reqScannerSubscription']['reqId'])
     ans = ServerResponse(reqId, self.end_check_list_result[int(reqId)])
     return ans
Ejemplo n.º 8
0
 def cancel_scanner_request(self, scannerRequestId):
     self.request_data(ReqData.cancelScannerSubscription(scannerRequestId))
Ejemplo n.º 9
0
 def get_scanner_parameters(self):
     self.request_data(ReqData.reqScannerParameters())
     for x in self.end_check_list_result:
         return self.end_check_list_result[x]