Пример #1
0
        public BackTestWorker(IBaseData baseData, string stockId, Action <string> writeToLogAction, Action <Exception> writeToErrorLogAction, bool alwaysRun = true)
            : base(baseData)
        {
            _baseData = baseData;
            _stockId  = stockId;

            _startDate = baseData.CurrentTime;
            _endDate   = DateTime.Now;

            _writeToLogAction      = writeToLogAction;
            _writeToErrorLogAction = writeToErrorLogAction;

            // create StorageSpace
            _storage = new CurrentPriceStorage();

            // create Turtle Database
            _turtleDbOperations  = new TurtleDatabaseOperations(baseData.SystemConfig.SystemInfo.ProductionTurtleDBConnectionString);
            _memoryDbOperations  = new MemoryDatabaseOperations();
            _prepareMemoryDbTask = Task.Run(() => PrepareMemoryDatabaseAsync());

            //
            _stockPriceNotificationChecker = BaseData.CreateStockPriceNotificationChecker();

            // create EmailService and EmailTemplateProvider
            _logger = BaseData.GetLogger();
            _notificationService          = new BackTestNotificationService(_logger);
            _notificationTemplateProvider = new BackTestNotificationTemplateProvider();
        }
Пример #2
0
 /// <summary>
 /// Event invocator for the DataConsolidated event. This should be invoked
 /// by derived classes when they have consolidated a new piece of data.
 /// </summary>
 /// <param name="consolidated">The newly consolidated data</param>
 protected void OnDataConsolidated(RenkoBar consolidated)
 {
     DataConsolidated?.Invoke(this, consolidated);
     _currentBar = consolidated;
     _dataConsolidatedHandler?.Invoke(this, consolidated);
     Consolidated = consolidated;
 }
Пример #3
0
 /// <summary>
 /// Invoked for each piece of data from the source file
 /// </summary>
 /// <param name="data">The data to be processed</param>
 public void Process(IBaseData data)
 {
     if (_predicate(data))
     {
         _processor.Process(data);
     }
 }
Пример #4
0
        /// <summary>
        /// Converts the specified base data instance into a lean data file csv line.
        /// This method takes into account the fake that base data instances typically
        /// are time stamped in the exchange time zone, but need to be written to disk
        /// in the data time zone.
        /// </summary>
        public static string GenerateLine(IBaseData data, Resolution resolution, DateTimeZone exchangeTimeZone, DateTimeZone dataTimeZone)
        {
            var clone = data.Clone();

            clone.Time = data.Time.ConvertTo(exchangeTimeZone, dataTimeZone);
            return(GenerateLine(clone, clone.Symbol.ID.SecurityType, resolution));
        }
Пример #5
0
        /// <summary>
        /// 用于图像处理后并显示图像
        /// </summary>
        /// <param name="objCfg">图像处理参数配置对象</param>
        /// <param name="objIBaseData">图像数据对象</param>
        public void ShowImageProcess(IImageProcessConfig objCfg, IBaseData objIBaseData)
        {
            //检查图像是否改变并更新Buffer
            __UpdateBufferSize(objIBaseData);

            if (null != objIBaseData)
            {
                if (GX_FRAME_STATUS_LIST.GX_FRAME_STATUS_SUCCESS == objIBaseData.GetStatus())
                {
                    if (m_bIsColor)
                    {
                        IntPtr pBufferColor = objIBaseData.ImageProcess(objCfg);
                        Marshal.Copy(pBufferColor, m_byColorBuffer, 0, __GetStride(m_nWidth, m_bIsColor) * m_nHeigh);
                        __ShowImage(m_byColorBuffer);
                    }
                    else
                    {
                        IntPtr pBufferMono = objIBaseData.ImageProcess(objCfg);
                        Marshal.Copy(pBufferMono, m_byMonoBuffer, 0, __GetStride(m_nWidth, m_bIsColor) * m_nHeigh);

                        __ShowImage(m_byMonoBuffer);
                    }
                }
            }
        }
