QuickStart - Execute market order

Spotware's avatar

Spotware since: 23 Sep 2013;

  14 Jan 2014, 12:59
QuickStart - Execute market order
//  ​The cBot when started will execute an  order, setting Take Profit and Stop Loss.
//  In the event that the position opens it prints a message to the log.
//  In the event that the position closes it prints a message to the log and executes another order.

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot()]
    public class SampleBuyOrder : Robot
    {

        [Parameter("Buy", DefaultValue = true)]
        public bool TradeTypeBuy { get; set; }

        [Parameter(DefaultValue = 10000)]
        public int Volume { get; set; }

        [Parameter(DefaultValue = "myLabel")]
        public string cBotLabel { get; set; }

        
        [Parameter(DefaultValue = 10)]
        public double TakeProfitPips { get; set; }
        
        [Parameter(DefaultValue = 10)]
        public double StopLossPips { get; set; }

        protected TradeType cBotTradeType
        {
            get { return TradeTypeBuy ? TradeType.Buy : TradeType.Sell; }
        }
 
        protected override void OnStart()
        {
            Positions.Opened += OnPositionsOpened;
            Positions.Closed += OnPositionsClosed;

            ExecuteMarketOrder(cBotTradeType, Symbol, Volume, cBotLabel, StopLossPips, TakeProfitPips);                       

        }

        protected void OnPositionsOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;
            if (position.Label == cBotLabel)
                Print("Position opened by cBot");
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            if (position.Label == cBotLabel)
            {
                Print("Position closed by cBot");
                ExecuteMarketOrder(cBotTradeType, Symbol, Volume, cBotLabel, StopLossPips, TakeProfitPips);
            }
        }

    }
}

TRADERS FIRST™ Vote for your favorite features: https://ctrader.com/forum/suggestions
cwik_m's avatar

cwik_m since: 26 Jun 2018;

  29 Jun 2018, 09:15

May I ask what exactly this piece of code does ?

And why its necessary in the above sample cBot code?

Positions.Opened += OnPositionsOpened;
Positions.Closed += OnPositionsClosed;
~ Don't stop until you're proud
PanagiotisCharalampous's avatar

PanagiotisCharalampous since: 13 Jan 2017;

  29 Jun 2018, 10:13

Hi cwik_m,

The above code is just an example. The code assigns event handlers to the specific events. Event handlers are methods called each time the event occurs. In this case, they are called whenever a position opens or closes.

Best Regards,

Panagiotis

Head of Community Management at cTrader

kerrifox19 since: 11 Dec 2019;

  17 Dec 2019, 16:40
RE:

It's a good example, indeed. Thanks.

shugmorris184 since: 30 May 2020;

  30 May 2020, 15:41

Any time a trader seeks to execute a market order, this means the trader is willing to buy at the asking price or sell at the bid price. Thus, the person executing a market order is immediately giving up the bid-ask spread mcdonalds survey code