Trigger stays true until false

Created at 12 Dec 2016, 16:03
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!
DA

davidp13

Joined 06.05.2014

Trigger stays true until false
12 Dec 2016, 16:03


Hi. I have a trigger that should stay true until it is false no matter how many bars have passed once the trigger is true. Basically it is the latest candle crosses and close above the 15ema. As long as price stays above the 15ema the trigger is true. Once price closes below the 15ema the trigger is false. 

How does one go about storing that state? Thanks


@davidp13
Replies

cyfer
12 Dec 2016, 23:05

Concept :

Test the Candle that Closed for the Close price : [index-1]

Test the Previous Value of the EMA  : EMA15.Result[index-1]

Set a Bool Switch For CandleAboveEma & CandleBelowEMA 

Code :

//Init
private bool CandleAboveEma ,CandleBelowEMA ;

Calculate()
{
   if(Marketseries.Close[index-1] > EMA15.Resulst[Index-1])
     {
       CandleAboveEma = True ;
       CandleBelowEMA  = False ;
     }
   else if(Marketseries.Close[index-1] < EMA15.Resulst[Index-1])
    {
      CandleAboveEma = True ;
      CandleBelowEMA  = False ;
    }
}

sure you can make only one Bool and test it ,but his will make sense if you're developing a big Indicator or Robot.

you must test the Previous EMA Value & Previous Candle . .. not the current values . 

 


@cyfer

cyfer
13 Dec 2016, 00:02

Line 13 & 14 should be the opposite in terms of True & False 

 

its a shame that we can't edit our own posts . 


@cyfer

davidp13
13 Dec 2016, 07:14

RE:

Thanks cyfer. So if I put this code in Int. will it store the value until it changes? 

Again thank you for helping out.

cyfer said:

Concept :

Test the Candle that Closed for the Close price : [index-1]

Test the Previous Value of the EMA  : EMA15.Result[index-1]

Set a Bool Switch For CandleAboveEma & CandleBelowEMA 

Code :

//Init
private bool CandleAboveEma ,CandleBelowEMA ;

Calculate()
{
   if(Marketseries.Close[index-1] > EMA15.Resulst[Index-1])
     {
       CandleAboveEma = True ;
       CandleBelowEMA  = False ;
     }
   else if(Marketseries.Close[index-1] < EMA15.Resulst[Index-1])
    {
      CandleAboveEma = True ;
      CandleBelowEMA  = False ;
    }
}

sure you can make only one Bool and test it ,but his will make sense if you're developing a big Indicator or Robot.

you must test the Previous EMA Value & Previous Candle . .. not the current values . 

 

 


@davidp13

cyfer
14 Dec 2016, 01:32

Hello 

You can discard the Bool Switches cause the calculations are Switches by nature 

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class CTDN_EMA_Signal : Indicator
    {
        private MovingAverage MA;
        private bool aboveEMA, belowEma;

        [Parameter("MA_Period", DefaultValue = 15)]
        public int MA_Period { get; set; }

        [Parameter("MA_Type", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType MA_Type { get; set; }
//+--------------------------------------------------------------+
        protected override void Initialize()
        {
            MA = Indicators.MovingAverage(MarketSeries.Close, MA_Period, MA_Type);
        }
//+--------------------------------------------------------------+
        public override void Calculate(int index)
        {
            if (MarketSeries.Close[index - 1] > MA.Result[index - 1])
            {
                aboveEMA = true;
                belowEma = false;
                ChartObjects.DrawText("Signal", "AboveEma", StaticPosition.BottomLeft, Colors.Green);
            }
            if (MarketSeries.Close[index - 1] < MA.Result[index - 1])
            {
                aboveEMA = false;
                belowEma = true;
                ChartObjects.DrawText("Signal", "BelowEma", StaticPosition.BottomLeft, Colors.Red);
            }

        }
    }
}

 


@cyfer