Crash in visual mode during backtest and StopLoss not set duing backtest

Created at 02 Jun 2024, 06:46
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
SH

shijimijiru

Joined 06.05.2024

Crash in visual mode during backtest and StopLoss not set duing backtest
02 Jun 2024, 06:46


I encountered a crash of cBot during visual mode backtest.
The following is the screenshot. The cTrader version is “Raw Traing Ltd cTrader 4.9.2”.


Also, with the code
ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volume, "Test", 20, 40);
Sometimes, the stoploss is not set in backtest. 
The following is the screenshot of positions in issue. 


you can see the S/L column is empty even I set stoploss to 20.
 


@shijimijiru
Replies

PanagiotisCharalampous
10 Jun 2024, 06:56

Hi there,

We have received the exception and the issue will be solved in an upcoming release.

Regarding the SL issue, we would need the cBot code and backtesting parameters in order to reproduce this and explain what happens.

Best regards,

Panagiotis


@PanagiotisCharalampous

Clark22
11 Jun 2024, 13:52

check/print the spread when executing the order with SL. 


@Clark22

ys2310
16 Jun 2024, 04:43

RE: Crash in visual mode during backtest and StopLoss not set duing backtest

PanagiotisCharalampous said: 

Hi there,

We have received the exception and the issue will be solved in an upcoming release.

Regarding the SL issue, we would need the cBot code and backtesting parameters in order to reproduce this and explain what happens.

Best regards,

Panagiotis

Hi Panagiotis,

In visual backtesting another instance, I received another crash with error code #6271DD1D. 

Can you also solve this error in upcoming release please?


@ys2310

ys2310
16 Jun 2024, 04:43

RE: Crash in visual mode during backtest and StopLoss not set duing backtest

PanagiotisCharalampous said: 

Hi there,

We have received the exception and the issue will be solved in an upcoming release.

Regarding the SL issue, we would need the cBot code and backtesting parameters in order to reproduce this and explain what happens.

Best regards,

Panagiotis

Hi Panagiotis,

In visual backtesting another instance, I received another crash with error code #6271DD1D. 

Can you also solve this error in upcoming release please?


@ys2310

ys2310
18 Jun 2024, 02:49 ( Updated at: 18 Jun 2024, 05:18 )

RE: RE: Crash in visual mode during backtest and StopLoss not set duing backtest

ys2310 said: 

PanagiotisCharalampous said: 

Hi there,

We have received the exception and the issue will be solved in an upcoming release.

Regarding the SL issue, we would need the cBot code and backtesting parameters in order to reproduce this and explain what happens.

Best regards,

Panagiotis

Hi Panagiotis,

Here is the test code that produce the SL issue, I'm using Nikkei225, Hm1 for testing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.FullAccess, TimeZone = TimeZones.EasternStandardTime)]
    public class Test2 : Robot
    {        
        [Parameter(DefaultValue = 10, MinValue = 0, MaxValue = 500, Step = 10)]
        public int lossCut { get; set; }
        
        [Parameter(DefaultValue = 10, MinValue = 0, MaxValue = 500, Step = 10)]
        public int takeProfit { get; set; }
        
        double volume = 0.0;
        
        protected override void OnStart()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate
        }

        protected override void OnBarClosed()
        {
            volume = 10;
            
            var last_buy_position = Positions.Where(x => x.Label == "Test" && x.SymbolName == Symbol.Name && x.TradeType == TradeType.Buy).OrderByDescending(x => x.EntryTime).FirstOrDefault();
            
            // Handle price updates here
            if(MarketData.GetBars(TimeFrame, "US500").ClosePrices.Last(0) > MarketData.GetBars(TimeFrame, "US500").OpenPrices.Last(0))
               if (last_buy_position == null || Symbol.Ask < last_buy_position.EntryPrice - 100 * Symbol.TickSize)
                   ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volume, "Test", lossCut, takeProfit, (Server.Time - Bars.Last(0).OpenTime).Milliseconds.ToString());            
        }
        protected override void OnTick() {
            foreach(var position in Positions.FindAll("Test", Symbol.Name))
                if(position.StopLoss == null)
                    ModifyPositionAsync(position, Symbol.Ask - 20 * Symbol.PipSize, position.TakeProfit);
                
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}


@ys2310

PanagiotisCharalampous
18 Jun 2024, 05:36

RE: RE: RE: Crash in visual mode during backtest and StopLoss not set duing backtest

ys2310 said: 

ys2310 said: 

PanagiotisCharalampous said: 

Hi there,

We have received the exception and the issue will be solved in an upcoming release.

Regarding the SL issue, we would need the cBot code and backtesting parameters in order to reproduce this and explain what happens.

Best regards,

Panagiotis

Hi Panagiotis,

