Getting unusual trades while: Trying to open trade in each change in candle colour.

Created at 25 Nov 2023, 16:20
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

Getting unusual trades while: Trying to open trade in each change in candle colour.
25 Nov 2023, 16:20


I'm trying to take trade When there is that change in color of the candle. Although the code seems 2 BHK to be working alright but sometimes it is not closing the previous trade and opening a new trade. Even there is the change in colour of candle Can someone suggest me What is being wrong here? It will be a great help .

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.IndiaStandardTime, AccessRights = AccessRights.FullAccess)]
    public class TradeOnCandleColor : Robot
    {
        [Parameter("Take Profit (pips)", DefaultValue = 0, MinValue = 0)]
        public int TakeProfit { get; set; }

        [Parameter("Volume Lot", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Volume { get; set; }
        private bool _buy = true;
        private bool _sell = true;
        protected override void OnStart(){}
        protected override void OnBar()
        {
            if (Bars.ClosePrices.Last(0) > Bars.OpenPrices.Last(0) && _buy)
            {
                foreach(var position in Positions) 
                {
                    ClosePosition(position);               
                }
                _buy = false;
                _sell = true;
                if (IsWithinTradingHours())
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(Volume), "CC",null ,TakeProfit);
             }
             else if (Bars.ClosePrices.Last(0) < Bars.OpenPrices.Last(0) && _sell)
             {
                foreach(var position in Positions) 
                {
                  ClosePosition(position);                       
                }
                _sell = false;
                _buy = true;
                if (IsWithinTradingHours())
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, Symbol.QuantityToVolumeInUnits(Volume), "CC", null,TakeProfit);
             }
         }
            // Check if the current time is within the trading hours
         private bool IsWithinTradingHours()
        {
            float h = Bars.OpenTimes.LastValue.Hour;
            float m = Bars.OpenTimes.LastValue.Minute;
            return h+m/60 >= 9.5 && h+m/60  <= 15.5;
        }
    }
}

Below is a screenshot of cbot working alright.

 

Below is the code working not as expected as you can see, sell trade should be closed just after one candle but it is not getting close there, rather than it is getting closed after a long time.

 

 


@ashish.sah.np
Replies

PanagiotisCharalampous
26 Nov 2023, 07:39

Reply to: Getting unusual trades while: Trying to open trade in each change in candle colour.

Hi there,

You should implement your trading logic using the last closed bar and not the current bar i.e. use Last(1) and not Last(0). Usually on the opening of the bar both close and open prices are the same.

Best regards,

Panagiotis


@PanagiotisCharalampous