-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#4, Complete integrating Nuba.Finance.Google into Trady.Importer
- Loading branch information
Karl Wan
committed
Jul 6, 2017
1 parent
1550bdf
commit 4bee590
Showing
3 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters