Skip to content

Commit

Permalink
Switch to the updated Yahoo data source for the stock graph.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepj committed Feb 7, 2020
1 parent ff8bd29 commit a7a6b3c
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 180 deletions.
51 changes: 19 additions & 32 deletions Graph Views/XRGStockView.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ - (void)awakeFromNib {

djia = [[XRGStock alloc] init];
[djia setSymbol:@"%5EDJI"];
[djia setURL];


NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
m = [[XRGModule alloc] initWithName:@"Stock" andReference:self];
Expand Down Expand Up @@ -110,15 +108,13 @@ - (void)setStockSymbolsFromString:(NSString *)s {
[self reloadStockData];
}

// *** Memory leak when allocating in the for loop. Should be correct.
- (void)resetStockObjects {
int i;

[stockObjects removeAllObjects];
for (i = 0; i < [stockSymbols count]; i++) {
XRGStock *tmpStock = [[XRGStock alloc] init];
[tmpStock setSymbol:stockSymbols[i]];
[tmpStock setURL];
[stockObjects addObject:tmpStock];
}

Expand All @@ -136,20 +132,14 @@ - (void)reloadStockData {
}

- (bool)dataIsReady {
int i;
XRGStock *tmpStock;

if (!self.gettingData) return YES;

for (i = 0; i < [stockObjects count]; i++) {
tmpStock = stockObjects[i];
[tmpStock checkForData];
for (XRGStock *tmpStock in stockObjects) {
if ([tmpStock gettingData]) {
return NO;
}
}

[djia checkForData];
if ([djia gettingData]) {
return NO;
}
Expand Down Expand Up @@ -228,13 +218,13 @@ - (void)drawMiniGraph {
// draw the graph
NSArray *a = nil;
if ([appSettings stockGraphTimeFrame] == 0)
a = [showStock get1MonthValues:366];
a = [showStock get1MonthValues];
else if ([appSettings stockGraphTimeFrame] == 1)
a = [showStock get3MonthValues:366];
a = [showStock get3MonthValues];
else if ([appSettings stockGraphTimeFrame] == 2)
a = [showStock get6MonthValues:366];
a = [showStock get6MonthValues];
else if ([appSettings stockGraphTimeFrame] == 3)
a = [showStock get12MonthValues:366];
a = [showStock get12MonthValues];

if ([a count] > 0) {
// find the high, low and range of the graph
Expand Down Expand Up @@ -287,13 +277,13 @@ - (void)drawGraph {
// draw the graph
NSArray *a = nil;
if ([appSettings stockGraphTimeFrame] == 0)
a = [stockObjects[stockToShow] get1MonthValues:self.graphSize.width];
a = [stockObjects[stockToShow] get1MonthValues];
else if ([appSettings stockGraphTimeFrame] == 1)
a = [stockObjects[stockToShow] get3MonthValues:self.graphSize.width];
a = [stockObjects[stockToShow] get3MonthValues];
else if ([appSettings stockGraphTimeFrame] == 2)
a = [stockObjects[stockToShow] get6MonthValues:self.graphSize.width];
a = [stockObjects[stockToShow] get6MonthValues];
else if ([appSettings stockGraphTimeFrame] == 3)
a = [stockObjects[stockToShow] get12MonthValues:self.graphSize.width];
a = [stockObjects[stockToShow] get12MonthValues];

if ([a count] > 0) {
// find the high, low and range of the graph
Expand All @@ -314,7 +304,7 @@ - (void)drawGraph {
NSInteger count = [a count];
CGFloat *data = alloca(count * sizeof(CGFloat));

for (i = 0; i < count; i++) data[i] = [a[(count - 1 - i)] floatValue];
for (i = 0; i < count; i++) data[i] = [a[i] floatValue];

[self drawRangedGraphWithData:data size:[a count] currentIndex:(count - 1) upperBound:high lowerBound:low inRect:[self bounds] flipped:NO filled:YES color:[appSettings graphFG1Color]];
}
Expand All @@ -325,23 +315,20 @@ - (void)drawGraph {
if ([djia haveGoodDisplayData]) {
NSArray *a = nil;
if ([appSettings stockGraphTimeFrame] == 0)
a = [djia get1MonthValues: self.graphSize.width];
a = [djia get1MonthValues];
else if ([appSettings stockGraphTimeFrame] == 1)
a = [djia get3MonthValues: self.graphSize.width];
a = [djia get3MonthValues];
else if ([appSettings stockGraphTimeFrame] == 2)
a = [djia get6MonthValues: self.graphSize.width];
a = [djia get6MonthValues];
else if ([appSettings stockGraphTimeFrame] == 3)
a = [djia get12MonthValues: self.graphSize.width];
a = [djia get12MonthValues];

if (a != nil) {
int i;
if ([a count] > 0) {
float high, low;
low = high = [a[0] floatValue];
for (i = 1; i < [a count]; i++) {
if ([a[i] floatValue] > high)
high = [a[i] floatValue];
if ([a[i] floatValue] < low)
low = [a[i] floatValue];
for (NSNumber *closingPrice in a) {
high = MAX(high, [closingPrice floatValue]);
low = MIN(low, [closingPrice floatValue]);
}

r = (high - low) * .1;
Expand All @@ -351,7 +338,7 @@ - (void)drawGraph {
NSInteger count = [a count];
CGFloat *data = alloca(count * sizeof(CGFloat));

for (i = 0; i < count; i++) data[i] = [a[(count - 1 - i)] floatValue];
for (i = 0; i < count; i++) data[i] = [a[i] floatValue];

[self drawRangedGraphWithData:data size:[a count] currentIndex:(count - 1) upperBound:high lowerBound:low inRect:[self bounds] flipped:NO filled:NO color:[appSettings graphFG2Color]];
}
Expand Down
30 changes: 12 additions & 18 deletions Utility/XRGStock.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@

@interface XRGStock : NSObject

@property NSString *label;
@property (nonatomic) NSString *symbol;
@property (nullable,readonly) NSString *label;
@property (nullable,nonatomic) NSString *symbol;

@property XRGURL *surl;
@property XRGURL *immediateURL;
@property (nullable) NSURL *sURL;
@property (nullable) NSURL *immediateURL;

@property CGFloat currentPrice;
@property CGFloat lastChange;
Expand All @@ -47,25 +47,19 @@
@property BOOL haveGoodStockArray;
@property BOOL haveGoodDisplayData;

@property NSMutableArray *closingPrices;
@property NSMutableArray *volumes;
@property (nullable) NSArray<NSNumber *> *closingPrices;

- (NSString *)symbol;
- (NSString *)label;
- (void)setURL;
- (void)resetData;
- (void)loadData;
- (void)checkForData;
- (void)parseWebData:(NSString *)webData;

- (NSArray *)get1MonthValues:(int)max;
- (NSArray *)get3MonthValues:(int)max;
- (NSArray *)get6MonthValues:(int)max;
- (NSArray *)get12MonthValues:(int)max;
- (NSArray *)getCurrentPriceAndChange;
- (nullable NSArray<NSNumber *> *)get1MonthValues;
- (nullable NSArray<NSNumber *> *)get3MonthValues;
- (nullable NSArray<NSNumber *> *)get6MonthValues;
- (nullable NSArray<NSNumber *> *)get12MonthValues;
- (nullable NSArray<NSNumber *> *)getCurrentPriceAndChange;

- (NSString *)priceString;
- (NSString *)changeString;
- (nonnull NSString *)priceString;
- (nonnull NSString *)changeString;

- (BOOL)errorOccurred;

Expand Down
Loading

0 comments on commit a7a6b3c

Please sign in to comment.