namespace QuantConnect.Algorithm.CSharp
{
public class MeanReversion : QCAlgorithm
{
private Symbol _spy;
private RollingWindow<decimal> _Close;
private SimpleMovingAverage _spyDailySmaTrend;
private bool BullTrend;
private bool BullSetup;
private bool BullExit;
private const int Lookback = 5;
private const int TrendLen = 190;
private Minimum SomeDaysLow;
private Maximum SomeDaysHigh;
private decimal EntryPrice = 0.0m;
public override void Initialize()
{
SetStartDate(2016, 01, 01); //Set Start Date
SetEndDate(2017, 10, 27); //Set End Date
SetCash(100000); //Set Strategy Cash
AddEquity("SPY", Resolution.Daily);
_spy = Securities["SPY"].Symbol;
_Close = new RollingWindow<decimal>(Lookback);
_spyDailySmaTrend = SMA(_spy, TrendLen, Resolution.Daily);
SomeDaysLow = MIN("SPY", Lookback, Resolution.Daily, Field.Close);
SomeDaysHigh = MAX("SPY", Lookback, Resolution.Daily, Field.Close);
}
public void OnData(TradeBars data)
{
_Close.Add(data[_spy].Close);
if (!_spyDailySmaTrend.IsReady) return;
if (!_Close.IsReady) return;
BullTrend = _Close[0] > _spyDailySmaTrend;
BullSetup = data[_spy].Close < SomeDaysLow;
BullExit = _Close[0] > SomeDaysHigh ; // crosses over
Write_it(Time + " _Close[0]=" + _Close[0].ToString("0.00") +
" " + _Close[1].ToString("0.00") +
" " + _Close[2].ToString("0.00") +
" " + _Close[3].ToString("0.00") +
" " + _Close[4].ToString("0.00") +
" xDayLow=" + SomeDaysLow +
" " + BullTrend + " " + BullSetup);
if (!Portfolio.Invested && BullSetup && BullTrend )
{
var _ticket = MarketOrder("SPY", 1000);
EntryPrice = _Close[0];
Write_it(Time + " Buy -> _Close[0], Close[1]" +
_Close[0].ToString("0.00") + ", " + _Close[1].ToString("0.00") +
" SevenDayLow=" + SomeDaysLow);
}
if (Portfolio.Invested && BullExit)
{
MarketOrder("SPY", -1000);
Write_it(Time + " Sell -> Close " + _Close[0].ToString("0.00") + " Gain/Loss=" + (_Close[0]-EntryPrice) );
}
}
public void Write_it(String _string)
{
//Log(_string);
Debug(_string);
}
}
}
SomeDaysLow = MIN("SPY", Lookback, Resolution.Daily, Field.Close);
There are no trades generated by this code. The reason is that the current close value data[_spy].Close is already included in SomeDaysLow. Thus BullSetup becomes never true.
In my opionon N("SPY", Lookback, Resolution.Daily, Field.Close); should not include the current bar, but start with the previous bar ( min (Close[1], Close[2], Close[3], ...) and NOT min (Close[0], Close[1], Close[2], ...)
Or I am wrong or is there any other way forward.
regards
Ulrich
Ulrich
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!