-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiotaPkg.R
635 lines (546 loc) · 17.6 KB
/
iotaPkg.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
###########################################################################################################################################################################################################################
###########################################################################################################################################################################################################################
###########################################################################################################################################################################################################################
########################################################## 'iota' R-Package ############################################################################################################################
########################################################## Developed by ############################################################################################################################
########################################################## Thomas A. Pepperz ############################################################################################################################
########################################################## The Leeds School of Business at ############################################################################################################################
########################################################## University of Colorado, Boulder ############################################################################################################################
###########################################################################################################################################################################################################################
###########################################################################################################################################################################################################################
###########################################################################################################################################################################################################################
#### OPTIMIZING CRYPTOCURRENCY MARKET ENTRY & EXIT STRATEGIES: A TECHNICAL TRADING ANALYSIS OF IOTA ####
# NOTE: Work on 'iota' commenced Tuesday, August 15, 2017 at the University of Colorado, Boulder's Leeds School of Business,
# under the direction of Dr. Dan Zhang & the advisement of Dr. Chuan He.
# Set Up WorkSpace
rm(list = ls()) # Clear Workspace
options(scipen = 999)
options(prompt = "R> ")
options("getSymbols.warning4.0" = F)
setwd("~/Workspace/iota/iotaData/")
# Install and Load Required R-Packages #
myPkgs <-
c(
# Graphical Analysis of Structural Causal Models
"dagitty",
# Pron. 'dagger' - Manipulate & Evaluate Directed Acrylic Graphs (DAGs)
"dagR",
# Package for Graphical Modeling, Including DAGs - dag()
"gRbase",
# Miscellaneous Functions for ``Grid'' Graphics
"gridExtra",
# Tools for Working with URLs and HTTP
"httr",
# A General-Purpose Package for Dynamic Report Generation in R
"knitr",
# Provides Bindings to the libcurl C Library | Supports Retrieving Data Using the R “connection” Interface.
"curl",
# Convert JSON data from/to R objects & stream, validate, and prettify JSON data
"jsonlite",
#
"JSON",
# I/O Input/Output
"rio",
# Provides Functions for Reading & Writing XML & HTML Documents
"XML",
# CSV file I/O, e.g. the read_csv function
"readr",
# Data Visualization
"ggplot2",
# Scales Map Data & Auto-Determines Breaks & Labels For Axes & Legends
"scales",
# Network Analysis & Graph Visualization
"igraph",
#
"rJava",
#
"ploty",
#
"metricsgraphics",
#
"googleVis",
#
"dygraphs",
#
"ggiraph",
# Time Series
"zoo",
# Time Series Analysis and Computational Finance
"tseries",
# R Interface to 'tslib' (a Python Package) | Performs Fast Operations for Time Series Objects
"fts",
# Package of time series tools and utilities.
"timeSeries",
# Time Frame Coding Kernel
"tframe",
# Tidies Up Frecast Models & Prediction Work Flow, extending 'broom' package & 'forecast' objects to tidy DFs
"sweep",
# Breaks For Additive Season and Trend (BFAST) | Time Series Decomposition to Trend & Seasonal Componenets
"bfast",
# Manage Chronological & Calendrical Objects
"timeDate",
# Extract Time Components From Date-Object (mday(), wday(), et cetera)
"TimeProjection",
# Tools for Specifying Univariate Time Series Forecasts Using Regression Models,
"forecast",
# Used for querying and analyzing financial data such as stocks
"quantmod",
# 'Extensible Time Series' Class & Methods (converts to true time-based objects)
"xts",
# Econometric Tools for Performance & Risk Analysis
"PerformanceAnalytics",
# Tools for specifying time series regression models License GPL (>= 2)
"tsModel",
# Constructs 'Technical Trading Rules'
"TTR",
# Functions for Spatial Data Visualization & Modeling On Top of Static Maps from Online Sources (GoogleMaps etc.)
"ggmap",
# Creates Thematic Maps Like Choroplets & Bubble Maps From Spatial Data Visualizations.
"tmap",
# Tools for Reading and Handling Spatial Objects
"maptools",
# Loads US Census Boundary & Attribute Data as 'tidyverse' & 'sf'-Ready Data Frames
"tidycensus",
#An implementation of interpreted string literals, inspired byPython's Literal String Interpolation
"glue",
#
"rga",
#
"RSiteCatalyst",
#
"roxygen2",
#
"shiny",
#
"openxlsx",
#
"gmodels",
#
"janitor",
#
"car",
#
"rcdimple",
#
"foreach",
#
"highcharter",
#
"profvis",
#
"tidytext",
#
"diffobj",
#
"prophet",
#
"feather",
#
"leaflet",
#
"DT",
#
"listviewer",
#
"editR",
#
"wordcloud",
#
"twitterR",
#
"lubridate",
#
"stringr",
#
"validate",
#
"magrittr",
#
"reshape2",
#
"plyr",
#
"dplyr",
#
"tidyverse",
#
"purrr",
#
"rvest",
#
"tidyquant",
#
"sqldf",
#
"Hmisc",
#
"RMySQL",
#
"googlesheets",
#
"waffle",
#
"sentimentr",
#
"rtrie",
# Interface to the Poloniex Cryptocurrency Trading API
"PoloniexR",
# Unified Markets API Unterface (Bitstamp, Kraken, btc-e, Bitmarket)
"Rbitcoin",
#
"coinmarketcapr",
# Create Compact Hash Digests of R Objects
"digest",
# Provides bindings to 'libsodium' for encryption, decryption, signatures, password hashing
"sodium",
# R2HTML
"R2HTML"
)
# Install, Update, & Load Required Packages
installPkgs <- function(packages) {
new.packages <-
packages[!(packages %in% installed.packages()[, "Package"])]
if (length(new.packages))
install.packages(new.packages, dependencies = TRUE)
sapply(
packages,
require,
character.only = TRUE,
warn.conflicts = FALSE,
quietly = TRUE
)
}
# Install Packages
suppressPackageStartupMessages(suppressWarnings(suppressMessages(installPkgs(myPkgs))))
devtools::install_github("ropensci/internetarchive", build_vignettes = TRUE)
# Update Update
suppressPackageStartupMessages(suppressWarnings(suppressMessages(
update.packages(ask = FALSE, checkBuilt = TRUE)
)))
# Load & Attach to Global Enviornment Required Packages
lapply(myPkgs, require, character.only = TRUE)
The operators <<-
and -> > are normally only used in functions, and cause a search to be made through parent environments for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise assignment takes place in the global environment. Note that their semantics differ from that in the S language, but are useful in conjunction with the scoping rules of R. See ‘The R Language Definition’ manual for further details and examples.
In all the assignment operator expressions, x can be a name or an expression defining a part of an object to be replaced (e.g., z[[1]]). A syntactic name does not need to be quoted, though it can be (preferably by backticks).
rds forms of assignment <-
= <<- group right to left, the other from left to right.
Value
value. Thus one can use a <- b <- c <- 6.
assign versus get
#### SET WORKSPACE ####
rm(list = ls()) # Clear Workspace
options(scipen = 999)
options(prompt = "R> ")
setwd("~/Workspace/iota/iotaData/") # Set Working Directory (wd)
# Load package 'devtools'
library('devtools')
# Load package 'roxygen2'
library('roxoygen2')
# Load package 'formattable'
library('formattable')
options("getSymbols.warning4.0" = F)
#### SET UP R PACKAGE FRAMEWORK ####
# creates framework and four folders
devtools::create("iota")
#R CMD check: devltools:: check() | Cmd + shift + E
devtools::check()
#### API CALL TO BITFINEX ####
# Load packages
#### IOTA SUPPLY & SI UNITS ####
options(scipen = 999) # Deny Scientific Notation Formatting
# Total supply of IOTA in iota in SI units
Iota = 1 # Iota
kiIota = 10 ^ 3 # Kilo Iota
miIota = 10 ^ 6 # Mega Iota
giIota = 10 ^ 9 # Giga Iota
tiIota = 10 ^ 12 # Tera Iota
piIota = 10 ^ 15 # Peta Iota
iotaSply = ((3 ^ 33 - 1) / 2) # Total Iota Supply
iotaSply
#### FUNCTION: 'convertIOTA(quant, from, to)' CONVERT from and to any iota SI unit scale
#how can I restrict input for 'i' to integers only as great or less than 33^3-1)/2
convertIOTA <- function(quant, from, to) {
options(scipen = 999)
quant <- NULL
from <- NULL
to <- NULL
i <- 1 # Iota
ki = 10 ^ 3 # Kilo Iota
mi = 10 ^ 6 # Mega Iota
gi = 10 ^ 9 # Giga Iota
ti = 10 ^ 12 # Tera Iota
pi = 10 ^ 15 # Peta Iota
if (from == i && to == i) {
result <- quant
print(paste(result, 'Iota'))
}
if (from == i && to == ki) {
result <- quant / 1000
print(paste(result, 'Kilo Iota'))
}
if (from == i && to == mi) {
result <- quant / 1000000
print(paste(result, 'Mega Iota'))
}
if (from == i && to == gi) {
result <- quant / 100000000
print(paste(result, 'Giga Iota'))
}
if (from == i && to == ti) {
result <- quant / 100000000000
print(paste(result, 'Tera Iota'))
}
if (from == i && to == pi) {
result <- quant / 100000000000000
print(paste(result, 'Peta Iota'))
}
if (from == ki && to == i) {
result <- quant * 1000
print(paste(result, 'Iota'))
}
if (from == ki && to == ki) {
result <- quant
print(paste(result, 'Kilo Iota'))
}
if (from == ki && to == mi) {
result <- quant / 1000
print(paste(result, 'Mega Iota'))
}
if (from == ki && to == gi) {
result <- quant / 10000
print(paste(result, 'Giga Iota'))
}
if (from == ki && to == ti) {
result <- quant / 100000
print(paste(result, 'Tera Iota'))
}
if (from == ki && to == pi) {
result <- quant / 10000000
print(paste(result, 'Peta Iota'))
}
if (from == mi && to == i) {
result <- quant * 1000000
print(paste(result, 'Iota'))
}
if (from == mi && to == ki) {
result <- quant * 1000
print(paste(result, 'Kilo Iota'))
}
if (from == mi && to == mi) {
result <- quant
print(paste(result, 'Mega Iota'))
}
if (from == mi && to == gi) {
result <- quant / 1000
print(paste(result, 'Giga Iota'))
}
if (from == mi && to == ti) {
result <- quant / 1000000
print(paste(result, 'Tera Iota'))
}
if (from == mi && to == pi) {
result <- quant / 100000000
print(paste(result, 'Peta Iota'))
}
if (from == gi && to == i) {
result <- quant * 1000000000
print(paste(result, 'Iota'))
}
if (from == gi && to == ki) {
result <- quant * 1000000
print(paste(result, 'Kilo Iota'))
}
if (from == gi && to == mi) {
result <- quant * 1000
print(paste(result, 'Mega Iota'))
}
if (from == gi && to == gi) {
result <- quant
print(paste(result, 'Giga Iota'))
}
if (from == gi && to == ti) {
result <- quant / 1000
print(paste(result, 'Tera Iota'))
}
if (from == gi && to == pi) {
result <- quant / 1000000
print(paste(result, 'Peta Iota'))
}
if (from == ti && to == i) {
result <- quant * 1000000000000
print(paste(result, 'Iota'))
}
if (from == ti && to == ki) {
result <- quant * 1000000000
print(paste(result, 'Kilo Iota'))
}
if (from == ti && to == mi) {
result <- quant * 1000000
print(paste(result, 'Mega Iota'))
}
if (from == ti && to == gi) {
result <- quant * 1000
print(paste(result, 'Giga Iota'))
}
if (from == ti && to == ti) {
result <- quant
print(paste(result, 'Tera Iota'))
}
if (from == ti && to == pi) {
result <- quant / 1000
print(paste(result, 'Peta Iota'))
}
if (from == pi && to == i) {
result <- quant * 1000000000000000
print(paste(result, 'Iota'))
}
if (from == pi && to == ki) {
result <- quant * 1000000000000
print(paste(result, 'Kilo Iota'))
}
if (from == pi && to == mi) {
result <- quant * 1000000000
print(paste(result, 'Mega Iota'))
}
if (from == pi && to == gi) {
result <- quant * 1000000
print(paste(result, 'Giga Iota'))
}
if (from == pi && to == ti) {
result <- quant * 1000
print(paste(result, 'Tera Iota'))
}
if (from == pi && to == pi) {
result <- quant
print(paste(result, 'Peta Iota'))
}
}
convertIOTA(32423, ti , mi)
## I am missing some cases, such as greater down to lesser, this code assumes you only want to convert upwards
# Test function
print(convertIOTA(12312, ti, ki))
#### FUNCTION: 'perOfIotaSply()' ####
#divisor <- siUnit #how do I know if my local variables are global or local to my function? to my package environment
# The tertiary function perOfIotaSply() accepts three arguments:
perOfIotaSply <- function(quant, siUnit) {
i <- 1 # Iota
ki = 10 ^ 3 # Kilo Iota
mi = 10 ^ 6 # Mega Iota
gi = 10 ^ 9 # Giga Iota
ti = 10 ^ 12 # Tera Iota
pi = 10 ^ 15 # Peta Iota
result <- ((quant * siUnit) / iotaSply) * 100
paste('You own ', result, '% of the total Iota supply.', sep = "")
}
perOfIotaSply(10.937, gi)
#### Function: n 'iotaMrkCap()' accepts 'priceOf_miIOTA' as its argument and outputs market capitalization
iotaSply = ((3 ^ 33 - 1) / 2) # total IOTA
iotaSply
iotaMktCap <- function(price) {
mktCap <- price * iotaSply / 1000000
print(currency(mktCap))
}
# Call iotaMrkCap() function
iotaMktCap(price = 0.7955)
iotaMktCap()
####### NOTES
.onLoad <- function(libname, pkgname) {
op <- options()
op.devtools <- list(
devtools.path = "~/R-dev",
devtools.install.args = "",
devtools.name = "Thomas A. Pepperz",
devtools.desc.author = '"Thomas Pepperz <thomas.peppers@colorado.edu> [aut, cre]"',
devtools.desc.license = "What license is it under?",
devtools.desc.suggests = NULL,
devtools.desc = list()
)
toset <- !(names(op.devtools) %in% names(op))
if (any(toset))
options(op.devtools[toset])
invisible()
}
getOption("devtools.name")
tools::vignetteEngine().
# Load a script into a new environment and save it
env <- new.env(parent = emptyenv())
source("my-script.R", local = env)
save(envir = env, "my-script.Rdata")
# Later, in another R session
load("my-script.Rdata")
##### Function: Iota Wallet Random Address Generator ####
# Load Function Package Dependencies
library(digest)
library(sodium)
# Produce Cryptographic Hash for New Seed
newSeedCryptoHash <-
digest(
newSeed,
algo = c("xxhash64"),
serialize = TRUE,
file = FALSE,
length = Inf,
skip = "auto",
ascii = FALSE,
raw = FALSE,
seed = 0,
errormode = c("warn")
)
# Evaluate newSeed Hashes for Identity by Testing Hash Input Against Hash Output
xxhash64Input <-
newSeed
xxhash64Output <-
c("44bc2cf5ad770999",
"f06103773e8585df",
"ef46db3751d8e999")
for (i in seq(along = xxhash64Input)) {
xxhash64 <-
digest(xxhash64Input[i], algo = "xxhash64", serialize = FALSE)
cat(xxhash64, "\n")
stopifnot(identical(xxhash64, xxhash64Output[i]))
}
# Perhaps I need to impose a beginning char rule?
# Should I store the full 243-char tryte security address and then extract the 81-char dpending on request?
# Adapt checksum from JavaScript? isValidAddress()?
####
generateAddress <- function(secLevel = 1) {
if (secLevel == 1) {
secLevel <- 81
} else if (secLevel == 2) {
secLevel <- 162
} else if (secLevel == 3) {
secLevel <- 243
} else
secLevel <- 81
alphanumerics <- c(LETTERS, 0:9)
walletAddress <-
paste0(sample(alphanumerics, secLevel, replace = TRUE), collapse = "")
return(walletAddress)
}
generateAddress()
#### Function To Generate an Iota Wallet Seed ####
generateSeed <- function() {
seedLength = 81
trytesAlphabet = c(9, 9, LETTERS)
seed <-
paste0(sample(trytesAlphabet, seedLength, replace = TRUE), collapse = "")
return(seed)
}
newSeed <- generateSeed()
print(newSeed)
#### Iota API + Node Explorer
myIotaSeed = "PKK9NCTRHGA9XKWOLEXIPLXJGYL9WV9JVLHAOSUZMRVJEZ9YFDVIABVMMDKGSYFYPZFJPXLNSMFJHBNZJ"
myIotaAddress = "TJKNPQKBKIQDLMMEZVGICNCZHPZ9Y9UIPSLJHFHGIVXBH9PTJNDMGCHQCHBEZYRGOVTJVQGGIOHQZLVIBPSBRMYLJW"
trytesAlphabet = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#func NewSeed() Trytes {
# b := make([]byte, 81)
# if _, err := rand.Read(b)
# err != nil {
# panic(err)
# }
# seed := make([]byte, 81)
# for i, r := range b {
# seed[i] = TryteAlphabet[int(r)%len(TryteAlphabet)]
# }
# return Trytes(seed)
#}