cBot working not correctly
cBot working not correctly
03 Sep 2014, 16:44
I have the following code onTick and it throws some errors, but it works fine when there is an opened order with SL and TP set:
Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.
Crashed in OnTick with InvalidOperationException: Nullable object must have a value.
var PendingOrdersStrategy1 = 0;
foreach (var order in PendingOrders)
{
if (order.Label == Strategy1)
{
PendingOrdersStrategy1 = PendingOrdersStrategy1 + 1;
}
}
var position = Positions.Find("", Symbol);
if (position != null && position.StopLoss != 0 && position.TakeProfit != 0 && position.Comment == Strategy1)
{
var TargetVolume = Symbol.NormalizeVolume(position.Volume * Multiplier, RoundingMode.Up);
var TargetSLSS = position.EntryPrice;
var expirationTime = Server.Time.AddSeconds(SecondsTimeout);
sellOrderTargetPrice = Math.Round(position.StopLoss.Value - PipsAway * Symbol.PipSize, Symbol.Digits);
buyOrderTargetPrice = Math.Round(position.StopLoss.Value + PipsAway * Symbol.PipSize, Symbol.Digits);
if (position.TradeType == TradeType.Buy && PendingOrdersStrategy1 <= 1)
{
PlaceStopOrder(TradeType.Sell, Symbol, TargetVolume, sellOrderTargetPrice, Strategy1, TargetSLSS, position.EntryPrice, expirationTime, Strategy1);
}
if (position.TradeType == TradeType.Sell && PendingOrdersStrategy1 <= 1)
{
PlaceStopOrder(TradeType.Buy, Symbol, TargetVolume, buyOrderTargetPrice, Strategy1, TargetSLSS, position.EntryPrice, expirationTime, Strategy1);
}
}
if (History.FindLast(Strategy1, Symbol).NetProfit > 0)
{
foreach (var order in PendingOrders)
{
if (order.Label == Strategy1 && order.Comment == Strategy1)
{
CancelPendingOrder(order);
}
}
Replies
obial
03 Sep 2014, 16:54
I tried it by removing the below part and it is still the same errors:
if (History.FindLast(Strategy1, Symbol).NetProfit > 0)
{
foreach (var order in PendingOrders)
{
if (order.Label == Strategy1 && order.Comment == Strategy1)
{
CancelPendingOrder(order);
}
}
looks like there needs to be smth defined for the below, because it works fine when there is an opened order with set SL and TP, but I cannot find anything into the documentation or smth to debug !
var TargetVolume = Symbol.NormalizeVolume(position.Volume * Multiplier, RoundingMode.Up);
var TargetSLSS = position.EntryPrice;
var expirationTime = Server.Time.AddSeconds(SecondsTimeout);
sellOrderTargetPrice = Math.Round(position.StopLoss.Value - PipsAway * Symbol.PipSize, Symbol.Digits);
buyOrderTargetPrice = Math.Round(position.StopLoss.Value + PipsAway * Symbol.PipSize, Symbol.Digits);
@obial
Spotware
03 Sep 2014, 17:07
if (position != null && position.StopLoss != 0 && position.TakeProfit != 0 && position.Comment == Strategy1)
You need to check SL and TP for null instead of comparing it with 0.
You can also use Visual Studio to debug your algorithms: http://help.spotware.com/calgo/visual-studio/debug-cbots
@Spotware
Spotware
03 Sep 2014, 16:48
History.FindLast method could return null if there is no such historical trades. You need to check it for null.
@Spotware