A problem using orders in Ibridgepy on interactive brokers

Hello there:



I 've noticed that there is a problem using orders within ibridgepy, adn that is… once I send an order, there are tmes when a part of the shares you order to buy is traded by one exchange and another part is traded in another exhange… in order to fill the order.



Now, when If one is using a market order  the price of those share are generally not exactly the same, so when I want to fetch the price at which the order was executed, the order seems to fetch the price of the first portion of sahres traded… and not the "average"…  at least that was the problem I encountered with this code:







def initialize(context):

    context.flag = False

    context.flag1 = False

    context.security = superSymbol(secType='STK', symbol='KXIN', currency='USD', exchange='SMART', primaryExchange='NASDAQ')  

    context.risk = 4  

   symbol('VSTA')

   superSymbol(secType='STK', symbol='  ', currency='USD', exchange='SMART', primaryExchange='NASDAQ')  

   

def handle_data(context, data):

    position = get_position(context.security)

    print ('Position:',position.amount, '; In:',position.cost_basis,', Last:',data.current(context.security,'price'))

    if (context.flag1==False and position.amount == 0):

        order_percent (context.security, 0.001, style = MarketOrder())

        context.flag1= True

    if (context.flag==False and position.amount != 0):

             if data.current(context.security,'price')  <= (1-((context.risk)/100))position.cost_basis:

                  order(context.security,-1
position.amount , style=MarketOrder())

                  context.flag = True

    else:

          end            









How can I fetch the average price of the total number of shared traded? (not just the first part when there is a partial execution from more thn one exchanges)







 

Hi Ghery,



If you use the "get_all_open_orders()" function it will return you a dictionary where the key is orderId and the value is Order object. You can use this order object to get the average fill price like this "order.avgFillPrice".



Documentation of "get_all_open_orders()" function: Documentation -

Documentation of order object: Documentation -

Thanks a lot