Skip to content

Commit 504eedc

Browse files
authored
Add files via upload
1 parent fe4054e commit 504eedc

14 files changed

+20548
-0
lines changed

01_Basics_of_Python.ipynb

+1,978
Large diffs are not rendered by default.

02_Python_for_DataSciences.ipynb

+5,520
Large diffs are not rendered by default.

03 Introduction .ipynb

+611
Large diffs are not rendered by default.

04 Import financial data.ipynb

+1,522
Large diffs are not rendered by default.

05 Send orders.ipynb

+444
Large diffs are not rendered by default.

06_money_management.ipynb

+389
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,389 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "735390d1",
6+
"metadata": {},
7+
"source": [
8+
"# Chapter 06: Money management\n",
9+
"\n",
10+
"📈Join our community: https://discord.gg/wXjNPAc5BH\n",
11+
"\n",
12+
"📚Read our book: https://www.amazon.com/gp/product/B09HG18CYL \n",
13+
"\n",
14+
"🖥️Quantreo's YouTube channel: https://www.youtube.com/channel/UCp7jckfiEglNf_Gj62VR0pw"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"id": "c9512371",
21+
"metadata": {},
22+
"outputs": [],
23+
"source": [
24+
"# Import the libraries\n",
25+
"import MetaTrader5 as mt5\n",
26+
"from datetime import datetime\n",
27+
"import numpy as np"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 2,
33+
"id": "24a9d118",
34+
"metadata": {},
35+
"outputs": [
36+
{
37+
"data": {
38+
"text/plain": [
39+
"True"
40+
]
41+
},
42+
"execution_count": 2,
43+
"metadata": {},
44+
"output_type": "execute_result"
45+
}
46+
],
47+
"source": [
48+
"# Create the bound between MT5 and Python\n",
49+
"mt5.initialize()"
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"id": "d07b406f",
55+
"metadata": {},
56+
"source": [
57+
"### Place an order with take profit & stop loss"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 7,
63+
"id": "da38caed",
64+
"metadata": {},
65+
"outputs": [
66+
{
67+
"data": {
68+
"text/plain": [
69+
"'Done'"
70+
]
71+
},
72+
"execution_count": 7,
73+
"metadata": {},
74+
"output_type": "execute_result"
75+
}
76+
],
77+
"source": [
78+
"# Place a BUY order with take profit\n",
79+
"# Initialization value\n",
80+
"lot = 0.01\n",
81+
"symbol = \"EURUSD\"\n",
82+
"\n",
83+
"# Extract symbol point\n",
84+
"point = mt5.symbol_info(symbol).point\n",
85+
"\n",
86+
"# Choose the deviation\n",
87+
"deviation = 10\n",
88+
"\n",
89+
"# Find the filling mode of symbol\n",
90+
"filling_type = mt5.symbol_info(symbol).filling_mode\n",
91+
"\n",
92+
"# Create dictionnary request\n",
93+
"request = {\n",
94+
" \"action\": mt5.TRADE_ACTION_DEAL,\n",
95+
" \"symbol\": symbol,\n",
96+
" \"volume\": lot,\n",
97+
" \"type\": mt5.ORDER_TYPE_BUY,\n",
98+
" \"price\": mt5.symbol_info_tick(symbol).ask,\n",
99+
" \"deviation\": deviation,\n",
100+
" \"tp\": mt5.symbol_info_tick(symbol).ask + 100*point,\n",
101+
" \"sl\": mt5.symbol_info_tick(symbol).ask - 100*point, \n",
102+
" \"type_filling\": filling_type,\n",
103+
" \"type_time\": mt5.ORDER_TIME_GTC,\n",
104+
"}\n",
105+
"\n",
106+
"mt5.order_check(request).comment"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 7,
112+
"id": "cdb0bbb3",
113+
"metadata": {},
114+
"outputs": [
115+
{
116+
"data": {
117+
"text/plain": [
118+
"'Done'"
119+
]
120+
},
121+
"execution_count": 7,
122+
"metadata": {},
123+
"output_type": "execute_result"
124+
}
125+
],
126+
"source": [
127+
"# Place a SELL order with take profit\n",
128+
"# Initialization value\n",
129+
"lot = 0.01\n",
130+
"symbol = \"EURUSD\"\n",
131+
"\n",
132+
"# Extract symbol point\n",
133+
"point = mt5.symbol_info(symbol).point\n",
134+
"\n",
135+
"# Choose the deviation\n",
136+
"deviation = 10\n",
137+
"\n",
138+
"# Find the filling mode of symbol\n",
139+
"filling_type = mt5.symbol_info(symbol).filling_mode\n",
140+
"\n",
141+
"# Create dictionnary request\n",
142+
"request = {\n",
143+
" \"action\": mt5.TRADE_ACTION_DEAL,\n",
144+
" \"symbol\": symbol,\n",
145+
" \"volume\": lot,\n",
146+
" \"type\": mt5.ORDER_TYPE_SELL,\n",
147+
" \"price\": mt5.symbol_info_tick(symbol).bid,\n",
148+
" \"deviation\": deviation,\n",
149+
" \"tp\": mt5.symbol_info_tick(symbol).ask - 100 * point,\n",
150+
" \"sl\": mt5.symbol_info_tick(symbol).ask + 100 * point, \n",
151+
" \"type_filling\": filling_type,\n",
152+
" \"type_time\": mt5.ORDER_TIME_GTC,\n",
153+
"}\n",
154+
"\n",
155+
"mt5.order_check(request).comment"
156+
]
157+
},
158+
{
159+
"cell_type": "markdown",
160+
"id": "f2beacd7",
161+
"metadata": {},
162+
"source": [
163+
"### Find stop loss and take profit level for a specific risk percentage"
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": 5,
169+
"id": "43d6988c",
170+
"metadata": {},
171+
"outputs": [],
172+
"source": [
173+
"def risk_reward_threshold(symbol, buy=True, risk=0.01, reward=0.02):\n",
174+
" \n",
175+
" # Extract the leverage\n",
176+
" leverage = mt5.account_info().leverage\n",
177+
"\n",
178+
" # Compute the price\n",
179+
" price = mt5.symbol_info(symbol).ask\n",
180+
"\n",
181+
" # Extract the number of decimals\n",
182+
" nb_decimal = str(price)[::-1].find(\".\")\n",
183+
"\n",
184+
"\n",
185+
" # Compute the variations in percentage\n",
186+
" var_down = risk/leverage\n",
187+
" var_up = reward/leverage\n",
188+
"\n",
189+
"\n",
190+
" # Find the TP and SL threshold in absolute price\n",
191+
" if buy:\n",
192+
" price = mt5.symbol_info(symbol).ask\n",
193+
"\n",
194+
" # Compute the variations in absolute price\n",
195+
" price_var_down = var_down*price\n",
196+
" price_var_up = var_up * price\n",
197+
"\n",
198+
" tp = np.round(price + price_var_up, nb_decimal)\n",
199+
" sl = np.round(price - price_var_down, nb_decimal)\n",
200+
"\n",
201+
" else:\n",
202+
"\n",
203+
" price = mt5.symbol_info(symbol).bid\n",
204+
"\n",
205+
" # Compute the variations in absolute price\n",
206+
" price_var_down = var_down*price\n",
207+
" price_var_up = var_up * price\n",
208+
"\n",
209+
" tp = np.round(price - price_var_up, nb_decimal)\n",
210+
" sl = np.round(price + price_var_down, nb_decimal)\n",
211+
"\n",
212+
"\n",
213+
" \n",
214+
" print(f\"PRICE: {price} \\t Take Profit: {tp} \\t Stop Loss: {sl}\")"
215+
]
216+
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": 6,
220+
"id": "bc012463",
221+
"metadata": {},
222+
"outputs": [
223+
{
224+
"name": "stdout",
225+
"output_type": "stream",
226+
"text": [
227+
"PRICE: 1.1015 \t Take Profit: 1.1022 \t Stop Loss: 1.1011\n"
228+
]
229+
}
230+
],
231+
"source": [
232+
"risk_reward_threshold(\"EURUSD\",buy=True)"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": 18,
238+
"id": "fc2136a8",
239+
"metadata": {},
240+
"outputs": [
241+
{
242+
"data": {
243+
"text/plain": [
244+
"-1.002798720064676"
245+
]
246+
},
247+
"execution_count": 18,
248+
"metadata": {},
249+
"output_type": "execute_result"
250+
}
251+
],
252+
"source": [
253+
"(1.09682 - 1.09693) / 1.09693 *100 * 100"
254+
]
255+
},
256+
{
257+
"cell_type": "markdown",
258+
"id": "d98cfa33",
259+
"metadata": {},
260+
"source": [
261+
"### Find the volume depending of your capital"
262+
]
263+
},
264+
{
265+
"cell_type": "code",
266+
"execution_count": 28,
267+
"id": "a1a2de6c",
268+
"metadata": {},
269+
"outputs": [
270+
{
271+
"name": "stdout",
272+
"output_type": "stream",
273+
"text": [
274+
"INVESTED CAPITAL: 120\n",
275+
"LEVERAGE: 100\n",
276+
"INVESTED CAPITAL LEVERAGED: 12000\n",
277+
"TRADE SIZE: 10.0\n",
278+
"PRICE: 4312.75\n",
279+
"LOT SIZE: 0.27824473943539507\n",
280+
"MIN LOT: 0.1\n",
281+
"MAX LOT: 5.0\n",
282+
"NUMBER DECIMAL: 1\n",
283+
"LOT SIZE ROUNDED: 0.3\n",
284+
"LOT DOWN ROUNDED: 0.2\n",
285+
"GOOD SIZE LOT: 0.2\n"
286+
]
287+
},
288+
{
289+
"data": {
290+
"text/plain": [
291+
"0.2"
292+
]
293+
},
294+
"execution_count": 28,
295+
"metadata": {},
296+
"output_type": "execute_result"
297+
}
298+
],
299+
"source": [
300+
"def position_size(capital, symbol):\n",
301+
" mt5.initialize()\n",
302+
" print(f\"INVESTED CAPITAL: {capital}\")\n",
303+
" \n",
304+
" leverage = mt5.account_info().leverage\n",
305+
" print(f\"LEVERAGE: {leverage}\")\n",
306+
" \n",
307+
" invested_capital = capital * leverage\n",
308+
" print(f\"INVESTED CAPITAL LEVERAGED: {invested_capital}\")\n",
309+
" \n",
310+
" trade_size = mt5.symbol_info(symbol).trade_contract_size\n",
311+
" print(f\"TRADE SIZE: {trade_size}\")\n",
312+
" \n",
313+
" price = (mt5.symbol_info(symbol).ask + mt5.symbol_info(symbol).bid)/2\n",
314+
" print(f\"PRICE: {price}\")\n",
315+
"\n",
316+
" lot_size = invested_capital / trade_size / price\n",
317+
" print(f\"LOT SIZE: {lot_size}\")\n",
318+
" \n",
319+
" min_lot = mt5.symbol_info(symbol).volume_min\n",
320+
" print(f\"MIN LOT: {min_lot}\")\n",
321+
" \n",
322+
" max_lot = mt5.symbol_info(symbol).volume_max\n",
323+
" print(f\"MAX LOT: {max_lot}\")\n",
324+
"\n",
325+
"\n",
326+
" if min_lot<lot_size:\n",
327+
" number_decimal = str(min_lot)[::-1].find(\".\")\n",
328+
" print(f\"NUMBER DECIMAL: {number_decimal}\")\n",
329+
"\n",
330+
" if number_decimal>0:\n",
331+
" lot_size_rounded = np.round(lot_size, number_decimal)\n",
332+
" print(f\"LOT SIZE ROUNDED: {lot_size_rounded}\")\n",
333+
"\n",
334+
" if lot_size < lot_size_rounded:\n",
335+
" lot_size_rounded = np.round(lot_size_rounded - min_lot, number_decimal)\n",
336+
" print(f\"LOT DOWN ROUNDED: {lot_size_rounded}\")\n",
337+
"\n",
338+
" else:\n",
339+
" number_size_lot = len(str(min_lot))\n",
340+
"\n",
341+
" lot_size_rounded = int(np.round(lot_size, -number_size_lot))\n",
342+
"\n",
343+
" if lot_size < lot_size_rounded:\n",
344+
" lot_size_rounded = int(np.round(lot_size_rounded - number_size_lot, - number_size_lot))\n",
345+
" \n",
346+
" if lot_size_rounded>max_lot:\n",
347+
" lot_size_rounded = max_lot\n",
348+
" \n",
349+
" print(f\"GOOD SIZE LOT: {lot_size_rounded}\")\n",
350+
" return lot_size_rounded\n",
351+
" else: \n",
352+
" print(\"Invested capital is too small to be able to place an order\")\n",
353+
" \n",
354+
" \n",
355+
" \n",
356+
"position_size(120, \"SP500m\")"
357+
]
358+
},
359+
{
360+
"cell_type": "code",
361+
"execution_count": null,
362+
"id": "055bf5a5",
363+
"metadata": {},
364+
"outputs": [],
365+
"source": []
366+
}
367+
],
368+
"metadata": {
369+
"kernelspec": {
370+
"display_name": "Python 3 (ipykernel)",
371+
"language": "python",
372+
"name": "python3"
373+
},
374+
"language_info": {
375+
"codemirror_mode": {
376+
"name": "ipython",
377+
"version": 3
378+
},
379+
"file_extension": ".py",
380+
"mimetype": "text/x-python",
381+
"name": "python",
382+
"nbconvert_exporter": "python",
383+
"pygments_lexer": "ipython3",
384+
"version": "3.9.7"
385+
}
386+
},
387+
"nbformat": 4,
388+
"nbformat_minor": 5
389+
}

0 commit comments

Comments
 (0)