Exemplo n.º 1
0
def make__short__order(action,
                       qty,
                       limit=None,
                       profit_take=None,
                       training_stop_percent=None,
                       transmit=True,
                       parentId=None):
    order = Order()
    order.m_action = action
    order.m_totalQuantity = qty
    order.m_tif = "GTC"  #All orders are GTC by default with NO TIME LIMIT to auto cancel
    logger.debug('In Short Order Function')
    if limit == 2:
        logger.debug('In Limit is 2 section')
        if action == 'BUY':
            logger.debug('In Buy sections Profit Take')
            order.m_lmtPrice = truncate(
                float(tLow[0]) -
                ((float(truncate(float(tHigh[0]), 4)) -
                  float(truncate(float(tLow[0]), 4))) * int(RiskReward[0])), 4)
            logger.debug('Limit Price is %s', order.m_lmtPrice)
            logger.debug('RiskReward Ratio is %s', RiskRewardRatio)
            order.m_orderType = 'LMT'
            order.m_account = masteraccount
            order.m_transmit = transmit
        order.m_transmit = transmit
    elif limit == 1:
        # ENTRY   A simple stop order
        logger.debug('In Limit is 1 Function')
        order.m_orderType = 'STP'
        if action == 'BUY':
            logger.debug('In Limit 1. in Buy action')
            logger.debug('RiskReward Ratio is %s', RiskRewardRatio)
            order.m_lmtPrice = truncate(
                float(tLow[0]) -
                ((float(truncate(float(tHigh[0]), 4)) -
                  float(truncate(float(tLow[0]), 4))) * int(RiskReward[0])), 4)
            stopPrice = truncate(
                float(tLow[0]) -
                ((float(truncate(float(tHigh[0]), 4)) -
                  float(truncate(float(tLow[0]), 4))) * int(RiskReward[0])), 4)
            logger.debug('Stop Price is %s', stopPrice)
            #order.m_parentId = parentId
            order.m_account = masteraccount
            order.m_transmit = transmit

        if action == 'SELL':
            logger.debug('In Limit 1. in Sell action')
            #order.m_lmtPrice  = limit + int(np.around((limit*profit_take_percent)/100.0, 5)/0.00005)*0.00005
            order.m_lmtPrice = float(truncate(float(tLow[0]), 4)) - 0.0005
            stopPrice = float(truncate(float(tLow[0]), 4)) - 0.0005
            order.m_auxPrice = stopPrice
            order.m_parentId = parentId
            order.m_account = masteraccount
    # Important that we only send the order when all children are formed.
    order.m_transmit = transmit

    return order
Exemplo n.º 2
0
 def create_order(self, order_type, quantity, action):
     """
     Create an order object (market/limit) to go long/short.
     """
     order = Order()
     order.m_orderType = order_type
     order.m_totalQuantity = quantity
     order.m_action = action
     return order
Exemplo n.º 3
0
    def create_order(order_type, quantity, action):

        order = Order()
        order.m_action = action
        order.m_orderType = order_type
        order.m_totalQuantity = quantity
        order.m_lmtPrice = price

        return order
Exemplo n.º 4
0
def makeStkOrder(shares, action, price):
    order = Order()
    order.m_minQty = shares
    order.m_lmtPrice = price
    order.m_orderType = 'LMT'
    order.m_totalQuantity = shares
    #    order.m_outsideRth = True
    order.m_action = str(action).upper()
    return order
Exemplo n.º 5
0
def makeStkOrder(shares,action):
    order = Order()
    order.m_minQty = shares
#    order.m_lmtPrice = limit_price
    order.m_orderType = 'MKT'
    order.m_tif = 'OPG'
    order.m_totalQuantity = shares
    order.m_action = str(action).upper()
    return order
Exemplo n.º 6
0
 def create_order(self, account, orderType, totalQuantity, action, 
                  lmt_price=None):
     order = Order()
     order.m_account = account
     order.m_orderType = orderType
     order.m_totalQuantity = totalQuantity
     order.m_action = action
     if orderType == "LMT":
         order.m_lmtPrice = lmt_price
     return order
