TheSnowGuru / PyTrader-python-mt4-mt5-trading-api-connector-drag-n-drop

Open Source Trading Strategies & End-to-End solution connecting Metatrader4 & Metatrader5 💹 with Python with a simple drag and drop EA. Fully tested bug free & efficient solution for live & paper trading⭐ Full Documentation ready. Lightweight, efficient and stable implementation 🔥
https://discord.gg/wRMUNP8ERa
674 stars 14 forks source link

Limitation for get all closed positions #7

Closed brenoperucchi closed 3 years ago

brenoperucchi commented 3 years ago

Has some limitations about the number of information you could request?

I only get 14 most recent pieces of information but have more than 30 records.

(Pdb) timezone = pytz.timezone("Etc/UTC")
(Pdb) self.connect().Get_all_closed_positions(date_from=datetime(2020, 3, 3, tzinfo=timezone), date_to=datetime.now())

    position_ticket instrument  order_ticket position_type  magic_number  volume  open_price   open_time  close_price    close_time        comment  profit  swap  commission
0         832580774     EURUSD     832580946          sell          2000    0.03     1.20699  1610953450      1.20700  1.610953e+09  Perucchi Inc    -0.03   0.0         0.0
1         832580780     EURUSD     832580986          sell          2000    0.02     1.20699  1610953451      1.20700  1.610953e+09  Perucchi Inc    -0.02   0.0         0.0
2         832580815     EURUSD     832581007          sell          2000    0.03     1.20699  1610953453      1.20700  1.610953e+09  Perucchi Inc    -0.03   0.0         0.0
3         832580820     EURUSD     832581032          sell          2000    0.02     1.20699  1610953453      1.20700  1.610953e+09  Perucchi Inc    -0.02   0.0         0.0
4         832580854     EURUSD     832581072          sell          2000    0.03     1.20699  1610953456      1.20700  1.610953e+09  Perucchi Inc    -0.03   0.0         0.0
5         832580860     EURUSD     832581075          sell          2000    0.02     1.20699  1610953456      1.20696  1.610953e+09  Perucchi Inc     0.06   0.0         0.0
6         832583189     EURUSD     832596730          sell          2000    0.03     1.20680  1610953645      1.20682  1.610955e+09  Perucchi Inc    -0.06   0.0         0.0
7         832583195     EURUSD     832596740          sell          2000    0.02     1.20680  1610953645      1.20683  1.610955e+09  Perucchi Inc    -0.06   0.0         0.0
8         832585809     EURUSD     832596763          sell          2000    0.03     1.20690  1610953838      1.20685  1.610955e+09  Perucchi Inc     0.15   0.0         0.0
9         832594901     EURUSD     832596781          sell          2000    0.03     1.20659  1610954452      1.20685  1.610955e+09  Perucchi Inc    -0.78   0.0         0.0
10        832595835     EURUSD     832596799          sell          2000    0.03     1.20673  1610954498      1.20685  1.610955e+09  Perucchi Inc    -0.36   0.0         0.0
11        832597571     EURUSD     833245634          sell          2000    0.02     1.20673  1610954609      1.20592  1.610983e+09  Perucchi Inc     1.62   0.0         0.0
12        832597566     EURUSD     833245647          sell          2000    0.03     1.20673  1610954608      1.20592  1.610983e+09  Perucchi Inc     2.43   0.0         0.0
13        833654586     EURUSD     833658133          sell          2000    0.03     1.20739  1611001925      1.20742  1.611002e+09  Perucchi Inc    -0.09   0.0         0.0
14        833654590     EURUSD     833658137          sell          2000    0.02     1.20739  1611001926      1.20742  1.611002e+09  Perucchi Inc    -0.06   0.0         0.0`

Suggest:

Branly76 commented 3 years ago

Will check this, is it MT4 or MT5 ? For MT4 you can only retrieve the closed trades available in the MT4 terminal. As you know you can set the history of closed orders starting from day and so on. If in the terminal history you see only 12 closed orders, the python api can give back only this 12 closed orders, independent of your start date in the call,

BR

brenoperucchi commented 3 years ago

Will check this, is it MT4 or MT5 ? For MT4 you can only retrieve the closed trades available in the MT4 terminal. As you know you can set the history of closed orders starting from day and so on. If in the terminal history you see only 12 closed orders, the python api can give back only this 12 closed orders, independent of your start date in the call,

BR

Pytrader_MT5_EA_V2.02.ex5

Branly76 commented 3 years ago

I did i try to, got the next return. afbeelding

Used the following code

timezone = pytz.timezone("Etc/UTC") AllClosedPositions = MT.Get_all_closed_positions( date_from=datetime(2021, 1, 1, tzinfo=timezone), date_to=datetime.now())

Anyway i will do some more testing.

BR

Branly76 commented 3 years ago

Hi,

The date comparision with now has an item. From to is it including this day or not. I will adapt this and make an update. Used the following code: timezone = pytz.timezone("Etc/UTC") AllClosedPositions = MT.Get_all_closed_positions( date_from=datetime(2021, 1, 1, tzinfo=timezone), date_to=datetime(2021, 1, 20, tzinfo=timezone))

Got this result now: afbeelding

BR

Branly76 commented 3 years ago

Hi,

If your broker has a difference in time with your local time, this can happen. I tested on ICM broker and had the same problem.

timezone = pytz.timezone("Etc/UTC") AllClosedPositions = MT.Get_all_closed_positions( date_from=datetime(2021, 1, 1, tzinfo=timezone), date_to=datetime.now() + timedelta(hours=1))

For this broker just added 1 hour to local time

BR

brenoperucchi commented 3 years ago

Hi Branly, I was thinking about server time too but your solution or mine doesn't solved the problem. Maybe the server is delaying the information about history? For testing, I will change my broker to the ICM.

To use your API I need to obtain closed positions to finish the operation on my system.

meta.Get_all_closed_positions(date_from=datetime(2020, 1, 19, 0, tzinfo=timezone),date_to=datetime.now())

Screen Shot 2021-01-19 at 17 50 35

I closed the trade operation in a new demo account on MetaTrader 5 and after that, I try to get all closed positions from the API and I receive nothing.

(Pdb) meta.Get_all_closed_positions(date_from=datetime(2021, 1, 1, tzinfo=timezone), date_to=datetime.now() + timedelta(hours=1))
Empty DataFrame
Columns: [position_ticket, instrument, order_ticket, position_type, magic_number, volume, open_price, open_time, close_price, close_time, comment, profit, swap, commission]
Index: []
(Pdb)

ps: I woke up this morning and yesterday's results changes from 14 records to 40 without changing anything in the code. ps: I'm using ActivTrades as a broker.

Regards, Breno

brenoperucchi commented 3 years ago

I'm using ActivTrades https://www.activtrades.com/

Branly76 commented 3 years ago

Hi,

This can be the case depending on time difference between broker server time, which are the open/close times and your local time. I did a test with 20 trades and closed them directly. I was missing them all in the call. I went out for a while and tried again and yes they were there. Then i tried again and all not there. Then i changed/added the extra hour, and all where there again.

Will see if i can try with your broker too.

BR

brenoperucchi commented 3 years ago

You was right!

My time zone is 3 ahead of UTC but the server was 5 hours from me. When trying your suggested I changed to 3 hours and not verify the date-time of server information.

(date_from=datetime(2021, 1, 1, tzinfo=timezone), date_to=datetime.now() + timedelta(hours=3))

image

Sorry for this issue! BR

brenoperucchi commented 3 years ago

image

But one record was left out

image

Branly76 commented 3 years ago

HI,

Yep i know why. Iteration one to short. I changed already, will upload new version later.

BR

Branly76 commented 3 years ago

Version 2.03 is available

BR