Here is the test code that produce the SL issue, I'm using Nikkei225, Hm1 for testing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.FullAccess, TimeZone = TimeZones.EasternStandardTime)]
    public class Test2 : Robot
    {        
        [Parameter(DefaultValue = 10, MinValue = 0, MaxValue = 500, Step = 10)]
        public int lossCut { get; set; }
        
        [Parameter(DefaultValue = 10, MinValue = 0, MaxValue = 500, Step = 10)]
        public int takeProfit { get; set; }
        
        double volume = 0.0;
        
        protected override void OnStart()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate
        }

        protected override void OnBarClosed()
        {
            volume = 10;
            
            var last_buy_position = Positions.Where(x => x.Label == "Test" && x.SymbolName == Symbol.Name && x.TradeType == TradeType.Buy).OrderByDescending(x => x.EntryTime).FirstOrDefault();
            
            // Handle price updates here
            if(MarketData.GetBars(TimeFrame, "US500").ClosePrices.Last(0) > MarketData.GetBars(TimeFrame, "US500").OpenPrices.Last(0))
               if (last_buy_position == null || Symbol.Ask < last_buy_position.EntryPrice - 100 * Symbol.TickSize)
                   ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volume, "Test", lossCut, takeProfit, (Server.Time - Bars.Last(0).OpenTime).Milliseconds.ToString());            
        }
        protected override void OnTick() {
            foreach(var position in Positions.FindAll("Test", Symbol.Name))
                if(position.StopLoss == null)
                    ModifyPositionAsync(position, Symbol.Ask - 20 * Symbol.PipSize, position.TakeProfit);
                
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

Can you also tell me the broker? This usually happens when your SL falls within the symbol's spread. Please check if this is the case


@PanagiotisCharalampous

ys2310
18 Jun 2024, 05:48 ( Updated at: 18 Jun 2024, 09:04 )

RE: RE: RE: RE: Crash in visual mode during backtest and StopLoss not set duing backtest

Hi Panagiotis,

I'm using ICMarkets. How can I check and prevent this using code?

PanagiotisCharalampous said: 

ys2310 said: 

ys2310 said: 

PanagiotisCharalampous said: 

Hi there,

We have received the exception and the issue will be solved in an upcoming release.

Regarding the SL issue, we would need the cBot code and backtesting parameters in order to reproduce this and explain what happens.

Best regards,

Panagiotis

Hi Panagiotis,

Here is the test code that produce the SL issue, I'm using Nikkei225, Hm1 for testing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.FullAccess, TimeZone = TimeZones.EasternStandardTime)]
    public class Test2 : Robot
    {        
        [Parameter(DefaultValue = 10, MinValue = 0, MaxValue = 500, Step = 10)]
        public int lossCut { get; set; }
        
        [Parameter(DefaultValue = 10, MinValue = 0, MaxValue = 500, Step = 10)]
        public int takeProfit { get; set; }
        
        double volume = 0.0;
        
        protected override void OnStart()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate
        }

        protected override void OnBarClosed()
        {
            volume = 10;
            
            var last_buy_position = Positions.Where(x => x.Label == "Test" && x.SymbolName == Symbol.Name && x.TradeType == TradeType.Buy).OrderByDescending(x => x.EntryTime).FirstOrDefault();
            
            // Handle price updates here
            if(MarketData.GetBars(TimeFrame, "US500").ClosePrices.Last(0) > MarketData.GetBars(TimeFrame, "US500").OpenPrices.Last(0))
               if (last_buy_position == null || Symbol.Ask < last_buy_position.EntryPrice - 100 * Symbol.TickSize)
                   ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volume, "Test", lossCut, takeProfit, (Server.Time - Bars.Last(0).OpenTime).Milliseconds.ToString());            
        }
        protected override void OnTick() {
            foreach(var position in Positions.FindAll("Test", Symbol.Name))
                if(position.StopLoss == null)
                    ModifyPositionAsync(position, Symbol.Ask - 20 * Symbol.PipSize, position.TakeProfit);
                
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

Can you also tell me the broker? This usually happens when your SL falls within the symbol's spread. Please check if this is the case

 


@ys2310

PanagiotisCharalampous
18 Jun 2024, 09:07

RE: RE: RE: RE: RE: Crash in visual mode during backtest and StopLoss not set duing backtest

ys2310 said: 

Hi Panagiotis,

I'm using ICMarkets. How can I check and prevent this using code?

PanagiotisCharalampous said: 

ys2310 said: 

ys2310 said: 

PanagiotisCharalampous said: 

Hi there,

We have received the exception and the issue will be solved in an upcoming release.

Regarding the SL issue, we would need the cBot code and backtesting parameters in order to reproduce this and explain what happens.

Best regards,

Panagiotis

Hi Panagiotis,

Here is the test code that produce the SL issue, I'm using Nikkei225, Hm1 for testing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.FullAccess, TimeZone = TimeZones.EasternStandardTime)]
    public class Test2 : Robot
    {        
        [Parameter(DefaultValue = 10, MinValue = 0, MaxValue = 500, Step = 10)]
        public int lossCut { get; set; }
        
        [Parameter(DefaultValue = 10, MinValue = 0, MaxValue = 500, Step = 10)]
        public int takeProfit { get; set; }
        
        double volume = 0.0;
        
        protected override void OnStart()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate
        }

        protected override void OnBarClosed()
        {
            volume = 10;
            
            var last_buy_position = Positions.Where(x => x.Label == "Test" && x.SymbolName == Symbol.Name && x.TradeType == TradeType.Buy).OrderByDescending(x => x.EntryTime).FirstOrDefault();
            
            // Handle price updates here
            if(MarketData.GetBars(TimeFrame, "US500").ClosePrices.Last(0) > MarketData.GetBars(TimeFrame, "US500").OpenPrices.Last(0))
               if (last_buy_position == null || Symbol.Ask < last_buy_position.EntryPrice - 100 * Symbol.TickSize)
                   ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volume, "Test", lossCut, takeProfit, (Server.Time - Bars.Last(0).OpenTime).Milliseconds.ToString());            
        }
        protected override void OnTick() {
            foreach(var position in Positions.FindAll("Test", Symbol.Name))
                if(position.StopLoss == null)
                    ModifyPositionAsync(position, Symbol.Ask - 20 * Symbol.PipSize, position.TakeProfit);
                
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

Can you also tell me the broker? This usually happens when your SL falls within the symbol's spread. Please check if this is the case

 

Hi there,

You can use Positions.Opened event and check if the stop loss was set. If the stop loss is null you can act accordingly i.e. place it at a valid distance.

Best regards,

Panagiotis


@PanagiotisCharalampous

ys2310
18 Jun 2024, 09:44 ( Updated at: 18 Jun 2024, 14:05 )

RE: RE: RE: RE: RE: RE: Crash in visual mode during backtest and StopLoss not set duing backtest

PanagiotisCharalampous said: 

ys2310 said: 

Hi Panagiotis,

I'm using ICMarkets. How can I check and prevent this using code?

PanagiotisCharalampous said: 

ys2310 said: 

ys2310 said: 

PanagiotisCharalampous said: 

Hi there,

We have received the exception and the issue will be solved in an upcoming release.

Regarding the SL issue, we would need the cBot code and backtesting parameters in order to reproduce this and explain what happens.

Best regards,

Panagiotis

Hi Panagiotis,

Here is the test code that produce the SL issue, I'm using Nikkei225, Hm1 for testing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.FullAccess, TimeZone = TimeZones.EasternStandardTime)]
    public class Test2 : Robot
    {        
        [Parameter(DefaultValue = 10, MinValue = 0, MaxValue = 500, Step = 10)]
        public int lossCut { get; set; }
        
        [Parameter(DefaultValue = 10, MinValue = 0, MaxValue = 500, Step = 10)]
        public int takeProfit { get; set; }
        
        double volume = 0.0;
        
        protected override void OnStart()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate
        }

        protected override void OnBarClosed()
        {
            volume = 10;
            
            var last_buy_position = Positions.Where(x => x.Label == "Test" && x.SymbolName == Symbol.Name && x.TradeType == TradeType.Buy).OrderByDescending(x => x.EntryTime).FirstOrDefault();
            
            // Handle price updates here
            if(MarketData.GetBars(TimeFrame, "US500").ClosePrices.Last(0) > MarketData.GetBars(TimeFrame, "US500").OpenPrices.Last(0))
               if (last_buy_position == null || Symbol.Ask < last_buy_position.EntryPrice - 100 * Symbol.TickSize)
                   ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volume, "Test", lossCut, takeProfit, (Server.Time - Bars.Last(0).OpenTime).Milliseconds.ToString());            
        }
        protected override void OnTick() {
            foreach(var position in Positions.FindAll("Test", Symbol.Name))
                if(position.StopLoss == null)
                    ModifyPositionAsync(position, Symbol.Ask - 20 * Symbol.PipSize, position.TakeProfit);
                
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

Can you also tell me the broker? This usually happens when your SL falls within the symbol's spread. Please check if this is the case

 

Hi there,

You can use Positions.Opened event and check if the stop loss was set. If the stop loss is null you can act accordingly i.e. place it at a valid distance.

Best regards,

Panagiotis

I want not to submit an order if the StopLoss price is invalid. 

so, I wrote a if check statement like below but my order is still without SL (empty SL). Why?

double stopLossPrice = Symbol.Bid - StopLossPips * Symbol.PipSize;
double takeProfitPrice = Symbol.Bid + TakeProfitPips * Symbol.PipSize;

if (stopLossPrice < Symbol.Ask - Symbol.Spread) // Check if stop loss is valid
     ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volume, "Test", StopLossPips, TakeProfitPips, stopLossPrice.ToString()); 


@ys2310