Exemplo n.º 7
0
 def create_order(order_type,quantity, action,time_in_force,price):
     
     order = Order()
     order.m_tif = time_in_force
     order.m_action = action
     order.m_orderType = order_type
     order.m_totalQuantity = quantity
     order.m_lmtPrice = price            
     
     return order
 def create_order(self, action, quantity):
     """Create an Interactive Brokers order object. This specifies whether or
     not we are selling or buying the asset, the quantity to exchange, and
     the order type of the trade (which is assumed to be a market order).
     """
     o = Order()
     o.m_orderType = "MKT"
     o.m_totalQuantity = quantity
     o.m_action = action
     return o
Exemplo n.º 9
0
def create_order(order_type, quantity, action, price=None):
    """Create an Order object (Market/Limit) to go long/short.

    order_type - 'MKT', 'LMT' for Market or Limit orders
    quantity - Integral number of assets to order
    action - 'BUY' or 'SELL'"""
    if price is None:
        order = Order()
        order.m_orderType = order_type
        order.m_totalQuantity = quantity
        order.m_action = action
    else:
        order = Order()
        order.m_orderType = order_type
        order.m_totalQuantity = quantity
        order.m_action = action
        order.m_lmtPrice = price

    return order
Exemplo n.º 10
0
 def _make_opt_order(self, order_id, client_id, order_dict):
     """docstring for _make_opt_contract"""
     order = Order()
     order.m_orderId = order_id
     order.m_clientId = client_id
     order.m_action = order_dict['m_action']
     order.m_lmtPrice = order_dict['price']
     order.m_totalQuantity = order_dict['volume']
     order.m_orderType = order_dict['m_order_type']
     return order
Exemplo n.º 11
0
    def buy(self, contract_Details, limit, stop):

        self.tws.reqIds(-1)
        time.sleep(0.5)
        oid = self.callback.next_ValidId
        parent = Order()
        parent.orderId = oid
        parent.m_action = "BUY"
        parent.m_orderType = "MKT"
        parent.m_totalQuantity = 300000
        parent.m_transmit = False
        print('|==========执行触发=================|')
        print('|执行时期:%s|' % datetime.now())
        print('|操作: BUY                        |')
        print('|执行前总额:%s' % self.callback.account_Summary[-1][3])
        print('|===================================|')
        self.tws.reqIds(-1)
        takeProfit = Order()
        takeProfit.orderId = parent.orderId + 1
        takeProfit.m_action = "SELL"
        takeProfit.m_orderType = "LMT"
        takeProfit.m_totalQuantity = 300000
        takeProfit.m_lmtPrice = limit
        takeProfit.m_parentId = oid
        takeProfit.m_transmit = False

        stopLoss = Order()
        stopLoss.orderId = parent.orderId + 2
        stopLoss.m_action = "SELL"
        stopLoss.m_orderType = "STP"
        #Stop trigger price
        stopLoss.m_auxPrice = stop
        stopLoss.m_totalQuantity = 300000
        stopLoss.m_parentId = oid
        stopLoss.m_transmit = True

        bracketOrder = [parent, takeProfit, stopLoss]

        for o in bracketOrder:
            self.tws.placeOrder(o.orderId, contract_Details, o)
            self.tws.reqIds(-1)
        time.sleep(1)
        time.sleep(2)
Exemplo n.º 12
0
def make_order(qty, limit_price, action):
    order = Order()
    order.m_minQty = qty
    order.m_lmtPrice = limit_price
    order.m_orderType = 'LMT'
    order.m_totalQuantity = qty
    order.m_action = action
    order.m_tif = 'GTC'
    order.m_outsideRth = True
    return order
Exemplo n.º 13
0
 def create_stock_order(self, quantity, is_buy, is_market_order=True):
     order = Order()
     order.m_totalQuantity = quantity
     order.m_orderType = \
         DataType.ORDER_TYPE_MARKET if is_market_order else \
         DataType.ORDER_TYPE_LIMIT
     order.m_action = \
         DataType.ORDER_ACTION_BUY if is_buy else \
         DataType.ORDER_ACTION_SELL
     return order
Exemplo n.º 14
0
 def create_order(self, order_type, quantity, action):
     """Create an Order object (Market/Limit) to go long/short.
     order_type - 'MKT', 'LMT' for Market or Limit orders
     quantity - Integral number of assets to order
     action - 'BUY' or 'SELL'"""
     order = Order()
     order.m_orderType = order_type
     order.m_totalQuantity = quantity
     order.m_action = action
     return order
