using System; using System.IO; using System.Net; using sharp.json; using System.Collections.Generic; using System.Linq; namespace sharp.trading.cache { public class HistoryCache { public static string BaseDataDirectory { get { return Path.Combine(TradingEnvironment.DataDirectory, "cache", "history"); } } public static Dictionary caches = new Dictionary(); private static string getCacheName(Market market) { return Path.Combine(market.Connection.UniqueProviderName, market.PayingSymbol, market.TradedSymbol); } private static string getCacheDir(Market market) { return Path.Combine(BaseDataDirectory, getCacheName(market)); } private static bool createIfNotExists(string directoryName) { if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); return false; } return false; } public static HistoryCache getCache(Market market) { string cname = getCacheName(market); if (!caches.ContainsKey(cname)) { caches[cname] = new HistoryCache(market); } return caches[cname]; } public string CacheName { get; private set; } public string DataDirectory { get; private set; } public string HistoricTradesFileName { get; private set; } //Dictionary history = new Dictionary(); SortedSet history = new SortedSet(); public HistoryCache(Market market){ CacheName = getCacheName(market); DataDirectory = Path.Combine(BaseDataDirectory, CacheName); createIfNotExists(DataDirectory); HistoricTradesFileName = Path.Combine(DataDirectory, "history.json"); load(); // Import Ticks if nothing available if (this.history.Count == 0){ Tick[] ticks = market.getTicks(); int n = 0; foreach (Tick tick in ticks){ HistoricTrade ht = new HistoricTrade(); ht.TimeStamp = tick.TimeStamp; ht.Price = (tick.CloseAt + tick.OpenAt) / 2; ht.Volume = tick.BaseVolume / ht.Price; ht.TotalPrice = ht.Price * ht.Volume; ht.UniqueID = n++; history.Add(ht); } } } private void load(){ if (File.Exists(HistoricTradesFileName)){ JSON json = JSON.ReadFrom(HistoricTradesFileName); foreach (HistoricTrade trade in json.To()){ this.history.Add(trade); } }; } private void save(){ lock(this) { HistoricTrade[] trades = this.history.ToArray(); JSON json = JSONConverter.From(trades); json.WriteTo(HistoricTradesFileName, true); } } public void Update(HistoricTrade[] trades){ lock (this) { foreach (HistoricTrade trade in trades) { if (!history.Contains(trade)) { history.Add(trade); } } } save(); } public WeightedAverage calulateWeightedAveragesPerVolume(double volume) { return calulateWeightedAveragesPerVolume(new double[] { volume })[0]; } public WeightedAverage[] calulateWeightedAveragesPerVolume(double[] volumes) { WeightedAverage[] averages = new WeightedAverage[volumes.Length]; for (int n = 0; n 0) { double v = volumes[n] > trade.Volume ? trade.Volume : volumes[n]; averages[n].TotalPrice += trade.Price * v; averages[n].Volume += v; volumes[n] -= v; used++; } } if (used == 0) { break; } } } return averages; } private static void checkDataDirectory(){ if (!Directory.Exists(BaseDataDirectory)){ Directory.CreateDirectory(BaseDataDirectory); } } } }