Automatically ExecuteMarketOrder if no position open

Created at 08 Oct 2016, 15:53
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!
RI

richard_alcaide

Joined 02.11.2015

Automatically ExecuteMarketOrder if no position open
08 Oct 2016, 15:53


Can anyone provide me a code that would let the bot to ExecuteMarketOrder buy or sell if no position currently open.


@richard_alcaide
Replies

... Deleted by UFO ...

richard_alcaide
09 Oct 2016, 09:01

RE:

lucian said:

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 richard : Robot
    {
        [Parameter("Buy ?")]
        public bool buy { get; set; }
        [Parameter("Sell ?")]
        public bool sell { get; set; }
        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnBar()
        {
            if (Positions.Count == 0 && buy)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "Label", null, null, null);
            }
            if (Positions.Count == 0 && sell)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "Label", null, null, null);
            }
        }

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

 

Many thanks Lucian I just backtested it and it works!


@richard_alcaide

richard_alcaide
09 Oct 2016, 09:05

RE:

lucian said:

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 richard : Robot
    {
        [Parameter("Buy ?")]
        public bool buy { get; set; }
        [Parameter("Sell ?")]
        public bool sell { get; set; }
        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnBar()
        {
            if (Positions.Count == 0 && buy)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "Label", null, null, null);
            }
            if (Positions.Count == 0 && sell)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "Label", null, null, null);
            }
        }

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

 

Many thanks Lucian I just backtested it and it works!


@richard_alcaide