QuickStart - Labels

Spotware's avatar

Spotware since: 23 Sep 2013;

  15 Jan 2014, 12:37
QuickStart - Labels
// -------------------------------------------------------------------------------------------------
//
//   The Labels Robot demonstrates the use of Label to differentiate positions opened by a robot
//
// -------------------------------------------------------------------------------------------------

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class LabelsSample : Robot
    {
        [Parameter("Buy", DefaultValue = true)]
        public bool TradeTypeBuy { get; set; }

        [Parameter(DefaultValue = "My Label 1")]
        public string MyLabel { get; set; }

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

        [Parameter("Stop Loss (pips)", DefaultValue = 100)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 100)]
        public int TakeProfit { get; set; }
        
        [Parameter(DefaultValue = 2)]
        public int MaxPositions { get; set; }
        
        protected TradeType cBotTradeType
        {
            get { return TradeTypeBuy ? TradeType.Buy : TradeType.Sell; }
        }

        protected override void OnStart()
        {
            Positions.Opened += PositionsOnOpened;
        }
        private void PositionsOnOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;

            Print(
                position.Label == MyLabel
                    ? "Position opened by LabelsSample cBot at {0}"
                    : "Position opened manually at {0}", position.EntryPrice);
        }


        protected override void OnBar()
        {
            if (Positions.Count < MaxPositions)
            {
                ExecuteMarketOrder(cBotTradeType, Symbol, Volume, MyLabel, StopLoss, TakeProfit);
            }
        }

        
    }
}
TRADERS FIRSTâ„¢ Vote for your favorite features: https://ctrader.com/forum/suggestions