Untitled

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ImprovedRSIcBot : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [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; }

        private Supertrend supertrend10;
        private double maxSpread = 0.31; // Maximum allowed spread

        public Supertrend supertrend;
        public AverageTrueRange atr;

        protected override void OnStart()
        {
            supertrend = Indicators.Supertrend(10, 3);
            atr = Indicators.AverageTrueRange(10, MovingAverageType.WilderSmoothing);
            supertrend10 = Indicators.Supertrend(SupertrendPeriod10, SupertrendMultiplier10);
        }

        protected override void OnTick()
        {
            // Check if an open trade exists
            if (Positions.Count > 0)
            {
                // Check if the current spread exceeds the maximum allowed spread
                if (Symbol.Spread > maxSpread)
                {
                    // Close the open trade
                    foreach (var position in Positions)
                    {
                        ClosePosition(position);
                        Print("Closed position due to high spread.");
                    }
                }
            }

            // Your existing trading logic here...
            double stop = 0.5;

            // Check conditions for Supertrend 10 (ATR 10, Multiplier 1)
            bool isUpTrend10 = supertrend10.UpTrend.IsRising();
            double minATR = 0.0001; // Set your desired minimum ATR value
            int lastIndex = MarketSeries.Close.Count - 1;
            bool hasMinATR = atr.Result[lastIndex] > minATR;

            if (Symbol.Spread <= maxSpread && supertrend.UpTrend.IsRising() && Positions.Count == 0 && isUpTrend10 && hasMinATR)
            {
                foreach (var order in PendingOrders)
                {
                    CancelPendingOrder(order);
                }

                // Calculate stop-loss and take-profit levels here
                double entryPrice = MarketSeries.Open[lastIndex];
                double stopLoss = entryPrice - (stop * Symbol.PipSize);
                double takeProfit = entryPrice + (2 * stop * Symbol.PipSize);

                PlaceLimitOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(Quantity * 10), supertrend.UpTrend.LastValue, "Buy", stopLoss, takeProfit, null, null, true);
            }
        }
    }
}