Price Crossovers from below and from above.

Created at 21 Dec 2021, 19: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!
KO

kojomidoji

Joined 13.10.2021

Price Crossovers from below and from above.
21 Dec 2021, 19:10


Hello may i have someone getting me codes for price  crossovers.  1. Where the price crosses the upper Bollinger band from below 2. Where the price  crosses the same upper Bollinger band from above. 

The codes i have gotten from the forum so far only deal generally  with the price just closing greater than the bands.


@kojomidoji
Replies

amusleh
22 Dec 2021, 09:24

Hi,

Try this:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private BollingerBands _bbands;

        protected override void OnStart()
        {
            _bbands = Indicators.BollingerBands(Bars.ClosePrices, 20, 2, MovingAverageType.Simple);
        }

        protected override void OnBar()
        {
            // When a bar crossed upper band from below
            if (Bars.Last(2).High < _bbands.Top.Last(2) && Bars.Last(1).High > _bbands.Top.Last(1))
            {

            }

            // When a bar crossed upper band from above
            if (Bars.Last(2).Low > _bbands.Top.Last(2) && Bars.Last(1).Low < _bbands.Top.Last(1))
            {

            }
        }
    }
}

 


@amusleh

kojomidoji
27 Dec 2021, 16:00

RE:

kojomidoji said:

Hello may i have someone getting me codes for price  crossovers.  1. Where the price crosses the upper Bollinger band from below 2. Where the price  crosses the same upper Bollinger band from above. 

The codes i have gotten from the forum so far only deal generally  with the price just closing greater than the bands.

Hello amusleh,

So many for the reply and the codes. .

I have added the codes accordingly but unfortunately the cbot could not detect many of the signals that meet the conditions and the rules set in the strategy as shown below 

1. Buy conditions:  Bollinger bands Bulge + Bollinger bottom band crossover from below + RSI less or equal to 30%

2. Sell conditions:  Bollinger bands Bulge + Bollinger top band crossover from above + RSI greater or equal to 70%

 

During 01/07/2021 to 6/12/2021 backtesting periods the following have been observed?

 

1. many potential signals (Buy and Sell) were not detected by the cbot  as shown in the attached screenshots signals 1 to 6.

2. also attached are some of the the few signals  detected and captured by the cbot

So i will be most grateful if you will be able to explain why the cbot could not capture majority of the potential signals based on the strategy rules as shown .

Also find below the sections of the Buy and sell codes:

"// Function to find the buy signals

        private bool BuyEntry(int index)
        {

            // We use this variable to return in the function
            bool result = false;

            // We check if we have a Squeeze / Bulge setup and enter it in these parameters
            bool squeeze = bbsqueeze.Bandwidth.Last(index) - bbsqueeze.Low.Last(index) <= 0 ? true : false;

            bool bulge = bbsqueeze.High.Last(index) - bbsqueeze.Bandwidth.Last(index) <= 0 ? true : false;

            bool rsiminL = _rsi.Result.LastValue <= 30 ? true : false;

            // If we have a Bulge

             if (bulge)

          if ((Bars.Last(2).High < boll.Bottom.Last(2) && Bars.Last(1).High > boll.Bottom.Last(1)) && _rsi.Result.Last(index) <= 30)
            {
                //Print("Blue Line touched or is over the Green Line, bulge condition met | BB% = {0} | Buy condition met.", bbpercent.Result.Last(index));
                Print("Bollinger bottom band crossover from below, and RSI below or equals 30% | RSI = {0} | Buy condition met.", _rsi.Result.Last(index));

                result = true;
            }

            // We return the result
            return result;
        }"

   "// Function to find the sell signals

        private bool SellEntry(int index)
        {

            // We use this variable to return in the function
            bool result = false;

            // We check if we have a Bulge setup and enter it in these parameters
            bool bulge = bbsqueeze.High.Last(index) - bbsqueeze.Bandwidth.Last(index) <= 0 ? true : false;

          if (bulge)
           if ((Bars.Last(2).High < boll.Top.Last(2) && Bars.Last(1).High > boll.Top.Last(1)) && _rsi.Result.Last(index) >= 70)
            {
                // Print("Blue Line touched or is over the Green Line, bulge condition met | BB% = {0} | Sell condition met.", bbpercent.Result.Last(index));
                Print("Bollinger top band crossover from above, and RSI above  or equals 70% | RSI  = {0} | Sell condition met.", _rsi.Result.Last(index) >= 70);
                result = true;
            }

            // We return the result
            return result;
        }"

 


@kojomidoji

... Deleted by UFO ...

amusleh
28 Dec 2021, 08:00

Hi,

On your first post you just asked for Bollinger bands cross, you didn't mentioned any other indicator.

When you post code please use the editor "Insert Code Snippet" option.

The issue with your code is invalid indicator index, if you are using this code on a cBot try to use .Last(1) or .Last(2), you shouldn't pass the bar index which starts from beginning because the Last method uses reverse indexing.

And the LastValue property gives you the latest value inside indicator series, you should not use it because it will give non deterministic results as the last value isn't completed yet and it can change.

For more please check code examples on automate API references.


@amusleh