-
Notifications
You must be signed in to change notification settings - Fork 0
/
Indicators.py
181 lines (154 loc) · 6.31 KB
/
Indicators.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from Poloniex_API import Poloniex_API
import json
import datetime
import time
import sys, getopt
import os
s=""
k=""
poloAPI = Poloniex_API(s, k)
def fib(candles):
high = sorted([x["high"] for x in candles["candleStick"]])[-1]
low = sorted([x["low"] for x in candles["candleStick"]])[0]
float = high-low
return (high, 0.628*float+low, 0.5*float+low, 0.382*float+low, 0.236*float+low, low)
def macd(candles, length, interval_length):
ema12, ema12_line = exponential_moving_average(12, candles, length, interval_length,[])
ema26, ema26_line = exponential_moving_average(26, candles, length, interval_length,[])
macd_line = [ema12_line[-(i+1)] - ema26_line[-(i+1)] for i in range(len(ema26_line))]
print macd_line
# print "ema26\n"
# print ema26
# print ema26_line
# print len(ema26_line)
# print "ema12\n"
# print ema12
# print ema12_line
# print len(ema12_line)
print "Not implemented"
return 0
def average_gain_loss(number_of_intervals, candles, length, interval_length, gains=[], losses=[]):
if length*60/interval_length > number_of_intervals:
if len(candles["candleStick"]) == number_of_intervals:
#if len(candles) == number_of_intervals:
# get open prices
opens = [x["open"] for x in candles["candleStick"]]
# get close prices
close = [x["close"] for x in candles["candleStick"]]
#calculate gains and losses
gains_losses = [close[-(i+1)]-close[-(i+2)] for i in range(len(opens)-1)]
# separate gains and losses
all_gains = [x for x in gains_losses if x > 0]
all_losses = [x for x in gains_losses if x < 0 ]
# calculate simple ave gain and ave loss
ave_gain = abs(sum(all_gains))/number_of_intervals
ave_loss = abs(sum(all_losses))/number_of_intervals
else:
previous_candles = {"candleStick": candles["candleStick"][:-1]}
current_gain_loss = candles["candleStick"][-1]["close"]-candles["candleStick"][-2]["close"]
# previous_candles = candles[:-1]
# current_gain_loss = candles[-1]-candles[-2]
if current_gain_loss > 0:
current_gain = abs(current_gain_loss)
current_loss = 0
elif current_gain_loss < 0:
current_loss = abs(current_gain_loss)
current_gain = 0
else:
current_gain = 0
current_loss = 0
all_ave_gains, all_ave_losses = average_gain_loss(number_of_intervals, previous_candles, length, interval_length, gains, losses)
ave_gain = (all_ave_gains[-1]*(number_of_intervals-1) + current_gain)/number_of_intervals
ave_loss = (all_ave_losses[-1]*(number_of_intervals-1) + current_loss)/number_of_intervals
gains.append(ave_gain)
losses.append(ave_loss)
return gains, losses
def RSI(number_of_intervals, candles, length, interval_length):
rsi = []
average_gains, average_losses = average_gain_loss(number_of_intervals, candles, length, interval_length)
# print average_losses
# print "\n"
# print average_gains
for i in range(len(average_losses)):
rsi.append(100-100/(1+average_gains[i]/average_losses[i]))
return rsi
def simple_moving_average(candles, number_of_intervals):
# Total number periods minus number periods for SMA
starting_point = len(candles["candleStick"])-number_of_intervals
# all closing prices
closes = [x["close"] for x in candles["candleStick"]]
# sum of previous number_of_intervals closes
closes_sum = sum(closes, starting_point)
# sma
sma = closes_sum/number_of_intervals
return sma
def exponential_moving_average(number_of_intervals, candles, length, interval_length, emas=[]):
if length*60/interval_length > number_of_intervals:
if len(candles["candleStick"]) == number_of_intervals:
ema = simple_moving_average(candles, number_of_intervals)
else:
multiplier = 2.0/(number_of_intervals+1)
close = candles["candleStick"][-1]["close"]
previous_candles = {"candleStick": candles["candleStick"][:-1]}
previous_ema, emas= exponential_moving_average(number_of_intervals, previous_candles, length, interval_length, emas)
ema = (close - previous_ema) * multiplier + previous_ema
else:
print "Time period too short"
#figure out how to save all intermediate emas
emas.append(ema)
return ema, emas
def average_volume(candles, number_of_intervals):
# Total number periods minus number periods for SMA
starting_point = len(candles["candleStick"])-number_of_intervals
# all closing prices
volumes = [x["volume"] for x in candles["candleStick"]]
# sum of previous number_of_intervals closes
sum_volumes = sum(volumes, starting_point)
# sma
ave_volume = sum_volumes/number_of_intervals
return ave_volume
## Setup Poloniex API Trading connection
def trade():
# Checking available Margin NetValue
balances = poloAPI.returnMarginAccountSummary()
MarginValue = float(balances["netValue"])
# length_of_intervals is in min
# length_of_time is in hours
#candles = poloAPI.returnChartData("BTC_BTS", length_of_intervals*60, (time.time() - length_of_time*3600), time.time())
#Setting up Buy parameters
if(1):
rate = 0.00002
amount = MarginValue*2/0.00002
lendingRate = 0.0001
currencyPair = "BTC_BTS"
# execute Margin Buy
#ret = poloAPI.getMarginPosition("BTC_STR")
ret = poloAPI.marginBuy(currencyPair, rate, 100, lendingRate)
#ret = poloAPI.closeMarginPosition(currencyPair)
ret2 = poloAPI.cancel(currencyPair, ret["orderNumber"])
print json.dumps(ret, sort_keys = True, indent =4, separators=(',', ':'))
print json.dumps(ret2, sort_keys = True, indent =4, separators=(',', ':'))
# if __name__ == "__main__":
# try:
# opts, args = getopt.getopt(argv, "", [])
# except getopt.GetoptError:
# print "Error"
#fix for recursion overflow
length_of_intervals = 30 # in mins
length_of_time = 1*24 # in hours
candles = poloAPI.returnChartData("BTC_BTS", length_of_intervals*60, (time.time() - length_of_time*3600), time.time())
#print json.dumps(candles, sort_keys = True, indent =4, separators=(',', ':'))
macd(candles, length_of_time, length_of_intervals)
# ema30, intermediate_emas = exponential_moving_average(30, candles, length_of_time, length_of_intervals,[])
# print ema30
# print intermediate_emas
# ema26, intermediate_emas2 = exponential_moving_average(26, candles, length_of_time, length_of_intervals,[])
# ema12, intermediate_emas1 = exponential_moving_average(12, candles, length_of_time, length_of_intervals,[])
# print "ema26\n"
# print ema26
# print intermediate_emas2
# print "ema12\n"
# print ema12
# print intermediate_emas1
#print RSI(14, candles, length_of_time, length_of_intervals)
#trade()