Exemplo n.º 15
0
def makeOrder(action, qty, price):
    newOrder = Order()
    newOrder.m_action = action
    newOrder.m_totalQuantity = qty
    newOrder.m_orderType = 'LMT'
    newOrder.m_lmtPrice = price
    newOrder.m_tif = ''
    newOrder.m_parentId = 0
    newOrder.m_discretionaryAmt = 0
    newOrder.m_transmit = True
    return newOrder
Exemplo n.º 16
0
def make_order(action, quantity, price=None):
    # build an order object
    # action :
    # quantity : volume of security to transact
    # price : price point of security

    if price is not None:
        order = Order()
        order.m_orderType = 'LMT'
        order.m_totalQuantity = quantity
        order.m_action = action
        order.m_lmtPrice = price

    else:
        order = Order()
        order.m_ordertype = 'MKT'
        order.m_totalQuantity = quantity
        order.m_action = action

    return order
Exemplo n.º 17
0
    def create_order(self, account, orderType, totalQuantity, action, lmt, transmit):
        order = Order()
        order.m_account = account
        order.m_orderType = orderType
        order.m_totalQuantity = totalQuantity
        order.m_action = action
        order.m_lmtPrice = lmt
        # order.m_trailStopPrice=stp
        order.m_transmit = transmit

        return order
Exemplo n.º 18
0
def create_order(order_type, quantity, action):
    "Create an Order Object (Market/Limit) to go long/short"

    "order type- 'MKT', 'LMT', etc - MIT, LIT, TLT, all order types we have learned "
    "quantity- how many you want to buy or sell, remember than options are 1 (for each 100 shares of stocks/ multiply price by 100 of the option)"

    order = Order()
    order.m_orderType = order_type
    order.m_totalQuantity = quantity
    order.m_action = action
    return order
Exemplo n.º 19
0
def make_order(action, quantity):

    global orderID, clientID

    order = Order()
    order.m_orderId = orderID
    order.m_clientId = clientID
    order.m_action = action
    order.m_totalQuantity = quantity
    order.m_orderType = "MKT"
    return order
Exemplo n.º 20
0
    def test_handle_exec_details(self):

        # Create an order and send it
        contract = create_ib_futures_contract('GC', exp_month=5, exp_year=2016)
        order = create_order('MKT', 1, limit_price=None)
        order = Order()
        order.m_action = 'BUY'
        order.m_totalQuantity = 1
        order.m_orderType = 'MKT'
        self.ib_connection.place_order(contract, order)
        time.sleep(3)
Exemplo n.º 21
0
def make_orders(order_type, action, quantity, price):
    order = Order()
    order.m_orderType = order_type
    order.m_totalQuantity = quantity
    order.m_action = action
    order.m_lmtPrice = price
    print("----ORDER----")
    print("Order Type = ", order_type)
    print("Action = ", action)
    print("Quantity = ", quantity)
    print("Price = ", price)
    return order
Exemplo n.º 22
0
 def create_order(self, order_type, quantity, action):
     '''
     Create an Order object (Market/Limit) to go Long/Short
     :param order_type: 'MKT', 'LMT' for Market or Limit orders
     :param quantity: integer, number of assets to order
     :param action: 'BUY' or 'SELL'
     '''
     order = Order()
     order.m_orderType = order_type
     order.m_totalQuantity = quantity
     order.m_action = action
     return order
