Supertrend Bot identifying change in direction

Created at 19 Dec 2022, 17:42
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!
DY

dykesn92

Joined 31.03.2020

Supertrend Bot identifying change in direction
19 Dec 2022, 17:42


I am trying to make a bot which uses supertrends change in direction as a confluence but my code seems to work only in 1 direction but also not every time

 

 

 

protected override void  OnBar()
        {
       
      if( supertrend.UpTrend.Last(1)>0  && supertrend.DownTrend.Last(0)>0  )
       {
       Print("uptrend : ", supertrend.UpTrend.Last(1) );
        
       Print("downtrend : ", supertrend.DownTrend.Last(0));
       }
       
       
       else if( supertrend.DownTrend.Last(1)>0  && supertrend.UpTrend.Last(0)>0  )
       {
       Print("uptrend : ", _supertrend.UpTrend.Last(0) );
        
       Print("downtrend : ", _supertrend.DownTrend.Last(1));
       }

 

I can't see anything specifically wrong with this but it doesn't seem to work?

It only tracks when it changes to down trend and only now and again

Can anyone help?
           


@dykesn92
Replies

PanagiotisChar
20 Dec 2022, 09:31

Hi there,

Please share the complete code so that we can help

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

dykesn92
20 Dec 2022, 14:08 ( Updated at: 21 Dec 2023, 09:23 )

Current code with comments on observations and bugs

Thanks so code is below, it is working in parts but not fully like it should. It only opened 4 trades when it's meant to open 50/60

 

Few comments,

 

 I use supertrend.Uptrend.last(1) > 0 as it returns NA when the opposite direction. When I make logic on this though it locks the last(x) part of the function so I found setting this to a variable and starting the loop on the variable bypasses this issue.

I built this code out to contain a martingdale system in the close trade function however removed it as sells stopped opening.

On tick and on candle are the same thing.

I think the issue might be wiith my if functions, I have tried using if and else if as well as nested, functions etc but this code is the only time I have been able to get it to trade in both directions, despite other attempts being more accurate at entering and exiting trades, they all only opened buys. (Don't have these attempts sadly as I removed lots of code to clean it up for debugging and forgot to save.)

 

 

 

 

 

using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
    // This sample cBot shows how to use the Supertrend indicator
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SupertrendSample : Robot
    {
        private double _volumeInUnits;
        private Supertrend _supertrend;
        [Parameter("Volume (Lots)", DefaultValue = 0.01)]
        public double VolumeInLots { get; set; }
        [Parameter("Stop Loss (Pips)", DefaultValue = 10000)]
        public double StopLossInPips { get; set; }
        [Parameter("Take Profit (Pips)", DefaultValue = 10000)]
        public double TakeProfitInPips { get; set; }
        [Parameter("Label", DefaultValue = "SurferBot")]
        public string Label { get; set; }
        [Parameter("INNER_UPPER", DefaultValue = 55,Group ="RSI",Step = 0.5)]
        public double RSI_INNER_UPPER { get; set; }
        [Parameter("INNER_LOWER", DefaultValue = 45,Group ="RSI",Step = 0.5)]
        public double RSI_INNER_LOWER { get; set; }
        [Parameter("OUTER_UPPER", DefaultValue = 70,Group ="RSI",Step = 0.5)]
        public double RSI_OUTER_UPPER { get; set; }
        [Parameter("OUTER_LOWER", DefaultValue = 30,Group ="RSI",Step = 0.5)]
        public double RSI_OUTER_LOWER { get; set; }
        [Parameter("Period", DefaultValue = 14,Group ="RSI")]
        public int RSI_PERIOD { get; set; }
        [Parameter("SL to BE", DefaultValue = 0,Group ="Trade Management")]
        public int BE_PIPS { get; set; }
        [Parameter("Trailing SL", DefaultValue = 0,Group ="Trade Management")]
        public int Trail_SL { get; set; }
        [Parameter("UPTREND", DefaultValue = 0)]
        private int UP_TREND { get; set; }
        [Parameter("DOWNTREND", DefaultValue = 0)]
        private int DOWN_TREND { get; set; }
         [Parameter("Max Trades", DefaultValue = 1)]
        public int MAX_TRADES { get; set; }
        [Parameter("TRADE_COUNT", DefaultValue = 0)]
        private int TRADE_COUNT { get; set; }
        
        

        
        public Position[] BotPositions
        {
            get
            {
                return Positions.FindAll(Label);
            }
        }
        protected override void OnStart()
        {
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
            _supertrend = Indicators.Supertrend(10, 3);
        }
        protected override void  OnBar()
     {

       
       
       if( _supertrend.UpTrend.Last(0)>0)   
       { 
       UP_TREND =1; 
       DOWN_TREND =0; 

       }
          if (UP_TREND ==1) {
       
                     if (_supertrend.DownTrend.Last(1)>0)
          {
           
           Print("downtrend moved to uptrend",_supertrend.DownTrend.Last(1));
            //ClosePositionBoth(); 
             ClosePositions(TradeType.Sell);// close position
             if (TRADE_COUNT < MAX_TRADES )
            {ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            TRADE_COUNT ++ ;} //inc trade count
           }
       }

      if( _supertrend.DownTrend.Last(0)>0)   
       { 
       UP_TREND =0; 
       DOWN_TREND =1; 

       }
          if (DOWN_TREND ==1) {
       
                     if (_supertrend.UpTrend.Last(1)>0)
          {
           
           Print("uptrend moved to downtrend",_supertrend.DownTrend.Last(0));
             ClosePositions(TradeType.Buy);// close position
            if (TRADE_COUNT < MAX_TRADES )
            {ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
             TRADE_COUNT ++ ;} //inc trade count
           }
       }



       
      }   
        
        /*private void ClosePositionBoth()
        {
            foreach (var position in BotPositions)
            {
                if (position.Label != "SurferBot") continue;
                ClosePosition(position);
            }
        }*/
        private void ClosePositions(TradeType tradeType)
        {
            foreach (var position in BotPositions)
            {
                if (position.TradeType != tradeType) continue;
                if (position.Label != "SurferBot") continue;
                ClosePosition(position);
                TRADE_COUNT=0;
                
            }
        }
        
        
    }
}

 

 


@dykesn92

PanagiotisChar
21 Dec 2022, 09:31

Hi there,

Try commenting the lines below

if (_supertrend.DownTrend.Last(1) > 0)
if (_supertrend.UpTrend.Last(1) > 0)

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar