CGC_Codes

Quantum

Jun 7th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using QuantSys.TradeEngine;
  7. using QuantSys.TradeEngine.MarketInterface.FXCMInterface;
  8. using QuantSys.TradeEngine.MarketInterface.FXCMInterface.Functions;
  9. using QuantSys.Util;
  10.  
  11. namespace QuantSys.MarketData
  12. {
  13.  
  14.     public class Quantum : IEnumerable<Tick>
  15.     {
  16.         private readonly SortedList<DateTime, Tick> _data;
  17.         public Quantum(SortedList<DateTime, Tick> data)
  18.         {
  19.             _data = data;
  20.         }
  21.  
  22.         public Quantum()
  23.         {
  24.             _data = new SortedList<DateTime, Tick>();
  25.         }
  26.  
  27.         public Tick[] ToArray()
  28.         {
  29.             return _data.Values.ToArray();
  30.         }
  31.         public SortedList<DateTime, Tick> Data
  32.         {
  33.             get { return _data; }
  34.         }
  35.  
  36.         public Symbol Symbol { get; set; }
  37.         public Timeframe Period { get; set; }
  38.  
  39.         public Tick this[int index]
  40.         {
  41.             get { return _data.Values[index]; }
  42.         }
  43.  
  44.         public void CombineWith(Quantum other)
  45.         {
  46.             foreach (KeyValuePair<DateTime, Tick> kvp in other.Data)
  47.             {
  48.                 if(!_data.ContainsKey(kvp.Key))
  49.                     _data.Add(kvp.Key, kvp.Value);
  50.             }
  51.         }
  52.  
  53.         public IEnumerator<Tick> GetEnumerator()
  54.         {
  55.             return Data.Values.GetEnumerator();
  56.         }
  57.  
  58.         IEnumerator IEnumerable.GetEnumerator()
  59.         {
  60.             return GetEnumerator();
  61.         }
  62.  
  63.  
  64.         public static Quantum ExcelToQuantum(string filename, string symbol, int startIndex = 0)
  65.         {
  66.             object[,] denseMatrix;
  67.             ExcelUtil.Open(@filename, out denseMatrix);
  68.             var s = new Symbol(symbol);
  69.  
  70.             var mData = new SortedList<DateTime, Tick>();
  71.  
  72.             for (int i = denseMatrix.GetLength(0) - startIndex; i > 1; i--)
  73.             {
  74.                 try
  75.                 {
  76.                     DateTime dateTime = (DateTime)denseMatrix[i, 1];
  77.  
  78.                     var t = new Tick(
  79.                         0,
  80.                         (double)denseMatrix[i, 6],
  81.                         (double)denseMatrix[i, 7],
  82.                         (double)denseMatrix[i, 8],
  83.                         (double)denseMatrix[i, 9],
  84.                         0,
  85.                         (double)denseMatrix[i, 2],
  86.                         (double)denseMatrix[i, 3],
  87.                         (double)denseMatrix[i, 4],
  88.                         (double)denseMatrix[i, 5],
  89.                         (double)denseMatrix[i, 10],
  90.                         dateTime
  91.                         );
  92.                     t.Symbol = s;
  93.                     mData.Add(dateTime, t);
  94.                 }
  95.                 catch (Exception e)
  96.                 {
  97.                     Console.WriteLine(e.Message);
  98.                 }
  99.             }
  100.  
  101.             return (new Quantum(mData) { Symbol = s });
  102.         }
  103.  
  104.         public static Quantum QuantumFromLiveData(string symbol, string timeframe, int ticks)
  105.         {
  106.             FXSession session = new FXSession();
  107.             session.InitializeSession();
  108.             HistoricPriceEngine h = new HistoricPriceEngine(session);
  109.             h.GetLongHistoricPrices(symbol, timeframe, ticks);
  110.             while(!h.Complete) Thread.Sleep(100);
  111.  
  112.             return (h.Data);
  113.         }
  114.  
  115.  
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment