-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stock.cs
404 lines (373 loc) · 15.1 KB
/
Stock.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Screener
{
public class Stock
{
private bool[] inScreener = new bool[2];
private DateTime earningsDate;
private double high52W;
private double epsNextY;
private double epsThisY;
private double recom;
private double currentRatio;
private double targetPrice;
private double price;
private int fund;
private int growth;
private int valuation;
private int totalScore;
private int totalScore2;
private int zacksRank;
private string beforeAfterClose;
private string zacksString;
private string symbol;
private string industry;
public string SymbolValue
{
get { return symbol; }
set { symbol = value; }
}
public string IndustryValue
{
get { return industry; }
set { industry = value; }
}
public int FundValue
{
get { return fund; }
set { fund = value; }
}
public int GrowthValue
{
get { return growth; }
set { growth = value; }
}
public int ValuationValue
{
get { return valuation; }
set { valuation = value; }
}
public double High52WValue
{
get { return high52W; }
set { high52W = value; }
}
public double RecomValue
{
get { return recom; }
set { recom = value; }
}
public double CurrentRatioValue
{
get { return currentRatio; }
set { currentRatio = value; }
}
public double EPSNextYValue
{
get { return epsNextY; }
set { epsNextY = value; }
}
public double EPSThisYValue
{
get { return epsThisY; }
set { epsThisY = value; }
}
public double TargetPriceValue
{
get { return targetPrice; }
set { targetPrice = value; }
}
public double PriceValue
{
get { return price; }
set { price = value; }
}
public string BeforeAfterCloseValue
{
get { return beforeAfterClose; }
set { beforeAfterClose = value; }
}
public int TotalScoreValue
{
get { return totalScore; }
set { totalScore = value; }
}
public int TotalScore2Value
{
get { return totalScore2; }
set { totalScore2 = value; }
}
public int ZacksRankValue
{
get { return zacksRank; }
set { zacksRank = value; }
}
public string ZacksStringValue
{
get { return zacksString; }
set { zacksString = value; }
}
public DateTime GetEarningsDate() { return earningsDate; }
public string GetEarningsDateString()
{
if (earningsDate != new DateTime(0)) { return earningsDate.ToShortDateString(); }
return "NA";
}
public void SetEarningsDate(string value)
{
if(value == "-")
{
earningsDate = new DateTime(0);
return;
}
var dateStrings = value.Split(new char[] { ' ', '/' });
var tempDate = DateTime.Now;
earningsDate = new DateTime(tempDate.Year, GetMonthDigit(dateStrings[0]), int.Parse(dateStrings[1]), tempDate.Hour, tempDate.Minute, tempDate.Second);
if(dateStrings.Length > 2)
{
beforeAfterClose = dateStrings[2];
} else
{
beforeAfterClose = "b";
}
}//end SetEarningsDate
public void SetInScreener(int index, bool isIn)
{
if(index > 1 || 0 > index) { return; }
inScreener[index] = isIn;
}//end
public bool GetInScreener(int index)
{
if(index > 1 || 0 > index) { return false; }
return inScreener[index];
}//end
/// <summary>
/// Calculates the total score of the Stock object
/// </summary>
public void CalculateTotalScore()
{
totalScore = GetFundOrGrowthScore(FundValue) + GetFundOrGrowthScore(GrowthValue)+ GetValuationScore() +
GetRecomScore() + GetZacksRankScore() + GetHigh52WScore() + GetCurrentRatioScore() + GetEarningsDateScore();
totalScore2 = (GetFundOrGrowthScore(FundValue) * 5) + (GetFundOrGrowthScore(GrowthValue) * 3)+
(GetValuationScore() * 2) + (GetRecomScore() * 3) + (GetZacksRankScore() * 10) + GetEPSNextYScore(true) +
GetEPSThisYScore(true) + GetTargetPriceScore(true);
}//end CalculateTotalScore
/// <summary>
/// Gets the attributes of the Stock object in an enumerable object
/// </summary>
/// <returns>The attributes of the Stock object</returns>
public IEnumerable<object> GetAttributesEnumerable(bool secondScreener)
{
if(secondScreener)
{
return new List<object>() { symbol, industry, fund, growth, valuation, epsThisY, epsNextY, recom, targetPrice, GetEarningsDateString(), zacksString, totalScore2 };
} else
{
return new List<object>() { symbol, industry, fund, growth, valuation, high52W, recom, currentRatio, GetEarningsDateString(), zacksString, totalScore };
}//end if-else
}//end GetAttributesEnum
/// <summary>
/// Returns the score of the Fund or Value attribute depending on which value is sent to the function
/// </summary>
/// <param name="val">The value to be evaluated</param>
/// <returns></returns>
public int GetFundOrGrowthScore(int val) { return (val >= 7 ? 4 : val >= 4 ? 2 : -2); }
/// <summary>
/// Determines the score of the Valuation attribute and returns it to the calling program
/// </summary>
/// <returns></returns>
public int GetValuationScore() { return (ValuationValue >= 5 ? 4 : ValuationValue >= 3 ? 2 : -2); }
/// <summary>
/// Determines the score of the High52W attribute and returns it to the calling program
/// </summary>
/// <returns></returns>
public int GetHigh52WScore() { return (High52WValue <= -30 ? 4 : High52WValue <= -10 || High52WValue == Double.MinValue ? 2 : -2); }
/// <summary>
/// Determines the score of the Recom attribute and returns it to the calling program
/// </summary>
/// <returns></returns>
public int GetRecomScore() { return (RecomValue <= 2 ? 4 : RecomValue <= 3 || RecomValue == Double.MinValue ? 2 : -2); }
/// <summary>
/// Determines the score of the Current Ratio attribute and returns it to the calling program
/// </summary>
/// <returns></returns>
public int GetCurrentRatioScore() { return (CurrentRatioValue >= 3 ? 4 : CurrentRatioValue >= 1 || CurrentRatioValue == Double.MinValue ? 2 : -2); }
/// <summary>
/// Determines the score of the Zacks Rank attribute and returns it to the calling program
/// </summary>
/// <returns></returns>
public int GetZacksRankScore() { return (ZacksRankValue == 1 ? 6 : ZacksRankValue == 2 ? 4 : ZacksRankValue == 3 ? 2 : ZacksRankValue == 4 ? -4 : -6); }
public int GetEPSNextYScore(bool multiply = false)
{
var res = (EPSNextYValue >= 25 ? 4 : 25 > EPSNextYValue && EPSNextYValue >= 0 ? 2 : -2);
if(multiply)
{
return res * 5;
}
return res;
}
public int GetEPSThisYScore(bool multiply = false)
{
var res = (EPSThisYValue >= 25 ? 4 : 25 > EPSThisYValue && EPSThisYValue >= 0 ? 2 : -2);
if(multiply)
{
return res * 5;
}
return res;
}
public int GetTargetPriceScore(bool multiply = false)
{
var res = (TargetPriceValue > PriceValue ? 4 : -2);
if(multiply)
{
return res * 2;
}
return res;
}
/// <summary>
/// Determines the color of the attribute to be used in document saving and printing
/// </summary>
/// <returns></returns>
public System.Drawing.Color[] GetFormattingColors(bool secondScreener)
{
if(secondScreener)
{
return new System.Drawing.Color[] { GetAttributeColor(GetFundOrGrowthScore(FundValue)), GetAttributeColor(GetFundOrGrowthScore(GrowthValue)),
GetAttributeColor(GetValuationScore()),GetAttributeColor(GetEPSThisYScore()), GetAttributeColor(GetEPSNextYScore()),
GetAttributeColor(GetRecomScore()), GetAttributeColor(GetTargetPriceScore()),
GetAttributeColor(GetEarningsDateScore()), GetAttributeColor(GetZacksRankScore(), true) };
} else
{
return new System.Drawing.Color[] { GetAttributeColor(GetFundOrGrowthScore(FundValue)), GetAttributeColor(GetFundOrGrowthScore(GrowthValue)),
GetAttributeColor(GetValuationScore()),GetAttributeColor(GetHigh52WScore()),
GetAttributeColor(GetRecomScore()), GetAttributeColor(GetCurrentRatioScore()),
GetAttributeColor(GetEarningsDateScore()), GetAttributeColor(GetZacksRankScore(), true) };
}//end if-else
}//end if
private System.Drawing.Color GetAttributeColor(int val, bool isZacks = false)
{
if(!isZacks)
{
return val == 4 ? System.Drawing.Color.Green : val == 2 ? System.Drawing.Color.Yellow : System.Drawing.Color.Red;
} else
{
return val == 6 ? System.Drawing.Color.Green : val == 4 ? System.Drawing.Color.Blue :
val == 2 ? System.Drawing.Color.Yellow : val == -4 ? System.Drawing.Color.Orange : System.Drawing.Color.Red;
}
}//end GetAttributeColor
/// <summary>
/// Returns the score for the earningsDate attribute.
/// </summary>
/// <returns></returns>
private int GetEarningsDateScore()
{
var temp = GetEarningsDateTimeSpan();
/*Determines if the earnings date is today or not. If so determines if the earnings is from before or after close.
If before check to verify that the time is after 9:30am, if after time needs to be after 4:00pm. If the earnings
date is after today's date always negative. If before determine the range, ranges are 1 <= x <= 70, 71 <= x < 4 months
and 4 months after*/
if (temp == 0)
{
if (BeforeAfterCloseValue == "b")
{
return (earningsDate.TimeOfDay > new TimeSpan(9, 30, 0) ? 4 : -2);
}
else
{
return (earningsDate.TimeOfDay > new TimeSpan(16, 0, 0) ? 4 : -2);
}//end nested if-else
}
else
{
if (1 <= temp && temp <= 70)
{
return 4;
}
else if ((70 < temp && temp < (earningsDate - DateTime.Today.AddMonths(-4)).Days) || earningsDate == new DateTime(0))
{
return 2;
}
else
{
return -2;
}//end nested if-else if-else
}//end if-else
}//end getEarningsDateScore
/// <summary>
/// Determines the number of days between the current day and the earningsDate value
/// </summary>
/// <returns>The number of days</returns>
private int GetEarningsDateTimeSpan()
{
if (earningsDate > new DateTime(0))
{
/*Creates a variable to add to the hours, minutes, and seconds of a temp DateTime object to verify if the Month, Day, and Year
are all the same. The earnings date is less than the current date, returns the day interval from today to earningsDate. If
the same, returns 0, else return -366*/
int minusHours = earningsDate.TimeOfDay.Hours * -1;
int minusMinutes = earningsDate.TimeOfDay.Minutes * -1;
int minusSeconds = earningsDate.TimeOfDay.Seconds * -1;
var temp = new DateTime(earningsDate.Year, earningsDate.Month, earningsDate.Day, earningsDate.Hour + minusHours, earningsDate.Minute + minusMinutes, earningsDate.Second + minusSeconds);
var compare = DateTime.Today.CompareTo(temp);
if (compare == 1) //DateTime.Today is later than earningsDate
{
return (DateTime.Now - earningsDate).Days;
}
else if (compare == 0)//Same day
{
return 0;
}
else //DateTime.Today is before earningsDate
{
return -366;
}
}
else
{
return -366;
}
//return (earningsDate > new DateTime(0) ? (DateTime.Now - earningsDate).Days : -366);
}//end get EarningsDateTimeSpan
/// <summary>
/// Returns the digit for the month
/// </summary>
/// <param name="month">The month abbreviation string from Finviz</param>
/// <returns>The month in int format</returns>
private int GetMonthDigit(string month)
{
switch(month)
{
case "Jan":
return 1;
case "Feb":
return 2;
case "Mar":
return 3;
case "Apr":
return 4;
case "May":
return 5;
case "Jun":
return 6;
case "Jul":
return 7;
case "Aug":
return 8;
case "Sep":
return 9;
case "Oct":
return 10;
case "Nov":
return 11;
case "Dec":
return 12;
default:
return 1;
}//end switch
}//end GetMonthDigit
}//end Stock
}//end Screener