Supertrend SL and Trail

Created at 14 May 2021, 23:32
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!
FR

freyrthegreat

Joined 26.02.2021

Supertrend SL and Trail
14 May 2021, 23:32


Hi, 

Im trying to build a cBot with my manual strategy while being very new to C++.

Im starting with supertrend first before stacking it with other parameters.

Using the built in sample, ive managed to almost get what i wanted with this indicator.

The purpose is to set the trade SL below/above the supertrend value and continue to trail it.

Ive tried to change the StopLossinPips to _supertrend.Uptrend.LastValue but doesnt seem to work. i did managed to close the trade based a change in direction. but its not ideal as the change in direction will only happen after a candle has finished forming, as the price could move a lot beyond the supertrend. Any help would be appreciated.

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 = 10)]
        public double StopLossInPips { get; set; }
        [Parameter("Take Profit (Pips)", DefaultValue = 10)]
        public double TakeProfitInPips { get; set; }
        [Parameter("Label", DefaultValue = "Sample")]
        public string Label { 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(1) < Bars.LowPrices.Last(1))
            {
                ClosePositions(TradeType.Sell);
            }

            if (_supertrend.DownTrend.Last(1) > Bars.HighPrices.Last(1))
            {
                ClosePositions(TradeType.Buy);
            }
            if (_supertrend.UpTrend.Last(1) < Bars.LowPrices.Last(1) && _supertrend.DownTrend.Last(2) > Bars.HighPrices.Last(2))
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, _supertrend.DownTrend.LastValue, _supertrend.UpTrend.LastValue);
            }
            if (_supertrend.DownTrend.Last(1) > Bars.HighPrices.Last(1) && _supertrend.UpTrend.Last(2) < Bars.LowPrices.Last(2))
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, _supertrend.UpTrend.LastValue, _supertrend.DownTrend.LastValue);
            }
        }
        private void ClosePositions(TradeType tradeType)
        {
            foreach (var position in BotPositions)
            {
                if (position.TradeType != tradeType)
                    continue;
                ClosePosition(position);
            }
        }
    }
}

 

 


@freyrthegreat
Replies

PanagiotisCharalampous
17 May 2021, 08:37

Hi freyrthegreat,

SL and TP should be set in pips. You are using absolute prices instead. You should convert in pips first.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

freyrthegreat
17 May 2021, 08:48

RE:

PanagiotisCharalampous said:

Hi freyrthegreat,

SL and TP should be set in pips. You are using absolute prices instead. You should convert in pips first.

Best Regards,

Panagiotis 

Join us on Telegram

Hi,

 

Apologies. I forgot to remove the SL and TP parameter before posting.

But im looking to use the supertrend value to set SL and trail it. 

 


@freyrthegreat

... Deleted by UFO ...

PanagiotisCharalampous
17 May 2021, 10:43

Hi freyrthegreat,

You can try something like this

               var position = ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, null, null).Position;
                position.ModifyStopLossPrice(Math.Round(_supertrend.DownTrend.LastValue, Symbol.Digits));
                position.ModifyTakeProfitPrice(Math.Round(_supertrend.UpTrend.LastValue, Symbol.Digits));

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

freyrthegreat
18 May 2021, 09:22

RE:

PanagiotisCharalampous said:

Hi freyrthegreat,

You can try something like this

               var position = ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, null, null).Position;
                position.ModifyStopLossPrice(Math.Round(_supertrend.DownTrend.LastValue, Symbol.Digits));
                position.ModifyTakeProfitPrice(Math.Round(_supertrend.UpTrend.LastValue, Symbol.Digits));

Best Regards,

Panagiotis 

Join us on Telegram

Hi panagiotis,

i recompiled i and tested the bot on my demo account. seems like the trade is succesfully opened based on criteria. but it did not open with SL.

and looking at the log there was an error modifying position as well stating "invalidstoplosstakeprofit" and "invalidrequest".

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
    // This sample cBot shows how to use the Supertrend indicator
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Supertrendvar : Robot
    {
        private double _volumeInUnits;
        private Supertrend _supertrend;
        [Parameter("Volume (Lots)", DefaultValue = 0.01)]
        public double VolumeInLots { get; set; }
        [Parameter("Label", DefaultValue = "Sample")]
        public string Label { 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()
        {
xa
            if (_supertrend.UpTrend.Last(1) < Bars.LowPrices.Last(1) && _supertrend.DownTrend.Last(2) > Bars.HighPrices.Last(2))
            {
                var position = ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, null, null).Position;
                position.ModifyStopLossPrice(Math.Round(_supertrend.DownTrend.LastValue, Symbol.Digits));
                position.ModifyTakeProfitPrice(Math.Round(_supertrend.UpTrend.LastValue, Symbol.Digits));
            }
            if (_supertrend.DownTrend.Last(1) > Bars.HighPrices.Last(1) && _supertrend.UpTrend.Last(2) < Bars.LowPrices.Last(2))
            {
                var position = ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, null, null).Position;
                position.ModifyStopLossPrice(Math.Round(_supertrend.UpTrend.LastValue, Symbol.Digits));
                position.ModifyTakeProfitPrice(Math.Round(_supertrend.DownTrend.LastValue, Symbol.Digits));
            }
        }
        private void ClosePositions(TradeType tradeType)
        {
            foreach (var position in BotPositions)
            {
                if (position.TradeType != tradeType)
                    continue;
                ClosePosition(position);
            }
        }
    }
}


@freyrthegreat

PanagiotisCharalampous
18 May 2021, 09:27

Hi freyrthegreat,

You could try this instead

            if (_supertrend.UpTrend.Last(1) < Bars.LowPrices.Last(1) && _supertrend.DownTrend.Last(2) > Bars.HighPrices.Last(2))
            {
                var position = ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, null, null).Position;
                position.ModifyStopLossPrice(Math.Round(_supertrend.UpTrend.LastValue, Symbol.Digits));
            }
            if (_supertrend.DownTrend.Last(1) > Bars.HighPrices.Last(1) && _supertrend.UpTrend.Last(2) < Bars.LowPrices.Last(2))
            {
                var position = ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, null, null).Position;
                position.ModifyStopLossPrice(Math.Round(_supertrend.DownTrend.LastValue, Symbol.Digits));
            }

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

firemyst
22 May 2021, 10:24

Currently, there's known issues with the built in cTrader SuperTrend indicator which might be causing you problems as well.

See this thread:

You might be trying to set a SL on an ST value which doesn't exist.

 


@firemyst

freyrthegreat
25 May 2021, 04:44

RE:

firemyst said:

Currently, there's known issues with the built in cTrader SuperTrend indicator which might be causing you problems as well.

See this thread:

You might be trying to set a SL on an ST value which doesn't exist.

 

that might explain some of the results i got while backtesting where my trade closed at seemingly random position. thanks for the heads up.


@freyrthegreat

PanagiotisCharalampous
25 May 2021, 08:55

Hi firemyst,

Issues with SuperTrend have been resolved

freyrthegreat,

This probably happens because you are using the current bar SuperTrend value, which might change until the bar is closed. Try using Last(1) instead.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous