def _create_session(self) -> IBClient: """Start a new session. Go to initiate an IBClient object and the session will be passed onto trader object Creates a new session with the IB Client API and logs the user into the new session. Returns: ---- IBClient -- A IBClient object with an authenticated sessions. """ ib_client = IBClient(username=self.username, account=self.account, is_server_running=True) #Start a new session ib_client.create_session() return ib_client
class InteractiveBrokersSession(TestCase): """Will perform a unit test for the Interactive Brokers session.""" def setUp(self) -> None: """Set up the Session.""" # Grab configuration values. config = ConfigParser() file_path = pathlib.Path('config/config.ini').resolve() config.read(file_path) # Load the details. self.paper_account = config.get('main', 'PAPER_ACCOUNT') self.paper_username = config.get('main', 'PAPER_USERNAME') # Initalize the Client self.ibw_client = IBClient(username=self.paper_username, account=self.paper_account) def test_creates_instance_of_session(self): """Ensure the instance was created.""" self.assertIsInstance(self.ibw_client, IBClient) self.assertEqual(self.paper_account, self.ibw_client.account) self.assertEqual(self.paper_username, self.ibw_client.username) self.assertEqual(False, self.ibw_client.authenticated) def test_session_properties(self): """Checks different session properties.""" self.assertEqual(self.ibw_client._operating_system, sys.platform) def test_ibw_portal_path(self): """Ensures the Portal Path Matches.""" folder_path = pathlib.Path('clientportal.beta.gw').resolve() self.assertEqual(self.ibw_client.client_portal_folder, folder_path) def test_session_state_path(self): """Ensures the Session JSON path is valid.""" session_state_path = pathlib.Path("ibw/server_session.json").resolve() self.assertEqual(session_state_path, self.ibw_client.session_state_path) def test_create_session(self): """Test Creating the session.""" session_response = self.ibw_client.create_session() self.assertTrue(session_response) self.assertTrue(self.ibw_client.authenticated) def tearDown(self) -> None: """Teardown the Session.""" self.ibw_client = None
from ibw.client import IBClient from ibw.configAlex import REGULAR_ACCOUNT, REGULAR_PASSWORD, REGULAR_USERNAME, PAPER_ACCOUNT, PAPER_PASSWORD, PAPER_USERNAME # Create a new session of the IB Web API. ib_client = IBClient(username=PAPER_USERNAME, password=PAPER_PASSWORD, account=PAPER_ACCOUNT) ''' SESSIONS ''' # create a new session. ib_client.create_session() # close the current session. # ib_client.close_session() ''' ACCOUNT DETAILS ''' # grab the account data. account_data = ib_client.portfolio_accounts() print(account_data) print('') # grab account portfolios account_positions = ib_client.portfolio_account_positions( account_id=PAPER_ACCOUNT, page_id=0) print(account_positions) print('')