Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.6 kB
0
Indexable
Never
using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SupertrendStrategy : Robot
    {
        private Supertrend supertrend10;
        private Supertrend supertrend11;
        private Supertrend supertrend12;

        private ExponentialMovingAverage ema200;
        private AverageTrueRange atr;
        private DateTime lastTradeTime;

        [Parameter("Supertrend Period (ATR 10)", DefaultValue = 10)]
        public int SupertrendPeriod10 { get; set; }

        [Parameter("Supertrend Multiplier (ATR 10)", DefaultValue = 1.0)]
        public double SupertrendMultiplier10 { get; set; }
        

        [Parameter("Supertrend Period (ATR 11)", DefaultValue = 11)]
        public int SupertrendPeriod11 { get; set; }

        [Parameter("Supertrend Multiplier (ATR 11)", DefaultValue = 2.0)]
        public double SupertrendMultiplier11 { get; set; }
        

        [Parameter("Supertrend Period (ATR 12)", DefaultValue = 12)]
        public int SupertrendPeriod12 { get; set; }

        [Parameter("Supertrend Multiplier (ATR 12)", DefaultValue = 3.0)]
        public double SupertrendMultiplier12 { get; set; }


        [Parameter("EMA 200 Period", DefaultValue = 300)]
        public int EMA200Period { get; set; }

        [Parameter("ATR Period", DefaultValue = 14)]
        public int ATRPeriod { get; set; }

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

        [Parameter("Trade Cooldown (Minutes)", DefaultValue = 360)]
        public int TradeCooldownMinutes { get; set; }

        protected override void OnStart()
        {
            supertrend10 = Indicators.Supertrend(SupertrendPeriod10, SupertrendMultiplier10);
            supertrend11 = Indicators.Supertrend(SupertrendPeriod11, SupertrendMultiplier11);
            supertrend12 = Indicators.Supertrend(SupertrendPeriod12, SupertrendMultiplier12);

            ema200 = Indicators.ExponentialMovingAverage(MarketSeries.Close, EMA200Period);
            atr = Indicators.AverageTrueRange(ATRPeriod, MovingAverageType.Simple);
            lastTradeTime = MarketSeries.OpenTime.LastValue;
        }

        private bool ShouldTradeNow()
        {
            // Check if enough time has passed since the last trade
            return (MarketSeries.OpenTime.LastValue - lastTradeTime).TotalMinutes >= TradeCooldownMinutes;
        }

        protected override void OnBar()
        {
            if (ShouldTradeNow())
            {
                int lastIndex = MarketSeries.Close.Count - 1;

                // Check conditions for Supertrend 10 (ATR 10, Multiplier 1)
                bool isUpTrend10 = supertrend10.UpTrend.IsRising();
                bool isDownTrend10 = supertrend10.DownTrend.IsFalling();

                // Check conditions for Supertrend 11 (ATR 11, Multiplier 2)
                bool isUpTrend11 = supertrend11.UpTrend.IsRising();
                bool isDownTrend11 = supertrend11.DownTrend.IsFalling();

                // Check conditions for Supertrend 12 (ATR 12, Multiplier 3)
                bool isUpTrend12 = supertrend12.UpTrend.IsRising();
                bool isDownTrend12 = supertrend12.DownTrend.IsFalling();

                // Check if the last 10 bars were above SMA for buy trade
                bool isAboveSMA10 = IsAboveSMA(MarketSeries.Close, lastIndex, 20);

                // Check if the last 10 bars were below SMA for sell trade
                bool isBelowSMA10 = IsBelowSMA(MarketSeries.Close, lastIndex, 20);

                // Close buy positions if at least two Supertrends turn red or if the last 10 bars were below SMA
                if (Positions.Count > 0 && ((isDownTrend10 && isDownTrend11) || (isDownTrend10 && isDownTrend12) || (isDownTrend11 && isDownTrend12) || isBelowSMA10))
                {
                    foreach (var position in Positions)
                    {
                        if (position.TradeType == TradeType.Buy)
                            ClosePosition(position);
                    }
                }

                // Close sell positions if at least two Supertrends turn green or if the last 10 bars were above SMA
                if (Positions.Count > 0 && ((isUpTrend10 && isUpTrend11) || (isUpTrend10 && isUpTrend12) || (isUpTrend11 && isUpTrend12) || isAboveSMA10))
                {
                    foreach (var position in Positions)
                    {
                        if (position.TradeType == TradeType.Sell)
                            ClosePosition(position);
                    }
                }

                // Open new positions for buy trade if all Supertrends are rising, and the last 10 bars were above SMA
                if (Positions.Count == 0 &&
                    isUpTrend10 && isUpTrend11 && isUpTrend12 &&
                    isAboveSMA10)
                {
                    // Buy signal (All Supertrends are rising, and the last 10 bars were above SMA)
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.NormalizeVolumeInUnits(Symbol.LotSize), "Buy Order", 15, 30);
                }

                // Open new positions for sell trade if all Supertrends are falling, and the last 10 bars were below SMA
                else if (Positions.Count == 0 &&
                         isDownTrend10 && isDownTrend11 && isDownTrend12 &&
                         isBelowSMA10)
                {
                    // Sell signal (All Supertrends are falling, and the last 10 bars were below SMA)
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, Symbol.NormalizeVolumeInUnits(Symbol.LotSize), "Sell Order", 15, 30);
                }
            }
        }

        private bool IsAboveSMA(DataSeries series, int index, int period)
        {
            for (int i = 0; i < period; i++)
            {
                if (series[index - i] <= ema200.Result[index - i])
                {
                    return false;
                }
            }
            return true;
        }

        private bool IsBelowSMA(DataSeries series, int index, int period)
        {
            for (int i = 0; i < period; i++)
            {
                if (series[index - i] >= ema200.Result[index - i])
                {
                    return false;
                }
            }
            return true;
        }
    }
}