Why isn't this scalping working in TradingView but not Ctrader?

Created at 19 Dec 2022, 10:04
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!
AS

ashish.sah.np

Joined 19.12.2022

Why isn't this scalping working in TradingView but not Ctrader?
19 Dec 2022, 10:04


I coded the following automated strategy in cTrader, but it doesn't seem to open any positions while backtesting. But the same logic is working fine in TradingView. Am I doing something wrong here, or am I missing something? Any community help will be really helpful for me as i have just started using the cTrdader and C# language.

 


using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleEmacBot : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Source", Group = "EMA")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", Group = "EMA", DefaultValue = 5)]
        public int Periods { get; set; }

        private ExponentialMovingAverage ema;

        protected override void OnStart()
        {
            ema = Indicators.ExponentialMovingAverage(Source, Periods);
        }
        protected override void OnTick()
        {
            var currentCandleClose = Bars.ClosePrices.Last(0);
            var currentCandleOpen = Bars.OpenPrices.Last(0);
            var previousCandleHigh = Bars.HighPrices.Last(1); 
            var previousCandleLow = Bars.LowPrices.Last(1);
            var emaValue = ema.Result.Last(0);
            var StopLossBuy = previousCandleLow;
            var TakeProfitBuy = previousCandleLow*3;
            var StopLossSell = previousCandleHigh;
            var TakeProfitSell = previousCandleHigh*3;

            if (((previousCandleHigh < emaValue )&& (currentCandleClose < emaValue)) && (currentCandleOpen <= currentCandleClose) )
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, Quantity, "Buy", StopLossBuy, TakeProfitBuy);
            }

            if (((previousCandleLow > emaValue) && (currentCandleClose > emaValue)) && (currentCandleOpen >= currentCandleClose))
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, Quantity, "Sell", StopLossSell, TakeProfitSell);
            }
        }
    }
}

 


@ashish.sah.np
Replies

PanagiotisChar
20 Dec 2022, 09:17

Hi there,

Here is your problem

You are passing the volume as quantity but the input should be in units. See below

ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.NormalizeVolumeInUnits(Quantity), "Buy", StopLossBuy, TakeProfitBuy);

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

ashish.sah.np
20 Dec 2022, 15:37 ( Updated at: 21 Dec 2023, 09:23 )

RE:

PanagiotisChar said:

Hi there,

Here is your problem

You are passing the volume as quantity but the input should be in units. See below

ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.NormalizeVolumeInUnits(Quantity), "Buy", StopLossBuy, TakeProfitBuy);

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

 

Hello Thanks for the support. Really appreciate.

Could you please assist me in setting the previous high as a stop loss by converting the price difference in pips correctly? I have tried with the below code, but in the positions tab, T/P and S/L are showing blank for some trades while the majority of trades are closed, so I'm wondering if my below method is correct or not.

And the way I am taking the EMA value, is it correct? This will be a big help for me. Thanks in advance. May God bless your trading portfolio.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleEmacBot : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Source", Group = "EMA")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", Group = "EMA", DefaultValue = 5)]
        public int Periods { get; set; }

        private ExponentialMovingAverage ema;

        protected override void OnStart()
        {
            ema = Indicators.ExponentialMovingAverage(Source, Periods);
        }
        protected override void OnBar()
        {
            var currentCandleClose = Bars.ClosePrices.Last(1);
            var currentCandleOpen = Bars.OpenPrices.Last(1);
            var previousCandleHigh = Bars.HighPrices.Last(2); 
            var previousCandleLow = Bars.LowPrices.Last(2);
            var StopLossBuy = (Math.Abs(previousCandleLow - Bars.OpenPrices.Last(0)))/Symbol.PipSize;
            var TakeProfitBuy = ((Math.Abs(previousCandleLow - Bars.OpenPrices.Last(0)))/Symbol.PipSize)*4;
            var StopLossSell = (Math.Abs(previousCandleHigh - Bars.OpenPrices.Last(0)))/Symbol.PipSize;
            var TakeProfitSell =((Math.Abs(previousCandleHigh- Bars.OpenPrices.Last(0)))/Symbol.PipSize)*4;
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);

            if (((previousCandleHigh < ema.Result.Last(2) )&& (currentCandleClose < ema.Result.Last(1))) && (Bars.OpenPrices.Last(0) <= currentCandleClose) )
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "Buy", StopLossBuy, TakeProfitBuy);
            }

            if (((previousCandleLow > ema.Result.Last(2)) && (currentCandleClose > ema.Result.Last(1))) && (Bars.OpenPrices.Last(0) >= currentCandleClose))
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, "Sell", StopLossSell, TakeProfitSell);
            }
        }
    }
}

@ashish.sah.np

PanagiotisChar
21 Dec 2022, 09:40

Hi again,

Make sure your SL is set outside the spread. Else it won't be placed.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

ashish.sah.np
21 Dec 2022, 16:53

RE:

PanagiotisChar said:

Hi again,

Make sure your SL is set outside the spread. Else it won't be placed.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

Is there anyway to check whether SL and TP are inside the spread and to excuse such trades?


@ashish.sah.np