Exemplo n.º 23
0
    def createOrder(self, quantity, price=0., stop=0., tif="DAY", \
        fillorkill=False, iceberg=False, transmit=True, rth=False, **kwargs):
        # https://www.interactivebrokers.com/en/software/api/apiguide/java/order.htm
        order = Order()
        order.m_clientId      = self.clientId
        order.m_action        = dataTypes["ORDER_ACTION_BUY"] if quantity>0 else dataTypes["ORDER_ACTION_SELL"]
        order.m_totalQuantity = abs(quantity)

        if "orderType" in kwargs:
            order.m_orderType = kwargs["orderType"]
        else:
            order.m_orderType = dataTypes["ORDER_TYPE_MARKET"] if price==0 else dataTypes["ORDER_TYPE_LIMIT"]

        order.m_lmtPrice      = price # LMT  Price
        order.m_auxPrice      = stop  # STOP Price
        order.m_tif           = tif   # DAY, GTC, IOC, GTD
        order.m_allOrNone     = int(fillorkill)
        order.hidden          = iceberg
        order.m_transmit      = int(transmit)
        order.m_outsideRth    = int(rth==False)

        # The publicly disclosed order size for Iceberg orders
        if iceberg & ("blockOrder" in kwargs):
            order.m_blockOrder = kwargs["m_blockOrder"]

        # The percent offset amount for relative orders.
        if "percentOffset" in kwargs:
            order.m_percentOffset = kwargs["percentOffset"]

        # The order ID of the parent order,
        # used for bracket and auto trailing stop orders.
        if "parentId" in kwargs:
            order.m_parentId = kwargs["parentId"]

        # oca group (Order Cancels All)
        # used for bracket and auto trailing stop orders.
        if "ocaGroup" in kwargs:
            order.m_ocaGroup = kwargs["ocaGroup"]
            if "ocaType" in kwargs:
                order.m_ocaType = kwargs["ocaType"]
            else:
                order.m_ocaType = 2 # proportionately reduced size of remaining orders

        # For TRAIL order
        if "trailingPercent" in kwargs:
            order.m_trailingPercent = kwargs["trailingPercent"]

        # For TRAILLIMIT orders only
        if "trailStopPrice" in kwargs:
            order.m_trailStopPrice = kwargs["trailStopPrice"]


        return order
Exemplo n.º 24
0
    def create_order(self, order_type, quantity, action):
        ''' Create an Order object (Market/Limit) to go long/short.

        order_type - 'MKT', 'LMT' for market or limit orders
        quantity - integer number of units to order
        action - 'BUY' or 'SELL'
        '''
        order = Order()
        order.m_orderType = order_type
        order.m_totalQuantity = quantity
        order.m_action = action
        return order
Exemplo n.º 25
0
def make_order(o_Id, c_Id, action, qty, Price=None):
    order = Order()
    order.m_orderId = o_Id
    order.m_clientId = c_Id
    order.m_action = action
    order.m_totalQuantity = qty
    if Price == None:
        order.m_orderType = "MKT"
    else:
        order.m_orderType = "LMT"
        order.m_lmtPrice = float(Price)
    return order
Exemplo n.º 26
0
def make_order(action, quantity, price=None):
    order = Order()
    order.m_action = action
    order.m_totalQuantity = quantity

    if price is not None:
        order.m_orderType = 'LMT'
        order.m_lmtPrice = price
    else:
        order.m_orderType = 'MKT'

    return order
Exemplo n.º 27
0
def make_long_order(action,
                    qty,
                    limit=None,
                    profit_take=None,
                    training_stop_percent=None,
                    transmit=True,
                    parentId=None):
    order = Order()
    order.m_action = action
    order.m_totalQuantity = qty[0]
    logger.debug('qty is %s', qty[0])
    logger.debug('qty type is %s', type(qty))
    order.m_tif = "GTC"  #All orders are GTC by default with NO TIME LIMIT to auto cancel
    logger.debug('In Long Order Function')
    logger.debug('Limit Value is %s', limit)
    # ENTRY   A simple stop order
    order.m_orderType = 'STP'
    logger.debug('In Long Order Function when Limit is 1')
    logger.debug('Action is %s', action)
    # Rounding is due to FX, we cannot create an order with bad price, and FX book increments at 0.00005 only!
    #order.m_lmtPrice  = limit - int(np.around((limit*profit_take_percent)/100.0, 5)/0.00005)*0.00005
    logger.debug('In Buy action')

    lmtPrice = float(
        truncate((float(gap_close) +
                  (float(ATRentrymultiple[0]) * float(ATRVal[0]))), 2))
    logger.debug('Buy Stop Limit"Limit" Price or better to buy %s', lmtPrice)
    float(lmtPrice)
    logger.debug('lmt price type is %s', type(lmtPrice))

    order.m_lmtPrice = lmtPrice

    stopPrice = float(
        truncate((float(gap_close) +
                  (float(ATRentrymultiple[0]) * float(ATRVal[0]))), 2))
    logger.debug('Buy Stop Limit"Stop" Price to fire a buy order %s',
                 stopPrice)
    float(stopPrice)
    logger.debug('stop price type is %s', type(stopPrice))
    order.m_auxPrice = stopPrice

    order.m_triggerMethod = 2
    order.m_parentId = parentId
    order.m_account = masteraccount
    logger.debug('Buy Upper Limit Price %s', order.m_lmtPrice)
    logger.debug('Buy Upper Limit Type %s', type(order.m_lmtPrice))
    logger.debug('Stop Entry Trigger Price %s ', stopPrice)
    logger.debug('Stop Entry Trigger Type %s ', type(stopPrice))
    logger.debug('Parent ID %s', order.m_parentId)
    logger.debug('Account is %s', order.m_account)
    order.m_transmit = transmit
    return order
