How to stop cBot?

Created at 28 Apr 2015, 14:20
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!
HI

hiba7rain

Joined 20.07.2014

How to stop cBot?
28 Apr 2015, 14:20


Hi

Can any one help to show How to stop cBot after X number of trades or X number of profit pips?

 

thanks


@hiba7rain
Replies

hiba7rain
04 May 2015, 16:17

RE:

?hiba7rain said:

Hi

Can any one help to show How to stop cBot after X number of trades or X number of profit pips?

 

thanks

 


@hiba7rain

mindbreaker
04 May 2015, 17:22

RE: RE:

hiba7rain said:

?hiba7rain said:

Hi

Can any one help to show How to stop cBot after X number of trades or X number of profit pips?

 

thanks

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }


        int tradeStart = 0;
        int tradeCurrent = 0;

        protected override void OnStart()
        {
            // Put your initialization logic here
            foreach (var trade in History)
            {
                tradeStart++;
            }
        }

        protected override void OnTick()
        {
            // colose id

            foreach (var trade in History)
            {
                foreach (var trade in History)
                {
                    tradeCurrent++;
                }

            }
            
            if(tradeCurrent - tradeStart >= 10){
            Print("Close bot after 10 closed positions")
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

after 10 trade closed


@mindbreaker

mindbreaker
04 May 2015, 17:23

RE: RE: RE:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }


        int tradeStart = 0;
        int tradeCurrent = 0;

        protected override void OnStart()
        {
            // Put your initialization logic here
            foreach (var trade in History)
            {
                tradeStart++;
            }
        }

        protected override void OnTick()
        {
            // colose id

            foreach (var trade in History)
            {
                tradeCurrent++;
            }

            if (tradeCurrent - tradeStart >= 10)
            {
                Print("Close bot after 10 closed positions");
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 


@mindbreaker

mindbreaker
04 May 2015, 17:30

RE: RE: RE: RE:

Pips count and stop:

            // profit pips
            double pips = 0;
            foreach (var trade in Positions)
            {
                pips = pips + trade.Pips;
            }
            if (pips > 100)
            {
                Stop();
            }

 


@mindbreaker

mindbreaker
04 May 2015, 17:32

RE: RE: RE: RE: RE:

and when stop:

        protected override void OnStop()
        {
            // Put your deinitialization logic here
            Print("Bot was stoped!");
        }

 


@mindbreaker

hiba7rain
04 May 2015, 21:48

RE: RE: RE: RE: RE: RE:

many thanks to you mindbreaker

it works with me except that the cbot stopped after 2 treads instead of 10 as set i dont know why :)

ill check it again 

do you have a skype ?

mindbreaker said:

and when stop:

        protected override void OnStop()
        {
            // Put your deinitialization logic here
            Print("Bot was stoped!");
        }

 

 


@hiba7rain

mindbreaker
04 May 2015, 22:36

i dont test it :) I dont have skype.
@mindbreaker

hiba7rain
05 May 2015, 21:19

RE:

thanks again :)

so do you develop cbots and indicators ?

mindbreaker said:

i dont test it :) I dont have skype.

 


@hiba7rain

mindbreaker
06 May 2015, 11:22

RE: RE:

This post was removed by moderator.


@mindbreaker

gainer
12 Feb 2016, 14:11

RE: RE: RE: RE: RE:

mindbreaker said:

Pips count and stop:

            // profit pips
            double pips = 0;
            foreach (var trade in Positions)
            {
                pips = pips + trade.Pips;
            }
            if (pips > 100)
            {
                Stop();
            }

I used the Stop(); in some of my cBots and I noticed that the better way to obtain an immediate break of the execution is to add also a return; after the stop();

in my case a check to avoid to use live accounts my mistake:

remember to add a reference to "System.Windows.Forms"
-----

