Skip to content

Commit 217fe51

Browse files
Update README.md
1 parent ce7ce8a commit 217fe51

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

README.md

+65
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,72 @@ Paper Trading on IB
708708
Once a strategy has been back tested for basic operation it should be paper traded i.e. where simulated trades are generated and marked using live data to gain confidence in your overall trading strategy and goal.
709709

710710
Be careful to login to TWS using the Paper Trading option (see arrow):
711+
![Paper Trading](images/image-06.png "Paper Trading")
711712

713+
In addition to selecting “Paper Trading” the execution of the strategy also needs to be changed to use IB as the broker to place orders and update positions.
714+
715+
```python
716+
import backtrader as bt
717+
718+
from atreyu_backtrader_api import IBData
719+
from atreyu_backtrader_api import IBStore
720+
721+
from test_strategy import TestStrategy
722+
723+
import datetime as dt
724+
from datetime import datetime, date, time
725+
726+
cerebro = bt.Cerebro()
727+
728+
ibstore = IBStore(host='127.0.0.1',
729+
port=7497,
730+
clientId=35)
731+
732+
data = ibstore.getdata(name="AAPL", # Data name
733+
dataname='AAPL', # Symbol name
734+
secType='STK', # SecurityType is STOCK
735+
exchange='SMART', # Trading exchange IB's SMART exchange
736+
currency='USD' # Currency of SecurityType
737+
)
738+
739+
cerebro.adddata(data)
740+
741+
broker = ibstore.getbroker()
742+
743+
# Set the broker
744+
cerebro.setbroker(broker)
745+
746+
# Add the test strategy
747+
cerebro.addstrategy(TestStrategy)
748+
749+
# Add a FixedSize sizer according to the stake
750+
cerebro.addsizer(bt.sizers.FixedSize, stake=10)
751+
752+
cerebro.run()
753+
```
754+
755+
Output
756+
------
757+
```
758+
...
759+
2022-08-17 19:06:33.352691, Close, 174.84
760+
2022-08-17 19:06:33.352741, Close, 174.84
761+
2022-08-17 19:06:33.352781, Close, 174.84
762+
2022-08-17 19:06:33.601432, SELL EXECUTED, Price: 174.83, Cost: 1749.40, Comm: 1.04
763+
2022-08-17 19:06:33.601432, OPERATION PROFIT, GROSS: -0.10, NET: -2.14
764+
2022-08-17 19:06:33.601432, Close, 174.83
765+
2022-08-17 19:06:33.601462, Close, 174.83
766+
2022-08-17 19:06:33.601522, Close, 174.85
767+
2022-08-17 19:06:33.601522, BUY CREATE @MKT: 174.85
768+
2022-08-17 19:06:33.601522, BUY CREATED Size: 10 @ MKT
769+
2022-08-17 19:06:33.601533, Close, 174.85
770+
...
771+
```
772+
773+
Once the strategy is initiated the orders start appearing in TWS
774+
----------------------------------------------------------------
775+
776+
![Main Trading](images/image-07.png "Main Trading")
712777

713778
Disclaimer
714779
----------

0 commit comments

Comments
 (0)