Exemplo n.º 28
0
def make__short__order(action, qty, limit = None, profit_take=None, training_stop_percent=None, transmit=True, parentId=None):
        order = Order()
        order.m_action = action
        order.m_totalQuantity = qty
        order.m_tif = "GTC"  #All orders are GTC by default with NO TIME LIMIT to auto cancel
        logger.debug('In Short Order Function')
        logger.debug('Limit Value is %s', limit)
        if limit == 2: # Profit Target
            logger.debug('In Limit is 2 subfunction')
            if action == 'BUY':
                logger.debug('In Profit Order Subfunction')
                #logger.debug('Profit Pips is: %s', ProfitPips)
                logger.debug('Limit Price: %s', order.m_lmtPrice)
                #logger.debug('Risk Reward Ratio is: %s', RiskRewardRatio)
                
                order.m_lmtPrice  = truncate((float(tLow[0]) - 0.0022), 4)
                order.m_orderType = 'LMT'
                order.m_account = masteraccount
                order.m_transmit = transmit
            order.m_transmit = transmit
            logger.debug('transmitted order')
        elif limit == 1:
            logger.debug('In Limit is 1 subfunction')
       	# ENTRY   A simple stop order
            order.m_orderType = 'STP'
#            if action == 'BUY':
#                logger.debug('In Buy subfunction')
#                #logger.debug('Profit Pips is: %s', ProfitPips)
#                #logger.debug('Risk Reward Ratio is: %s', RiskRewardRatio)
#                order.m_lmtPrice  = (float(tLow[0]) - 0.0002)
#                stopPrice = truncate((float(tLow[0]) - 0.0002),4)   #Stop Order -- Entry Price
#                logger.debug('STP Price: %s', stopPrice)
#                
#                order.m_auxPrice = stopPrice
#                #order.m_parentId = parentId
#                order.m_account = masteraccount
#                order.m_transmit = transmit

            if action == 'SELL':
                logger.debug('In Sell subfunction')
                #order.m_lmtPrice  = limit + int(np.around((limit*profit_take_percent)/100.0, 5)/0.00005)*0.00005
                order.m_lmtPrice  = (float(tLow[0]) - 0.0002)
                logger.debug('Limit Price: %s', order.m_lmtPrice)
                stopPrice = truncate((float(tLow[0]) - 0.0002),4)
                logger.debug('Stop Price: %s', stopPrice)
                order.m_auxPrice = stopPrice;
                order.m_parentId = parentId
                order.m_account = masteraccount
        # Important that we only send the order when all children are formed.
        order.m_transmit = transmit
        logger.debug('transmitted order')
        return order    
Exemplo n.º 29
0
    def create_order(self, order_type, quantity, action):
        """
        Crea un oggetto Ordine (Market/Limit) per andare long/short.

        order_type - "MKT", "LMT" per ordini a mercato o limite
        quantity - Numero intero di asset dell'ordine
        action - 'BUY' o 'SELL'
        """
        order = Order()
        order.m_orderType = order_type
        order.m_totalQuantity = quantity
        order.m_action = action
        return order
Exemplo n.º 30
0
 def _make_order(self, action, price, quantity):
     order = Order()
     order.m_action = action
     order.m_lmtPrice = price
     order.m_totalQuantity = quantity
     order.m_orderId = self.order_id
     order.m_clientId = 0
     order.m_permid = 0
     order.m_auxPrice = 0
     order.m_tif = 'DAY'
     order.m_transmit = True
     order.m_orderType = 'LMT'
     return order