-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathema.py
90 lines (71 loc) · 2.31 KB
/
ema.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
import logging
import sys
import forex as f
class EMA:
def __init__(self):
pass
@staticmethod
def _fl(value):
return float("{:.2f}".format(value))
def process(self, data):
item1 = None
expanding1 = None
buy = None
sell = None
prof_trades = 0
loss_trades = 0
pl = 0.
for item in data:
if item1:
emad1 = item1.emas - item1.emal
emad = item.emas - item.emal
bull = emad > 0.
bear = emad < 0.
bull1 = emad1 > 0.
bear1 = emad1 < 0.
profit = 0.
crossover = bull and bear1 or bear and bull1
if crossover:
if bull and bear1:
if buy:
profit = item.close - buy
buy = None
else:
if sell:
profit = sell - item.close
sell = None
if profit > 0.:
prof_trades += 1
else:
loss_trades += 1
expanding = abs(emad) > abs(emad1)
contracting = abs(emad) < abs(emad1)
if not crossover and expanding1 and contracting:
if bull:
sell = item.close
elif bear:
buy = item.close
expanding1 = expanding
pl += profit
item1 = item
prof_pc = EMA._fl(prof_trades * 100 / (prof_trades + loss_trades)) if prof_trades or loss_trades else 0.
return prof_trades, prof_pc, loss_trades, pl
if __name__ == '__main__':
logging.basicConfig(
filename="/tmp/ema.log",
filemode='w',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s' +
'%(message)s line:%(lineno)d',
datefmt='%H:%M:%S',
level=logging.INFO)
# years = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018]
#
years = [2018]
# day data
datafile = sys.argv[1]
data = f.filter(
f.load_candles(datafile),
f.filter_year,
years)
ema = EMA()
print(ema.process(data))