Overall Statistics
Total Trades
93
Average Win
2.36%
Average Loss
-7.09%
Compounding Annual Return
3.694%
Drawdown
65.600%
Expectancy
0.219
Net Profit
100.241%
Sharpe Ratio
0.269
Loss Rate
9%
Win Rate
91%
Profit-Loss Ratio
0.33
Alpha
0
Beta
0
Annual Standard Deviation
0.25
Annual Variance
0.063
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$202.17
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(1998, 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 "MSFT".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);
    			}
    		}
    	}
    }
}