Skip to content

Commit

Permalink
#4, Complete integrating Nuba.Finance.Google into Trady.Importer
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Wan committed Jul 6, 2017
1 parent 1550bdf commit 4bee590
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
43 changes: 43 additions & 0 deletions Trady.Importer/GoogleFinanceImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Trady.Core.Period;
using Nuba.Finance.Google;
using System.Linq;

namespace Trady.Importer
{
public class GoogleFinanceImporter: IImporter
{
LatestQuotesService _lqs;

public GoogleFinanceImporter()
{
_lqs = new LatestQuotesService();
}

const char SymbolSeparator = '/';

static readonly IDictionary<PeriodOption, int> PeriodMap = new Dictionary<PeriodOption, int>
{
{PeriodOption.PerSecond, Frequency.EverySecond},
{PeriodOption.PerMinute, Frequency.EveryMinute},
{PeriodOption.Hourly, Frequency.EveryHour},
{PeriodOption.Daily, Frequency.EveryDay}
};

public async Task<IList<Core.Candle>> ImportAsync(string symbol, DateTime? startTime = default(DateTime?), DateTime? endTime = default(DateTime?), PeriodOption period = PeriodOption.Daily, CancellationToken token = default(CancellationToken))
{
if (!PeriodMap.TryGetValue(period, out int frequency))
throw new ArgumentException("This importer only supports second, minute, hourly & daily data");

if (!symbol.Contains(SymbolSeparator.ToString()))
throw new ArgumentException("The input symbol should be in the form of \'{Market}/{Symbol}\'");

string[] syms = symbol.Split(SymbolSeparator);
var candles = await Task.Run(() => _lqs.GetValues(syms[0].ToUpper(), syms[1], PeriodMap[period], startTime, endTime));
return candles.Select(c => new Core.Candle(c.Date, c.Open, c.High, c.Low, c.Close, c.Volume)).OrderBy(c => c.DateTime).ToList();
}
}
}
7 changes: 4 additions & 3 deletions Trady.Importer/Trady.Importer.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<!--<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />-->
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
Expand Down Expand Up @@ -48,8 +48,9 @@
<PrivateAssets>All</PrivateAssets>
</PackageReference>-->
<PackageReference Include="Quandl.NET" Version="1.1.1" />
<PackageReference Include="StooqApi" Version="1.0.5" />
<PackageReference Include="YahooFinanceApi" Version="1.0.10" />
<PackageReference Include="StooqApi" Version="1.0.4" />
<PackageReference Include="YahooFinanceApi" Version="1.0.9" />
<PackageReference Include="Nuba.Finance.Google" Version="1.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Trady.Core\Trady.Core.csproj" />
Expand Down
12 changes: 12 additions & 0 deletions Trady.Test/ImporterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ public ImporterTest()
CultureInfo.CurrentCulture = new CultureInfo("nl-nl");
}

[TestMethod]
public void ImportByGoogleFinance()
{
var importer = new GoogleFinanceImporter();
var candle = importer.ImportAsync("NASDAQ/AAPL", new DateTime(2017, 1, 3), new DateTime(2017, 1, 3)).Result.First();
Assert.AreEqual(candle.Open, 115.8m);
Assert.AreEqual(candle.High, 116.33m);
Assert.AreEqual(candle.Low, 114.76m);
Assert.AreEqual(candle.Close, 116.15m);
Assert.AreEqual(candle.Volume, 28_781_865);
}

[TestMethod]
public void ImportByQuandlYahoo()
{
Expand Down

0 comments on commit 4bee590

Please sign in to comment.