protected override void OnStart()
{
    if (Account.IsLive && System.Windows.Forms.MessageBox.Show("WARNING!!! You are on a LIVE ACCOUNT !!!\r\n\r\nDo you want to continue anyway ?", label, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.No)
    {
        Stop();
        return;
    }
}

 


@gainer

gainer
12 Feb 2016, 14:58

RE: RE: RE: RE: RE: RE:

gainer said:

mindbreaker said:

Pips count and stop:

            // profit pips
            double pips = 0;
            foreach (var trade in Positions)
            {
                pips = pips + trade.Pips;
            }
            if (pips > 100)
            {
                Stop();
            }

I used the Stop(); in some of my cBots and I noticed that the better way to obtain an immediate break of the execution is to add also a return; after the stop();

in my case a check to avoid to use live accounts my mistake:

remember to add a reference to "System.Windows.Forms"
-----

protected override void OnStart()
{
    if (Account.IsLive && System.Windows.Forms.MessageBox.Show("WARNING!!! You are on a LIVE ACCOUNT !!!\r\n\r\nDo you want to continue anyway ?", label, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.No)
    {
        Stop();
        return;
    }
}

 

by the way... I had to use the full declaration "System.Windows.Forms" in the code, to avoid a conflict with another DLL, but the best way to use it is of course this

1) add the reference to "System.Windows.Forms" to your cBot so you can declare the "using"

-----

...

using System.Windows.Forms;

...


if (Account.IsLive && MessageBox.Show("WARNING!!! You are on a LIVE ACCOUNT!!!\r\n\r\nDo you want to continue anyway?", label, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
{
    Stop();
    return;
}

...

 


@gainer

hiba7rain
15 Feb 2016, 14:10

RE: RE: RE: RE: RE: RE: RE:

Thanks Gainer

i will use return after stop and check it again

gainer said:

gainer said:

mindbreaker said:

Pips count and stop:

            // profit pips
            double pips = 0;
            foreach (var trade in Positions)
            {
                pips = pips + trade.Pips;
            }
            if (pips > 100)
            {
                Stop();
            }

I used the Stop(); in some of my cBots and I noticed that the better way to obtain an immediate break of the execution is to add also a return; after the stop();

in my case a check to avoid to use live accounts my mistake:

remember to add a reference to "System.Windows.Forms"
-----

protected override void OnStart()
{
    if (Account.IsLive && System.Windows.Forms.MessageBox.Show("WARNING!!! You are on a LIVE ACCOUNT !!!\r\n\r\nDo you want to continue anyway ?", label, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.No)
    {
        Stop();
        return;
    }
}

 

by the way... I had to use the full declaration "System.Windows.Forms" in the code, to avoid a conflict with another DLL, but the best way to use it is of course this

1) add the reference to "System.Windows.Forms" to your cBot so you can declare the "using"

-----

...

using System.Windows.Forms;

...


if (Account.IsLive && MessageBox.Show("WARNING!!! You are on a LIVE ACCOUNT!!!\r\n\r\nDo you want to continue anyway?", label, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
{
    Stop();
    return;
}

...

 

 


@hiba7rain

gainer
16 Feb 2016, 15:23

RE: RE: RE: RE: RE: RE: RE: RE:

hiba7rain said:

Thanks Gainer

i will use return after stop and check it again

 

You're the welcome!

If the "Stop()" in your case doesn't work well either, you can call directly the c# command:

  1. add the "using System.Diagnostics" reference
  2. call the c# Process.GetCurrentProcess().Close();
using System;
using System.Diagnostics; // <---- ADD THIS
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        int ticks = 0;

        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            Print(ticks++);

            if (ticks == 10)
            {
                Process.GetCurrentProcess().Close();
            }
        }

        protected override void OnStop()
        {

        }
    }
}

 


@gainer

hiba7rain
16 Feb 2016, 16:28

RE: RE: RE: RE: RE: RE: RE: RE: RE:

Thanks Gainer, as always you are very helpful 

i should test the codes 

by the way are experience with array, as you can see am trying to have array code to compare the values of two points say last two high or low points

thanks

 

gainer said:

hiba7rain said:

Thanks Gainer

i will use return after stop and check it again

 

You're the welcome!

If the "Stop()" in your case doesn't work well either, you can call directly the c# command:

  1. add the "using System.Diagnostics" reference
  2. call the c# Process.GetCurrentProcess().Close();
using System;
using System.Diagnostics; // <---- ADD THIS
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        int ticks = 0;

        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            Print(ticks++);

            if (ticks == 10)
            {
                Process.GetCurrentProcess().Close();
            }
        }

        protected override void OnStop()
        {

        }
    }
}

 

 


@hiba7rain

firemyst
02 Apr 2019, 09:03

Does stopping a bot close all open positions?

Hi everyone:

Reading this thread and for clarification -- does stopping a bot close all open positions and pending orders?

Or (most likely) do they have to be programmatically closed in the OnStop method since the orders/positions are on the server?

Thank you


@firemyst

PanagiotisCharalampous
02 Apr 2019, 12:22

Hi FireMyst.

No it does not. You need to close them programmaticaly if you wish to do so.

Best Regards,

Panagiotis


@PanagiotisCharalampous

firemyst
03 Apr 2019, 02:57

RE:

Panagiotis Charalampous said:

Hi FireMyst.

No it does not. You need to close them programmaticaly if you wish to do so.

Best Regards,

Panagiotis

 

Thank you for the confirmation. :-)


@firemyst