-
Notifications
You must be signed in to change notification settings - Fork 0
/
coinApi.R
211 lines (191 loc) · 7.29 KB
/
coinApi.R
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# To separate entries in logfiles
print("################## Start Log Entry ######################")
library(rgdax)
library(mailR)
library(stringi)
library(curl)
library(xts)
library(TTR)
# Build functions
curr_bal_usd <- function(x){
m <- accounts(api.key = "your_api_key", secret = "your_secret", passphrase = "your_passphrase")#[3,3]
m <- subset(m$available, m$currency == 'USD')
m
}
curr_bal_eth <- function(x){
n <- accounts(api.key = "your_api_key", secret = "your_secret", passphrase = "your_passphrase")
n <- subset(n$available, n$currency == 'ETH')
n
}
curr_ema13_api <- function(x){
df <- rgdax::public_candles(product_id = "ETH-USD",
granularity = 900)
ema13_gdax <- tail(TTR::EMA(df[,5],
n = 13),
n = 1)
ema13_gdax
}
curr_ema34_api <- function(x){
df <- rgdax::public_candles(product_id = "ETH-USD",
granularity = 900)
ema34_gdax <- tail(TTR::EMA(df[,5],
n = 34),
n = 1)
ema34_gdax
}
curr_rsi14_api <- function(x){
df <- rgdax::public_candles(product_id = "ETH-USD",
granularity = 900)
rsi_gdax <- tail(TTR::RSI(df[,5],
n = 14),
n = 1)
rsi_gdax
}
# v.2
rsi14_api_less_one <- function(x){
df <- rgdax::public_candles(product_id = "ETH-USD",
granularity = 900)
rsi_gdax_less_one <- head(tail(TTR::RSI(df[,5],
n = 14),
n = 2),n=1)
rsi_gdax_less_one
}
rsi14_api_less_two <- function(x){
df <- rgdax::public_candles(product_id = "ETH-USD",
granularity = 900)
rsi_gdax_less_two <- head(tail(TTR::RSI(df[,5],
n = 14),
n = 3),n=1)
rsi_gdax_less_two
}
rsi14_api_less_three <- function(x){
df <- rgdax::public_candles(product_id = "ETH-USD",
granularity = 900)
rsi_gdax_less_three <- head(tail(TTR::RSI(df[,5],
n = 14),
n = 4),n=1)
rsi_gdax_less_three
}
rsi14_api_less_four <- function(x){
df <- rgdax::public_candles(product_id = "ETH-USD",
granularity = 900)
rsi_gdax_less_four <- head(tail(TTR::RSI(df[,5],
n = 14),
n = 5),n=1)
rsi_gdax_less_four
}
# v.2
bid <- function(x){
bid <- public_orderbook(product_id = "ETH-USD", level = 1)
bid <- bid$bids[1]
bid
}
ask <- function(x){
ask <- public_orderbook(product_id = "ETH-USD", level = 1)
ask <- ask$asks[1]
ask
}
usd_hold <- function(x){
holds(currency = "USD", "your_api_key", "your_secret", "your_passphrase")
}
eth_hold <- function(x){
holds <- holds(currency = "ETH", "your_api_key", "your_secret", "your_passphrase")
holds
}
cancel_orders <- function(x){
cancel_orders <- cancel_order("your_api_key", "your_secret", "your_passphrase")
cancel_orders
}
buy_exe <- function(x){
# get order size in iterative manner
order_size <- round(curr_bal_usd()/ask(),3)[1]-0.005
# place initial order
while(curr_bal_eth() == 0){
#order_size <- curr_bal_usd() / ask() - 0.009
add_order(product_id = "ETH-USD", api.key = "your_api_key", secret = "your_secret", passphrase = "your_passphrase",
type="limit", price = bid(), side = "b", size = order_size )
# sleep to see if order takes
Sys.sleep(17)
# check to see if ETH bal >= order amt
if(curr_bal_eth() > 0){"buysuccess"}else{
cancel_orders() # if curr_eth_bal not > 0, cancel order and start over
}
}
}
sell_exe <- function(x){
# place initial order
while(curr_bal_eth() > 0){
add_order("ETH-USD", api.key = "your_api_key", secret = "your_secret", passphrase = "your_passphrase",
type="limit", price = ask(), side = "s", size = curr_bal_eth())
# sleep to see if order takes
Sys.sleep(17)
# check to see if ETH bal >= order amt
if(curr_bal_eth() == 0){"buysuccess"}else{
cancel_orders() # if curr_eth_bal not > 0, cancel order and start over
}
}
}
position <- (read.csv("C:/R_Directory/position.csv", header = TRUE))[1,2]
# v.2
# Store variables so don't exceed rate limit of API
curr_rsi14_api <- curr_rsi14_api()
Sys.sleep(2)
rsi14_api_less_one <- rsi14_api_less_one()
Sys.sleep(2)
rsi14_api_less_two <- rsi14_api_less_two()
Sys.sleep(2)
rsi14_api_less_three <- rsi14_api_less_three()
Sys.sleep(2)
rsi14_api_less_four <- rsi14_api_less_four()
order_price_tiered3 <- 99999
order_price_tiered5 <- 99999
order_price_tiered8 <- 99999
#v.2
# Actual Trading Loop
if(curr_bal_usd() >= 20){ # if have more than $20 USD start loop
if(curr_rsi14_api >= 30 & # and current rsi >= 35 # v.2
rsi14_api_less_one <= 30 & # previous close RSI <= 35 # v.2
rsi14_api_less_two < 30 | rsi14_api_less_three < 30 | rsi14_api_less_four < 30) { # i-2, i-3 or i-4 RSI < 35 # v.2
# 1 Buy
buy_exe()
Sys.sleep(180)
# 2 save buy price in csv on desktop
position <- write.csv(bid(), file = "C:/R_Directory/position.csv")
# 3 send email
send.mail(from = "[email protected]",
to = c("[email protected]"),
replyTo = c("Reply to someone else <[email protected]>"),
subject = "GDAX ETH Test - Buy",
body = paste("Your model says buy right now at price",bid()),
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "your_username", passwd = "your_password", ssl = TRUE),
authenticate = TRUE,
send = TRUE)
# 4 print for logs
print("buy")
Sys.sleep(3)
# v.5
# 5 Enter tiered limit sell orders
# Order 1: take 1/3 profits at 1% gain
order_size_tiered3 <- round(curr_bal_eth()/3,3)
order_price_tiered3 <- round(bid() * 1.01,2)
Sys.sleep(1)
add_order(product_id = "ETH-USD", api.key = "your_api_key", secret = "your_secret", passphrase = "your_passphrase",
type="limit", price = order_price_tiered3, side = "s", size = order_size_tiered3 )
Sys.sleep(20)
# Order 2: take 1/3 profits at 4% gain
order_size_tiered5 <- round(curr_bal_eth()/2,3)
order_price_tiered5 <- round(bid() * 1.04,2)
add_order(product_id = "ETH-USD", api.key = "your_api_key", secret = "your_secret", passphrase = "your_passphrase",
type="limit", price = order_price_tiered5, side = "s", size = order_size_tiered5)
Sys.sleep(20)
# Order 3: take 1/3 profits at 7% gain
order_size_tiered8 <- round(curr_bal_eth(),3)
order_price_tiered8 <- round(bid() * 1.07,2)
add_order(product_id = "ETH-USD", api.key = "your_api_key", secret = "your_secret", passphrase = "your_passphrase",
type="limit", price = order_price_tiered8, side = "s", size = order_size_tiered8 )
# v.5
}else{"nobuy"}
}else{"nobuy"}
# print systime for logs
Sys.time()
print("################## End Log Entry ######################")