Close positions if pice hit stoploss

Created at 30 Oct 2017, 12:22
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!
DO

doanthedung

Joined 30.10.2017

Close positions if pice hit stoploss
30 Oct 2017, 12:22


Hi bros,

I am a newbie progarmer, i need help to code cbot can close all positios at only special symbol (as EURUSD or GBPUSD...) when bid/ask price > or < constant price i set.

Thanks a lot!


@doanthedung
Replies

PanagiotisCharalampous
01 Nov 2017, 10:31

Hi doanthedung,

Thanks for posting in our forum. Can you be more specific on what kind of help do you need?

Best Regards,

Panagiotis


@PanagiotisCharalampous

doanthedung
01 Nov 2017, 10:38

RE:

Panagiotis Charalampous said:

Hi doanthedung,

Thanks for posting in our forum. Can you be more specific on what kind of help do you need?

Best Regards,

Panagiotis

As I buy EURUSD at 1.2345 and i want to close all positions at EURUSD when bid pirce down 1.2300, like that.


@doanthedung

PanagiotisCharalampous
01 Nov 2017, 11:21

Hi doanthedung,

See an example below on how to close positions when the Symbol's Ask price has gone below a certain level, set by you. 

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 Price { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnTick()
        {
            foreach (var position in Positions)
            {
                if (Symbol.Ask < Price)
                {
                    ClosePosition(position);
                }
            }
        }

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

I hope this helps you develop your algorithm further. Let me know if you need anything else.

Best Regards, 

Panagiotis


@PanagiotisCharalampous

doanthedung
02 Nov 2017, 09:48

RE:

Panagiotis Charalampous said:

Hi doanthedung,

See an example below on how to close positions when the Symbol's Ask price has gone below a certain level, set by you. 

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 Price { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnTick()
        {
            foreach (var position in Positions)
            {
                if (Symbol.Ask < Price)
                {
                    ClosePosition(position);
                }
            }
        }

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

I hope this helps you develop your algorithm further. Let me know if you need anything else.

Best Regards, 

Panagiotis

Thanks! But the code close all positons at all symbols.

I buy EURUSD and GBPUSD, i use this cBot at GBPUSD, when price hit, this code close all positon at EURUSD and GBPUSD. Can you repair code close positon at only symbol use cBot?


@doanthedung

PanagiotisCharalampous
02 Nov 2017, 17:30

Hi doanthedung,

See below

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 Price { get; set; }

        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            foreach (var position in Positions)
            {
                if (Symbol.Code == position.SymbolCode && Symbol.Ask < Price)
                {
                    ClosePosition(position);
                }
            }
        }

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

Best Regards,

Panagiotis


@PanagiotisCharalampous

doanthedung
08 Nov 2017, 18:55

It work, Thank very much!


@doanthedung