Пример #6
0
 //ctor
 public Company
 (
     IBaseData <ICronTask> cronTaskData,
     IBaseData <IEmail> emailData,
     IBaseData <IEmailSetting> emailSettingData,
     IBaseData <ISegment> segmentData,
     IBaseData <ISource> sourceData,
     IBaseData <IUser> userData,
     IBaseData <IUserIdentifier> userIdentifierData,
     IBaseData <IUserIdentifierType> userIdentifierTypeData,
     IBaseData <IUserType> userTypeData,
     IUserIdentifierDlcData userIdentifierDataDlc
 )
 {
     _cronTaskData           = cronTaskData;
     _emailData              = emailData;
     _emailSettingData       = emailSettingData;
     _segmentData            = segmentData;
     _sourceData             = sourceData;
     _userData               = userData;
     _userIdentifierData     = userIdentifierData;
     _userIdentifierTypeData = userIdentifierTypeData;
     _userTypeData           = userTypeData;
     _userIdentifierDataDlc  = userIdentifierDataDlc;
 }
 /// <summary>
 /// Invoked for each piece of data from the source file
 /// </summary>
 /// <param name="data">The data to be processed</param>
 public void Process(IBaseData data)
 {
     foreach (var processor in _processors)
     {
         processor.Process(data);
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="OrderSubmissionData"/> class
        /// </summary>
        public OrderSubmissionData(IBaseData lastData)
        {
            if (lastData == null)
            {
                return;
            }

            var tick = lastData as Tick;

            if (tick != null)
            {
                LastPrice = tick.Value;
                BidPrice  = tick.BidPrice;
                AskPrice  = tick.AskPrice;
                return;
            }

            var quoteBar = lastData as QuoteBar;

            if (quoteBar != null)
            {
                LastPrice = quoteBar.Close;
                BidPrice  = quoteBar.Bid?.Close ?? lastData.Value;
                AskPrice  = quoteBar.Ask?.Close ?? lastData.Value;
            }
            else
            {
                LastPrice = lastData.Value;
                BidPrice  = lastData.Value;
                AskPrice  = lastData.Value;
            }
        }
Пример #9
0
        /// <summary>
        /// 检查图像是否改变并更新Buffer
        /// </summary>
        /// <param name="objIBaseData">图像数据对象</param>
        private void __UpdateBufferSize(IBaseData objIBaseData)
        {
            if (null != objIBaseData)
            {
                if (__IsCompatible(m_bitmapForSave, m_nWidth, m_nHeigh, m_bIsColor))
                {
                    m_nPayloadSize = (int)objIBaseData.GetPayloadSize();
                    m_nWidth       = (int)objIBaseData.GetWidth();
                    m_nHeigh       = (int)objIBaseData.GetHeight();
                }
                else
                {
                    m_nPayloadSize = (int)objIBaseData.GetPayloadSize();
                    m_nWidth       = (int)objIBaseData.GetWidth();
                    m_nHeigh       = (int)objIBaseData.GetHeight();

                    m_byRawBuffer   = new byte[m_nPayloadSize];
                    m_byMonoBuffer  = new byte[__GetStride(m_nWidth, m_bIsColor) * m_nHeigh];
                    m_byColorBuffer = new byte[__GetStride(m_nWidth, m_bIsColor) * m_nHeigh];

                    //更新BitmapInfo
                    m_objBitmapInfo.bmiHeader.biWidth  = m_nWidth;
                    m_objBitmapInfo.bmiHeader.biHeight = m_nHeigh;
                    Marshal.StructureToPtr(m_objBitmapInfo, m_pBitmapInfo, false);
                }
            }
        }
Пример #10
0
        /// <summary>
        /// 存储图像
        /// </summary>
        /// <param name="objIBaseData">图像数据对象</param>
        /// <param name="strFilePath">显示图像文件名</param>
        public void SaveBmp(IBaseData objIBaseData, string strFilePath)
        {
            GX_VALID_BIT_LIST emValidBits = GX_VALID_BIT_LIST.GX_BIT_0_7;

            //检查图像是否改变并更新Buffer
            __UpdateBufferSize(objIBaseData);

            if (null != objIBaseData)
            {
                emValidBits = __GetBestValudBit(objIBaseData.GetPixelFormat());
                if (m_bIsColor)
                {
                    IntPtr pBufferColor = objIBaseData.ConvertToRGB24(emValidBits, GX_BAYER_CONVERT_TYPE_LIST.GX_RAW2RGB_NEIGHBOUR, false);
                    Marshal.Copy(pBufferColor, m_byColorBuffer, 0, __GetStride(m_nWidth, m_bIsColor) * m_nHeigh);
                    __UpdateBitmapForSave(m_byColorBuffer);
                }
                else
                {
                    IntPtr pBufferMono = IntPtr.Zero;
                    if (__IsPixelFormat8(objIBaseData.GetPixelFormat()))
                    {
                        pBufferMono = objIBaseData.GetBuffer();
                    }
                    else
                    {
                        pBufferMono = objIBaseData.ConvertToRaw8(emValidBits);
                    }
                    Marshal.Copy(pBufferMono, m_byMonoBuffer, 0, __GetStride(m_nWidth, m_bIsColor) * m_nHeigh);

                    __UpdateBitmapForSave(m_byMonoBuffer);
                }
                m_bitmapForSave.Save(strFilePath, ImageFormat.Bmp);
            }
        }
Пример #11
0
 internal void AddNewData(IBaseData newMarketData)
 {
     Bid      = newMarketData.Value - Spread / 2;
     Ask      = newMarketData.Value + Spread / 2;
     LastTick = newMarketData;
     NewData.Add(newMarketData);
 }
Пример #12
0
        private string GetDataValues(IBaseData data_)
        {
            PropertyInfo[] propertyInfos = data_.GetType().GetProperties();
            string         type;
            PropertyInfo   propertyInfo;
            object         fieldValue;
            string         valueString = string.Empty;
            StringBuilder  sb          = new StringBuilder();

            for (int i = 0; i < propertyInfos.Length; i++)
            {
                propertyInfo = propertyInfos[i];
                if (propertyInfo.GetSetMethod(true) == null)
                {
                    continue;
                }
                fieldValue  = propertyInfo.GetValue(data_, null);
                type        = SqLiteHelper.GetColumnType(propertyInfo.PropertyType.Name);
                valueString = type == SqLiteHelper.TEXT ? string.Format("{0}{1}{2}", "'", fieldValue.ToString(), "'") : fieldValue.ToString();
                if (i == 0)
                {
                    sb.Append(valueString);
                }
                else
                {
                    sb.Append(string.Format(",{0}", valueString));
                }
            }

            return(sb.ToString());
        }
Пример #13
0
        private string GetFieldsNames(IBaseData data_)
        {
            List <PropertyInfo> propertyInfos = new List <PropertyInfo>(
                data_.GetType().GetProperties().Where(prop_ => Attribute.IsDefined(prop_, typeof(DbFieldAttribute)))
                );

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < propertyInfos.Count; i++)
            {
                if (propertyInfos[i].GetSetMethod(true) == null)
                {
                    continue;
                }
                if (i == 0)
                {
                    sb.Append(propertyInfos[i].Name);
                }
                else
                {
                    sb.Append(string.Format(",{0}", propertyInfos[i].Name));
                }
            }
            return(sb.ToString());
        }
Пример #14
0
        public HObject Show(IBaseData objIBaseData)
        {
            GX_VALID_BIT_LIST emValidBits = GX_VALID_BIT_LIST.GX_BIT_0_7;

            if (null != objIBaseData)
            {
                emValidBits = GetBestValudBit(objIBaseData.GetPixelFormat());
                if (GX_FRAME_STATUS_LIST.GX_FRAME_STATUS_SUCCESS == objIBaseData.GetStatus())
                {
                    IntPtr pBufferMono = IntPtr.Zero;
                    if (IsPixelFormat8(objIBaseData.GetPixelFormat()))
                    {
                        pBufferMono = objIBaseData.GetBuffer();
                    }
                    else
                    {
                        pBufferMono = objIBaseData.ConvertToRaw8(emValidBits);
                    }
                    Marshal.Copy(pBufferMono, m_byMonoBuffer, 0, nWidth * nHeigh);

                    unsafe
                    {
                        fixed(byte *p = m_byMonoBuffer)
                        {
                            HOperatorSet.GenEmptyObj(out HObject image);
                            image.Dispose();
                            HOperatorSet.GenImage1(out image, "byte", nWidth, nHeigh, new IntPtr(p));
                            return(image);
                        }
                    }
                }
            }
            return(null);
        }
Пример #15
0
        /// <summary>
        /// Computes the Slow Stochastic %K.
        /// </summary>
        /// <param name="period">The period.</param>
        /// <param name="constantK">The constant k.</param>
        /// <param name="input">The input.</param>
        /// <returns>The Slow Stochastics %K value.</returns>
        private decimal ComputeStochK(int period, int constantK, IBaseData input)
        {
            var stochK = _maximum.Samples >= (period + constantK - 1) ? _sumFastK / constantK : decimal.Zero;

            _sumSlowK.Update(input.Time, stochK);
            return(stochK * 100);
        }
Пример #16
0
        private void UpdateImageData(IBaseData objIBaseData)
        {
            try
            {
                GX_VALID_BIT_LIST emValidBits = GX_VALID_BIT_LIST.GX_BIT_0_7;
                if (null != objIBaseData)
                {
                    emValidBits = DahengHelper.GetBestValidBit(objIBaseData.GetPixelFormat());
                    if (GX_FRAME_STATUS_LIST.GX_FRAME_STATUS_SUCCESS == objIBaseData.GetStatus())
                    {
                        if (isColor)
                        {
                            IntPtr buffer = objIBaseData.ConvertToRGB24(emValidBits, GX_BAYER_CONVERT_TYPE_LIST.GX_RAW2RGB_NEIGHBOUR, true);
                            FillRGB24(buffer);
                        }
                        else
                        {
                            IntPtr buffer = objIBaseData.GetBuffer();
                            FillY800(buffer);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }

            waitHandle.Set();
        }
Пример #17
0
 /// <summary>
 /// Updates this consolidator with the specified data
 /// </summary>
 /// <param name="data">The new data for the consolidator</param>
 public void Update(IBaseData data)
 {
     using (Py.GIL())
     {
         _consolidator.Update(data);
     }
 }
Пример #18
0
        /// <summary>
        /// Updates the state of this indicator with the given value and returns true
        /// if this indicator is ready, false otherwise
        /// </summary>
        /// <param name="input">The value to use to update this indicator</param>
        /// <returns>True if this indicator is ready, false otherwise</returns>
        public bool Update(IBaseData input)
        {
            if (_previousInput != null && input.Time < _previousInput.Time)
            {
                // if we receive a time in the past, log and return
                Log.Error($"This is a forward only indicator: {Name} Input: {input.Time:u} Previous: {_previousInput.Time:u}. It will not be updated with this input.");
                return(IsReady);
            }
            if (!ReferenceEquals(input, _previousInput))
            {
                // compute a new value and update our previous time
                Samples++;

                if (!(input is T))
                {
                    throw new ArgumentException($"IndicatorBase.Update() 'input' expected to be of type {typeof(T)} but is of type {input.GetType()}");
                }
                _previousInput = (T)input;

                var nextResult = ValidateAndComputeNextValue((T)input);
                if (nextResult.Status == IndicatorStatus.Success)
                {
                    Current = new IndicatorDataPoint(input.Time, nextResult.Value);

                    // let others know we've produced a new data point
                    OnUpdated(Current);
                }
            }
            return(IsReady);
        }
Пример #19
0
        /// <summary>
        /// Initializes an instance of <see cref="PandasData"/> with a sample <see cref="IBaseData"/> object
        /// </summary>
        /// <param name="baseData"><see cref="IBaseData"/> object that contains information to be saved in an instance of <see cref="PandasData"/></param>
        public PandasData(IBaseData baseData)
        {
            var columns = "open,high,low,close";

            if (baseData is TradeBar)
            {
                columns += ",volume";
            }
            else if (baseData is QuoteBar)
            {
                columns += ",askopen,askhigh,asklow,askclose,asksize,bidopen,bidhigh,bidlow,bidclose,bidsize";
            }
            else if (baseData is DynamicData)
            {
                // We get the fields of DynamicData from the storage dictionary
                // and add the field named 'value' since it is the reference value
                columns = "value," + string.Join(",", ((DynamicData)baseData).GetStorageDictionary().Keys);
            }

            _series    = columns.Split(',').ToDictionary(k => k, v => new List <double>());
            _symbol    = baseData.Symbol;
            _timeIndex = new List <DateTime>();

            Levels = 2;
            if (_symbol.SecurityType == SecurityType.Future)
            {
                Levels = 3;
            }
            if (_symbol.SecurityType == SecurityType.Option)
            {
                Levels = 5;
            }
        }
Пример #20
0
 /// <summary>
 /// Invoked for each piece of data from the source file
 /// </summary>
 /// <param name="data">The data to be processed</param>
 public void Process(IBaseData data)
 {
     foreach (var processor in _processors)
     {
         processor.Process(data);
     }
 }
Пример #21
0
        public void TestSaveItemCustomProperty()
        {
            campfire = new CampfireItem();
            campfire.Data.ItemName = "Campfire Custom Props";
            campfire.Data.itemUUID = System.Guid.NewGuid().ToString();

            // Test add string custom property
            StringProperty MyDescriptionProperty = new StringProperty("description", "Campfire really warm :)");

            campfire.Data.Properties.Add(MyDescriptionProperty);

            // string description = (campfire.Data as WorldData).Properties.Get<string>("description");
            // Debug.Log("The saved value is [" + description + "]");

            // Test add vector3 custom property
            Vector3Property MyVector3Property = new Vector3Property("position", new Vector3(11, 22, 33));

            campfire.Data.Properties.Add(MyVector3Property);

            IBaseData dataInterface = campfire as IBaseData;

            if (dataInterface != null)
            {
                DBModel.CreateItem(dataInterface);
                Debug.Log("[TestSaveItemCustomProperty] The item with custom properties was successfully created.");
            }
            else
            {
                Debug.LogError("[TestSaveItemCustomProperty] The item with custom properties data does not implement the IBaseData interface.");
            }
        }
Пример #22
0
        public CollectionInserter(IBaseData baseData, Entity.CollectionEntities context)
        {
            _baseData = baseData;
            _context  = context;

            _dvdHash = new Dictionary <string, Entity.tDVD>();
        }
Пример #23
0
        /// <summary>
        /// Updates this consolidator with the specified data
        /// </summary>
        /// <param name="data">The new data for the consolidator</param>
        public void Update(IBaseData data)
        {
            var currentValue = _selector(data);
            var volume       = _volumeSelector(data);

            decimal?close = null;

            // if we're already in a bar then update it
            if (_currentBar != null)
            {
                _currentBar.Update(data.Time, currentValue, volume);

                // if the update caused this bar to close, fire the event and reset the bar
                if (_currentBar.IsClosed)
                {
                    close = _currentBar.Close;
                    OnDataConsolidated(_currentBar);
                    _currentBar = null;
                }
            }

            if (_currentBar == null)
            {
                var open = close ?? currentValue;
                if (_evenBars && !close.HasValue)
                {
                    open = Math.Ceiling(open / _barSize) * _barSize;
                }

                _currentBar = new RenkoBar(data.Symbol, data.Time, _barSize, open, volume);
            }
        }
 /// <summary>
 /// Invoked for each piece of data from the source file
 /// </summary>
 /// <param name="data">The data to be processed</param>
 public void Process(IBaseData data)
 {
     if (_predicate(data))
     {
         _processor.Process(data);
     }
 }
Пример #25
0
 public CurrentPriceWorker(IBaseData baseData)
     : base(baseData)
 {
     Name = "Current Price Worker";
     Kind = ServiceWorkerKind.CurrentPriceWorker;
     SetWorkerStartTimeEndTime(ServiceWorkerKind.CurrentPriceWorker);
     WriteToWorkerLog($"{Name} has been created");
 }
Пример #26
0
 /// <summary>
 /// Updates this consolidator with the specified data
 /// </summary>
 /// <param name="data">The new data for the consolidator</param>
 public void Update(IBaseData data)
 {
     if (!(data is TInput))
     {
         throw new ArgumentNullException("data", "Received type of " + data.GetType().Name + " but expected " + typeof(TInput).Name);
     }
     Update((TInput)data);
 }
        /// <summary>
        /// Event invocator for the DataConsolidated event. This should be invoked
        /// by derived classes when they have consolidated a new piece of data.
        /// </summary>
        /// <param name="consolidated">The newly consolidated data</param>
        protected virtual void OnDataConsolidated(RenkoBar consolidated)
        {
            DataConsolidated?.Invoke(this, consolidated);

            _dataConsolidatedHandler?.Invoke(this, consolidated);

            Consolidated = consolidated;
        }
 public MovingAverageSellStrategy(IBaseData baseData,
                                  bool isBackTest = false,
                                  bool testStatus = false)
     : base(baseData)
 {
     _isBackTest = isBackTest;
     _testStatus = testStatus;
 }
Пример #29
0
        /// <summary>
        /// Event invocator for the DataConsolidated event. This should be invoked
        /// by derived classes when they have consolidated a new piece of data.
        /// </summary>
        /// <param name="consolidated">The newly consolidated data</param>
        private void OnDataConsolidated(RenkoBar consolidated)
        {
            DataConsolidated?.Invoke(this, consolidated);

            _dataConsolidatedHandler?.Invoke(this, consolidated);

            Consolidated = consolidated;
        }
Пример #30
0
        public static decimal CalculateTodayATR(this IBaseData provider, decimal todayHighPrice, decimal todayLowPrice, decimal previousClosePrice)
        {
            // 要取絕對值之後,再選出差距最大者
            decimal temp1 = Math.Abs(todayHighPrice - todayLowPrice);
            decimal temp2 = Math.Abs(todayHighPrice - previousClosePrice);
            decimal temp3 = Math.Abs(previousClosePrice - todayLowPrice);

            return(Math.Max(temp3, Math.Max(temp1, temp2)));
        }
Пример #31
0
        private static void UpdateField(IBaseData e, IEntity copy, long fieldsToSave)
        {
            long bw = e.Bitwise ?? default(long);

            if ((fieldsToSave & bw) == bw)
            {
                copy.__Elements.Find(c => c.ID == e.ID).Data = e.Data.ToString();
            }
        }
Пример #32
0
Файл: Data.cs Проект: TheJP/cpib
        // pre: a has type DecimalData
        public static decimal decimalGet(IBaseData a)
        {
            if (a is IntData)
            {
                return(((IntData)a).getData());
            }

            return(((DecimalData)a).getData());
        }
Пример #33
0
        /// <summary>
        /// Invoked for each piece of data from the source file
        /// </summary>
        /// <param name="data">The data to be processed</param>
        public void Process(IBaseData data)
        {
            // grab the correct consolidator for this symbol
            IDataConsolidator consolidator;
            if (!_consolidators.TryGetValue(data.Symbol, out consolidator))
            {
                consolidator = _createConsolidator(data);
                consolidator.DataConsolidated += OnDataConsolidated;
                _consolidators[data.Symbol] = consolidator;
            }

            consolidator.Update(data);
        }
Пример #34
0
        /// <summary>
        /// Handles the <see cref="IDataConsolidator.DataConsolidated"/> event
        /// </summary>
        private void OnDataConsolidated(object sender, IBaseData args)
        {
            _destination.Process(args);

            // we've already checked this frontier time, so don't scan the consolidators
            if (_frontier >= args.EndTime) return;
            _frontier = args.EndTime;

            // check the other consolidators to see if they also need to emit
            foreach (var consolidator in _consolidators.Values)
            {
                // back up the time a single instance, this allows data at exact same
                // time to still come through
                consolidator.Scan(args.EndTime.AddTicks(-1));
            }
        }
Пример #35
0
        /// <summary>
        /// Invoked for each piece of data from the source file
        /// </summary>
        /// <param name="data">The data to be processed</param>
        public void Process(IBaseData data)
        {
            Writer writer;
            if (!_writers.TryGetValue(data.Symbol, out writer))
            {
                writer = CreateTextWriter(data);
                _writers[data.Symbol] = writer;
            }

            // flush every so often
            if (++writer.ProcessCount%TicksPerFlush == 0)
            {
                writer.TextWriter.Flush();
            }

            var line = LeanData.GenerateLine(data, data.Symbol.ID.SecurityType, _resolution);
            writer.TextWriter.WriteLine(line);
        }
Пример #36
0
 /// <summary>
 /// Updates this consolidator with the specified data. This method is
 /// responsible for raising the DataConsolidated event
 /// </summary>
 /// <param name="data">The new data for the consolidator</param>
 public void Update(IBaseData data)
 {
     if (Type == RenkoType.Classic)
         UpdateClassic(data);
     else
         UpdateWicked(data);
 }
Пример #37
0
        private void UpdateClassic(IBaseData data)
        {
            var currentValue = _selector(data);
            var volume = _volumeSelector(data);

            decimal? close = null;
            
            // if we're already in a bar then update it
            if (_currentBar != null)
            {
                _currentBar.Update(data.Time, currentValue, volume);

                // if the update caused this bar to close, fire the event and reset the bar
                if (_currentBar.IsClosed)
                {
                    close = _currentBar.Close;
                    OnDataConsolidated(_currentBar);
                    _currentBar = null;
                }
            }

            if (_currentBar == null)
            {
                var open = close ?? currentValue;
                if (_evenBars && !close.HasValue)
                {
                    open = Math.Ceiling(open/_barSize)*_barSize;
                }
                _currentBar = new RenkoBar(data.Symbol, data.Time, _barSize, open, volume);
            }
        }
Пример #38
0
        /// <summary>
        /// Get the output zip file
        /// </summary>
        /// <param name="baseDirectory">Base output directory for the zip file</param>
        /// <param name="data">Data we're writing</param>
        /// <returns></returns>
        private string ZipOutputFile(string baseDirectory, IBaseData data)
        {
            var file = string.Empty;
            //Further determine path based on the remaining data: security type.
            switch (_securityType)
            {
                case SecurityType.Equity:
                case SecurityType.Forex:
                    //Base directory includes the market
                    file = Path.Combine(baseDirectory, _resolution.ToString().ToLower(), _symbol.ToLower(), Compression.CreateZipFileName(_symbol, _securityType, data.Time, _resolution));

                    if (_resolution == Resolution.Daily || _resolution == Resolution.Hour)
                    {
                        file = Path.Combine(baseDirectory, _resolution.ToString().ToLower(), Compression.CreateZipFileName(_symbol, _securityType, data.Time, _resolution));
                    }
                    break;
                default:
                    throw new Exception("Sorry this security type is not yet supported by the LEAN data writer: " + _securityType);
            }
            return file;
        }
Пример #39
0
        private static IDataConsolidator CreateConsolidator(Resolution resolution, TickType tickType, IBaseData data, bool sourceIsTick)
        {
            var securityType = data.Symbol.ID.SecurityType;
            switch (securityType)
            {
                case SecurityType.Base:
                case SecurityType.Equity:
                case SecurityType.Cfd:
                case SecurityType.Forex:
                    return new TickConsolidator(resolution.ToTimeSpan());

                case SecurityType.Option:
                    if (tickType == TickType.Trade)
                    {
                        return sourceIsTick
                            ? new TickConsolidator(resolution.ToTimeSpan())
                            : (IDataConsolidator) new TradeBarConsolidator(resolution.ToTimeSpan());
                    }
                    if (tickType == TickType.Quote)
                    {
                        return sourceIsTick
                            ? new TickQuoteBarConsolidator(resolution.ToTimeSpan())
                            : (IDataConsolidator) new QuoteBarConsolidator(resolution.ToTimeSpan());
                    }
                    break;
            }
            throw new NotImplementedException("Consolidator creation is not defined for " + securityType + " " + tickType);
        }
Пример #40
0
 /// <summary>
 /// Converts the specified base data instance into a lean data file csv line.
 /// This method takes into account the fake that base data instances typically
 /// are time stamped in the exchange time zone, but need to be written to disk
 /// in the data time zone.
 /// </summary>
 public static string GenerateLine(IBaseData data, Resolution resolution, DateTimeZone exchangeTimeZone, DateTimeZone dataTimeZone)
 {
     var clone = data.Clone();
     clone.Time = data.Time.ConvertTo(exchangeTimeZone, dataTimeZone);
     return GenerateLine(clone, clone.Symbol.ID.SecurityType, resolution);
 }
Пример #41
0
        private void Falling(IBaseData data)
        {
            decimal limit;

            while (_closeRate < (limit = (_openRate - BarSize)))
            {
                var wicko = new RenkoBar(data.Symbol, _openOn, _closeOn,
                    BarSize, _openRate, _highRate, limit, limit);

                _lastWicko = wicko;

                OnDataConsolidated(wicko);

                _openOn = _closeOn;
                _openRate = limit;
                _highRate = limit;
            }
        }
Пример #42
0
        private void UpdateWicked(IBaseData data)
        {
            var rate = data.Price;

            if (_firstTick)
            {
                _firstTick = false;

                _openOn = data.Time;
                _closeOn = data.Time;
                _openRate = rate;
                _highRate = rate;
                _lowRate = rate;
                _closeRate = rate;
            }
            else
            {
                _closeOn = data.Time;

                if (rate > _highRate)
                    _highRate = rate;

                if (rate < _lowRate)
                    _lowRate = rate;

                _closeRate = rate;

                if (_closeRate > _openRate)
                {
                    if (_lastWicko == null ||
                        (_lastWicko.Direction == BarDirection.Rising))
                    {
                        Rising(data);

                        return;
                    }

                    var limit = (_lastWicko.Open + BarSize);

                    if (_closeRate > limit)
                    {
                        var wicko = new RenkoBar(data.Symbol, _openOn, _closeOn,
                            BarSize, _lastWicko.Open, limit, _lowRate, limit);

                        _lastWicko = wicko;

                        OnDataConsolidated(wicko);

                        _openOn = _closeOn;
                        _openRate = limit;
                        _lowRate = limit;

                        Rising(data);
                    }
                }
                else if (_closeRate < _openRate)
                {
                    if (_lastWicko == null ||
                        (_lastWicko.Direction == BarDirection.Falling))
                    {
                        Falling(data);

                        return;
                    }

                    var limit = (_lastWicko.Open - BarSize);

                    if (_closeRate < limit)
                    {
                        var wicko = new RenkoBar(data.Symbol, _openOn, _closeOn,
                            BarSize, _lastWicko.Open, _highRate, limit, limit);

                        _lastWicko = wicko;

                        OnDataConsolidated(wicko);

                        _openOn = _closeOn;
                        _openRate = limit;
                        _highRate = limit;

                        Falling(data);
                    }
                }
            }
        }
Пример #43
0
        /// <summary>
        /// Converts the specified base data instance into a lean data file csv line
        /// </summary>
        public static string GenerateLine(IBaseData data, SecurityType securityType, Resolution resolution)
        {
            var milliseconds = data.Time.TimeOfDay.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
            var longTime = data.Time.ToString(DateFormat.TwelveCharacter);

            switch (securityType)
            {
                case SecurityType.Equity:
                    switch (resolution)
                    {
                        case Resolution.Tick:
                            var tick = (Tick) data;
                            return ToCsv(milliseconds, Scale(tick.LastPrice), tick.Quantity, tick.Exchange, tick.SaleCondition, tick.Suspicious ? "1" : "0");

                        case Resolution.Minute:
                        case Resolution.Second:
                            var bar = (TradeBar) data;
                            return ToCsv(milliseconds, Scale(bar.Open), Scale(bar.High), Scale(bar.Low), Scale(bar.Close), bar.Volume);

                        case Resolution.Hour:
                        case Resolution.Daily:
                            var bigBar = (TradeBar) data;
                            return ToCsv(longTime, Scale(bigBar.Open), Scale(bigBar.High), Scale(bigBar.Low), Scale(bigBar.Close), bigBar.Volume);
                    }
                    break;

                case SecurityType.Forex:
                case SecurityType.Cfd:
                    switch (resolution)
                    {
                        case Resolution.Tick:
                            var tick = (Tick) data;
                            return ToCsv(milliseconds, tick.BidPrice, tick.AskPrice);

                        case Resolution.Second:
                        case Resolution.Minute:
                            var bar = (TradeBar) data;
                            return ToCsv(milliseconds, bar.Open, bar.High, bar.Low, bar.Close);

                        case Resolution.Hour:
                        case Resolution.Daily:
                            var bigBar = (TradeBar) data;
                            return ToCsv(longTime, bigBar.Open, bigBar.High, bigBar.Low, bigBar.Close);
                    }
                    break;

                case SecurityType.Option:
                    var putCall = data.Symbol.ID.OptionRight == OptionRight.Put ? "P" : "C";
                    switch (resolution)
                    {
                        case Resolution.Tick:
                            var tick = (Tick)data;
                            if (tick.TickType == TickType.Trade)
                            {
                                return ToCsv(milliseconds,
                                    Scale(tick.LastPrice), tick.Quantity, tick.Exchange, tick.SaleCondition, tick.Suspicious ? "1": "0");
                            }
                            if (tick.TickType == TickType.Quote)
                            {
                                return ToCsv(milliseconds,
                                    Scale(tick.BidPrice), tick.BidSize, Scale(tick.AskPrice), tick.AskSize, tick.Exchange, tick.Suspicious ? "1" : "0");
                            }
                            break;

                        case Resolution.Second:
                        case Resolution.Minute:
                            // option data can be quote or trade bars
                            var quoteBar = data as QuoteBar;
                            if (quoteBar != null)
                            {
                                return ToCsv(milliseconds,
                                    ToCsv(quoteBar.Bid), quoteBar.LastBidSize, 
                                    ToCsv(quoteBar.Ask), quoteBar.LastAskSize);
                            }
                            var tradeBar = data as TradeBar;
                            if (tradeBar != null)
                            {
                                return ToCsv(milliseconds,
                                    Scale(tradeBar.Open), Scale(tradeBar.High), Scale(tradeBar.Low), Scale(tradeBar.Close), tradeBar.Volume);
                            }
                            break;

                        case Resolution.Hour:
                        case Resolution.Daily:
                            // option data can be quote or trade bars
                            var bigQuoteBar = data as QuoteBar;
                            if (bigQuoteBar != null)
                            {
                                return ToCsv(longTime,
                                    ToCsv(bigQuoteBar.Bid), bigQuoteBar.LastBidSize,
                                    ToCsv(bigQuoteBar.Ask), bigQuoteBar.LastAskSize);
                            }
                            var bigTradeBar = data as TradeBar;
                            if (bigTradeBar != null)
                            {
                                return ToCsv(longTime,
                                    ToCsv(bigTradeBar), bigTradeBar.Volume);
                            }
                            break;

                        default:
                            throw new ArgumentOutOfRangeException("resolution", resolution, null);
                    }
                    break;
            }

            throw new NotImplementedException("LeanData.GenerateLine has not yet been implemented for security type: " + securityType + " at resolution: " + resolution);
        }
Пример #44
0
 /// <summary>
 /// Function used to select a trade bar that has double the values of the input trade bar
 /// </summary>
 private static TradeBar SelectorDoubleTradeBar(IBaseData baseData)
 {
     var bar = (TradeBar)baseData;
     return new TradeBar
     {
         Close = 2 * bar.Close,
         DataType = bar.DataType,
         High = 2 * bar.High,
         Low = 2 * bar.Low,
         Open = 2 * bar.Open,
         Symbol = bar.Symbol,
         Time = bar.Time,
         Value = 2 * bar.Value,
         Volume = 2 * bar.Volume,
         Period = bar.Period
     };
 }
Пример #45
0
        /// <summary>
        /// Creates the <see cref="TextWriter"/> that writes data to csv files
        /// </summary>
        private Writer CreateTextWriter(IBaseData data)
        {
            var entry = LeanData.GenerateZipEntryName(data.Symbol, data.Time.Date, _resolution, _tickType);
            var relativePath = LeanData.GenerateRelativeZipFilePath(data.Symbol, data.Time.Date, _resolution, _tickType)
                .Replace(".zip", string.Empty);
            var path = Path.Combine(Path.Combine(_dataDirectory, relativePath), entry);
            var directory = new FileInfo(path).Directory.FullName;
            if (!Directory.Exists(directory))
            {
                // lock before checking again
                lock (DirectoryCreateSync) if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
            }

            return new Writer(path, new StreamWriter(path));
        }
Пример #46
0
        /// <summary>
        /// Generate a single line of the data for this security type
        /// </summary>
        /// <param name="data">Data we're generating</param>
        /// <returns>String line for this basedata</returns>
        private string GenerateFileLine(IBaseData data)
        {
            var line = string.Empty;
            var format = "{0},{1},{2},{3},{4},{5}";
            var milliseconds = data.Time.TimeOfDay.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
            var longTime = data.Time.ToString(DateFormat.TwelveCharacter);

            switch (_securityType)
            {
                case SecurityType.Equity:
                    switch (_resolution)
                    {
                        case Resolution.Tick:
                            var tick = data as Tick;
                            if (tick != null)
                            {
                                line = string.Format(format, milliseconds, Scale(tick.LastPrice), tick.Quantity, tick.Exchange, tick.SaleCondition, tick.Suspicious);
                            }
                            break;
                        case Resolution.Minute:
                        case Resolution.Second:
                            var bar = data as TradeBar;
                            if (bar != null)
                            {
                                line = string.Format(format, milliseconds, Scale(bar.Open), Scale(bar.High), Scale(bar.Low), Scale(bar.Close), bar.Volume);
                            }
                            break;
                        case Resolution.Hour:
                        case Resolution.Daily:
                            var bigBar = data as TradeBar;
                            if (bigBar != null)
                            {
                                line = string.Format(format, longTime, Scale(bigBar.Open), Scale(bigBar.High), Scale(bigBar.Low), Scale(bigBar.Close), bigBar.Volume);
                            }
                            break;
                    }
                    break;

                case SecurityType.Forex:
                case SecurityType.Cfd:
                    switch (_resolution)
                    {
                        case Resolution.Tick:
                            var fxTick = data as Tick;
                            if (fxTick != null)
                            {
                                line = string.Format("{0},{1},{2}", milliseconds, fxTick.BidPrice, fxTick.AskPrice);
                            }
                            break;
                        case Resolution.Second:
                        case Resolution.Minute:
                            var fxBar = data as TradeBar;
                            if (fxBar != null)
                            {
                                line = string.Format("{0},{1},{2},{3},{4}", milliseconds, fxBar.Open, fxBar.High, fxBar.Low, fxBar.Close);
                            }
                            break;
                        case Resolution.Hour:
                        case Resolution.Daily:
                            var dailyBar = data as TradeBar;
                            if (dailyBar != null)
                            {
                                line = string.Format("{0},{1},{2},{3},{4}", longTime, dailyBar.Open, dailyBar.High, dailyBar.Low, dailyBar.Close);
                            }
                            break;
                    }
                    break;
            }
            return line;
        }