Overall Statistics
Total Trades
88
Average Win
0.77%
Average Loss
0%
Compounding Annual Return
31.678%
Drawdown
8.600%
Expectancy
0
Net Profit
36.429%
Sharpe Ratio
2.501
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.18
Beta
0.273
Annual Standard Deviation
0.112
Annual Variance
0.012
Information Ratio
-0.447
Tracking Error
0.185
Treynor Ratio
1.021
Total Fees
$88.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.02m;    //0.02m=0.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>();
        
        public override void Initialize() 
        {
	        SetStartDate(2016, 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,AAPL,SPY".Split(','))
            {
            	// Subscribe the ticker with resolution higher than 30 min (minute, for example)
            	AddSecurity(SecurityType.Equity, ticker, Resolution.Minute);
            	
            	//- 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;
            	
            	if (!Portfolio[symbol].Invested) 
            	{
            		var sto = _sto[symbol];
            		if (!sto.IsReady) continue;
            	
            		if (sto.StochK > sto.StochD && sto.StochD < _overBought)
                	{
                		SetHoldings(symbol, _weight);
                	}
            	}
            	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.StochD);
        	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);
    			}
    		}
    	}
    }
}