Skip to content

Commit

Permalink
fix Dataframe to DataFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
Emerson Pedroso committed Feb 26, 2021
1 parent 6b5d6e2 commit 3f90223
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 24 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ timeframe = "M1"
fromDate = "24/02/2021"
toDate = "24/02/2021"

history = mt.historyDataframe(symbol,timeframe,fromDate,toDate)
history = mt.historyDataFrame(symbol,timeframe,fromDate,toDate)
print(history)

open high low close volume spread
Expand Down Expand Up @@ -195,7 +195,7 @@ date
symbol = "EURUSD"
timeframe = "M1"

history = mt.ShorthistoryDataframe(symbol,timeframe,10)
history = mt.ShorthistoryDataFrame(symbol,timeframe,10)
print(history)

open high low close volume spread
Expand Down Expand Up @@ -306,6 +306,21 @@ date

```

# for converting DataFrame to local time

```python
from ejtraderMT import Metatrader

symbol = "EURUSD"
timeframe = "M1"
fromDate = "24/02/2021"
toDate = "24/02/2021"

# Example add True to end of command
history = mt.historyDataFrame(symbol,timeframe,fromDate,toDate,True)

```

# history dictionary "array"

#### Short History from Date to Date dict
Expand Down
86 changes: 69 additions & 17 deletions ejtraderMT/api/platafrom.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime
import time
from pytz import timezone
from tzlocal import get_localzone
tz = timezone('UTC')

class Functions:
Expand Down Expand Up @@ -207,7 +208,10 @@ class Metatrader:
def __init__(self,host=None):
self.api = Functions(host)
self.livePrice = self.api.live_socket()
self. liveEvent = self.api.streaming_socket()
self.liveEvent = self.api.streaming_socket()
self.mytimezone = get_localzone()
self.utcoffset = self.offset()




Expand Down Expand Up @@ -315,6 +319,18 @@ def positions(self):
def orders(self):
return json.loads(json.dumps(self.api.Command(action="ORDERS")))

def offset(self):
utc = datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')
accountInfo = self.api.Command(action="ACCOUNT")
date = accountInfo['time']
trade = datetime.strptime(date, '%Y.%m.%d %H:%M:%S')
zone = datetime.strptime(utc, '%Y-%m-%d %H:%M:%S')
duration = trade - zone
duration_in_s = duration.total_seconds()
hour = int(duration_in_s)

return hour


def Shorthistory(self, symbol, chartTF, fromDate):

Expand All @@ -338,19 +354,25 @@ def history(self, symbol, chartTF, fromDate, toDate):
return data


def historyDataframe(self, symbol, chartTF, fromDate, toDate):
def historyDataFrame(self, symbol, chartTF, fromDate, toDate, localTime=False):

if(chartTF == 'TICK'):
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=symbol, chartTF=chartTF, fromDate=convertDate(fromDate), toDate=convertDate(toDate))))
data_frame = pd.DataFrame(data['data'], columns=['date', 'bid', 'ask'])
data_frame = data_frame.set_index(['date'])
data_frame.index = pd.to_datetime(data_frame.index,unit='s')
if localTime:
data_frame.index = pd.to_datetime(data_frame.index,unit='s')
data_frame.index = data_frame.index.tz_localize(self.utcoffset)
data_frame.index = data_frame.index.tz_convert(self.mytimezone)
self.api.Command(action="RESET")
else:
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=symbol, chartTF=chartTF, fromDate=convertDate(fromDate), toDate=convertDate(toDate))))
data_frame = pd.DataFrame(data['data'], columns=['date', 'open', 'high', 'low','close','volume','spread'])
data_frame = data_frame.set_index(['date'])
data_frame.index = pd.to_datetime(data_frame.index,unit='s')
if localTime:
data_frame.index = pd.to_datetime(data_frame.index,unit='s')
data_frame.index = data_frame.index.tz_localize(self.utcoffset)
data_frame.index = data_frame.index.tz_convert(self.mytimezone)
self.api.Command(action="RESET")
return data_frame
def timeframe_to_sec(self, timeframe):
Expand All @@ -370,25 +392,31 @@ def timeframe_to_sec(self, timeframe):
return TIMECANDLE[timeframe]


def ShorthistoryDataframe(self, symbol, chartTF, fromDate):
def ShorthistoryDataFrame(self, symbol, chartTF, fromDate, localTime=False):

if(chartTF == 'TICK'):
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=symbol, chartTF=chartTF, fromDate=datetime.utcnow().timestamp() - (fromDate * 60))))
data_frame = pd.DataFrame(data['data'], columns=['date', 'bid', 'ask'])
data_frame = data_frame.set_index(['date'])
data_frame.index = pd.to_datetime(data_frame.index,unit='s')
if localTime:
data_frame.index = pd.to_datetime(data_frame.index,unit='s')
data_frame.index = data_frame.index.tz_localize(self.utcoffset)
data_frame.index = data_frame.index.tz_convert(self.mytimezone)
self.api.Command(action="RESET")
else:
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=symbol, chartTF=chartTF, fromDate=datetime.utcnow().timestamp() - (fromDate * (self.timeframe_to_sec(chartTF) * 60)))))
data_frame = pd.DataFrame(data['data'], columns=['date', 'open', 'high', 'low','close','volume','spread'])
data_frame = data_frame.set_index(['date'])
data_frame.index = pd.to_datetime(data_frame.index,unit='s')
if localTime:
data_frame.index = pd.to_datetime(data_frame.index,unit='s')
data_frame.index = data_frame.index.tz_localize(self.utcoffset)
data_frame.index = data_frame.index.tz_convert(self.mytimezone)
self.api.Command(action="RESET")
return data_frame



def historyMultiDataFrame(self, symbol, symbols, chartTF, fromDate, toDate):
def historyMultiDataFrame(self, symbol, symbols, chartTF, fromDate, toDate, localTime=False):
actives = symbols
main = pd.DataFrame()
current = pd.DataFrame()
Expand All @@ -399,36 +427,48 @@ def historyMultiDataFrame(self, symbol, symbols, chartTF, fromDate, toDate):
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=active, chartTF=chartTF, fromDate=convertDate(fromDate), toDate=convertDate(toDate))))
main = pd.DataFrame(data['data'], columns=['date', 'bid', 'ask'])
main = main.set_index(['date'])
main.index = pd.to_datetime(main.index,unit='s')
if localTime:
main.index = pd.to_datetime(main.index,unit='s')
main.index = main.index.tz_localize(self.utcoffset)
main.index = main.index.tz_convert(self.mytimezone)
self.api.Command(action="RESET")
else:
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=active, chartTF=chartTF, fromDate=convertDate(fromDate), toDate=convertDate(toDate))))
main = pd.DataFrame(data['data'], columns=['date', 'open', 'high', 'low','close','volume','spread'])
main = main.set_index(['date'])
main.index = pd.to_datetime(main.index,unit='s')
if localTime:
main.index = pd.to_datetime(main.index,unit='s')
main.index = main.index.tz_localize(self.utcoffset)
main.index = main.index.tz_convert(self.mytimezone)
self.api.Command(action="RESET")

else:
if(chartTF == 'TICK'):
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=active, chartTF=chartTF, fromDate=convertDate(fromDate), toDate=convertDate(toDate))))
current = pd.DataFrame(data['data'], columns=['date', 'bid', 'ask'])
current = current.set_index(['date'])
current.index = pd.to_datetime(current.index,unit='s')
if localTime:
current.index = pd.to_datetime(current.index,unit='s')
current.index = current.index.tz_localize(self.utcoffset)
current.index = current.index.tz_convert(self.mytimezone)
current.columns = [f'BID{active}',f'ASK{active}']
self.api.Command(action="RESET")
main = pd.merge(main,current, how='inner', left_index=True, right_index=True)
else:
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=active, chartTF=chartTF, fromDate=convertDate(fromDate), toDate=convertDate(toDate))))
current = pd.DataFrame(data['data'], columns=['date', 'open', 'high', 'low','close','volume','spread'])
current = current.set_index(['date'])
current.index = pd.to_datetime(current.index,unit='s')
if localTime:
current.index = pd.to_datetime(current.index,unit='s')
current.index = current.index.tz_localize(self.utcoffset)
current.index = current.index.tz_convert(self.mytimezone)
current.columns = [f'OPEN{active}',f'HIGH{active}',f'LOW{active}',f'CLOSE{active}',f'VOLUME{active}',f'SPREAD{active}']
self.api.Command(action="RESET")
main = pd.merge(main,current, how='inner', left_index=True, right_index=True)
main = main.loc[~main.index.duplicated(keep = 'first')]
return main

def ShorthistoryMultiDataFrame(self, symbol, symbols, chartTF, fromDate):
def ShorthistoryMultiDataFrame(self, symbol, symbols, chartTF, fromDate, localTime=False):
actives = symbols
main = pd.DataFrame()
current = pd.DataFrame()
Expand All @@ -439,29 +479,41 @@ def ShorthistoryMultiDataFrame(self, symbol, symbols, chartTF, fromDate):
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=active, chartTF=chartTF, fromDate=datetime.utcnow().timestamp() - (fromDate * 60))))
main = pd.DataFrame(data['data'], columns=['date', 'bid', 'ask'])
main = main.set_index(['date'])
main.index = pd.to_datetime(main.index,unit='s')
if localTime:
main.index = pd.to_datetime(main.index,unit='s')
main.index = main.index.tz_localize(self.utcoffset)
main.index = main.index.tz_convert(self.mytimezone)
self.api.Command(action="RESET")
else:
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=active, chartTF=chartTF, fromDate=datetime.utcnow().timestamp() - (fromDate * (self.timeframe_to_sec(chartTF) * 60)))))
main = pd.DataFrame(data['data'], columns=['date', 'open', 'high', 'low','close','volume','spread'])
main = main.set_index(['date'])
main.index = pd.to_datetime(main.index,unit='s')
if localTime:
main.index = pd.to_datetime(main.index,unit='s')
main.index = main.index.tz_localize(self.utcoffset)
main.index = main.index.tz_convert(self.mytimezone)
self.api.Command(action="RESET")

else:
if(chartTF == 'TICK'):
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=active, chartTF=chartTF, fromDate=datetime.utcnow().timestamp() - (fromDate * 60))))
current = pd.DataFrame(data['data'], columns=['date', 'bid', 'ask'])
current = current.set_index(['date'])
current.index = pd.to_datetime(current.index,unit='s')
if localTime:
current.index = pd.to_datetime(current.index,unit='s')
current.index = current.index.tz_localize(self.utcoffset)
current.index = current.index.tz_convert(self.mytimezone)
current.columns = [f'BID{active}',f'ASK{active}']
self.api.Command(action="RESET")
main = pd.merge(main,current, how='inner', left_index=True, right_index=True)
else:
data = json.loads(json.dumps(self.api.Command(action="HISTORY", actionType="DATA", symbol=active, chartTF=chartTF, fromDate=datetime.utcnow().timestamp() - (fromDate * (self.timeframe_to_sec(chartTF) * 60)))))
current = pd.DataFrame(data['data'], columns=['date', 'open', 'high', 'low','close','volume','spread'])
current = current.set_index(['date'])
current.index = pd.to_datetime(current.index,unit='s')
if localTime:
current.index = pd.to_datetime(current.index,unit='s')
current.index = current.index.tz_localize(self.utcoffset)
current.index = current.index.tz_convert(self.mytimezone)
current.columns = [f'OPEN{active}',f'HIGH{active}',f'LOW{active}',f'CLOSE{active}',f'VOLUME{active}',f'SPREAD{active}']
self.api.Command(action="RESET")
main = pd.merge(main,current, how='inner', left_index=True, right_index=True)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pandas>=0.25.1
pyzmq>=19.0.2
pyzmq>=19.0.2
tzlocal==2.1
6 changes: 3 additions & 3 deletions test/MultipleDataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
symbol = "EURUSD"
symbols = [symbol,"GBPUSD","AUDUSD"]
timeframe = "M1"
fromDate = "01/01/2021"
toDate = "01/02/2021"
fromDate = "24/02/2021"
toDate = "26/02/2021"

data = api.historyDataFrame(symbol,symbols,timeframe,fromDate,toDate)
data = api.historyDataFrame(symbol,timeframe,fromDate,toDate,True)



Expand Down
2 changes: 1 addition & 1 deletion test/ShortMultipleDataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
timeframe = "M1"


data = api.ShorthistoryDataframe(symbol,timeframe,3)
data = api.ShorthistoryDataFrame(symbol,timeframe,3)


print(data)
Expand Down

0 comments on commit 3f90223

Please sign in to comment.