<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Basics
Let's first take a look at what's inside the ib_insync package
Step1:  Importing
The following two lines are used at the top of all notebooks. The first line imports everything and the second
starts an event loop to keep the notebook live updated
Step2:  Note that startLoop() only works in notebooks, not in regular Python programs.
Connecting
The main player of the whole package is the "IB" class. Let's create an IB instance and connect to a running TWS/IBG application
Step3:  If the connection failed, then verify that the application has the API port enabled and double-check the hostname and port. For IB Gateway the default port is 4002. Make sure the clientId is not already in use.
If the connection succeeded, then ib will be synchronized with TWS/IBG. The "current state" is now available via methods such as ib.positions(), ib.trades(), ib.openTrades(), ib.accountValues() or ib.tickers(). Let's list the current positions
Step4:  Or filter the account values to get the liquidation value
Step5:  The "current state" will automatically be kept in sync with TWS/IBG. So an order fill will be added as soon as it is reported, or account values will be updated as soon as they change in TWS.
Contracts
Contracts can be specified in different ways
Step6:  Sending a request
The IB class has nearly all request methods that the IB API offers. The methods that return a result will block until finished and then return the result. Take for example reqContractDetails
Step7:  Current state vs request
Doing a request involves network traffic going up and down and can take considerable time. The current state on the other hand is always immediately available. So it is preferable to use the current state methods over requests. For example, use ib.openOrders() in preference over ib.reqOpenOrders(), or ib.positions() over ib.reqPositions(), etc
Step8:  Logging
The following will put log messages of INFO and higher level under the current active cell
Step9:  To see all debug messages (including network traffic)
Step10:  Disconnecting
The following will disconnect ib and clear all its state
<ASSISTANT_TASK:>
Python Code:
import ib_insync
print(ib_insync.__all__)
Explanation: Basics
Let's first take a look at what's inside the ib_insync package:
End of explanation
from ib_insync import *
util.startLoop()
Explanation: Importing
The following two lines are used at the top of all notebooks. The first line imports everything and the second
starts an event loop to keep the notebook live updated:
End of explanation
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=10)
Explanation: Note that startLoop() only works in notebooks, not in regular Python programs.
Connecting
The main player of the whole package is the "IB" class. Let's create an IB instance and connect to a running TWS/IBG application:
End of explanation
ib.positions()
Explanation: If the connection failed, then verify that the application has the API port enabled and double-check the hostname and port. For IB Gateway the default port is 4002. Make sure the clientId is not already in use.
If the connection succeeded, then ib will be synchronized with TWS/IBG. The "current state" is now available via methods such as ib.positions(), ib.trades(), ib.openTrades(), ib.accountValues() or ib.tickers(). Let's list the current positions:
End of explanation
[v for v in ib.accountValues() if v.tag == 'NetLiquidationByCurrency' and v.currency == 'BASE']
Explanation: Or filter the account values to get the liquidation value:
End of explanation
Contract(conId=270639)
Stock('AMD', 'SMART', 'USD')
Stock('INTC', 'SMART', 'USD', primaryExchange='NASDAQ')
Forex('EURUSD')
CFD('IBUS30')
Future('ES', '20180921', 'GLOBEX')
Option('SPY', '20170721', 240, 'C', 'SMART')
Bond(secIdType='ISIN', secId='US03076KAA60');
Explanation: The "current state" will automatically be kept in sync with TWS/IBG. So an order fill will be added as soon as it is reported, or account values will be updated as soon as they change in TWS.
Contracts
Contracts can be specified in different ways:
* The ibapi way, by creating an empty Contract object and setting its attributes one by one;
* By using Contract and giving the attributes as keyword argument;
* By using the specialized Stock, Option, Future, Forex, Index, CFD, Commodity,
  Bond, FuturesOption, MutualFund or Warrant contracts.
Some examples:
End of explanation
contract = Stock('TSLA', 'SMART', 'USD')
ib.reqContractDetails(contract)
Explanation: Sending a request
The IB class has nearly all request methods that the IB API offers. The methods that return a result will block until finished and then return the result. Take for example reqContractDetails:
End of explanation
%time l = ib.positions()
%time l = ib.reqPositions()
Explanation: Current state vs request
Doing a request involves network traffic going up and down and can take considerable time. The current state on the other hand is always immediately available. So it is preferable to use the current state methods over requests. For example, use ib.openOrders() in preference over ib.reqOpenOrders(), or ib.positions() over ib.reqPositions(), etc:
End of explanation
util.logToConsole()
Explanation: Logging
The following will put log messages of INFO and higher level under the current active cell:
End of explanation
import logging
util.logToConsole(logging.DEBUG)
Explanation: To see all debug messages (including network traffic):
End of explanation
ib.disconnect()
Explanation: Disconnecting
The following will disconnect ib and clear all its state:
End of explanation
<END_TASK>