Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAnalysis_Index.py
More file actions
366 lines (262 loc) · 13.1 KB
/
Copy pathAnalysis_Index.py
File metadata and controls
366 lines (262 loc) · 13.1 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
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
import matplotlib.pylab as plt
import numpy as np
import pandas as pd
import yfinance as yf
class Quote :
mDate = "Date"
mOpen = "Open"
mHigh = "High"
mLow = "Low"
mClose = "Close"
mAdjClose = "Adj Close"
mVolume = "Volume"
class Metrics :
def __init__( self , date , wick_2_wick_range , body_2_body_range ) :
self.Date = date
self.Wick_2_Wick_Range = wick_2_wick_range
self.Body_2_Body_Range = body_2_body_range
class Metrics2 :
def __init__( self , year , min_wick , max_wick , min_body , max_body ) :
self.Year = year
self.Min_Wick = min_wick
self.Max_Wick = max_wick
self.Min_Body = min_body
self.Max_Body = max_body
class Analysis_Index :
def __init__( self , TradingSymbol ) :
print( "Constructor Called" )
print( "Creating Storage to Store Historical (Financial) Data" )
self.df_history_data = pd.DataFrame(
columns = [ Quote.mDate , Quote.mOpen , Quote.mHigh , Quote.mLow , Quote.mClose , Quote.mAdjClose ,
Quote.mVolume ]
)
print( "Stock Symbol is " , TradingSymbol )
self.get_history_data( TradingSymbol )
self.evaluate_Metrics( )
# self.print_All_stats( )
# self.Get_Results_on_Quartile_framework( )
self.Visualize_Data( )
def Visualize_Data( self ) :
try :
processing_year = 0
no_of_record = len( self.list_metrics )
counter = 0
datetime = [ ]
wick_2_wick = [ ]
body_2_body = [ ]
for record in self.list_metrics :
counter = counter+1
if processing_year == 0 : # first read
processing_year = record.Date.year
print( "Step 1 :First Record .." )
if processing_year != record.Date.year :
print( "Step 2: New Record Coming.." )
print( "evaluating last record" )
""" Plot data to Charts- Graph"""
self.plot_point_into_multi_graphh( datetime , wick_2_wick , processing_year , "Wick Range" ,
"ON WICK ABS(H-L)"
)
self.plot_point_into_multi_graphh( datetime , body_2_body , processing_year , "Body Range" ,
"On Body ABS(O-C)"
)
processing_year = record.Date.year
""" clear all items in list"""
del datetime[ : ]
del wick_2_wick[ : ]
del body_2_body[ : ]
"""Add Item in temp List """
datetime.append( record.Date )
wick_2_wick.append( record.Wick_2_Wick_Range )
body_2_body.append( record.Body_2_Body_Range )
if (no_of_record == counter) : # last item in record
print( "Step 3 :Last Record - Evaluating" )
self.plot_point_into_multi_graphh( datetime , wick_2_wick , processing_year , "Wick Range" ,
"ON WICK ABS(H-L)"
)
self.plot_point_into_multi_graphh( datetime , body_2_body , processing_year , "Body Range" ,
"On Body ABS(O-C)"
)
except :
print( " Failed to Genertae Data" )
def get_history_data( self , TradingSymbol ) :
try :
"""Call Yahoo Finance Function () to get data """
self.df_history_data = yf.download( TradingSymbol , start = "2019-01-01" , end = "2019-12-31"
).reset_index( )
# self.df_history_data = data.DataReader( TradingSymbol , start = "2023-01-01" , end = "2023-10-01" )
# self.df_history_data = self.df_history_data.reset_index()
""" date coloumn is itself index"""
""" Convert Date to DateTime """
self.df_history_data[ Quote.mDate ] = pd.to_datetime( self.df_history_data[ Quote.mDate ] )
_count_rows_in_df = len( self.df_history_data )
print( "Row(s) present in Fetching(s) Financial Data : " , _count_rows_in_df )
self.Strat_Analysing( )
except :
print( "Error(s) Occur while Downloading.." )
def Strat_Analysing( self ) :
if len( self.df_history_data ) == 0 :
print( "No Data Found" )
return
List_processing_year_record = [ ]
self.list_metrics = [ ]
for index , bar in self.df_history_data.iterrows( ) :
# print(bar)
_get_current_processing_year = bar[ Quote.mDate ].year
if _get_current_processing_year not in List_processing_year_record :
List_processing_year_record.append( _get_current_processing_year )
# print( "Processing Year is :" , _get_current_processing_year )
try :
wick_2_wick = abs( bar[ Quote.mHigh ]-bar[ Quote.mLow ] )
body_2_body = abs( bar[ Quote.mOpen ]-bar[ Quote.mClose ] )
""" Creaet object For Metrics """
O = Metrics( bar[ Quote.mDate ] , wick_2_wick , body_2_body )
if wick_2_wick > 100 :
print( bar[ Quote.mDate ] , wick_2_wick , body_2_body )
self.list_metrics.append( O )
# print("Year is :" ,bar.index )
except :
print( "Unknown Error(s) Occured" )
return
def evaluate_Metrics( self ) :
min_wick_range = 0
min_body_range = 0
max_wick_range = 0
max_body_range = 0
processing_year = 0
metrics = [ ]
self.List_metrics_return = [ ]
last_record = len( self.list_metrics )
scanning_rows = 0
for metric in self.list_metrics :
scanning_rows = scanning_rows+1
if (processing_year != metric.Date.year) :
""" Save Last(Process) Recod """
if processing_year != 0 :
m = Metrics2( processing_year , min_wick_range , max_wick_range , min_body_range , max_body_range )
self.List_metrics_return.append( m )
processing_year = metric.Date.year
min_wick_range = metric.Wick_2_Wick_Range
min_body_range = metric.Body_2_Body_Range
max_wick_range = metric.Wick_2_Wick_Range
max_body_range = metric.Body_2_Body_Range
""" Major Calculation """
if min_body_range > metric.Body_2_Body_Range :
min_body_range = metric.Body_2_Body_Range
if min_wick_range > metric.Wick_2_Wick_Range :
min_wick_range = metric.Wick_2_Wick_Range
if max_wick_range < metric.Wick_2_Wick_Range :
max_wick_range = metric.Wick_2_Wick_Range
if max_body_range < metric.Body_2_Body_Range :
max_body_range = metric.Body_2_Body_Range
""" Record last Result """
if processing_year not in metrics : # n- 1 Year
metrics.append( processing_year )
# print("MA" ,processing_year )
if scanning_rows == last_record : # nth Year
print( "Last Record is --" )
m = Metrics2( processing_year , min_wick_range , max_wick_range , min_body_range , max_body_range )
self.List_metrics_return.append( m )
# if(metric.Wick_2_Wick_Range)
# """ print Analysis """
# print("MIN WICK Range :" , min_wick_range)
# print( "MIN BODY Range :" , min_body_range )
#
# print( "MAX WICK Range :" , max_wick_range )
# print( "MAX BODY Range :" , max_body_range )
print( "Task evaluate_Metrics is Completed : Now Start get ALl Statitics Result " )
def print_All_stats( self ) :
print( "Processing year ," , "MIN WICK_2_WICK ," , "MAX WICK_2_WICK," ,
"MIN BODY_2_BODY," , "MAX BODY_2_BODY,"
)
for stat in self.List_metrics_return :
# print("Processing year :" ,stat.Year , "MIN WICK_2_WICK :" , round(stat.Min_Wick,2), "MAX WICK_2_WICK" , round(stat.Max_Wick,2) ,
# "MIN BODY_2_BODY : ", round(stat.Min_Body,2) , "MAX BODY_2_BODY :" ,round( stat.Max_Body))
print( stat.Year , "," , round( stat.Min_Wick , 2 ) , "," , round( stat.Max_Wick , 2 ) , "," ,
round( stat.Min_Body , 2 ) , "," , round( stat.Max_Body )
)
def Get_Results_on_Quartile_framework( self ) :
try :
print( "Quartile" )
processing_year = 0
_list_wick = [ ]
_list_body = [ ]
total_rows = len( self.list_metrics )
counter = 0
for item in self.list_metrics :
counter = counter+1
""" Step 1 :Tramsfer ( update) First Record """
if (processing_year != item.Date.year and processing_year != 0) or (
total_rows == counter and processing_year != 0) :
processing_year = item.Date.year
print( "Processing Quartile on :" , processing_year )
""" perform Quartile """
_list_wick.sort( )
_list_body.sort( )
print( "On Wick " )
self.calculating_Quartile( _list_wick , processing_year , "ON WICK ABS(H-L)" )
print( "On Body " )
self.calculating_Quartile( _list_body , processing_year , "On BODY ABS(O-C) " )
""" Clean(Clear) Run Time List """
_list_body.clear( )
_list_wick.clear( )
"""Step 2 : Read and Load """
processing_year = item.Date.year
_list_wick.append( item.Wick_2_Wick_Range )
_list_body.append( item.Body_2_Body_Range )
# print( "Test" , item.Date.year )
except :
print( "Error(s) Occured" )
def calculating_Quartile( self , list_input , processing_year , Message ) :
try :
list = np.array( list_input )
q1 = np.quantile( list , .25 )
q2 = np.quantile( list , .50 )
q3 = np.quantile( list , .75 )
_100 = np.quantile( list , .100 )
print( "25 % is :" , q1 )
print( "50 % is :" , q2 )
print( "75 % is :" , q3 )
print( "100% is :" , _100 )
"""PLT Data """
plt.boxplot( list , vert = False )
# Add quartile annotations
plt.annotate( f'Q1: {q1:.2f}' , xy = (q1 , 1) , xytext = (q1 , 1.1) ,
arrowprops = dict( facecolor = 'black' , shrink = 0.05 )
)
plt.annotate( f'Q2: {q2:.2f}' , xy = (q2 , 1) , xytext = (q2 , 1.1) ,
arrowprops = dict( facecolor = 'black' , shrink = 0.05 )
)
plt.annotate( f'Q3: {q3:.2f}' , xy = (q3 , 1) , xytext = (q3 , 1.1) ,
arrowprops = dict( facecolor = 'black' , shrink = 0.05 )
)
# Add title and labels
plt.title( f'Box Plot of Data with Quartiles Year {processing_year} , {Message}' )
plt.xlabel( 'Values' )
# Show the plot
plt.show( )
except :
print( "Error(s) While Calcualating Quartile" )
def plot_point_into_graphh( self , date_time , datapoints , processing_year , message , type_body_or_wick ) :
try :
plt.plot( date_time , datapoints )
plt.xlabel( "Date Time" ) # add X-axis label
plt.ylabel( message ) # add Y-axis label
plt.title( f"Processing Year :{processing_year} on {type_body_or_wick}" ) # add title
plt.show( )
except :
print( "Failed to Visualize Current Data " )
def plot_point_into_multi_graphh( self , date_time , datapoints , processing_year , message , type_body_or_wick ) :
try :
# plt.plot( date_time , datapoints )
plt.plot( date_time , datapoints , 'k.-' , label = f'Range {type_body_or_wick} ' )
AVERAGE = 0
if len( datapoints ) > 0 :
AVERAGE = sum( datapoints ) / len( datapoints )
plt.axhline( AVERAGE , color = 'red' , linestyle = '--' , linewidth = 3 , label = 'Average' )
plt.xlabel( "Date Time" ) # add X-axis label
plt.ylabel( message ) # add Y-axis label
legend = plt.legend( loc = 'upper right' )
plt.title( f"Processing Year :{processing_year} {type_body_or_wick}" ) # add title
plt.show( )
except Exception as e :
print( f"Failed to Visualize Current Data {e}")
You can’t perform that action at this time.
