Skip to content

Commit

Permalink
Merge pull request #19 from dshe/master
Browse files Browse the repository at this point in the history
historical API improvements
  • Loading branch information
Karl Wan authored Oct 20, 2017
2 parents a55047c + 47e8e49 commit de91273
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 146 deletions.
27 changes: 14 additions & 13 deletions YahooFinanceApi.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -38,7 +39,7 @@ public void PeriodTest()
};
periods.ToList().ForEach(p =>
{
var hist = Yahoo.GetHistoricalAsync(aaplTag, new DateTime(2017, 1, 3), DateTime.Now, p, true).Result.First();
var hist = Yahoo.GetHistoricalAsync(aaplTag, new DateTime(2017, 1, 3), DateTime.Now, p).Result.First();
Assert.Equal(dict[p], hist.Open);
});
}
Expand All @@ -48,7 +49,7 @@ public void HistoricalTest()
{
const string aaplTag = "aapl";

var hist0 = Yahoo.GetHistoricalAsync(aaplTag, new DateTime(2017, 1, 3), new DateTime(2017, 1, 4), Period.Daily, true).Result;
var hist0 = Yahoo.GetHistoricalAsync(aaplTag, new DateTime(2017, 1, 3), new DateTime(2017, 1, 4), Period.Daily).Result;
var hist = hist0.First();
Assert.Equal(115.800003m, hist.Open);
Assert.Equal(116.330002m, hist.High);
Expand All @@ -61,7 +62,7 @@ public void HistoricalTest()
[Fact]
public void DividendTest()
{
const string aaplTag = "aapl";
const string aaplTag = "aapl";

var hist = Yahoo.GetDividendsAsync(aaplTag, new DateTime(2016, 2, 4), new DateTime(2016, 2, 5)).Result.First();
Assert.Equal(0.52m, hist.Dividend);
Expand Down Expand Up @@ -97,7 +98,7 @@ public void ParallelTest()

Parallel.For(0, 10, n =>
{
var hist = Yahoo.GetHistoricalAsync(aaplTag, new DateTime(2017, 1, 3), new DateTime(2017, 1, 4), Period.Daily, true).Result.First();
var hist = Yahoo.GetHistoricalAsync(aaplTag, new DateTime(2017, 1, 3), new DateTime(2017, 1, 4), Period.Daily).Result.First();
Assert.Equal(28_781_900, hist.Volume);
});
}
Expand All @@ -108,7 +109,7 @@ public void HistoricalDatesTest_US()
var from = new DateTime(2017, 10, 10);
var to = new DateTime(2017, 10, 12);

var hist = Yahoo.GetHistoricalAsync("C", from, to, Period.Daily, ascending: true).Result;
var hist = Yahoo.GetHistoricalAsync("C", from, to, Period.Daily).Result;

Assert.Equal(3, hist.Count());

Expand All @@ -126,7 +127,7 @@ public void HistoricalDatesTest_UK()
var from = new DateTime(2017, 10, 10);
var to = new DateTime(2017, 10, 12);

var hist = Yahoo.GetHistoricalAsync("BA.L", from, to, Period.Daily, ascending: true).Result;
var hist = Yahoo.GetHistoricalAsync("BA.L", from, to, Period.Daily).Result;

Assert.Equal(3, hist.Count());

Expand All @@ -145,7 +146,7 @@ public void HistoricalDatesTest_TW()
var from = new DateTime(2017, 10, 11);
var to = new DateTime(2017, 10, 13);

var hist = Yahoo.GetHistoricalAsync("2498.TW", from, to, Period.Daily, ascending: true).Result;
var hist = Yahoo.GetHistoricalAsync("2498.TW", from, to, Period.Daily).Result;

Assert.Equal(3, hist.Count());

Expand All @@ -159,25 +160,25 @@ public void HistoricalDatesTest_TW()


[Theory]
[InlineData("SPY", "BRK-B")] // USA -5 (DST from second Sunday of Mar to first Sunday of Nov)
[InlineData("TD.TO", "CZX.V")] // Canada
[InlineData("BP.L", "VFEM.L")] // London
[InlineData("SPY")] // USA
[InlineData("TD.TO")] // Canada
[InlineData("BP.L")] // London
[InlineData("AIR.PA")] // Euronext
[InlineData("AIR.DE")] // Xetra
[InlineData("UNITECH.BO")] // Bombay
[InlineData("2800.HK")] // Hong Kong
[InlineData("000001.SS")] // Shanghai
[InlineData("2448.TW")] // Taiwan
[InlineData("005930.KS")] // Korea +9
[InlineData("BHP.AX", "ANZ.AX")] // Sydney +10 (DST from first Sunday in Oct to first Sunday in Apr)
[InlineData("005930.KS")] // Korea
[InlineData("BHP.AX")] // Sydney
public void HistoricalDatesTest(params string[] symbols)
{
var from = new DateTime(2017, 9, 12);
var to = from.AddDays(2);

foreach (var symbol in symbols)
{
var hist = Yahoo.GetHistoricalAsync(symbol, from, to, Period.Daily, ascending: true).Result;
var hist = Yahoo.GetHistoricalAsync(symbol, from, to, Period.Daily).Result;

Assert.Equal(3, hist.Count());

Expand Down
6 changes: 3 additions & 3 deletions YahooFinanceApi.Tests/YahooFinanceApi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
25 changes: 7 additions & 18 deletions YahooFinanceApi/Candle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,18 @@ namespace YahooFinanceApi
{
public class Candle: ITick
{
public Candle(DateTime dateTime, decimal open, decimal high, decimal low, decimal close, long volume, decimal adjustedClose)
{
DateTime = dateTime;
Open = open;
High = high;
Low = low;
Close = close;
Volume = volume;
AdjustedClose = adjustedClose;
}
public DateTime DateTime { get; internal set; }

public DateTime DateTime { get; }
public decimal Open { get; internal set; }

public decimal Open { get; }
public decimal High { get; internal set; }

public decimal High { get; }
public decimal Low { get; internal set; }

public decimal Low { get; }
public decimal Close { get; internal set; }

public decimal Close { get; }
public long Volume { get; internal set; }

public long Volume { get; }

public decimal AdjustedClose { get; }
public decimal AdjustedClose { get; internal set; }
}
}
21 changes: 0 additions & 21 deletions YahooFinanceApi/DateTimeComparer.cs

This file was deleted.

10 changes: 2 additions & 8 deletions YahooFinanceApi/DividendTick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ namespace YahooFinanceApi
{
public class DividendTick: ITick
{
public DividendTick(DateTime dateTime, decimal dividend)
{
DateTime = dateTime;
Dividend = dividend;
}
public DateTime DateTime { get; internal set; }

public DateTime DateTime { get; }

public decimal Dividend { get; }
public decimal Dividend { get; internal set; }
}
}
8 changes: 3 additions & 5 deletions YahooFinanceApi/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private static DateTime ConvertTimeFromEstToUtc(this DateTime dt)
private static readonly TimeZoneInfo TzEst = TimeZoneInfo
.GetSystemTimeZones()
.Where(tz => tz.Id == "Eastern Standard Time" || tz.Id == "America/New_York")
.SingleOrDefault();
.Single();

internal static string ToUnixTimestamp(this DateTime dt)
=> DateTime.SpecifyKind(dt, DateTimeKind.Unspecified)
Expand All @@ -33,9 +33,7 @@ public static string Name<T>(this T @enum)
}

public static string GetRandomString(int length)
{
const string Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
return string.Join("", Enumerable.Range(0, length).Select(i => Chars[new Random().Next(Chars.Length)]));
}
=> Guid.NewGuid().ToString().Substring(0, length);

}
}
79 changes: 59 additions & 20 deletions YahooFinanceApi/RowExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,73 @@ namespace YahooFinanceApi
{
static class RowExtension
{
internal static bool IgnoreEmptyRows;

public static Candle ToCandle(this string[] row)
=> new Candle(row[0].ToSpecDateTime(),
Convert.ToDecimal(row[1], CultureInfo.InvariantCulture),
Convert.ToDecimal(row[2], CultureInfo.InvariantCulture),
Convert.ToDecimal(row[3], CultureInfo.InvariantCulture),
Convert.ToDecimal(row[4], CultureInfo.InvariantCulture),
Convert.ToInt64(row[6], CultureInfo.InvariantCulture),
Convert.ToDecimal(row[5], CultureInfo.InvariantCulture));
{
var candle = new Candle
{
DateTime = row[0].ToDateTime(),
Open = row[1].ToDecimal(),
High = row[2].ToDecimal(),
Low = row[3].ToDecimal(),
Close = row[4].ToDecimal(),
AdjustedClose = row[5].ToDecimal(),
Volume = row[6].ToInt64()
};

if (IgnoreEmptyRows &&
candle.Open == 0 && candle.High == 0 && candle.Low == 0 && candle.Close == 0 &&
candle.AdjustedClose == 0 && candle.Volume == 0)
return null;

public static Candle ToFallbackCandle(this string[] row)
=> new Candle(row[0].ToSpecDateTime(), 0, 0, 0, 0, 0, 0);
return candle;
}

public static DividendTick ToDividendTick(this string[] row)
=> new DividendTick(row[0].ToSpecDateTime(),
Convert.ToDecimal(row[1], CultureInfo.InvariantCulture));
{
var tick = new DividendTick
{
DateTime = row[0].ToDateTime(),
Dividend = row[1].ToDecimal()
};

public static DividendTick ToFallbackDividendTick(this string[] row)
=> new DividendTick(row[0].ToSpecDateTime(), 0);
if (IgnoreEmptyRows && tick.Dividend == 0)
return null;

return tick;
}

public static SplitTick ToSplitTick(this string[] row)
=> new SplitTick(row[0].ToSpecDateTime(),
Convert.ToInt32(row[1].Split('/')[0], CultureInfo.InvariantCulture),
Convert.ToInt32(row[1].Split('/')[1], CultureInfo.InvariantCulture));
{
var tick = new SplitTick { DateTime = row[0].ToDateTime() };

var split = row[1].Split('/');
if (split.Length == 2)
{
tick.AfterSplit = split[0].ToDecimal();
tick.BeforeSplit = split[1].ToDecimal();
}

if (IgnoreEmptyRows && tick.AfterSplit == 0 && tick.BeforeSplit == 0)
return null;

return tick;
}

private static DateTime ToDateTime(this string str)
=> DateTime.Parse(str, CultureInfo.InvariantCulture);

public static SplitTick ToFallbackSplitTick(this string[] row)
=> new SplitTick(row[0].ToSpecDateTime(), 0, 0);
private static Decimal ToDecimal(this string str)
{
Decimal.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out Decimal result);
return result;
}

internal static DateTime ToSpecDateTime(this string dateTimeStr)
=> Convert.ToDateTime(dateTimeStr, CultureInfo.InvariantCulture);
private static Int64 ToInt64(this string str)
{
Int64.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out Int64 result);
return result;
}
}
}
13 changes: 4 additions & 9 deletions YahooFinanceApi/SplitTick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ namespace YahooFinanceApi
{
public class SplitTick: ITick
{
public DateTime DateTime { get; }
public decimal BeforeSplit { get; }
public decimal AfterSplit { get; }
public DateTime DateTime { get; internal set; }

public SplitTick(DateTime dateTime, decimal afterSplit, decimal beforeSplit)
{
AfterSplit = afterSplit;
BeforeSplit = beforeSplit;
DateTime = dateTime;
}
public decimal BeforeSplit { get; internal set; }

public decimal AfterSplit { get; internal set; }
}
}
Loading

0 comments on commit de91273

Please sign in to comment.