-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathCoreFunctions.py
88 lines (66 loc) · 1.93 KB
/
CoreFunctions.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
"""
@author: Khera
"""
import time
from binance.client import Client
import csv
import pandas as pd
import numpy as np
def emaPoints(data, dataPoints):
ema1 = []
for i in range(len(data)):
ema = 0
if i > 0:
prevEma = ema1[i-1]
multiplyer = 2/(dataPoints+1)
ema = (float(data[i][4]) - prevEma)*multiplyer+prevEma
ema1.append(ema)
return ema1
#%%
def emaPointsMacd(data, dataPoints):
ema1 = []
for i in range(len(data)):
ema = 0
if i > 0:
prevEma = ema1[i-1]
multiplyer = 2/(dataPoints+1)
ema = (data[i] - prevEma)*multiplyer+prevEma
ema1.append(ema)
return ema1
def macd(data):
ema12 = emaPoints(data,12)
ema26 = emaPoints(data,26)
macd = []
for i in range(len(ema12)):
m = ema12[i]-ema26[i]
macd.append(m)
signal = emaPointsMacd(macd,9)
return macd, signal
#%%
def makeTrainingData(data):
macda, signal = macd(data)
features = []
for i in range(100,len(data)):
#x = [macda[i], signal[i]]
x = [macda[i], signal[i]]
features.append(x)
return features
def getCoinBalance(client, currency):
balance = float(client.get_asset_balance(asset=currency)['free'])
return balance
def calculateRsi(data):
RSI_N = 14
RSI_THRESHOLD = 8
closings = np.asarray(data, dtype=np.float)[-RSI_N - 1:, 4]
diffs = np.diff(closings)
ups = diffs.clip(min=0)
downs = diffs.clip(max=0)
ups_avg = pd.ewma(ups, span=RSI_N)[-1]
downs_avg = -pd.ewma(downs, span=RSI_N)[-1]
rs = ups_avg / downs_avg
rsi = 100 - 100 / (1 + rs)
return rsi
def executeBuy(client, market, qtyBuy):
order = client.order_market_buy(symbol=market,quantity=qtyBuy)
def executeSell(client, market, qtySell):
order = client.order_market_sell(symbol=market, quantity=qtySell)