Untitled

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class EURUSDScalpingBot : Robot
    {
        [Parameter("RSI Period", DefaultValue = 15)]
        public int RSIPeriod { get; set; }

        [Parameter("RSI Overbought Level", DefaultValue = 70)]
        public int RSIOverbought { get; set; }

        [Parameter("RSI Oversold Level", DefaultValue = 30)]
        public int RSIOversold { get; set; }

        [Parameter("SMA Period", DefaultValue = 45)]
        public int SMAPeriod { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 5)]
        public double TakeProfitPips { get; set; }

        [Parameter("Minimum Profit (pips)", DefaultValue = 5)]
        public double MinimumProfitPips { get; set; }

        [Parameter("Position Close Range (pips)", DefaultValue = 2)]
        public double PositionCloseRangePips { get; set; }

        [Parameter("Max Loss Percentage", DefaultValue = 1)]
        public double MaxLossPercentage { get; set; }

        private RelativeStrengthIndex _rsi;
        private MovingAverage _sma;

        protected override void OnStart()
        {
            _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RSIPeriod);
            _sma = Indicators.SimpleMovingAverage(Bars.ClosePrices, SMAPeriod);
        }

        protected override void OnTick()
        {
            double rsiValue = _rsi.Result.LastValue;
            double smaValue = _sma.Result.LastValue;

            foreach (var position in Positions)
            {
                if (position.Label.Contains("RSI"))
                {
                    double currentProfitPips = Symbol.PipValue * (position.EntryPrice - Bars.ClosePrices.Last(0));

                    if (position.TradeType == TradeType.Sell)
                    {
                        double takeProfitPrice = position.EntryPrice - Symbol.PipSize * TakeProfitPips * 2;
                        double stopLossPrice = CalculateStopLossForSell();

                        if (currentProfitPips >= MinimumProfitPips)
                        {
                            ModifyPosition(position, takeProfitPrice, stopLossPrice);
                        }

                        if (Math.Abs(position.EntryPrice - Symbol.Bid) <= Symbol.PipSize * PositionCloseRangePips)
                        {
                            ClosePosition(position);
                        }
                    }
                    else if (position.TradeType == TradeType.Buy)
                    {
                        double takeProfitPrice = position.EntryPrice + Symbol.PipSize * TakeProfitPips * 2;
                        double stopLossPrice = CalculateStopLossForBuy();

                        if (currentProfitPips >= MinimumProfitPips)
                        {
                            ModifyPosition(position, takeProfitPrice, stopLossPrice);
                        }

                        if (Math.Abs(position.EntryPrice - Symbol.Ask) <= Symbol.PipSize * PositionCloseRangePips)
                        {
                            ClosePosition(position);
                        }
                    }
                }
            }

            // Check for open trades with excessive loss
            double maxLossAmount = Account.Balance * MaxLossPercentage / 100.0;
            foreach (var position in Positions)
            {
                if (position.Pips * Symbol.PipValue < -maxLossAmount)
                {
                    ClosePosition(position);
                }
            }

            if (Time.Hour == 14 && Time.Minute == 30)
            {
                CloseAllPositions();
            }

            if (rsiValue > RSIOverbought && Bars.OpenPrices.Last(1) > smaValue && Bars.OpenPrices.Last(2) < smaValue)
            {
                ExecuteSell();
            }
            else if (rsiValue < RSIOversold && Bars.OpenPrices.Last(1) < smaValue && Bars.OpenPrices.Last(2) > smaValue)
            {
                ExecuteBuy();
            }
        }

        private void ExecuteSell()
        {
            double takeProfitPrice = Symbol.Bid - Symbol.PipSize * TakeProfitPips * 2;
            double stopLossPrice = CalculateStopLossForSell();
            TradeResult result = ExecuteMarketOrder(TradeType.Sell, Symbol, Symbol.NormalizeVolumeInUnits(Symbol.LotSize), "Sell Order", null, stopLossPrice, takeProfitPrice, "RSI Sell");
            if (result.IsSuccessful)
            {
                Print("Sell order executed at price: " + result.Position.EntryPrice);
            }
            else
            {
                Print("Failed to execute sell order. Error: " + result.Error);
            }
        }

        private void ExecuteBuy()
        {
            double takeProfitPrice = Symbol.Ask + Symbol.PipSize * TakeProfitPips * 2;
            double stopLossPrice = CalculateStopLossForBuy();
            TradeResult result = ExecuteMarketOrder(TradeType.Buy, Symbol, Symbol.NormalizeVolumeInUnits(Symbol.LotSize), "Buy Order", null, stopLossPrice, takeProfitPrice, "RSI Buy");
            if (result.IsSuccessful)
            {
                Print("Buy order executed at price: " + result.Position.EntryPrice);
            }
            else
            {
                Print("Failed to execute buy order. Error: " + result.Error);
            }
        }

        private double CalculateStopLossForSell()
        {
            double lowestLow = double.MaxValue;
            for (int i = Bars.LowPrices.Count - 1; i >= 0; i--)
            {
                if (Bars.LowPrices[i] < lowestLow)
                {
                    lowestLow = Bars.LowPrices[i];
                }
            }
            return lowestLow - Symbol.PipSize * 3; // Adjust the multiplier as needed
        }

        private double CalculateStopLossForBuy()
        {
            double highestHigh = double.MinValue;
            for (int i = Bars.HighPrices.Count - 1; i >= 0; i--)
            {
                if (Bars.HighPrices[i] > highestHigh)
                {
                    highestHigh = Bars.HighPrices[i];
                }
            }
            return highestHigh + Symbol.PipSize * 3; // Adjust the multiplier as needed
        }

        private void CloseAllPositions()
        {
            foreach (var position in Positions)
            {
                ClosePosition(position);
            }
        }

        private void ModifyPosition(Position position, double takeProfitPrice, double stopLossPrice)
        {
            if (position.StopLoss != stopLossPrice || position.TakeProfit != takeProfitPrice)
            {
                ClosePosition(position);

                if (position.TradeType == TradeType.Buy)
                {
                    ExecuteBuy();
                }
                else if (position.TradeType == TradeType.Sell)
                {
                    ExecuteSell();
                }
            }
        }
    }
}