Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.2 kB
0
Indexable
Never
using cAlgo.API;
using cAlgo.API.Indicators;
using System;
using cAlgo.API.Internals;
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ImprovedRSIcBot : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.00000000)]
        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;
        
        [Parameter("Maximum Allowed Spread", DefaultValue = 3, MinValue = 0.0)]
        public int MaxSpreadInPips { get; set; }

            
        public double MaxSpread
        {
            get { return MaxSpreadInPips * Symbol.PipSize; }
        }

        public Supertrend supertrend;
        public AverageTrueRange atr;
        public StochasticOscillator stochastic;

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

   protected override void OnTick()
   {
   
    if (Server.Time.Hour < 7 || Server.Time.Hour >= 17)
            {
                Print("Trading is disabled during non-trading hours");
                foreach (var position in Positions)
                    ClosePosition(position);
                foreach (var order in PendingOrders)
                {
                    CancelPendingOrder(order);
                }
                return;
            }
    double currentSpread = Symbol.Spread;
  

    int lastIndex = MarketSeries.Close.Count - 1;

    
   foreach (var order in PendingOrders)
    {
        if (currentSpread > 0.3)
        {

                CancelPendingOrder(order);
          
        }
    }


    bool isUpTrend10 = supertrend10.UpTrend.IsRising();
      bool isDownTrend10 = supertrend10.UpTrend.IsFalling();
    double minATR = 0.0001; 
    bool hasMinATR = atr.Result[lastIndex] > minATR;


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

            PlaceLimitOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(Quantity * 10), supertrend.UpTrend.LastValue, "Buy", 10, 1000, null, null, true);
        
      }
      else if (  supertrend.UpTrend.IsFalling() && Positions.Count == 0  && isDownTrend10 && hasMinATR)
    {
        foreach (var order in PendingOrders)
        {
            CancelPendingOrder(order);
        }

            PlaceLimitOrder(TradeType.Sell, SymbolName, Symbol.QuantityToVolumeInUnits(Quantity * 10), supertrend.UpTrend.LastValue, "Sell", 10, 1000, null, null, true);

        
      }
    }
  }
}