using System;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;
namespace QuantConnect.Algorithm.Examples
{
/// <summary>
///
/// Look for Down Trend.
/// </summary>
public class MyDownTrend : QCAlgorithm
{
private const string Symbol = "EURUSD";
private SimpleMovingAverage fast;
private SimpleMovingAverage slow;
private MovingAverageConvergenceDivergence macd;
private Delay[] PriceDelays;
private decimal PriceResistancePrev;
private decimal PriceResistanceCurrent;
private Delay[] MacdDelays;
private decimal MacdResistancePrev;
private decimal MacdResistanceCurrent;
private decimal ExitPrice;
private int BarsAfterSell;
public override void Initialize()
{
// set up our analysis span
SetStartDate(2009, 01, 01);
SetEndDate(2015, 01, 01);
// request SPY data with minute resolution
AddSecurity(SecurityType.Forex, Symbol, Resolution.Minute);
var consolidator = ResolveConsolidator(Symbol, TimeSpan.FromMinutes(5));
int delays = 5;
macd = new MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential);
RegisterIndicator(Symbol, macd, consolidator);
PriceDelays = new Delay[delays];
MacdDelays = new Delay[delays];
for(int i = 0; i < delays; i++){
PriceDelays[i] = new Delay(i);
RegisterIndicator(Symbol, PriceDelays[i], consolidator);
MacdDelays[i] = (new Delay(i)).Of(macd.Fast);
}
PriceResistancePrev = PriceResistanceCurrent = -1.0m;
MacdResistancePrev = MacdResistanceCurrent = -1.0m;
fast = new SimpleMovingAverage(75);
slow = new SimpleMovingAverage(200);
RegisterIndicator(Symbol, fast, consolidator);
RegisterIndicator(Symbol, slow, consolidator);
}
//private DateTime previous;
public void OnData(TradeBars data)
{
var holdings = Portfolio[Symbol].Quantity;
if (PriceDelays[4] < PriceDelays[2] &&
PriceDelays[3] < PriceDelays[2] &&
PriceDelays[1] < PriceDelays[2] &&
PriceDelays[0] < PriceDelays[2])
{
PriceResistancePrev = PriceResistanceCurrent;
PriceResistanceCurrent = PriceDelays[2];
}
if (MacdDelays[4] < MacdDelays[2] &&
MacdDelays[3] < MacdDelays[2] &&
MacdDelays[1] < MacdDelays[2] &&
MacdDelays[0] < MacdDelays[2])
{
MacdResistancePrev = MacdResistanceCurrent;
MacdResistanceCurrent = MacdDelays[2];
}
if (PriceResistancePrev == -1.0m || PriceResistanceCurrent == -1.0m ||
MacdResistancePrev == -1.0m || MacdResistanceCurrent == -1.0m)
{
return;
}
if (holdings < 0)
{
BarsAfterSell++;
if (ExitPrice == -1.0m &&BarsAfterSell == 2)
{
ExitPrice = Securities[Symbol].Price;
}
else if (Securities[Symbol].Price >= ExitPrice || BarsAfterSell >= 5)
{
Liquidate(Symbol);
}
}
else
{
if (!(PriceResistancePrev > PriceResistanceCurrent))
{
return;
}
if (!(MacdResistancePrev > MacdResistanceCurrent && MacdResistanceCurrent < 0))
{
return;
}
if (!(fast < slow))
{
return;
}
if (macd.Fast < macd.Slow)
{
Log("SELL >> " + Securities[Symbol].Price);
SetHoldings(Symbol, -1.0);
ExitPrice = -1.0m;
BarsAfterSell = 0;
}
}
Plot(Symbol, "Price", data[Symbol].Price);
// a couple things to notice in this method:
// 1. We never need to 'update' our indicators with the data, the engine takes care of this for us
// 2. We can use indicators directly in math expressions
// 3. We can easily plot many indicators at the same time
// wait for our slow ema to fully initialize
//if (!slow.IsReady) return;
// only once per day
//if (previous.Date == data.Time.Date) return;
// define a small tolerance on our checks to avoid bouncing
//const decimal tolerance = 0.00015m;
//var holdings = Portfolio[Symbol].Quantity;
// we only want to go long if we're currently short or flat
//if (holdings <= 0)
//{
// if the fast is greater than the slow, we'll go long
// if (fast > slow * (1 + tolerance))
//{
// Log("BUY >> " + Securities[Symbol].Price);
// SetHoldings(Symbol, 1.0);
//}
//}
// we only want to liquidate if we're currently long
// if the fast is less than the slow we'll liquidate our long
//if (holdings > 0 && fast < slow)
//{
// Log("SELL >> " + Securities[Symbol].Price);
// Liquidate(Symbol);
//}
//Plot(Symbol, "Price", data[Symbol].Price);
//Plot("Ribbon", "Price", data[Symbol].Price);
// easily plot indicators, the series name will be the name of the indicator
//Plot(Symbol, fast, slow);
//Plot("Ribbon", ribbon);
//previous = data.Time;
}
}
}