protected override void OnCalculate(bool fullRecalculation, DataBarUpdateType? updateType) { if (fullRecalculation) { double[] values1 = DataProvider.GetValuesAsDouble(_dataSource, 0, DataProvider.BarCount); Results.AddSetValues("Volume", 0, values1.Length, true, values1); } else { // How many steps to overlap for safety. int overlap = Math.Max(0, Math.Min(5, Results.SetLength - 1)); if (Results.SetLength > DataProvider.BarCount) {// This happens sometimes and causes exception. Results.ClipTo(DataProvider.BarCount); return; } int startIndex = Math.Max(0, Results.SetLength - overlap); int count = DataProvider.BarCount - startIndex; double[] values1 = DataProvider.GetValuesAsDouble(_dataSource, startIndex, count); Results.AddSetValues("Volume", startIndex, count, true, values1); } }
/// <summary> /// Quote dataDelivery was updated. /// </summary> protected override void OnDataBarPeriodUpdate(DataBarUpdateType updateType, int updatedBarsCount) { Indicator indicator = this.ObtainIndicator("Rsi"); if (indicator.Results.GetValueSetCurrentValue(0).HasValue == false || this.CanPlaceOrders == false) { return; } Trace(indicator.Results.GetValueSetCurrentValue(0).Value.ToString()); // Get the value of the [0] index operationResult set of this indicator. // Each indicator can have many operationResult sets - each represeting a "line" on the chart. if (indicator.Results.GetValueSetCurrentValue(0) > BuyValue) {// If the Rsi operationResult is above BuyValue, take some action. if (this.CurrentPositionVolume == 0) {// Open an order if none are already opened. this.OpenBuyOrder(10000); } } else if (indicator.Results.GetValueSetCurrentValue(0) < SellValue) { // If the Rsi operationResult is below SellValue, take some other action. if (this.CurrentPositionVolume > 0) {// Close the first open order. //this.OpenAndPendingOrders[0].Close(); this.ClosePosition(); } } }
/// <summary> /// Quote dataDelivery was updated. /// </summary> protected override void OnDataBarPeriodUpdate(DataBarUpdateType updateType, int updatedBarsCount) { Indicator indicator = this.ObtainIndicator("Rsi"); if (indicator.Results.GetValueSetCurrentValue(0).HasValue == false || this.CanPlaceOrders == false) { return; } Trace(indicator.Results.GetValueSetCurrentValue(0).Value.ToString()); if (this.CurrentPositionVolume == 0) { // Get the value of the [0] index operationResult set of this indicator. // Each indicator can have many operationResult sets - each represeting a "line" on the chart. if (indicator.Results.GetValueSetCurrentValue(0) > BuyValue) {// If the Rsi operationResult is above BuyValue, take some action. this.OpenBuyOrder(10000); } else if (indicator.Results.GetValueSetCurrentValue(0) < SellValue) { this.OpenSellOrder(10000); } } else {// Maybe we need to close an open position. if (this.CurrentPositionVolume > 0 && indicator.Results.GetValueSetCurrentValue(0) < ClosePositionValue) {// If the Rsi operationResult is below SellValue, close buy position. this.ClosePosition(); } if (this.CurrentPositionVolume < 0 && indicator.Results.GetValueSetCurrentValue(0) > ClosePositionValue) {// If the Rsi operationResult is above SellValue, close sell position. this.ClosePosition(); } } }
void _dataProvider_DataBarHistoryUpdateEvent(IDataBarHistoryProvider provider, DataBarUpdateType updateType, int updatedBarsCount) { Calculate(false, updateType); }
void DataBarHistory_DataBarHistoryUpdateEvent(IDataBarHistoryProvider provider, DataBarUpdateType updateType, int updatedBarsCount) { if (updateType == DataBarUpdateType.Initial) {// Only executes on initial adding of many items. WinFormsHelper.BeginFilteredManagedInvoke(this, new GeneralHelper.GenericDelegate <bool, bool>(chartControl.MasterPane.FitDrawingSpaceToScreen), true, true); } }
protected virtual void OnDataBarPeriodUpdate(DataBarUpdateType updateType, int updatedBarsCount) { }
/// <summary> /// Since we are in back test delivery, it is possible to receive this before we are initialized, /// since the underlying data bar provider gets launched before this class does. /// </summary> void _sourceDataBarProvider_DataBarHistoryUpdateEvent(IDataBarHistoryProvider provider, DataBarUpdateType updateType, int updatedBarsCount) { SystemMonitor.CheckError(provider == _sourceDataBarProvider, "Data provider update not expected."); RestoreLastStep(); if (_pendingSessionInfo.HasValue && _pendingRequest.HasValue) {// Executed only the first time. _pendingSessionInfo = null; _pendingRequest = null; RequestDataHistoryUpdate(_pendingSessionInfo.Value, _pendingRequest.Value, false); } }
protected override void OnCalculate(bool fullRecalculation, DataBarUpdateType? updateType) { if (_isUpdating == false) { GeneralHelper.FireAndForget(Update); } }
/// <summary> /// Perform calculation on the given piece of dataDelivery. /// </summary> protected abstract void OnCalculate(bool fullRecalculation, DataBarUpdateType? updateType);
/// <summary> /// Calculation helper function. /// </summary> public void Calculate(bool fullRecalculation, DataBarUpdateType? updateType) { if (this.Initialized == false || this.Enabled == false) { return; } if (_dataProvider.BarCount == 0) { return; } // Calculate the indicator level lines. OnCalculate(fullRecalculation, updateType); //// Start the mostly child performed signal analisys. //Signals.PerformCrossingResultAnalysis(ProvideSignalAnalysisLines()); //Signals.PerformExtremumResultAnalysis(); if (IndicatorCalculatedEvent != null) { IndicatorCalculatedEvent(this, fullRecalculation); } }
void DataBarHistory_DataBarHistoryUpdateEvent(IDataBarHistoryProvider provider, DataBarUpdateType updateType, int updatedBarsCount) { OnDataBarPeriodUpdate(updateType, updatedBarsCount); }
void DataBarHistory_DataBarHistoryUpdateEvent(IDataBarHistoryProvider provider, DataBarUpdateType updateType, int updatedBarsCount) { if (updateType != DataBarUpdateType.CurrentBarUpdate) {// Do the full update only on actual bar update. UpdateValues(updatedBarsCount); } else { RaiseSeriesValuesUpdated(true); } }
protected override void OnDataBarPeriodUpdate(IDataBarHistoryProvider provider, DataBarUpdateType updateType, int updatedBarsCount) { lock (this) { if (provider != null) { //Console.WriteLine("Bars: {0}", provider.BarCount); //Keep bars have no more limit than the quotes if (provider.DataBarLimit > this.QuotesLimit) { provider.DataBarLimit = this.QuotesLimit; } } } }
protected override void OnCalculate(bool fullRecalculation, DataBarUpdateType? updateType) { double[] values1, values2; int startingIndex = 0; int indecesCount = DataProvider.BarCount - 1; if (UseOpenClose) { values1 = DataProvider.GetValuesAsDouble(DataBar.DataValueEnum.Open, startingIndex, indecesCount); values2 = DataProvider.GetValuesAsDouble(DataBar.DataValueEnum.Close, startingIndex, indecesCount); } else { values1 = DataProvider.GetValuesAsDouble(DataBar.DataValueEnum.High, startingIndex, indecesCount); values2 = DataProvider.GetValuesAsDouble(DataBar.DataValueEnum.Low, startingIndex, indecesCount); } System.Diagnostics.Debug.Assert(values1.Length == values2.Length); if (indecesCount == 0) { Results.AddSetValues("ZigZag", startingIndex, indecesCount, true, new double[] { }); return; } // This is simple indicator, it can directly write to the signals array. double[] signals = new double[indecesCount]; signals[0] = (double)ZigZagStates.PeakHigh; int lastPeakIndex = 0; ZigZagStates lastPeakState = ZigZagStates.PeakHigh; // Perform actual calculation. for (int i = 0; i < values1.Length; i++) { double requiredDifferenceValue = (SignificancePercentage / 100) * values1[i]; double high = Math.Max(values1[i], values2[i]); double low = Math.Min(values1[i], values2[i]); double lastPeakValue = 0; if (lastPeakState == ZigZagStates.PeakHigh) { lastPeakValue = Math.Max(values1[lastPeakIndex], values2[lastPeakIndex]); } else if (lastPeakState == ZigZagStates.PeakLow) { lastPeakValue = Math.Min(values1[lastPeakIndex], values2[lastPeakIndex]); } bool newLow = (lastPeakState == ZigZagStates.PeakHigh && (lastPeakValue - low >= requiredDifferenceValue)) || (lastPeakState == ZigZagStates.PeakLow && low < lastPeakValue); bool newHigh = (lastPeakState == ZigZagStates.PeakLow && (high - lastPeakValue >= requiredDifferenceValue)) || (lastPeakState == ZigZagStates.PeakHigh && high > lastPeakValue); //System.Diagnostics.Debug.Assert(newLow == false || newHigh == false); if (newLow && newHigh) {// Favor the extension of existing peak in this case. if (lastPeakState == ZigZagStates.PeakHigh) { newLow = false; } else { newHigh = false; } } if (newHigh) { if (lastPeakState == ZigZagStates.PeakHigh) {// Update the high. signals[lastPeakIndex] = (double)ZigZagStates.Default; } else if (lastPeakState == ZigZagStates.PeakLow) {// New high found. lastPeakState = ZigZagStates.PeakHigh; } else { System.Diagnostics.Debug.Fail("Unexpected case."); } lastPeakIndex = i; signals[i] = (double)ZigZagStates.PeakHigh; } else if (newLow) { if (lastPeakState == ZigZagStates.PeakLow) {// Update the low. signals[lastPeakIndex] = (double)ZigZagStates.Default; } else if (lastPeakState == ZigZagStates.PeakHigh) {// New low found. lastPeakState = ZigZagStates.PeakLow; } lastPeakIndex = i; signals[lastPeakIndex] = (double)ZigZagStates.PeakLow; } } // Finish with a signal. if (signals[signals.Length - 1] == (double)ZigZagStates.Default) { if (lastPeakState == ZigZagStates.PeakHigh) { signals[signals.Length - 1] = (double)ZigZagStates.PeakLow; } else { signals[signals.Length - 1] = (double)ZigZagStates.PeakHigh; } } // Finally create the results, that shows a line moving between the peak values - like a proper ZigZag indicator. lastPeakIndex = 0; lastPeakState = ZigZagStates.PeakHigh; double[] results = new double[indecesCount]; for (int i = 1; i < signals.Length; i++) { if (signals[i] != (double)ZigZagStates.Default) { double lastPeakValue, currentPeakValue; if (lastPeakState == ZigZagStates.PeakHigh) { lastPeakValue = Math.Max(values1[lastPeakIndex], values2[lastPeakIndex]); currentPeakValue = Math.Min(values1[i], values2[i]); } else { lastPeakValue = Math.Min(values1[lastPeakIndex], values2[lastPeakIndex]); currentPeakValue = Math.Max(values1[i], values2[i]); } double[] midValues = MathHelper.CreateConnectionValues(lastPeakValue, currentPeakValue, i - lastPeakIndex); midValues.CopyTo(results, lastPeakIndex); lastPeakIndex = i; lastPeakState = (ZigZagStates)signals[i]; } } Results.AddSetValues("ZigZag", startingIndex, indecesCount - 1, true, results); // Only after we add the operationResult set, the signals become available. Signals.Signals = signals; }
protected virtual void OnDataBarPeriodUpdate(IDataBarHistoryProvider provider, DataBarUpdateType updateType, int updatedBarsCount) { }
// Parameters format of TaLibCore functions. //int startIdx, - mandatory //int endIdx, - mandatory //double[] inReal[added 0/1] or/and inOpen or/and inLow or/and inHigh or/and inClose //int/double optIn[NAME] or/and another one or none - parameters //out int outBegIdx, //out int outNBElement, //double/int[] out[Real/Integer] and or another one // Example: //TicTacTec.TA.Library.Core.RetCode code = TicTacTec.TA.Library.Core.Sma(0, indecesCount - 1, _closeResultValues, Period, out beginIndex, out number, ma); } /// <summary> /// Actual calculation routine. /// </summary> protected override void OnCalculate(bool fullRecalculation, DataBarUpdateType? updateType) { // Format of a TA baseMethod. //int startIdx, - mandatory //int endIdx, - mandatory //double[] inReal[added 0/1] or/and inOpen or/and inLow or/and inHigh or/and inClose //int/double optIn[NAME] or/and another one or none - parameters //out int outBegIdx, //out int outNBElement, //double/int[] out[Real/Integer] and or another one // Example: //TicTacTec.TA.Library.Core.RetCode code = TicTacTec.TA.Library.Core.Sma(0, indecesCount - 1, _closeResultValues, Period, out beginIndex, out number, ma); } EstablishLookback(); // Prepare parameters. int dataProviderStartIndex = 0; if (fullRecalculation == false) { dataProviderStartIndex = Math.Max(0, Results.SetLength - _lookbackCount.Value - 1); } int startIndex = 0; int endIndex = DataProvider.BarCount - dataProviderStartIndex - 1; //if (StepByStepCalculation) //{ // int outBeginIdxPosition; // object[] parameters = SetupParameters(startIndex, endIndex, dataProviderStartIndex, out outBeginIdxPosition); // for (int i = 0; i < DataProvider.BarCount - Results.SetLength; i++) // { // parameters[0] = i; // parameters[1] = endIndex - i; // Calc(parameters, outBeginIdxPosition, dataProviderStartIndex); // } //} int outBeginIdxPosition; object[] parameters = SetupParameters(startIndex, endIndex, dataProviderStartIndex, out outBeginIdxPosition); PerformCalculation(parameters, outBeginIdxPosition, dataProviderStartIndex); }
void DataBarHistory_DataBarHistoryUpdateEvent(IDataBarHistoryProvider provider, DataBarUpdateType updateType, int updatedBarsCount) { if (updateType == DataBarUpdateType.Initial) {// Only executes on initial adding of many items. WinFormsHelper.BeginFilteredManagedInvoke(this, new GeneralHelper.GenericDelegate<bool, bool>(chartControl.MasterPane.FitDrawingSpaceToScreen), true, true); } }