Search code examples
pythoninteractive-brokersibpy

How to print variable from another file


In the code bellow I am receiving the position of XAUUSD from Interactive Brokers. Can someone tell me how can I catch updated value of the variable maypos in another python file?

from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
import time
import os
import datetime
import random


def error_handler(msg):
    print ("Server Error: %s" % msg)
    return


def replies_handler(msg):
    print ("Server Response: %s, %s" % (msg.typeName, msg))
    return


def print_portfolio_position(msg):

    if msg.contract.m_symbol == "XAUUSD":
       mypos = msg.position
       print("Position:", msg.contract.m_symbol, msg.position, 
               msg.marketPrice, msg.contract.m_currency, msg.contract.m_secType)


if __name__ == "__main__":

    while 1:


        time.sleep(5)
        conn = Connection.create(port=4096, clientId=1)
        conn.connect()
        conn.register(error_handler, 'Error')
        conn.register(print_portfolio_position, 'UpdatePortfolio')
        conn.register(replies_handler, 'UpdateAccountValue')
        conn.reqAccountUpdates(1, '')
        time.sleep(2)
        conn.disconnect()

Solution

  • Question: how can I catch updated value of the variable maypos in another python file?

    from another python file import maypos
    
    value = maypos.updated value