CBOT help needed

Created at 22 Apr 2021, 16:29
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
80

8064102

Joined 18.07.2020

CBOT help needed
22 Apr 2021, 16:29


Could someone please help me with my cbot? Basically on my chart I have set up two indicators:

SMA 1- Simple Moving Average set to 3 periods (source - HIGH)

SMA 2- Simple Moving Average set to 3 periods (source - LOW)

 

CBOT Action:

When price moves 5 pips ABOVE SMA 1 execute a SELL trade

When price moves 5 pips BELOW SMA 2 execute a BUY trade

 

Trade BUY/SELL Execute Settings

Trade Amount 10,000

Stop loss - 5 pips

Take profit - 5 pips

 

Conclusion

I believe I've managed to get the buy trade working but I can't get the sell trade to execute. Also having trouble with the setting to get it to execute the trade when price is 5 pips above SMA 1 or 5 pips below SMA 2.

ANY HELP IS GREATLY APPRECIATED:

 

 

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DoubleSMABotinTick : Robot
    {

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

        [Parameter("SMA1 Period", DefaultValue = 3)]
        public int PeriodsSma1 { get; set; }
        [Parameter("SMA2 Period", DefaultValue = 3)]
        public int PeriodsSma2 { get; set; }

        [Parameter("SMA1 Pips", DefaultValue = 5)]
        public int SMA1_Pips { get; set; }
        [Parameter("SMA2 Pips", DefaultValue = 5)]
        public int SMA2_Pips { get; set; }

        [Parameter("Take Profit", DefaultValue = 5)]
        public int TP { get; set; }
        [Parameter("Stop Loss", DefaultValue = 5)]
        public int SL { get; set; }


        private SimpleMovingAverage _sma1 { get; set; }
        private SimpleMovingAverage _sma2 { get; set; }


        protected override void OnStart()
        {



            _sma1 = Indicators.SimpleMovingAverage(Bars.HighPrices, PeriodsSma1);
            _sma2 = Indicators.SimpleMovingAverage(Bars.LowPrices, PeriodsSma2);
        }

        protected override void OnTick()
        {
            //Print(Symbol.Bid - (SMA1_Pips / 10000));
            if (IsPoOpen() && (Symbol.Ask + (SMA1_Pips / 10000)) < _sma1.Result.LastValue)
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "DSMA", SL, TP);
            }
            //Print(Symbol.Bid - (SMA2_Pips / 10000));
            if (IsPoOpen() && (Symbol.Bid - (SMA2_Pips / 10000)) < _sma2.Result.LastValue)
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "DSMA", SL, TP);
            }
        }

        protected bool IsPoOpen()
        {
            var pos = Positions.FindAll("DSMA", SymbolName);

            if (pos.Length == 0)
            {
                return true;
                Print("Node");
            }
            else
            {
                return false;
                Print(pos);
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}


@8064102
Replies

rosscortb
03 Oct 2024, 14:16 ( Updated at: 04 Oct 2024, 05:17 )

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
   [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
   public class DoubleSMABotinTick : Robot
   {
       [Parameter("Volume", DefaultValue = 10000)]
       public int volume { get; set; }

       [Parameter("SMA1 Period", DefaultValue = 3)]
       public int PeriodsSma1 { get; set; }

       [Parameter("SMA2 Period", DefaultValue = 3)]
       public int PeriodsSma2 { get; set; }

       [Parameter("SMA1 Pips", DefaultValue = 5)]
       public int SMA1_Pips { get; set; }

       [Parameter("SMA2 Pips", DefaultValue = 5)]
       public int SMA2_Pips { get; set; }

       [Parameter("Take Profit", DefaultValue = 5)]
       public int TP { get; set; }

       [Parameter("Stop Loss", DefaultValue = 5)]
       public int SL { get; set; }

       private SimpleMovingAverage _sma1;
       private SimpleMovingAverage _sma2;

       protected override void OnStart()
       {
           _sma1 = Indicators.SimpleMovingAverage(Bars.HighPrices, PeriodsSma1);
           _sma2 = Indicators.SimpleMovingAverage(Bars.LowPrices, PeriodsSma2);
       }

       protected override void OnTick()
       {
           double pipValue = Symbol.PipSize * SMA1_Pips;

           if (IsPoOpen() && Symbol.Bid > _sma1.Result.LastValue + pipValue)
           {
               ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "DSMA", SL, TP);
           }

           if (IsPoOpen() && Symbol.Ask < _sma2.Result.LastValue - pipValue)
           {
               ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "DSMA", SL, TP);
           }
       }

       protected bool IsPoOpen()
       {
           var pos = Positions.FindAll("DSMA", SymbolName);
           return pos.Length == 0;
       }

       protected override void OnStop()
       {
           // Put your deinitialization logic here
       }
   }
}
 


@rosscortb