using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;
namespace QuantConnect.Algorithm.CSharp
{
public class InspiriAlgorithm : QCAlgorithm
{
//region "Variables"
private const int hPeriods = 15;
private const int rsiPeriods = 15;
private const decimal minPrice = 25;
private const decimal minVolume = 500000;
private const decimal tpPercent = 1.01m;
//region "Indicators"
public SimpleMovingAverage hSMA;
public RelativeStrengthIndex iRSI;
RollingWindow<TradeBar> lastMinute = new RollingWindow<TradeBar>(1);
public override void Initialize()
{
UniverseSettings.Resolution = Resolution.Minute;
SetStartDate(2015, 11, 25);
SetEndDate(2015, 11, 30);
SetCash(10000);
AddUniverse(coarse =>
{
return (from cf in coarse
where cf.Price > minPrice
where cf.Volume > minVolume
select cf.Symbol);
});
}
public void OnData(TradeBars data)
{
foreach (Security Stock in Securities.Values)
{
hSMA = SMA(Stock.Symbol, hPeriods, Resolution.Minute, Field.High); //x => ((TradeBar)x).High
iRSI = RSI(Stock.Symbol, rsiPeriods, MovingAverageType.Simple, Resolution.Minute);
lastMinute.Add(data[Stock.Symbol]);
if ( Portfolio.Count < 2)
{
if ( lastMinute[0].High < hSMA && iRSI < 5 )
{
Log("BUY >> " + Stock.Price);
SetHoldings(Stock.Symbol, 0.5m);
var takeProfit = Stock.Price*tpPercent;
LimitOrder(Stock.Symbol, - Stock.Holdings.Quantity, takeProfit);
}
}
if (Portfolio.Count > 0 && lastMinute[0].High >= hSMA)
{
Log("SELL >> " + Stock.Price);
Liquidate(Stock.Symbol);
}
}
}
}
}