| Overall Statistics |
|
Total Trades 0 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $0.00 |
using QuantConnect.Indicators.CandlestickPatterns;
// Demonstrate a crash caused by using HMA in RSI.
namespace QuantConnect.Algorithm.CSharp
{
public class Algo : QCAlgorithm
{
public static Algo Self;
private int WarmupPeriods = 200;
Indicator Rsi = new RelativeStrengthIndex("foo", 14, MovingAverageType.Hull);
bool First = false;
public override void Initialize()
{
SetStartDate(2018, 4, 9); //Set Start Date
SetEndDate(2018, 4, 25); //Set End Date
AddForex("GBPUSD", Resolution.Minute);
}
public override void OnData(Slice data)
{
try {
var q = data.QuoteBars["GBPUSD"];
if (First) {
var h = History<QuoteBar>(q.Symbol, WarmupPeriods+1, Resolution.Minute); // +1 because we often get 1 less record returned than we expect.
if (h.Count() < WarmupPeriods) throw new Exception(string.Format("WarmIndicators: not enough history, expected {0} got {1} for {2} {3}", WarmupPeriods, h.Count(), q.Symbol.ToString(), q.Time));
foreach (var q2 in h) {
IndicatorDataPoint dp2 = new IndicatorDataPoint(q2.Time, q2.Value);
Rsi.Update(dp2);
}
First = false;
}
IndicatorDataPoint dp = new IndicatorDataPoint(q.Time, q.Value);
Rsi.Update(dp);
}
catch (Exception ex) {
Log(ex.StackTrace);
throw;
}
}
}
}/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Represents the Relative Strength Index (RSI) developed by K. Welles Wilder.
/// You can optionally specified a different moving average type to be used in the computation
/// </summary>
public class MyRelativeStrengthIndex : Indicator
{
private IndicatorDataPoint previousInput;
/// <summary>
/// Gets the type of indicator used to compute AverageGain and AverageLoss
/// </summary>
public MovingAverageType MovingAverageType { get; private set; }
/// <summary>
/// Gets the EMA for the down days
/// </summary>
public IndicatorBase<IndicatorDataPoint> AverageLoss { get; private set; }
/// <summary>
/// Gets the indicator for average gain
/// </summary>
public IndicatorBase<IndicatorDataPoint> AverageGain { get; private set; }
/// <summary>
/// Initializes a new instance of the RelativeStrengthIndex class with the specified name and period
/// </summary>
/// <param name="period">The period used for up and down days</param>
/// <param name="movingAverageType">The type of moving average to be used for computing the average gain/loss values</param>
public MyRelativeStrengthIndex(int period, MovingAverageType movingAverageType = MovingAverageType.Wilders)
: this("RSI" + period, period, movingAverageType)
{
}
/// <summary>
/// Initializes a new instance of the RelativeStrengthIndex class with the specified name and period
/// </summary>
/// <param name="name">The name of this indicator</param>
/// <param name="period">The period used for up and down days</param>
/// <param name="movingAverageType">The type of moving average to be used for computing the average gain/loss values</param>
public MyRelativeStrengthIndex(string name, int period, MovingAverageType movingAverageType = MovingAverageType.Wilders)
: base(name)
{
MovingAverageType = movingAverageType;
AverageGain = movingAverageType.AsIndicator(name + "Up", period);
AverageLoss = movingAverageType.AsIndicator(name + "Down", period);
}
/// <summary>
/// Gets a flag indicating when this indicator is ready and fully initialized
/// </summary>
public override bool IsReady
{
get { return AverageGain.IsReady && AverageLoss.IsReady; }
}
/// <summary>
/// Computes the next value of this indicator from the given state
/// </summary>
/// <param name="input">The input given to the indicator</param>
/// <returns>A new value for this indicator</returns>
protected override decimal ComputeNextValue(IndicatorDataPoint input)
{
decimal gain = 0m;
decimal loss = 0m;
if (previousInput != null && input.Value >= previousInput.Value)
{
gain = input.Value - previousInput.Value;
}
else if (previousInput != null && input.Value < previousInput.Value)
{
loss = previousInput.Value - input.Value;
}
decimal beforeGain = AverageGain;
decimal beforeLoss = AverageLoss;
AverageGain.Update(input.Time, gain);
AverageLoss.Update(input.Time, loss);
decimal afterGain = AverageGain;
decimal afterLoss = AverageLoss;
Algo.Self.Log(string.Format("this gain {0} before gain {1} after gain {2} this loss {3} before loss {4} after loss {5}", beforeGain, gain, AverageGain, beforeLoss, AverageLoss, afterLoss));
previousInput = input;
if (AverageLoss == 0m)
{
// all up days is 100
return 100m;
}
var rs = AverageGain / AverageLoss;
Algo.Self.Log(string.Format("this gain {0} this loss {1} average gain {2} ave loss {3} rs {4}", gain, loss, AverageGain, AverageLoss, rs));
return 100m - (100m / (1 + rs));
}
/// <summary>
/// Resets this indicator to its initial state
/// </summary>
public override void Reset()
{
AverageGain.Reset();
AverageLoss.Reset();
base.Reset();
}
}
}