| Overall Statistics |
|
Total Trades 151 Average Win 0.85% Average Loss 0% Compounding Annual Return 10.092% Drawdown 32.500% Expectancy 0 Net Profit 50.740% Sharpe Ratio 0.551 Loss Rate 0% Win Rate 100% Profit-Loss Ratio 0 Alpha 0.094 Beta -0.006 Annual Standard Deviation 0.169 Annual Variance 0.029 Information Ratio -0.04 Tracking Error 0.205 Treynor Ratio -15.667 Total Fees $151.00 |
using System.Drawing; // for Color
namespace QuantConnect
{
public class Teststochnoloss : QCAlgorithm
{
private int _kPeriod = 14;
private int _dPeriod = 3;
private int _overBought = 20; //a optimiser
private decimal _targetProfit = 0.025m; //0.02m=2% //Target profit for strategy. When achieve this exit.
//private decimal _weight = 0m;
private Dictionary<Symbol, Stochastic> _sto = new Dictionary<Symbol, Stochastic>();
private Dictionary<Symbol, decimal> _targetPrice = new Dictionary<Symbol, decimal>();
//RollingWindow <_stowindow> = new RollingWindow(4);
public override void Initialize()
{
SetStartDate(2013, 1, 16);
SetCash(10000); // Cash allocation
//SetBenchmark("AAPL"); // Customize benchmark
SetWarmup(20); // Warmup for the stochastic
//ini broker model
SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash);
foreach(var ticker in "IRBT,JLL,DIS".Split(',')) //IRBT,JLL,HAIN,DIS
{
// Subscribe the ticker with resolution higher than 30 min (minute, for example)
var security = AddSecurity(SecurityType.Equity, ticker, Resolution.Minute,fillDataForward: false, extendedMarketHours: false);
//AddSecurity(SecurityType.Equity, ticker, Resolution.Minute);
//security.SetLeverage(1m);
//- Create consolidators:
var consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(30));
// Create indicator
var sto = new Stochastic("STO_" + ticker, 14, _kPeriod, _dPeriod);
// Add indicator to a dictionary to later reference
_sto.Add(ticker, sto);
_targetPrice.Add(ticker, 0m);
// Register indicator for automatic updates at consolidator frequency
RegisterIndicator(ticker, sto, consolidator);
// Subscribe consolidator to this ticker.
SubscriptionManager.AddConsolidator(ticker, consolidator);
//
consolidator.DataConsolidated += OnConsolidorData;
var plotter = new Chart(ticker);
plotter.AddSeries(new Series("D", SeriesType.Line, " ",Color.Red));
plotter.AddSeries(new Series("K", SeriesType.Line, " ",Color.Blue));
plotter.AddSeries(new Series("Over Bought", SeriesType.Line, " ",Color.Black));
plotter.AddSeries(new Series("Buy", SeriesType.Scatter, index:0));
plotter.AddSeries(new Series("Sell", SeriesType.Scatter, index:0));// sur graph plot pas rouge
AddChart(plotter);
}
//_weight = 1m / Securities.Count;
}
public void OnData(TradeBars data)
{
if (IsWarmingUp) return;
foreach (var bar in data.Values)
{
var close = bar.Close;
var symbol = bar.Symbol;
int nbsec=-1;
if (!Portfolio[symbol].Invested)
{
var sto = _sto[symbol];
if (!sto.IsReady) continue;
//_stowindow.Add(_sto);
if (sto.StochK > sto.StochD && sto.StochD < _overBought)
{
//SetHoldings(symbol, _weight);
foreach(var security in Securities.Values) {
if(!security.Invested) {
nbsec++; //number of non invested securities
//Debug("nbsec " + security.Symbol);
}
}
//Debug("nbsec " + nbsec);
//Debug("nb securities " + (Securities.Count-1));
//Quit();
if(nbsec<(Securities.Count-1))
{
int quantity = ((int)Math.Floor(Portfolio.Cash /bar.Open))/(nbsec);
if(quantity*bar.Open>=1000)
{
//Debug("cash " + Portfolio.Cash + "open " + bar.Open + "nb secu" + nbsec);
Order(symbol, quantity);
}
}
else
{
int quantity = ((int)Math.Floor(Portfolio.Cash / bar.Open))/(Securities.Count-1);
if(quantity*bar.Open>=1000)
{
//Debug("cash " + Portfolio.Cash + "open " + bar.Open);
//Debug("nb securities " + (Securities.Count-1));
Order(symbol, quantity);
}
}
}
}
else
{
if (close >= _targetPrice[symbol])
{
Liquidate(symbol);
}
}
}
}
// Every 30 minutes data arrives here
public void OnConsolidorData(object s, TradeBar bar)
{
var sto = _sto[bar.Symbol];
Plot(bar.Symbol, "Price", bar.Close);
Plot(bar.Symbol,"D", sto.StochD);
Plot(bar.Symbol,"K", sto.StochK);
Plot(bar.Symbol,"Over Bought", _overBought);
}
public override void OnOrderEvent(OrderEvent orderEvent)
{
if (orderEvent.Status == OrderStatus.Filled)
{
var symbol = orderEvent.Symbol;
var price = orderEvent.FillPrice;
if (orderEvent.Direction == OrderDirection.Buy)
{
_targetPrice[symbol] = price * (1 + _targetProfit);
Plot(symbol, "Buy", price);
Log(string.Format("{0} -> Buy {1} for {2}, target at {3}",
Time.ToString("Y"), symbol, price, _targetPrice[symbol]));
}
if (orderEvent.Direction == OrderDirection.Sell)
{
Plot(symbol, "Sell", price);
}
}
}
public override void OnEndOfAlgorithm()
{
foreach (var trade in TradeBuilder.ClosedTrades)
{
Log(trade.Symbol + " " +
trade.EntryTime + " " +
trade.EntryPrice + " " +
trade.ExitTime + " " +
trade.ExitPrice + " " +
trade.ProfitLoss + " " +
Portfolio.Cash);// .EntryTime .EntryPrice .Direction .Quantity .ExitTime .ExitPrice
// .ProfitLoss .TotalFees .MAE // Maximum Adverse Excursion
//.MFE // Maximum Favorable Excursion
// .Duration .EndTradeDrawdown
}
}
}
}