support for creating a bot to open a position after 3 consecutive candles of the same color occur

Created at 21 Jan 2025, 16:51
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!
MA

mariolanzetta.cro

Joined 21.01.2025

support for creating a bot to open a position after 3 consecutive candles of the same color occur
21 Jan 2025, 16:51


Hi everyone, I'm creating cBot code to open a trade after 3 consecutive candles of the same color are closed. During the testing phase I find that the operation is opened without respecting the main parameter. Can you help me?


@mariolanzetta.cro
Replies

firemyst
22 Jan 2025, 00:13 ( Updated at: 22 Jan 2025, 10:05 )

You need to post your code so people can see what you're doing and lend suggestions


@firemyst

mariolanzetta.cro
22 Jan 2025, 10:38 ( Updated at: 22 Jan 2025, 13:57 )

RE: support for creating a bot to open a position after 3 consecutive candles of the same color occur

firemyst said: 

You need to post your code so people can see what you're doing and lend suggestions

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class BarOpenedExample : Robot
    {
    
        // Variable to count consecutive bullish candles
    private int bullishCount = 0;
    
            // take profit & stop loss
        private int takeProfitPips = 15; // 15 pips per take profit
        private int stopLossPips = 400;   // 400 pips per stop loss
        
        
    // Perform the operation every time a new candle closes
        protected override void OnBar() 
        {
          // Check if the previous candle is bullish (close > open)
        if (Bars.ClosePrices.Last(1) > Bars.OpenPrices.Last(1))
        {
            bullishCount++; // Increment bullish candlestick counter
        }
        else
        {
            bullishCount = 0;  // Reset the counter if the candle is not bullish
        }
         // When 3 consecutive bullish candles have been detected
        if (bullishCount == 3)
        {
        
                        // Calculate take profit and stop loss levels based on the defined pips
                var stopLossPrice = Symbol.Bid - (stopLossPips * Symbol.PipSize);
                var takeProfitPrice = Symbol.Bid + (takeProfitPips * Symbol.PipSize);
                
                
                ExecuteMarketOrder(TradeType.Buy, SymbolName, 3);
            }
            bullishCount = 0;  // Reset the counter to avoid opening multiple orders


        }
    }
}


@mariolanzetta.cro