Press pause button from bot when visual backtesting

Created at 01 Apr 2021, 11:09
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!
KO

kotas24

Joined 01.04.2021

Press pause button from bot when visual backtesting
01 Apr 2021, 11:09


Hello,

 

Is it possible to "press" pause button programmatically from cbot? What I am looking for is ability to pause receiving events (onTick, OnBar) and still have interactive chart, meaning I would still receive for example OnChartMouseDown event. This behaviour can be achieved by pressing pause button in backtesting window but I need to do this from cbot.

 

Thanks for your reply.

Jiri


@kotas24
Replies

amusleh
01 Apr 2021, 12:18 ( Updated at: 02 Apr 2021, 09:57 )

Hi,

There is no such a feature right now, if you don't want to receive OnTick/Bar events then you can just set a Boolean flag based on your pause condition and ignore the events. 


@amusleh

kotas24
01 Apr 2021, 12:26

RE:

amusleh said:

Hi,

There is no such a feature right now, if you want not receive OnTick/Bar events then you can just set a Boolean flag based on your pause condition and ignore the events. 

Thanks for the answer, but I don't want to lose these events, I want to wait for user action (e.g. mouse click on the chart) and then process onTick/Bar events


@kotas24

amusleh
01 Apr 2021, 12:38

RE: RE:

kotas24 said:

amusleh said:

Hi,

There is no such a feature right now, if you want not receive OnTick/Bar events then you can just set a Boolean flag based on your pause condition and ignore the events. 

Thanks for the answer, but I don't want to lose these events, I want to wait for user action (e.g. mouse click on the chart) and then process onTick/Bar events

You don't have to, just use a boolean flag to disable/enable the events whenever you need.


@amusleh

kotas24
01 Apr 2021, 13:39

RE: RE: RE:

amusleh said:

kotas24 said:

amusleh said:

Hi,

There is no such a feature right now, if you want not receive OnTick/Bar events then you can just set a Boolean flag based on your pause condition and ignore the events. 

Thanks for the answer, but I don't want to lose these events, I want to wait for user action (e.g. mouse click on the chart) and then process onTick/Bar events

You don't have to, just use a boolean flag to disable/enable the events whenever you need.

Sorry I probably missing the point, how can I disable/enable the events?


@kotas24

amusleh
02 Apr 2021, 11:15

This sample might help you:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PauseEvents : Robot
    {
        private bool _isPaused;

        protected override void OnStart()
        {
            Chart.MouseDown += Chart_MouseDown;
        }

        private void Chart_MouseDown(ChartMouseEventArgs obj)
        {
            if (obj.ShiftKey)
            {
                _isPaused = true;

                Print("Events paused");
            }
            else if (obj.CtrlKey)
            {
                _isPaused = false;

                Print("Events running");
            }
        }

        protected override void OnTick()
        {
            if (_isPaused) return;
        }

        protected override void OnBar()
        {
            if (_isPaused) return;
        }
    }
}

If you press shift + click then it will pause the OnTick/Bar, and if you press Ctrl + Click then it will allow the code on OnTick/Bar after is pause check to execute.


@amusleh

SYUNHUA
01 Nov 2021, 08:47

RE:

amusleh said:

This sample might help you:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PauseEvents : Robot
    {
        private bool _isPaused;

        protected override void OnStart()
        {
            Chart.MouseDown += Chart_MouseDown;
        }

        private void Chart_MouseDown(ChartMouseEventArgs obj)
        {
            if (obj.ShiftKey)
            {
                _isPaused = true;

                Print("Events paused");
            }
            else if (obj.CtrlKey)
            {
                _isPaused = false;

                Print("Events running");
            }
        }

        protected override void OnTick()
        {
            if (_isPaused) return;
        }

        protected override void OnBar()
        {
            if (_isPaused) return;
        }
    }
}

If you press shift + click then it will pause the OnTick/Bar, and if you press Ctrl + Click then it will allow the code on OnTick/Bar after is pause check to execute.

it is not working, just printed log, I have deleted onBar or Ontick to test. what else do I need to do


@SYUNHUA

amusleh
02 Nov 2021, 08:14

RE: RE:

SYUNHUA said:

amusleh said:

This sample might help you:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PauseEvents : Robot
    {
        private bool _isPaused;

        protected override void OnStart()
        {
            Chart.MouseDown += Chart_MouseDown;
        }

        private void Chart_MouseDown(ChartMouseEventArgs obj)
        {
            if (obj.ShiftKey)
            {
                _isPaused = true;

                Print("Events paused");
            }
            else if (obj.CtrlKey)
            {
                _isPaused = false;

                Print("Events running");
            }
        }

        protected override void OnTick()
        {
            if (_isPaused) return;
        }

        protected override void OnBar()
        {
            if (_isPaused) return;
        }
    }
}

If you press shift + click then it will pause the OnTick/Bar, and if you press Ctrl + Click then it will allow the code on OnTick/Bar after is pause check to execute.

it is not working, just printed log, I have deleted onBar or Ontick to test. what else do I need to do

Hi,

The above sample only disables OnTick/OnBar, it doesn't pause back tester.


@amusleh

radityo.ardi
10 Nov 2022, 05:40 ( Updated at: 10 Nov 2022, 05:50 )

RE: RE: RE:

 

I think there's no way to pause. Best you can do is to put the below line.

if (IsBacktesting)
{
    throw new Exception("THIS HAS TO STOP");
}

or

if (IsBacktesting)
{
    Print("Stopping here");
    Stop();
}

 


@radityo.ardi

agalbenus
26 Jul 2023, 16:28 ( Updated at: 26 Jul 2023, 16:38 )

The only viable way to pause a cBot runtime during backtesting (mainly visual mode) is using windows forms.

        public void Pause()
        {
            if (!IsDebugEnabled) return;
            Print("[DEBUG] pausing the cBot runtime.");
            var result = System.Windows.Forms.MessageBox.Show("Do you want to continue?", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

            if (result == DialogResult.OK)
            {
                Print("[DEBUG] resuming the cBot runtime.");
            }
            else
            {
                Stop();
            }
        }

You also need to enable windows forms in the dotnet solution 

<Project Sdk="Microsoft.NET.Sdk">
 <PropertyGroup>
   <TargetFramework>net6.0-windows</TargetFramework>
   <UseWindowsForms>true</UseWindowsForms>
 </PropertyGroup>
 <ItemGroup>
   <PackageReference Include="cTrader.Automate" Version="1.*" />
 </ItemGroup>
</Project>

and use dotnet sdk for compiling the cBot instead of embedded compiler. You can do that in cTrader settings→Automate→Select Compiler

 

 


@agalbenus