trophy

Long Only, ETF Alpha Streams Competition with $27,500 Prize Pool

Back

Runtime Error: Object reference not set to an instance of an object

Hello fellows,
I hacked together this to get a feeling of quantconnect and I get this error, wich suggests I have to declare somehow the apriOrdineTest object. (I'm new to both QC and CSharp so excuse me if this is a very dumb question)
I just dont understand why the commented out IF works and the current IF with more conditions does not.
The error is: "Runtime Error: Object reference not set to an instance of an object"


namespace QuantConnect.Algorithm.CSharp
{

public class BasicTemplateAlgorithm : QCAlgorithm
{
private Symbol spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
private HeikinAshi heikinHour;
private HeikinAshi heikinDay;
decimal spread = .0001m;
decimal price;
OrderTicket lastOrderTicket = null;
decimal prezzOrdineAltoDelGiorno = 0;
bool condizioneDiSicurezza = true;



public override void Initialize()
{
SetStartDate(2018, 10, 07); //Set Start Date
SetEndDate(DateTime.Now.Date.AddDays(-1));
SetCash(100000); //Set Strategy Cash

AddEquity("SPY", Resolution.Hour);
var eurusd = AddForex("EURUSD", Resolution.Hour, Market.Oanda);
eurusd.SetDataNormalizationMode(DataNormalizationMode.Raw);
heikinDay = HeikinAshi("EURUSD", Resolution.Daily);
heikinHour = HeikinAshi("EURUSD", Resolution.Hour);

}

public override void OnData(Slice data)
{
var tickets = Transactions.GetOpenOrderTickets().ToList();
var orders = Transactions.GetOpenOrders();
var holdings_eurusd = Portfolio["EURUSD"].Quantity;

apriOrdineTest();
controllaTicket();

}

// Override the base class event handler for order events
/*public override void OnOrderEvent(OrderEvent orderEvent)
{
var lastOrder = Transactions.getOrderById(lastOrderId);
var order = Transactions.GetOrderById(orderEvent.OrderId);
Console.WriteLine("{0}: {1}: {2}", Time, order.Type, orderEvent);

}*/


// funzione di prova che apre l'ordine /---------------------
public void apriOrdineTest(){

if(!heikinHour.IsReady) { // THIS ONE causes Runtime Error: Object reference not set to an instance of an object
var heikinValue = heikinHour;
}
else
{
if(condizioneDiSicurezza == true){
if(lastOrderTicket == null) {lastOrderTicket = StopMarketOrder("EURUSD", 1, Decimal.Add(heikinHour.High, spread));}
// Runtime Error: Object reference not set to an instance of an object
if(Transactions.GetOrderById(lastOrderTicket).Status == OrderStatus.Filled && Transactions.GetOrderById(lastOrderTicket).Price < heikinHour.High) {
prezzOrdineAltoDelGiorno = Transactions.GetOrderById(lastOrderTicket).Price;
lastOrderTicket = StopMarketOrder("EURUSD", 1, Decimal.Add(heikinHour.High, spread));
}
}
else // condizione sicurezza
{
Log("Non ci sono le condizioni di sicurezza per procedere");
}
}

/*if (lastOrderTicket == null){ // THIS ONE GOES

if(!heikinHour.IsReady) {
var heikinValue = heikinHour;
}
else // heikin hour is ready
{
//if(){}
Debug("lanciato apriOrdineTest()");
lastOrderTicket = StopMarketOrder("EURUSD", 1, Decimal.Add(heikinHour.High, spread));
Debug("lastOrderTicket = " + lastOrderTicket);
}

}*/

}

public bool ordinePrecedente(){
int lastOrder_id = Transactions.LastOrderId;
var lastOrder_status = Transactions.GetOrderById(lastOrder_id).Status;
var lastOrder_time = Transactions.GetOrderById(lastOrder_id).Time;
//var lastOrder_ticket = Transactions.GetOrderById(lastOrder_id);

Debug("lastOrder_status " + lastOrder_status.ToString() +" lastOrder_time " + lastOrder_time);

/*if(lastOrder_status ==){return true;}
else
{return false;}*/

return true;

//Values: New, Submitted, PartiallyFilled, Filled, Canceled, None, Invalid, CancelPending

}

public void controllaTicket(){ //---------------------
decimal peggiorePerdita = 0;

var tickets = Transactions
.GetOrderTickets(x => x.Status == OrderStatus.Filled);
DateTime ticketTime = Time;

int worstOrderId = 0;

Debug("Average price of " + Portfolio["EURUSD"].Quantity + " fills at average " + Portfolio["EURUSD"].AveragePrice);

foreach (var ticket in tickets){
var symbol = ticket.Symbol;
var fillQuantity = ticket.QuantityFilled;
var fillPrice = ticket.AverageFillPrice;
ticketTime = ticket.Time;

if (fillPrice - price > peggiorePerdita){
peggiorePerdita = fillPrice - price;
worstOrderId = ticket.OrderId;
}
}
}
}
}
Update Backtest







0

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.


hi,

if(!heikinHour.IsReady) { // THIS ONE causes Runtime Error: Object reference not set to an instance of an object
var heikinValue = heikinHour;
}

if NOT is READY set heikinValue?  --> maybe if(heikinHour.IsReady)    ???

The indicator works check the Logs output/file

0


Update Backtest





0

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.


Loading...

This discussion is closed