-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapp.py
169 lines (121 loc) · 3.42 KB
/
app.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
from typing import List
from pydantic import BaseModel
from fastapi import FastAPI
from ejtraderCT import Ctrader
app = FastAPI()
api = None
class LoginModel(BaseModel):
server: str
account: str
password: str
class SymbolModel(BaseModel):
symbols: List[str]
class OrderModel(BaseModel):
symbol: str
volume: float
stoploss: float
takeprofit: float
price: float = None
class ModifyModel(BaseModel):
id: str
stoploss: float
takeprofit: float
price: float = None
class IdModel(BaseModel):
id: str
@app.post("/login")
async def login():
global api
api = Ctrader(
os.getenv("HOST_NAME"), os.getenv("SENDER_COMPID"), os.getenv("PASSWORD")
)
if api.isconnected():
return {"message": "Logged in"}
else:
return {"error": "Check your credencials"}
@app.get("/quote/{symbol}")
async def quote(symbol: str = None):
if symbol:
quote = api.quote(symbol)
else:
quote = api.quote()
return quote
@app.post("/buy")
async def buy(order: OrderModel):
return {
"Position": api.buy(
order.symbol, order.volume, order.stoploss, order.takeprofit
)
}
@app.post("/sell")
async def sell(order: OrderModel):
return {
"Position": api.sell(
order.symbol, order.volume, order.stoploss, order.takeprofit
)
}
@app.post("/buyLimit")
async def buy_limit(order: OrderModel):
return {
"Order": api.buyLimit(
order.symbol, order.volume, order.stoploss, order.takeprofit, order.price
)
}
@app.post("/sellLimit")
async def sell_limit(order: OrderModel):
return {
"Order": api.sellLimit(
order.symbol, order.volume, order.stoploss, order.takeprofit, order.price
)
}
@app.post("/buyStop")
async def buy_stop(order: OrderModel):
return {
"Order": api.buyStop(
order.symbol, order.volume, order.stoploss, order.takeprofit, order.price
)
}
@app.post("/sellStop")
async def sell_stop(order: OrderModel):
return {
"Order": api.sellStop(
order.symbol, order.volume, order.stoploss, order.takeprofit, order.price
)
}
@app.get("/positions")
async def positions():
return api.positions()
@app.get("/orders")
async def orders():
return api.orders()
@app.post("/orderCancelById")
async def order_cancel_by_id(id_model: IdModel):
api.orderCancelById(id_model.id)
return {"message": "Order cancelled"}
@app.post("/positionCloseById")
async def position_close_by_id(id_model: IdModel):
api.positionCloseById(id_model.id)
return {"message": "Position closed"}
@app.post("/cancel_all")
async def cancel_all():
api.cancel_all()
return {"message": "All orders cancelled"}
@app.post("/close_all")
async def close_all():
api.close_all()
return {"message": "All positions closed"}
@app.post("/positionModify")
async def position_modify(modify: ModifyModel):
api.positionModify(modify.id, modify.stoploss, modify.takeprofit)
return {"message": "Position modified"}
@app.post("/orderModify")
async def order_modify(modify: ModifyModel):
api.orderModify(modify.id, modify.stoploss, modify.takeprofit, modify.price)
return {"message": "Order modified"}
@app.post("/logout")
async def logout():
return {"message": api.logout()}
@app.post("/status")
async def status():
return {"connected": api.isconnected()}