-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper_functions.py
123 lines (85 loc) · 3.58 KB
/
helper_functions.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
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 9 20:19:23 2021
@author: PAUL208
"""
#%% 1. Libraries
import requests
from bs4 import BeautifulSoup
import lxml
#%% Read telegram
def read_telegram(channel):
response = requests.get(channel)
soup = BeautifulSoup(response.content, 'html.parser')
links = soup.find_all('div')
messages =[link.text for link in links]
text_msg = messages[-3]
#text_msg = 'The coin we have picked to pump today is : #PIVXPIVX is looking perfect for a pump right now , our target is 1000%'
return(text_msg)
#%%
def read_telegram2(channel):
response = requests.get(channel)
soup = BeautifulSoup(response.content, 'html.parser')
msg_boxes = soup.find_all('div', class_ = 'tgme_widget_message_text js-message_text')
text_msg = msg_boxes[-1].text
return(text_msg)
#%% Read Telegram function, version 3
def read_telegram3(channel):
'''
This functions extracts and returns the last message in a telegram channel.
Arguments:
channel (string): Telegram channel URL
'''
response = requests.get(channel)
soup = BeautifulSoup(response.content, 'lxml')
msg_boxes = soup.find_all('div', class_ = 'tgme_widget_message_text js-message_text')
text_msg = msg_boxes[-1].text
return(text_msg)
#%% Get Binance trades
import numpy as np
import pandas as pd
from datetime import datetime
from datetime import timezone
from binance.client import Client
import pytz
# Instantiate a Client:
client = Client()
# Inputs:
# symbol
# start_time - has to be datetime object, with utc timezone
# end_time
symbol = 'MTHBTC'
start_time = '2021-06-27 16:58'
end_time = '2021-06-27 17:03'
start_time = datetime.strptime(start_time , '%Y-%m-%d %H:%M').replace(tzinfo=timezone.utc)
end_time = datetime.strptime(end_time , '%Y-%m-%d %H:%M').replace(tzinfo=timezone.utc)
def get_binance_trades(symbol, start_time, end_time):
# time in as string:
start_time = datetime.strptime(start_time , '%Y-%m-%d %H:%M').replace(tzinfo=timezone.utc)
end_time = datetime.strptime(end_time , '%Y-%m-%d %H:%M').replace(tzinfo=timezone.utc)
# prepare inputs
start_str = start_time.strftime("%d %B, %Y %H:%M")
df_trades = pd.DataFrame(columns = ['a','p','q','f','l','T','m','M'])
# Get the trades aggregator
agg_trades = client.aggregate_trade_iter(symbol = symbol, start_str = start_str)
# Download the trades
for trade in agg_trades:
df_temp = pd.DataFrame(trade, index=[0])
df_trades = df_trades.append(df_temp, ignore_index=True)
trade_time = pd.to_datetime(df_temp['T'], unit='ms')[0]
trade_time = trade_time.to_pydatetime()
# make datetime aware of the timezone
trade_time = pytz.utc.localize(trade_time)
# stop loop
if end_time < trade_time:
break
# Convert to dataframe and format
df_trades['T'] = pd.to_datetime(df_trades['T'], unit='ms')
df_trades.columns = ['Seller Order Id', 'Price', 'Quantity', 'Ignore', 'Ignore', 'Time', 'Sell', 'Ignore' ]
df_trades['Side'] = np.where(df_trades['Sell'] == True, 'Sell', 'Buy')
df_trades = df_trades[['Seller Order Id', 'Price', 'Quantity','Time', 'Side']]
df_trades[['Seller Order Id','Price','Quantity']] = \
df_trades[['Seller Order Id','Price','Quantity']].apply(pd.to_numeric)
# output the dataframe
return df_trades
#%%