Untitled

 avatar
unknown
plain_text
2 years ago
2.4 kB
3
Indexable
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class BollingerBandsRobot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", DefaultValue = 20)]
        public int Periods { get; set; }

        [Parameter("Standard Deviations", DefaultValue = 2)]
        public double StandardDeviations { get; set; }

        [Parameter("Volume", DefaultValue = 10000)]
        public double Volume { get; set; }

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

        [Parameter("Label")]
        public string Label { get; set; }

        private BollingerBands _bbands;
        private Position _position;

        protected override void OnStart()
        {
            _bbands = Indicators.BollingerBands(Source, Periods, StandardDeviations, MovingAverageType.Simple);

            Positions.Closed += OnPositionClosed;

            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, Label, StopLossPips, null);
        }

        protected override void OnTick()
        {
            if (_position == null)
                return;

            if (_position.TradeType == TradeType.Buy && Symbol.Bid >= _bbands.Top.Last(1))
            {
                ClosePosition(_position);
            }
            else if (_position.TradeType == TradeType.Sell && Symbol.Ask <= _bbands.Bottom.Last(1))
            {
                ClosePosition(_position);
            }
        }

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            _position = null;

            if (args.Position.Label == Label)
            {
                ExecuteMarketOrder(args.Position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy, Symbol, Volume, Label, StopLossPips, null);
            }
        }

        public override void CalculateCommission(string symbolName, OrderType orderType, double volumeInUnits, double volumeInLots, double openPrice, double closePrice, out double commissionInUnits, out double commissionInQuoteCurrency)
        {
            commissionInUnits = 0.0;
            commissionInQuoteCurrency = (volumeInLots * volumeInUnits * (Symbol.TickSize / 10)) * 2;
        }
    }
}
Editor is loading...