Crashed in OnBar with NullReferenceException: Object reference not set to an instance of an object.
Crashed in OnBar with NullReferenceException: Object reference not set to an instance of an object.
15 Jun 2016, 12:43
Hello,
I am trying to place this limit order below onBar() but I get the following error.
double buyLimit = (Symbol.Ask + Symbol.Bid) / 2;
var buyLmtOrder = PlaceLimitOrder(TradeType.Buy, Symbol, size, buyLimit, "mylabel", sellSL, TP);
ERROR
30/05/2016 21:05:00.742 | Crashed in OnBar with NullReferenceException: Object reference not set to an instance of an object.
Any ideas what is causing this?
Thanks
Replies
DELETED_USER
20 Jul 2016, 14:11
I am getting the same error again but now for this bit of code, any help would be much appreciated. I can't seem to find the nullexception, the algo prints "Execution time" so the error must be somewhere after.
Thanks!
if (Time.Hour == 20 && Time.Minute >= 55 && Time.Minute < 56)
{
Print("Execution Time");
}
else
{
//Print("We wait for the proper time");
return;
}
///Close Open Positions on Close
if (Positions.Count != 0)
{
ClosePosition(Positions.Find("algo"));
Print("Position Closed EOD");
}
if (HasPosition(Symbol))
return;
if (buySignal)
{
Print("Ready to Calculate Size");
CalculateSize(sellSL);
Print("Size Calculated");
double buyLimit = (Symbol.Ask + Symbol.Bid) / 2;
//var buyOrder = ExecuteMarketOrder(TradeType.Buy, Symbol, size, "algo", sellSL, TP);
Print("LimitPrice:{0}, Bid: {1}, Offer: {2}", buyLimit, Symbol.Bid, Symbol.Ask);
var buyLmtOrder = PlaceLimitOrder(TradeType.Buy, Symbol, size, buyLimit, "algo", sellSL, TP);
//ExecuteMarketOrder(TradeType.Buy, Symbol, size, "algo", sellSL, TP);
Print("TP: {0}, SellSL {1}", TP, sellSL);
//Print(LastResult.Position.Commissions);
}
else if (sellSignal)
{
Print("Ready to Calculate Size");
CalculateSize(buySL);
Print("Size Calculated");
double sellLimit = (Symbol.Ask + Symbol.Bid) / 2;
//var sellOrder = ExecuteMarketOrder(TradeType.Sell, Symbol, size, "algo", buySL, TP);
Print("LimitPrice:{0}, Bid: {1}, Offer: {2}", sellLimit, Symbol.Bid, Symbol.Ask);
var sellLmtOrder = PlaceLimitOrder(TradeType.Sell, Symbol, size, sellLimit, "algo", buySL, TP);
Print("TP: {0}, BuySL {1}", TP, buySL);
//Print(LastResult.Position.Commissions);
}
DELETED_USER
20 Jul 2016, 14:28
Another piece of code which I had trouble with in the past, which is to calculate the position size. Sometimes the logic works fine however other times I get Bad_Volume error since the size is rounded down to 0.
private void CalculateSize(double StopLoss)
{
double totalBalance = Account.Balance;
// Calculate the total risk allowed per trade.
double riskPerTrade = (totalBalance * RiskPercent) / 100;
// Add the stop loss, commission pips and spread to get the total pips used for the volume calculation.
double totalPips = StopLoss;
// Calculate the exact volume to be traded. Then round the volume to the nearest 10,000 and convert to an int so that it can be returned to the caller.
double exactVolume = Math.Round(riskPerTrade / (Symbol.PipValue * totalPips), 0);
Print("Volume", exactVolume);
size = (int)Symbol.NormalizeVolume(exactVolume, RoundingMode.Up);
//size = (((int)exactVolume / 1000) * 1000);
//size = (int)Math.Ceiling(exactVolume / 1000) * 1000;
Print("Size", size);
// Finally, check that the calculated volume is not greater than the MaxVolume parameter. If it is greater, reduce the volume to the MaxVolume value.
if (size > MaxVolume)
size = MaxVolume;
}
Jiri
15 Jun 2016, 13:29
Hi, I don't see anything wrong in the snippet you posted. Can you post more of the code?
@Jiri