ATR StopLoss problem with US30 indice

Created at 10 Nov 2020, 12:10
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!
TR

tradermatrix

Joined 24.07.2012

ATR StopLoss problem with US30 indice
10 Nov 2020, 12:10


Hello
with the example below, the c bot works perfectly with the currency. in optimization and in reality ...
and with indices like US30 it works perfectly with baktest or optimization.
But in reality, when starting up, he rejects the purchase or the sale because of:
10/11/2020 10:34:55.510 | → Order OID0 is REJECTED with error "Relative stop loss has invalid precision"
it is probably a decimal problem.
Do you have a solution and an adapted code for this bug
thanks in advance
cordially.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RSIcBotSLATR : Robot
    {
        [Parameter("RSI.ATR.Label n°", DefaultValue = "777")]
        public string RobotID { get; set; }

        [Parameter("Volume", DefaultValue = 1)]
        public double Volume { get; set; }

        [Parameter("RSI Source")]
        public DataSeries Source { get; set; }

        [Parameter("RSI Periods", DefaultValue = 9)]
        public int Periods { get; set; }

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

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

        [Parameter("ATR Periods", DefaultValue = 14)]
        public int ATRPeriod { get; set; }

        [Parameter("ATR Moving Average Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MovingAverageTypeATR { get; set; }

        [Parameter("SL = ATR multiple", DefaultValue = 2)]
        public double ATRfactor { get; set; }

        [Parameter("TP", DefaultValue = 300)]
        public double TP { get; set; }


        public double SL;

        private RelativeStrengthIndex rsi;
        private AverageTrueRange ATR;

        protected override void OnStart()
        {



            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            ATR = Indicators.AverageTrueRange(ATRPeriod, MovingAverageTypeATR);
        }




        protected override void OnTick()
        {

            var cBotPositions = Positions.FindAll(RobotID);

            if (cBotPositions.Length >= 1)
                return;

            SL = ATR.Result.LastValue * ATRfactor / Symbol.PipSize;

            if (rsi.Result.LastValue < BuyPoint)
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, RobotID, SL, TP);
            }
            else if (rsi.Result.LastValue > SellPoint)
            {

                ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, RobotID, SL, TP);

            }
        }

    }

}

 


@tradermatrix
Replies

PanagiotisCharalampous
10 Nov 2020, 14:13

Hi tradermatrix,

You should round the stop loss value to one decimal place before using it.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

tradermatrix
10 Nov 2020, 14:38

RE:

PanagiotisCharalampous said:

Hi tradermatrix,

You should round the stop loss value to one decimal place before using it.

Best Regards,

Panagiotis 

Join us on Telegram

Thank you for your prompt response.
do you have sample code to help me.
I have already tried several methods to round off but without success.
cordially


@tradermatrix

PanagiotisCharalampous
10 Nov 2020, 16:08

Hi tradermatrix,

Here you go

 SL = Math.Round(ATR.Result.LastValue * ATRfactor / Symbol.PipSize, 1);

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

tradermatrix
10 Nov 2020, 18:48

RE:

PanagiotisCharalampous said:

Hi tradermatrix,

Here you go

 SL = Math.Round(ATR.Result.LastValue * ATRfactor / Symbol.PipSize, 1);

Best Regards,

Panagiotis 

Join us on Telegram

Thank you for you precious help
It works...
cordially


@tradermatrix