forked from DaStar81/BitcoinTradingAlgorithmToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
111 lines (95 loc) · 4.48 KB
/
Copy pathexample.py
File metadata and controls
111 lines (95 loc) · 4.48 KB
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
import pandas as pd
import data
import dtools
from pybrain.datasets import SupervisedDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import RPropMinusTrainer
from pybrain.structure import RecurrentNetwork, FullConnection
from pybrain.structure.modules import LinearLayer, TanhLayer
import numpy as np
from matplotlib import pylab as plt
# 10-minute timeframe
time_str = "10min"
# our litecoin, statistical analysis options
ltc_opts = \
{ "debug": False,
"relative": False,
"calc_rolling": True,
"rolling": { time_str : { 12: pd.DataFrame(),
24: pd.DataFrame(),
50: pd.DataFrame() } },
"calc_mid": True,
"calc_ohlc": True,
"ohlc": { time_str : pd.DataFrame() },
"calc_indicators": True,
"indicators":{ "RSI" : { "data": pd.DataFrame(), "n":14 },
"ROC" : { "data": pd.DataFrame(), "n":20 },
"AMA" : { "data": pd.DataFrame(), "n":10, "fn":2.5, "sn":30 },
"CCI" : { "data": pd.DataFrame(), "n":20 },
"FRAMA": { "data": pd.DataFrame(), "n":10 },
"RVI2" : { "data": pd.DataFrame(), "n":14, "s":10 },
"MACD" : { "data": pd.DataFrame(), "f":12, "s":26, "m":9 },
"ADX" : { "data": pd.DataFrame(), "n":14 },
"ELI" : { "data": pd.DataFrame(), "n":14 },
"TMI" : { "data": pd.DataFrame(), "nb":10, "nf":5 }
},
"calc_std": True,
"std": { 13 : pd.DataFrame(), 21 : pd.DataFrame(), 34 : pd.DataFrame() },
"calc_crt": True,
"crt": { 1: pd.DataFrame(),
2: pd.DataFrame(),
3: pd.DataFrame(),
5: pd.DataFrame(),
8: pd.DataFrame() },
"instant": True,
"time_str": time_str }
# Or if you wanted to use an randomized genetic code ... add this in place of
# ltc_opts in Data() to use it
import genetic
# create a random individual
individual = genetic.rand_gene()
# mutate with 0-mean, 4-std dev gaussian random fcn & 40% chance of mutation
individual = genetic.mutate_gene( individual, 0, 4, 0.4)
# turn the genetic code into a Data options struct, with a 5 min timeframe
btc_opts = genetic.decode_gene( individual, 5) # this can be used in Data()
# warp and instant = True forces Data to calculate everything all in one
# pass. Other options would make it run in simulated real time. Load
# filename called "test.csv" from ./logs/ ... Data
# assumes all logs are in the local directory called logs
d = data.Data( warp=True, instant=True, ltc_opts=ltc_opts,
time_str=time_str, filename="test.csv")
# Pull out all indicators into a single pandas dataframe. Prefix all rows
# with "LTC"
ltc = d.ltc.combine( "LTC")
# take our ltc dataframe, and get targets (prices in next 10 minutes)
# in the form of compound return prices (other options are "PRICES",
# which are raw price movements)
dataset, tgt = dtools.gen_ds( ltc, 1, ltc_opts, "CRT")
# initialize a pybrain dataset
DS = SupervisedDataSet( len(dataset.values[0]), np.size(tgt.values[0]) )
# fill it
for i in xrange( len( dataset)):
DS.appendLinked( dataset.values[i], [ tgt.values[i]] )
# split 70% for training, 30% for testing
train_set, test_set = DS.splitWithProportion( .7)
# build our recurrent network with 10 hidden neurodes, one recurrent
# connection, using tanh activation functions
net = RecurrentNetwork()
hidden_neurodes = 10
net.addInputModule( LinearLayer(len( train_set["input"][0]), name="in"))
net.addModule( TanhLayer( hidden_neurodes, name="hidden1"))
net.addOutputModule( LinearLayer( len( train_set["target"][0]), name="out"))
net.addConnection(FullConnection(net["in"], net["hidden1"], name="c1"))
net.addConnection(FullConnection(net["hidden1"], net["out"], name="c2"))
net.addRecurrentConnection(FullConnection(net["out"], net["hidden1"], name="cout"))
net.sortModules()
net.randomize()
# train for 30 epochs (overkill) using the rprop- training algorithm
trainer = RPropMinusTrainer( net, dataset=train_set, verbose=True )
trainer.trainOnDataset( train_set, 30)
# test on training set
predictions_train = np.array( [ net.activate( train_set["input"][i])[0] for i in xrange( len(train_set)) ])
plt.plot( train_set["target"], c="k"); plt.plot( predictions_train, c="r"); plt.show()
# and on test set
predictions_test = np.array( [ net.activate( test_set["input"][i])[0] for i in xrange( len(test_set)) ])
plt.plot( test_set["target"], c="k"); plt.plot( predictions_test, c="r"); plt.show()