115 lines
5.2 KiB
Markdown
115 lines
5.2 KiB
Markdown
# copy_rates_from_pos
|
||
|
||
Get bars from the MetaTrader 5 terminal starting from the specified index.
|
||
|
||
```
|
||
copy_rates_from_pos(
|
||
symbol, // symbol name
|
||
timeframe, // timeframe
|
||
start_pos, // initial bar index
|
||
count // number of bars
|
||
)
|
||
|
||
```
|
||
|
||
Parameters
|
||
|
||
symbol
|
||
|
||
[in] Financial instrument name, for example, "EURUSD". Required unnamed parameter.
|
||
|
||
timeframe
|
||
|
||
[in] Timeframe the bars are requested for. Set by a value from the [TIMEFRAME](/en/docs/python_metatrader5/mt5copyratesfrom_py#timeframe) enumeration. Required unnamed parameter.
|
||
|
||
start_pos
|
||
|
||
[in] Initial index of the bar the data are requested from. The numbering of bars goes from present to past. Thus, the zero bar means the current one. Required unnamed parameter.
|
||
|
||
count
|
||
|
||
[in] Number of bars to receive. Required unnamed parameter.
|
||
|
||
Return Value
|
||
|
||
Returns bars as the numpy array with the named time, open, high, low, close, tick_volume, spread and real_volume columns. Returns None in case of an error. The info on the error can be obtained using [last_error()](/en/docs/python_metatrader5/mt5lasterror_py).
|
||
|
||
Note
|
||
|
||
See the [CopyRates()](/en/docs/series/copyrates) function for more information.
|
||
|
||
MetaTrader 5 terminal provides bars only within a history available to a user on charts. The number of bars available to users is set in the "[Max. bars in chart](https://www.metatrader5.com/en/terminal/help/startworking/settings#max_bars)" parameter.
|
||
|
||
Example:
|
||
|
||
```
|
||
from datetime import datetime
|
||
import MetaTrader5 as mt5
|
||
# display data on the MetaTrader 5 package
|
||
print("MetaTrader5 package author: ",mt5.__author__)
|
||
print("MetaTrader5 package version: ",mt5.__version__)
|
||
|
||
# import the 'pandas' module for displaying data obtained in the tabular form
|
||
import pandas as pd
|
||
pd.set_option('display.max_columns', 500) # number of columns to be displayed
|
||
pd.set_option('display.width', 1500) # max table width to display
|
||
|
||
# establish connection to MetaTrader 5 terminal
|
||
if not mt5.initialize():
|
||
print("initialize() failed, error code =",mt5.last_error())
|
||
quit()
|
||
|
||
# get 10 GBPUSD D1 bars from the current day
|
||
rates = mt5.copy_rates_from_pos("GBPUSD", mt5.TIMEFRAME_D1, 0, 10)
|
||
|
||
# shut down connection to the MetaTrader 5 terminal
|
||
mt5.shutdown()
|
||
# display each element of obtained data in a new line
|
||
print("Display obtained data 'as is'")
|
||
for rate in rates:
|
||
print(rate)
|
||
|
||
# create DataFrame out of the obtained data
|
||
rates_frame = pd.DataFrame(rates)
|
||
# convert time in seconds into the datetime format
|
||
rates_frame['time']=pd.to_datetime(rates_frame['time'], unit='s')
|
||
|
||
# display data
|
||
print("\nDisplay dataframe with data")
|
||
print(rates_frame)
|
||
|
||
Result:
|
||
MetaTrader5 package author: MetaQuotes Software Corp.
|
||
MetaTrader5 package version: 5.0.29
|
||
|
||
Display obtained data 'as is'
|
||
(1581552000, 1.29568, 1.30692, 1.29441, 1.30412, 68228, 0, 0)
|
||
(1581638400, 1.30385, 1.30631, 1.3001, 1.30471, 56498, 0, 0)
|
||
(1581897600, 1.30324, 1.30536, 1.29975, 1.30039, 49400, 0, 0)
|
||
(1581984000, 1.30039, 1.30486, 1.29705, 1.29952, 62288, 0, 0)
|
||
(1582070400, 1.29952, 1.3023, 1.29075, 1.29187, 57909, 0, 0)
|
||
(1582156800, 1.29186, 1.29281, 1.28489, 1.28792, 61033, 0, 0)
|
||
(1582243200, 1.28802, 1.29805, 1.28746, 1.29566, 66386, 0, 0)
|
||
(1582502400, 1.29426, 1.29547, 1.28865, 1.29283, 66933, 0, 0)
|
||
(1582588800, 1.2929, 1.30178, 1.29142, 1.30037, 80121, 0, 0)
|
||
(1582675200, 1.30036, 1.30078, 1.29136, 1.29374, 49286, 0, 0)
|
||
|
||
Display dataframe with data
|
||
time open high low close tick_volume spread real_volume
|
||
0 2020-02-13 1.29568 1.30692 1.29441 1.30412 68228 0 0
|
||
1 2020-02-14 1.30385 1.30631 1.30010 1.30471 56498 0 0
|
||
2 2020-02-17 1.30324 1.30536 1.29975 1.30039 49400 0 0
|
||
3 2020-02-18 1.30039 1.30486 1.29705 1.29952 62288 0 0
|
||
4 2020-02-19 1.29952 1.30230 1.29075 1.29187 57909 0 0
|
||
5 2020-02-20 1.29186 1.29281 1.28489 1.28792 61033 0 0
|
||
6 2020-02-21 1.28802 1.29805 1.28746 1.29566 66386 0 0
|
||
7 2020-02-24 1.29426 1.29547 1.28865 1.29283 66933 0 0
|
||
8 2020-02-25 1.29290 1.30178 1.29142 1.30037 80121 0 0
|
||
9 2020-02-26 1.30036 1.30078 1.29136 1.29374 49286 0 0
|
||
|
||
```
|
||
|
||
See also
|
||
|
||
[CopyRates](/en/docs/series/copyrates), [copy_rates_from](/en/docs/python_metatrader5/mt5copyratesfrom_py), [copy_rates_range](/en/docs/python_metatrader5/mt5copyratesrange_py), [copy_ticks_from](/en/docs/python_metatrader5/mt5copyticksfrom_py), [copy_ticks_range](/en/docs/python_metatrader5/mt5copyticksrange_py)
|