forked from HellAmbro/Trading212API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
110 lines (88 loc) · 2.88 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import sys
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import pandas as pd
from pytrading212 import * # just for simplicity, not recommended, import only what you use
from pytrading212.trading212 import Period
if __name__ == "__main__":
# email and password passed as program arguments,
# change this with your credentials
email = sys.argv[1]
password = sys.argv[2]
# Use your preferred web driver with your custom options
# options = Options()
# headless (optional)
# options.add_argument('--headless')
# options.add_argument('--disable-gpu')
# Chrome
driver = webdriver.Chrome(executable_path='chromedriver.exe')
# or Firefox
# driver = webdriver.Firefox()
trading212 = Trading212(email, password, driver, mode=Mode.DEMO)
market_order = trading212.execute_order(
MarketOrder(instrument_code="AMZN_US_EQ", quantity=2)
)
limit_order = trading212.execute_order(
LimitOrder(
instrument_code="AMZN_US_EQ",
quantity=2,
limit_price=3000,
time_validity=TimeValidity.DAY,
)
)
stop_order = trading212.execute_order(
StopOrder(
instrument_code="AMZN_US_EQ",
quantity=2,
stop_price=4000,
time_validity=TimeValidity.GOOD_TILL_CANCEL,
)
)
stop_limit = trading212.execute_order(
StopLimitOrder(
instrument_code="AMZN_US_EQ",
quantity=2,
limit_price=3000,
stop_price=4000,
time_validity=TimeValidity.GOOD_TILL_CANCEL,
)
)
quantity_order = trading212.execute_order(
EquityOrder(
"AMZN_US_EQ",
quantity=2,
limit_price=3000,
stop_price=4000,
time_validity=TimeValidity.GOOD_TILL_CANCEL,
)
)
value_order = trading212.execute_value_order(ValueOrder("AMZN_US_EQ", value=100))
# sell an equity that you own
value_sell_order = trading212.execute_value_order(
ValueOrder("AMZN_US_EQ", value=-100)
)
sell_limit = trading212.execute_order(
LimitOrder(
instrument_code="AMZN_US_EQ",
quantity=-2,
limit_price=4000,
time_validity=TimeValidity.GOOD_TILL_CANCEL,
)
)
print(quantity_order)
print(value_order)
funds = trading212.get_funds()
orders = trading212.get_orders()
print(funds)
print(orders)
portfolio = trading212.get_portfolio_composition()
performance = trading212.get_portfolio_performance(Period.LAST_DAY)
print(portfolio)
print(performance)
# Pandas integration examples
funds_df = pd.DataFrame(funds)
print(funds_df)
orders_df = pd.DataFrame(orders)
print(orders_df)
portfolio_df = pd.DataFrame(portfolio)
performance_df = pd.DataFrame(performance)