I'm new and and I try to learn by creating a simple algo. The algo should buy on stochastics signal (cross under classic 20 threshold) and sell only if the PNL of the trade is > to a % defined.

1. When I run the algo and I export the trades I noted that the exit conditions is not always respected (PNL < 2% for some trades). Why please ?

2. Also I would like to run it on 10 securities. I understand that I need to add securities and then lool to create the stochastic for each security. Can someone give me an example to create sur a loop please ?

3. Last, I would like to try other timeframe especially 30 minutes. How to do it with consolidator please ?

Thank you


using System.Drawing; // for Color
namespace QuantConnect

public class Teststochnoloss : QCAlgorithm
// stock name set up.
String symbol = "IRBT";

decimal targetProfit = 0.02m; //0.02m=0.2% //Target profit for strategy. When achieve this exit.
Stochastic sto;
int overBought = 20;//a optimiser
decimal sortie=100000m;

//Initialize the data and resolution you require for your strategy:
public override void Initialize()
//ini broker model
SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash);
//Start and End Date range for the backtest:
SetStartDate(2016, 1, 16);

//Add as many securities as you like. All the data will be passed into the event handler:
AddSecurity(SecurityType.Equity, symbol, Resolution.Hour);
//*how to use the algo on several stocks (a dozen max) ?
//* Is it possible to use 30 minutes timeframe please ?
// customize benchmark
// initializing stochastic
int KPeriod = 14;
int DPeriod = 3;
sto = STO(symbol,14,KPeriod,DPeriod);
// we can 'warm up' our indicators using the history function directly
//var history = History("SPY", 250);
// we can also warm up these indicators using the SetWarmup function
// SetWarmup will pump data through the entire algorithm, including OnData
// whereas the History function is handled by user code
//SetWarmup(250); // ask for 250 bars of warmup at registered resolution
//SetWarmup(TimeSpan.FromDays(3)); // ask for 3 calendar days of warmup
// these are calendar days, so beware of weekends
//Cash allocation

Chart plotter = new Chart("Plotter");
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



//Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
public void OnData(TradeBars data)
if (IsWarmingUp) return;
Log(string.Format("{0} {1}",sto.StochD,sto.StochK));

if (!Portfolio.HoldStock && sto.IsReady) //* is is not a doublon to put is ready with warmup ?

if(sto.StochK>sto.StochD && sto.StochD<overBought )
int quantity = (int)Math.Floor(Portfolio.Cash / data[symbol].Close);
var entry = data[symbol].Price;
sortie = entry * (1 + targetProfit); // target = 0.05 for 5% gain
//SetHoldings(symbol, 1);

Order(symbol, quantity);///sur plusieurs titres quantité?
Plot("Plotter", "Buy", data[symbol].Close);
Debug("Stoch no loss buy" + Time.ToString("Y"));
if (data[symbol].Price >= sortie)
Liquidate(symbol);//sortie a optimiser avec croisement sto ou trailinstop
Plot("Plotter","Sell", data[symbol].Close);
//exitDate = Time.Date;

// Later in your OnData(Slice data):
Plot("Plotter", "IRBT", data[symbol].Close);
Plot("Plotter","D", sto.StochD);
Plot("Plotter","K", sto.StochK);
Plot("Plotter","Over Bought", overBought);
//Plot("Buy", buyOrders);
//Plot("Sell", sellOrders);
//Plot(_plotter, "Price", data[symbol].Close);