Weird backtests results

Hello everybody, i am trying to build a strategy that works on bitcoin and does the following:

enter when specific price patterns (on daily bars) and time ranges conditions are met and use stop market orders to enter at specific prices. 

exit when price and time conditions  are met. as stop losses and tp


i am attaching the project so that everyone can see it.

the code compiles without errors but it runs weird backtests, it doesnt respect the rules i gave it...

hope someone can help me out


Update Backtest

namespace QuantConnect.Algorithm.CSharp
/// <summary>
/// Basic template algorithm simply initializes the date range and cash. This is a skeleton
/// framework you can use for designing an algorithm.
/// </summary>
public class BasicTemplateAlgorithm : QCAlgorithm
//private Symbol _spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
public RollingWindow<TradeBar> Close5m;
public RollingWindow<TradeBar> Close1d;
public decimal chiusura5;
public decimal chiusura1;
public decimal chiusura2;
public decimal chiusura3;
public decimal chiusura4;
public decimal aperturaoggi;
public decimal massimogiornata;
public decimal minimogiornata;
public bool condition1;
public bool condition90;
public SimpleMovingAverage value1;
public decimal percent;
public decimal stopValue;

private OrderTicket CurrentOrder;
private OrderTicket StopLoss;
//private OrderTicket ProfitTarget;
/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
public override void Initialize()
SetStartDate(2014, 7, 1); //Set Start Date
SetEndDate(2018, 2, 23); //Set End Date
SetCash(100000); //Set Strategy Cash

SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash);

// Find more symbols here:
AddCrypto("BTCUSD", Resolution.Minute);

var fiveMinutes = new TradeBarConsolidator(TimeSpan.FromMinutes(5));
fiveMinutes.DataConsolidated += OnFiveMinutes;
SubscriptionManager.AddConsolidator("BTCUSD", fiveMinutes);

var dailyMinutes = new TradeBarConsolidator(TimeSpan.FromMinutes(1440));
dailyMinutes.DataConsolidated += OnDaily;
SubscriptionManager.AddConsolidator("BTCUSD", dailyMinutes);

Close5m = new RollingWindow<TradeBar>(10);
Close1d = new RollingWindow<TradeBar>(10);

value1 = SMA("BTCUSD", 3, Resolution.Daily);


/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="data">Slice object keyed by symbol containing the stock data</param>
public override void OnData(Slice data)


public void OnFiveMinutes(object sender, TradeBar bar)
//if (!Close5m.IsReady) return;
//Debug("Close5min0: " + Close5m[0].Close);
//Debug("Close5min1: " + Close5m[1].Close);

//TimeSpan Sessstart = new TimeSpan(8, 0, 0);
//TimeSpan Sessend = new TimeSpan(14, 0, 0);
//TimeSpan Sessstop = new TimeSpan(13, 0, 0);
//TimeSpan now = DateTime.Now.TimeOfDay;
//Debug("Now: " + now.ToString());
condition1 = chiusura1 > chiusura2 && chiusura2 < chiusura3;
condition90 = aperturaoggi > value1;
//Debug("condition1: "+ condition1);
//Debug("condition90: "+ condition90);
if (!Portfolio.HoldStock && condition90 == true && condition1 == true && aperturaoggi > chiusura1)
var quantity = 0.01;
if (Time.Hour > 8 && Time.Hour <= 13)

// Buy
CurrentOrder = StopMarketOrder("BTCUSD", quantity, massimogiornata, tag:"longEntry");
// Set StopLoss order
//StopLoss = StopMarketOrder("BTCUSD", -quantity, massimogiornata);
// Set Profit Target
//ProfitTarget = LimitOrder("BTCUSD"), -quantity, price * (1m + TakeProfitPercent));
//SetHoldings("BTCUSD", 0.001);
else if (Portfolio.HoldStock)
var currentStopPrice = CurrentOrder.Get(OrderField.StopPrice);
StopLoss = StopMarketOrder("BTCUSD", -quantity,(currentStopPrice - stopValue), tag:"StopLoss");
else if ((Portfolio.Invested) && (Time.Hour >= 14))
StopLoss = Order("BTCUSD", -quantity);

//var btcHoldings = Portfolio.CashBook["BTC"].Amount;
//var usdCash = Portfolio.CashBook["USD"].Amount;
//Log($"{Time} - BTC holdings: {btcHoldings:F8} - USD cash: {usdCash:F2}");

private void OnDaily(object sender, TradeBar consolidated)
if (!Close1d.IsReady) return;
//Debug("CloseDaily0: " + Close1d[0].Close);
//Debug("CloseDaily1: " + Close1d[1].Close);
chiusura5 = Close1d[5].Close;
chiusura1 = Close1d[1].Close;
chiusura2 = Close1d[2].Close;
chiusura3 = Close1d[3].Close;
chiusura4 = Close1d[4].Close;
aperturaoggi = Close1d[0].Open;
massimogiornata = Close1d[0].High;
minimogiornata = Close1d[0].Low;

percent = chiusura1 / 100;
stopValue = 4*percent;
//Debug("percent: " + percent);
//Debug("stopValue: " + stopValue);
condition1 = chiusura1 > chiusura2 && chiusura2 < chiusura3 && chiusura3 > chiusura4 && chiusura4 > chiusura5;
condition90 = aperturaoggi > value1;
//Debug("chiusura1: " + chiusura1);
//Debug("chiusura2: " + chiusura2);
//Debug("chiusura3: " + chiusura3);
//Debug("aperturaoggi: " + aperturaoggi);
//Debug("massimogiornata: " + massimogiornata);
//Debug("minimogiornata: " + minimogiornata);
//Debug("condition1: "+ condition1);
//Debug("condition90: "+ condition90);



please use the "attach backtest" button in the comment section and add a completed backtest


I am trying but it won't let me use that button, here is the link.. hope it helps


is it a problem in the order management? 


i think there is problem because people are trading their algos it should work....

maybe describe the problems you see here?

and very important:

there is an OnOrderEvent code example in the documentation where you can follow the orders (with their ID) which get executed. In this OnOrderEvent that get called use the Time global variable which should also be decribed in the documentation so you SEE EXACTLE WHICH TIME THE ORDER GOT EXECUTED!

The problem is that it enters every 5 minutes the first trade as you see in the backtest, it's the same trade at the same entryprice . then it stops working


thanks for the suggestion 


i played a littlebit around

check if its ok....


Update Backtest


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


This discussion is closed