Error | CBot instance crashed with error #ED2F94F3.
Error | CBot instance crashed with error #ED2F94F3.
12 Nov 2024, 13:47
My cbot crashes after adding sma to the code and backtesting on 10 and 1 minute bars
[Parameter("Source", Group = "Moving Average")]
public DataSeries SourceSma
{
get;
set;
}
[Parameter("SMA Period", Group = "Moving Average", DefaultValue = 20)]
public int SmaPeriod
{
get;
set;
}
protected override void OnStart()
{
Sma20 = Indicators.SimpleMovingAverage(SourceSma, SmaPeriod);
}
it also works fine with 1 hr time frame, what had me shaking my head is that old bots I made work , but when i copy the exact same code here to a new cbot it doesn't work
Replies
PanagiotisCharalampous
13 Nov 2024, 06:33
RE: Error | CBot instance crashed with error #ED2F94F3.
samyelzallat said:
when I remove onbarclosed from the bot and use onbar it doesn't cause an error
Hi there,
Please provide the complete cBot code
Best regards,
Panagiotis
@PanagiotisCharalampous
samyelzallat
13 Nov 2024, 11:16
( Updated at: 13 Nov 2024, 11:22 )
RE: RE: Error | CBot instance crashed with error #ED2F94F3.
PanagiotisCharalampous said:
samyelzallat said:
when I remove onbarclosed from the bot and use onbar it doesn't cause an error
Hi there,
Please provide the complete cBot code
Best regards,
Panagiotis
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace cAlgo.Robots {
[Robot(AccessRights = AccessRights.None, AddIndicators = true)] public class WaveRider: Robot {
[Parameter("Trade Volume (in units)", DefaultValue = 2000, MinValue = 2000, Step = 2000)] public double TradeVolume {
get;
set;
} [Parameter("Partial Take Profit (in units)", DefaultValue = 1000, MinValue = 1000, Step = 1000)] public double VolumeToClose {
get;
set;
} [Parameter("ATR Period", Group = "ATR", DefaultValue = 20)] public int AtrPeriod {
get;
set;
} [Parameter("ATR MA Type", Group = "ATR", DefaultValue = MovingAverageType.Simple)] public MovingAverageType AtrMa {
get;
set;
}
private AverageTrueRange atr;
[Parameter("First Source", DefaultValue = "Open", Group = "Moving Averages")] public DataSeries OpenSmaSource {
get;
set;
} [Parameter("Second Source", DefaultValue = "Close", Group = "Moving Averages")] public DataSeries CloseSmaSource {
get;
set;
} [Parameter("SMA Period", Group = "Moving Averages", DefaultValue = 20)] public int SmaPeriod {
get;
set;
} [Parameter("SMA Period", Group = "Moving Averages", DefaultValue = 100)] public int Sma2Period {
get;
set;
}
private SimpleMovingAverage OpenSma;
private SimpleMovingAverage CloseSma;
private SimpleMovingAverage BiggerSma;
private
const string myComment = "Trendat";
private bool FirstTradeActive = false;
private bool IsItLong = false;
private bool IsItShort = false;
private bool ModifiedOnce = false;
private bool ModifiedTwice = false;
private bool MultipleTrades = false;
private bool GridStopped = false;
private bool StepOne = false;
private bool StepTwo = false;
private bool GridStopLossSet = false;
private double MultiplierReference = 0;
private double GlobalAtrThreshold;
private double GlobalFirstEntry;
private double GlobalExitProfit;
private double GlobalExitMeasure;
private double GlobalGrid;
private double GridSl;
double GridModifiedSL;
private double LotsTraded = 0;
protected override void OnStart() {
atr = Indicators.AverageTrueRange(AtrPeriod, AtrMa);
OpenSma = Indicators.SimpleMovingAverage(OpenSmaSource, SmaPeriod);
CloseSma = Indicators.SimpleMovingAverage(CloseSmaSource, SmaPeriod);
BiggerSma = Indicators.SimpleMovingAverage(CloseSmaSource, Sma2Period);
}
protected override void OnBarClosed() {
double closingPrice = Bars.ClosePrices.Last(1);
double openingPrice = Bars.OpenPrices.Last(1);
double closingPrice2ago = Bars.ClosePrices.Last(2);
double openingPrice2ago = Bars.OpenPrices.Last(2);
double closingPrice3ago = Bars.ClosePrices.Last(3);
double openingPrice3ago = Bars.OpenPrices.Last(3);
double closingPrice4ago = Bars.ClosePrices.Last(4);
double openingPrice4ago = Bars.OpenPrices.Last(4);
double closingPrice5ago = Bars.ClosePrices.Last(5);
double openingPrice5ago = Bars.OpenPrices.Last(5);
double closingPrice6ago = Bars.ClosePrices.Last(6);
double openingPrice6ago = Bars.OpenPrices.Last(6);
double closingPrice7ago = Bars.ClosePrices.Last(7);
double openingPrice7ago = Bars.OpenPrices.Last(7);
double closingPrice8ago = Bars.ClosePrices.Last(8);
double openingPrice8ago = Bars.OpenPrices.Last(8);
double closingPrice9ago = Bars.ClosePrices.Last(9);
double openingPrice9ago = Bars.OpenPrices.Last(9);
double closingPrice10ago = Bars.ClosePrices.Last(10);
double openingPrice10ago = Bars.OpenPrices.Last(10);
double OpenSmaLast = OpenSma.Result.Last(1);
double OpenSma3Ago = OpenSma.Result.Last(3);
double CloseSmaLast = CloseSma.Result.Last(1);
double CloseSma3Ago = CloseSma.Result.Last(3);
double BiggerSmaLast = BiggerSma.Result.Last(1);
bool BuySignal = CloseSmaLast > OpenSmaLast && OpenSmaLast > BiggerSmaLast && openingPrice3ago < OpenSma3Ago && closingPrice3ago > CloseSma3Ago && closingPrice2ago > openingPrice2ago && closingPrice > openingPrice;
bool SellSignal = CloseSmaLast < OpenSmaLast && CloseSmaLast < BiggerSmaLast && openingPrice3ago > OpenSma3Ago && closingPrice3ago < CloseSma3Ago && closingPrice2ago < openingPrice2ago && closingPrice < openingPrice;
double GridRange = Math.Abs(closingPrice2ago - openingPrice8ago);
double DistanceFromEntry = Math.Abs(GlobalFirstEntry - closingPrice);
int DistanceMultipled = 0;
if (GlobalGrid != 0) {
DistanceMultipled = (int) Math.Floor(DistanceFromEntry / GlobalGrid);
}
double SpreadAtr = Math.Round(atr.Result.Last(1) / Symbol.PipSize, 4);
double StopLossAtr = Math.Round(GridRange / Symbol.PipSize, 4) * 4;
double spread = Symbol.Spread / Symbol.PipSize;
double MultipleTradesMeasure = atr.Result.Last(1);
double MultipleTradesAtr = Math.Round((atr.Result.Last(1)) / Symbol.PipSize, 4);
GlobalExitProfit = MultipleTradesAtr;
GlobalExitMeasure = MultipleTradesMeasure;
var HourlyTf = MarketData.GetBars(TimeFrame.Hour);
double HrlyOpening = HourlyTf.OpenPrices.Last(0);
double HrlyHigh = HourlyTf.HighPrices.Last(0);
double HrlyLow = HourlyTf.LowPrices.Last(0);
double HrlyOpening2Ago = HourlyTf.OpenPrices.Last(1);
double HrlyClosing2Ago = HourlyTf.ClosePrices.Last(1);
double HrlyHigh2Ago = HourlyTf.HighPrices.Last(1);
double HrlyLow2Ago = HourlyTf.LowPrices.Last(1);
double HrlyOpening3Ago = HourlyTf.OpenPrices.Last(2);
double HrlyClosing3Ago = HourlyTf.ClosePrices.Last(2);
double HrlyCandleBody = Math.Abs(HrlyOpening2Ago - HrlyClosing2Ago);
double HrlyUpperWick;
double HrlyLowerWick;
bool PossibleReverse = false;
if (HrlyClosing2Ago > HrlyOpening2Ago) {
HrlyUpperWick = HrlyHigh2Ago - HrlyClosing2Ago;
HrlyLowerWick = HrlyOpening2Ago - HrlyLow2Ago;
if (HrlyUpperWick >= 0.4 * HrlyCandleBody) {
PossibleReverse = true;
}
} else if (HrlyClosing2Ago < HrlyOpening2Ago) {
HrlyUpperWick = HrlyHigh2Ago - HrlyOpening2Ago;
HrlyLowerWick = HrlyClosing2Ago - HrlyLow2Ago;
if (HrlyLowerWick >= 0.4 * HrlyCandleBody) {
PossibleReverse = true;
}
}
bool BullishHr = HrlyClosing2Ago > HrlyOpening2Ago && HrlyClosing3Ago > HrlyOpening3Ago;
bool BearishHr = HrlyClosing2Ago < HrlyOpening2Ago && HrlyClosing3Ago < HrlyOpening3Ago;
if (DistanceMultipled > MultiplierReference && !ModifiedOnce && !GridStopped && (IsItLong || IsItShort)) {
MultiplierReference = DistanceMultipled;
MultipleTrades = true;
TradeType tradeType = IsItLong ? TradeType.Buy : TradeType.Sell;
ExecuteMarketOrder(tradeType, SymbolName, TradeVolume, MultiplierReference.ToString(), GridSl, null, myComment);
LotsTraded += 0.01;
}
if (FirstTradeActive) {
return;
}
if (BuySignal && spread <= .9 && spread < SpreadAtr) {
ExecuteMarketOrder(TradeType.Buy, SymbolName, TradeVolume, "First Buy", StopLossAtr, null, myComment);
IsItLong = true;
FirstTradeActive = true;
GlobalFirstEntry = Symbol.Ask;
GlobalAtrThreshold = atr.Result.Last(1);
GlobalGrid = GridRange;
GridSl = StopLossAtr;
LotsTraded += 0.01;
} else if (SellSignal && spread <= .9 && spread < SpreadAtr) {
ExecuteMarketOrder(TradeType.Sell, SymbolName, TradeVolume, "First Sell", StopLossAtr, null, myComment);
IsItShort = true;
FirstTradeActive = true;
GlobalFirstEntry = Symbol.Bid;
GlobalAtrThreshold = atr.Result.Last(1);
GlobalGrid = GridRange;
GridSl = StopLossAtr;
LotsTraded += 0.01;
}
}
this will crash until you replace OnBarClosed with OnBar, which I did to have it work
also works with 1hr tf correctly, and it crashes with both fp markets and fusion markets accounts
@samyelzallat
PanagiotisCharalampous
13 Nov 2024, 12:21
RE: RE: RE: Error | CBot instance crashed with error #ED2F94F3.
samyelzallat said:
PanagiotisCharalampous said:
samyelzallat said:
when I remove onbarclosed from the bot and use onbar it doesn't cause an error
Hi there,
Please provide the complete cBot code
Best regards,
Panagiotis
using cAlgo.API;using cAlgo.API.Collections;using cAlgo.API.Indicators;using cAlgo.API.Internals;using cAlgo.Indicators;using Microsoft.VisualBasic;using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;namespace cAlgo.Robots { [Robot(AccessRights = AccessRights.None, AddIndicators = true)] public class WaveRider: Robot { [Parameter("Trade Volume (in units)", DefaultValue = 2000, MinValue = 2000, Step = 2000)] public double TradeVolume { get; set; } [Parameter("Partial Take Profit (in units)", DefaultValue = 1000, MinValue = 1000, Step = 1000)] public double VolumeToClose { get; set; } [Parameter("ATR Period", Group = "ATR", DefaultValue = 20)] public int AtrPeriod { get; set; } [Parameter("ATR MA Type", Group = "ATR", DefaultValue = MovingAverageType.Simple)] public MovingAverageType AtrMa { get; set; } private AverageTrueRange atr; [Parameter("First Source", DefaultValue = "Open", Group = "Moving Averages")] public DataSeries OpenSmaSource { get; set; } [Parameter("Second Source", DefaultValue = "Close", Group = "Moving Averages")] public DataSeries CloseSmaSource { get; set; } [Parameter("SMA Period", Group = "Moving Averages", DefaultValue = 20)] public int SmaPeriod { get; set; } [Parameter("SMA Period", Group = "Moving Averages", DefaultValue = 100)] public int Sma2Period { get; set; } private SimpleMovingAverage OpenSma; private SimpleMovingAverage CloseSma; private SimpleMovingAverage BiggerSma; private const string myComment = "Trendat"; private bool FirstTradeActive = false; private bool IsItLong = false; private bool IsItShort = false; private bool ModifiedOnce = false; private bool ModifiedTwice = false; private bool MultipleTrades = false; private bool GridStopped = false; private bool StepOne = false; private bool StepTwo = false; private bool GridStopLossSet = false; private double MultiplierReference = 0; private double GlobalAtrThreshold; private double GlobalFirstEntry; private double GlobalExitProfit; private double GlobalExitMeasure; private double GlobalGrid; private double GridSl; double GridModifiedSL; private double LotsTraded = 0; protected override void OnStart() { atr = Indicators.AverageTrueRange(AtrPeriod, AtrMa); OpenSma = Indicators.SimpleMovingAverage(OpenSmaSource, SmaPeriod); CloseSma = Indicators.SimpleMovingAverage(CloseSmaSource, SmaPeriod); BiggerSma = Indicators.SimpleMovingAverage(CloseSmaSource, Sma2Period); } protected override void OnBarClosed() { double closingPrice = Bars.ClosePrices.Last(1); double openingPrice = Bars.OpenPrices.Last(1); double closingPrice2ago = Bars.ClosePrices.Last(2); double openingPrice2ago = Bars.OpenPrices.Last(2); double closingPrice3ago = Bars.ClosePrices.Last(3); double openingPrice3ago = Bars.OpenPrices.Last(3); double closingPrice4ago = Bars.ClosePrices.Last(4); double openingPrice4ago = Bars.OpenPrices.Last(4); double closingPrice5ago = Bars.ClosePrices.Last(5); double openingPrice5ago = Bars.OpenPrices.Last(5); double closingPrice6ago = Bars.ClosePrices.Last(6); double openingPrice6ago = Bars.OpenPrices.Last(6); double closingPrice7ago = Bars.ClosePrices.Last(7); double openingPrice7ago = Bars.OpenPrices.Last(7); double closingPrice8ago = Bars.ClosePrices.Last(8); double openingPrice8ago = Bars.OpenPrices.Last(8); double closingPrice9ago = Bars.ClosePrices.Last(9); double openingPrice9ago = Bars.OpenPrices.Last(9); double closingPrice10ago = Bars.ClosePrices.Last(10); double openingPrice10ago = Bars.OpenPrices.Last(10); double OpenSmaLast = OpenSma.Result.Last(1); double OpenSma3Ago = OpenSma.Result.Last(3); double CloseSmaLast = CloseSma.Result.Last(1); double CloseSma3Ago = CloseSma.Result.Last(3); double BiggerSmaLast = BiggerSma.Result.Last(1); bool BuySignal = CloseSmaLast > OpenSmaLast && OpenSmaLast > BiggerSmaLast && openingPrice3ago < OpenSma3Ago && closingPrice3ago > CloseSma3Ago && closingPrice2ago > openingPrice2ago && closingPrice > openingPrice; bool SellSignal = CloseSmaLast < OpenSmaLast && CloseSmaLast < BiggerSmaLast && openingPrice3ago > OpenSma3Ago && closingPrice3ago < CloseSma3Ago && closingPrice2ago < openingPrice2ago && closingPrice < openingPrice; double GridRange = Math.Abs(closingPrice2ago - openingPrice8ago); double DistanceFromEntry = Math.Abs(GlobalFirstEntry - closingPrice); int DistanceMultipled = 0; if (GlobalGrid != 0) { DistanceMultipled = (int) Math.Floor(DistanceFromEntry / GlobalGrid); } double SpreadAtr = Math.Round(atr.Result.Last(1) / Symbol.PipSize, 4); double StopLossAtr = Math.Round(GridRange / Symbol.PipSize, 4) * 4; double spread = Symbol.Spread / Symbol.PipSize; double MultipleTradesMeasure = atr.Result.Last(1); double MultipleTradesAtr = Math.Round((atr.Result.Last(1)) / Symbol.PipSize, 4); GlobalExitProfit = MultipleTradesAtr; GlobalExitMeasure = MultipleTradesMeasure; var HourlyTf = MarketData.GetBars(TimeFrame.Hour); double HrlyOpening = HourlyTf.OpenPrices.Last(0); double HrlyHigh = HourlyTf.HighPrices.Last(0); double HrlyLow = HourlyTf.LowPrices.Last(0); double HrlyOpening2Ago = HourlyTf.OpenPrices.Last(1); double HrlyClosing2Ago = HourlyTf.ClosePrices.Last(1); double HrlyHigh2Ago = HourlyTf.HighPrices.Last(1); double HrlyLow2Ago = HourlyTf.LowPrices.Last(1); double HrlyOpening3Ago = HourlyTf.OpenPrices.Last(2); double HrlyClosing3Ago = HourlyTf.ClosePrices.Last(2); double HrlyCandleBody = Math.Abs(HrlyOpening2Ago - HrlyClosing2Ago); double HrlyUpperWick; double HrlyLowerWick; bool PossibleReverse = false; if (HrlyClosing2Ago > HrlyOpening2Ago) { HrlyUpperWick = HrlyHigh2Ago - HrlyClosing2Ago; HrlyLowerWick = HrlyOpening2Ago - HrlyLow2Ago; if (HrlyUpperWick >= 0.4 * HrlyCandleBody) { PossibleReverse = true; } } else if (HrlyClosing2Ago < HrlyOpening2Ago) { HrlyUpperWick = HrlyHigh2Ago - HrlyOpening2Ago; HrlyLowerWick = HrlyClosing2Ago - HrlyLow2Ago; if (HrlyLowerWick >= 0.4 * HrlyCandleBody) { PossibleReverse = true; } } bool BullishHr = HrlyClosing2Ago > HrlyOpening2Ago && HrlyClosing3Ago > HrlyOpening3Ago; bool BearishHr = HrlyClosing2Ago < HrlyOpening2Ago && HrlyClosing3Ago < HrlyOpening3Ago; if (DistanceMultipled > MultiplierReference && !ModifiedOnce && !GridStopped && (IsItLong || IsItShort)) { MultiplierReference = DistanceMultipled; MultipleTrades = true; TradeType tradeType = IsItLong ? TradeType.Buy : TradeType.Sell; ExecuteMarketOrder(tradeType, SymbolName, TradeVolume, MultiplierReference.ToString(), GridSl, null, myComment); LotsTraded += 0.01; } if (FirstTradeActive) { return; } if (BuySignal && spread <= .9 && spread < SpreadAtr) { ExecuteMarketOrder(TradeType.Buy, SymbolName, TradeVolume, "First Buy", StopLossAtr, null, myComment); IsItLong = true; FirstTradeActive = true; GlobalFirstEntry = Symbol.Ask; GlobalAtrThreshold = atr.Result.Last(1); GlobalGrid = GridRange; GridSl = StopLossAtr; LotsTraded += 0.01; } else if (SellSignal && spread <= .9 && spread < SpreadAtr) { ExecuteMarketOrder(TradeType.Sell, SymbolName, TradeVolume, "First Sell", StopLossAtr, null, myComment); IsItShort = true; FirstTradeActive = true; GlobalFirstEntry = Symbol.Bid; GlobalAtrThreshold = atr.Result.Last(1); GlobalGrid = GridRange; GridSl = StopLossAtr; LotsTraded += 0.01; } }
this will crash until you replace OnBarClosed with OnBar, which I did to have it work
also works with 1hr tf correctly, and it crashes with both fp markets and fusion markets accounts
Thank you. This issue will be fixed in an upcoming update.
@PanagiotisCharalampous
samyelzallat
12 Nov 2024, 15:00
when I remove onbarclosed from the bot and use onbar it doesn't cause an error
@samyelzallat