161 lines
3.7 KiB
C#
161 lines
3.7 KiB
C#
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<string, HistoryCache> caches = new Dictionary<string, HistoryCache>();
|
|
|
|
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<Int64, HistoricTrade> history = new Dictionary<long, HistoricTrade>();
|
|
SortedSet<HistoricTrade> history = new SortedSet<HistoricTrade>();
|
|
|
|
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<HistoricTrade[]>()){
|
|
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<volumes.Length; n++){
|
|
averages[n] = new WeightedAverage("", "");
|
|
}
|
|
|
|
lock (this)
|
|
{
|
|
foreach (HistoricTrade trade in this.history)
|
|
{
|
|
int used = 0;
|
|
|
|
for (int n = 0; n < volumes.Length; n++)
|
|
{
|
|
if (volumes[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);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|