Inside bar bot doesnt work

Created at 01 Jun 2021, 19:15
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!
CT

ctid3179522

Joined 21.01.2021

Inside bar bot doesnt work
01 Jun 2021, 19:15


     Hello, I tried to write a simple inside bar robot but for some reason it doesnt open as many trades as it should or none. What could be the problem?

 

 

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class InsideBarPatternbot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Parameter("volume", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double volume { get; set; }

           

        protected override void OnStart()
        {
            // Put your initialization logic here
        }


        protected override void OnTick()
        {
            

            if (Positions.Count == 0 && (Bars.ClosePrices.Last(2) - Bars.OpenPrices.Last(2)) > 0 & Bars.HighPrices.Last(2) > Bars.HighPrices.Last(1) & Bars.LowPrices.Last(2) < Bars.LowPrices.Last(1) & Symbol.Bid == Bars.HighPrices.Last(2))
            {

                


                var result = ExecuteMarketOrder(TradeType.Buy, SymbolName, 100000, "1", 10, 10);
                if (result.IsSuccessful)
                {
                    var position = result.Position;

                  


                    var sl = Bars.HighPrices.Last(2) - (0.4 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)));
                    var tp = Bars.HighPrices.Last(2) + (0.8 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)));

                    ModifyPosition(position, sl, tp);

               

                }
            }

            


            if (Positions.Count == 0 & (Bars.ClosePrices.Last(2) - Bars.OpenPrices.Last(2)) < 0 & Bars.HighPrices.Last(2) > Bars.HighPrices.Last(1) & Bars.LowPrices.Last(2) < Bars.LowPrices.Last(1) & Symbol.Bid == Bars.LowPrices.Last(2))
            {

var result = ExecuteMarketOrder(TradeType.Sell, SymbolName, 100000, "1", 10, 10);
                if (result.IsSuccessful)
                {
                    var position = result.Position;


                    var sl = Bars.LowPrices.Last(2) + (0.4 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)));
                    var tp = Bars.LowPrices.Last(2) - (0.8 * (Bars.HighPrices.Last(2) - Bars.LowPrices.Last(2)));


                    ModifyPosition(position, sl, tp);
                }
            }
        }


        
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}
 


@ctid3179522
Replies

PanagiotisCharalampous
02 Jun 2021, 08:09

Hi ctid3179522,

You need to be a bit more specific. What is the exact functionality you are trying to code? Where would you expect trades to open and they don't? 

Looking at the following conditions

Symbol.Bid == Bars.HighPrices.Last(2)

Symbol.Bid == Bars.LowPrices.Last(2)

 These will rarely be true.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

ctid3179522
02 Jun 2021, 08:59 ( Updated at: 21 Dec 2023, 09:22 )

RE:

PanagiotisCharalampous said:

Hi ctid3179522,

You need to be a bit more specific. What is the exact functionality you are trying to code? Where would you expect trades to open and they don't? 

Looking at the following conditions

Symbol.Bid == Bars.HighPrices.Last(2)

Symbol.Bid == Bars.LowPrices.Last(2)

 These will rarely be true.

Best Regards,

Panagiotis 

Join us on Telegram

 The 1st candle must contain the 2nd candle(1st high-low range contains de 2nd) , opens trade in current candle if price hits 1st candle high/low. If 1st candle is bullish than its buy order if its bearish than its sell order. I checked in backtesting and it doesnt open trades when these conditions are met like this year GBP/JPY  4 hour it opens no trades

 


@ctid3179522

PanagiotisCharalampous
02 Jun 2021, 10:29

Hi ctid3179522,

The way you coded the cBot, it checks only the last two candles. But from your screenshot it seems you are looking to compare any pair on the chart, not only the last two. For example, the candles you highlighted on the screen are Last(12) and Last(13). May you should be thinking in terms of a loop, to find the relevant candle pairs and save their indices. 

Also avoid using equalities in the conditions below

Symbol.Bid == Bars.HighPrices.Last(2)

Symbol.Bid == Bars.LowPrices.Last(2)

You should be checking if the price is higher or lower instead.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous