-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfictiveStockGenerator.py
171 lines (127 loc) · 6.42 KB
/
fictiveStockGenerator.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
# coding=utf-8
###############################################################################
################################### Imports ###################################
###############################################################################
import numpy as np
import pandas as pd
from scipy import signal
from dataDownloader import YahooFinance
###############################################################################
################################ Global variables #############################
###############################################################################
# Default values for the generation of the fictive stock market curves
MIN = 100
MAX = 200
PERIOD = 252
###############################################################################
############################ Class StockGenerator #############################
###############################################################################
class StockGenerator:
"""
GOAL: Generation of some fictive stock market curves
(linear, sinusoidal, triangle, etc.).
VARIABLES: /
METHODS: - linearUp: Generate a continuously increasing linear curve.
- linearDown: Generate a continuously decreasing linear curve.
- sinusoidal: Generate a (periodic) sinusoidal signal curve.
- triangle: Generate a (periodic) triangle signal curve.
"""
def linearUp (self, startingDate, endingDate, minValue=MIN, maxValue=MAX):
"""
GOAL: Generate a new fictive stock market as a continuously increasing
linear curve.
INPUTS: - startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
- minValue: Minimum price value.
- maxValue: Maximum price value.
OUTPUTS: - linearUpward: Generated fictive stock market dataframe.
"""
# Initialization of the new stock market dataframe
downloader = YahooFinance()
DowJones = downloader.getDailyData('DIA', startingDate, endingDate)
linearUpward = pd.DataFrame(index=DowJones.index)
# Generation of the fictive prices over the trading horizon
length = len(linearUpward.index)
prices = np.linspace(minValue, maxValue, num=length)
# Filling of the new fictive stock market dataframe
linearUpward['Open'] = prices
linearUpward['High'] = prices
linearUpward['Low'] = prices
linearUpward['Close'] = prices
linearUpward['Volume'] = 100000
return linearUpward
def linearDown (self, startingDate, endingDate, minValue=MIN, maxValue=MAX):
"""
GOAL: Generate a new fictive stock market as a continuously decreasing
linear curve.
INPUTS: - startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
- minValue: Minimum price value.
- maxValue: Maximum price value.
OUTPUTS: - linearDownward: Generated fictive stock market dataframe.
"""
# Initialization of the new stock market dataframe
downloader = YahooFinance()
DowJones = downloader.getDailyData('DIA', startingDate, endingDate)
linearDownward = pd.DataFrame(index=DowJones.index)
# Generation of the fictive prices over the trading horizon
length = len(linearDownward.index)
prices = np.linspace(minValue, maxValue, num=length)
prices = np.flip(prices)
# Filling of the new fictive stock market dataframe
linearDownward['Open'] = prices
linearDownward['High'] = prices
linearDownward['Low'] = prices
linearDownward['Close'] = prices
linearDownward['Volume'] = 100000
return linearDownward
def sinusoidal(self, startingDate, endingDate, minValue=MIN, maxValue=MAX, period=PERIOD):
"""
GOAL: Generate a new fictive stock market as a sinusoidal signal curve.
INPUTS: - startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
- minValue: Minimum price value.
- maxValue: Maximum price value.
- period: Period of the sinusoidal signal.
OUTPUTS: - sinusoidal: Generated fictive stock market dataframe.
"""
# Initialization of the new stock market dataframe
downloader = YahooFinance()
DowJones = downloader.getDailyData('DIA', startingDate, endingDate)
sinusoidal = pd.DataFrame(index=DowJones.index)
# Generation of the fictive prices over the trading horizon
length = len(sinusoidal.index)
t = np.linspace(0, length, num=length)
prices = minValue + maxValue / 2 * (np.sin(2 * np.pi * t / period) + 1) / 2
# Filling of the new fictive stock market dataframe
sinusoidal['Open'] = prices
sinusoidal['High'] = prices
sinusoidal['Low'] = prices
sinusoidal['Close'] = prices
sinusoidal['Volume'] = 100000
return sinusoidal
def triangle(self, startingDate, endingDate, minValue=MIN, maxValue=MAX, period=PERIOD):
"""
GOAL: Generate a new fictive stock market as a triangle signal curve.
INPUTS: - startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
- minValue: Minimum price value.
- maxValue: Maximum price value.
- period: Period of the triangle signal.
OUTPUTS: - triangle: Generated fictive stock market dataframe.
"""
# Initialization of the new stock market dataframe
downloader = YahooFinance()
DowJones = downloader.getDailyData('DIA', startingDate, endingDate)
triangle = pd.DataFrame(index=DowJones.index)
# Generation of the fictive prices over the trading horizon
length = len(triangle.index)
t = np.linspace(0, length, num=length)
prices = minValue + maxValue / 2 * np.abs(signal.sawtooth(2 * np.pi * t / period))
# Filling of the new fictive stock market dataframe
triangle['Open'] = prices
triangle['High'] = prices
triangle['Low'] = prices
triangle['Close'] = prices
triangle['Volume'] = 100000
return triangle