Пример #1
0
    def send_order(self, ticker, order_action, quantity, sec_type="STK", order_type="MKT", limit_price=None,trail_percent=None, trail_stop_price=None):
        contract = Contract()  # Creates a contract object from the import
        contract.symbol = ticker  # Sets the ticker symbol
        contract.secType = sec_type  # Defines the security type as stock
        contract.currency = "USD"  # Currency is US dollars
        # In the API side, NASDAQ is always defined as ISLAND in the exchange field
        contract.exchange = "SMART"
        # contract.PrimaryExch = "NYSE"
        contract.PrimaryExch = "NASDAQ"

        order = Order()
        order.action = order_action
        order.orderType = order_type
        order.totalQuantity = quantity

        if order_type=='TRAIL':
            if trail_percent is not None:
                order.trailingPercent = trail_percent
            if trail_stop_price is not None:
                order.trailStopPrice = trail_stop_price

        if order_type == 'LMT':
            if limit_price is not None:
                order.lmtPrice = limit_price

        self.placeOrder(self.nextOrderId, contract, order)
        return self.nextOrderId
Пример #2
0
            self.lines[reqId].set_last_time(value)
        #elif tickType == 84:
        #    print("last exchange: " + value)


def run_loop():
    app.run()


app = IBapi()
app.connect('127.0.0.1', 7498, 123)
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()
time.sleep(1)

contract = Contract()
contract.symbol = 'TSLA'
contract.secType = 'STK'
contract.exchange = 'SMART'
contract.currency = 'USD'
contract.PrimaryExch = 'ISLAND'

app.reqMarketDataType(2)
reqId = 0
#while True:
app.reqMktData(reqId, contract, '', False, False, [])
#time.sleep(20)
#app.cancelMktData(reqId)
#reqId += 1
#app.disconnect()