TMA Slope

Created at 03 Jun 2013, 23:08
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!
DO

DomnikInvest

Joined 18.04.2013

TMA Slope
03 Jun 2013, 23:08


Hi,

can somebody help me to make TMA Slope. This 3 functions is need to calc TMA Slope - from mq4:

//+------------------------------------------------------------------+

//| GetSlope()                                                       |

//+------------------------------------------------------------------+

double GetSlope(string symbol, int tf, int shift)

{

   double dblTma, dblPrev;

   double atr = iATR(symbol, tf, 100, shift + 10) / 10;

   double gadblSlope = 0.0;

   if ( atr != 0 )

   {

      dblTma = calcTmaTrue( symbol, tf, shift );

      dblPrev = calcPrevTrue( symbol, tf, shift );

      gadblSlope = ( dblTma - dblPrev ) / atr;

   }

   

   return ( gadblSlope );

 

}//End double GetSlope(int tf, int shift)

 

//+------------------------------------------------------------------+

//| calcTmaTrue()                                                    |

//+------------------------------------------------------------------+

double calcTmaTrue( string symbol, int tf, int inx )

{

   return ( iMA( symbol, tf, 21, 0, MODE_LWMA, PRICE_CLOSE, inx ) );

}

 

//+------------------------------------------------------------------+

//| calcPrevTrue()                                                   |

//+------------------------------------------------------------------+

double calcPrevTrue( string symbol, int tf, int inx )

{

   double dblSum  = iClose( symbol, tf, inx + 1 ) * 21;

   double dblSumw = 21;

   int jnx, knx;

   

   dblSum  += iClose( symbol, tf, inx ) * 20;

   dblSumw += 20;

         

   for ( jnx = 1, knx = 20; jnx <= 20; jnx++, knx-- )

   {

      dblSum  += iClose( symbol, tf, inx + 1 + jnx ) * knx;

      dblSumw += knx;

   }

   

   return ( dblSum / dblSumw );

}


@DomnikInvest
Replies

DomnikInvest
04 Jun 2013, 16:51

This is my first time diling with C# so be patience...hehe ... pleas help me ... I get error... And I dont know if I am going right way...

// -------------------------------------------------------------------------------
//
//    This is a Template used as a guideline to build your own Robot. 
//    Please use the “Feedback” tab to provide us with your suggestions about cAlgo’s API.
//
// -------------------------------------------------------------------------------

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false)]
    public class TMASlope : Indicator
    {
        private AverageTrueRange _averageTrueRange;
        private TriangularMovingAverage _triangularMovingAverage;
        
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main", Color = Colors.Turquoise, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries Result { get; set; }


        protected override void Initialize()
        {
            _triangularMovingAverage = Indicators.TriangularMovingAverage(MarketSeries.Close, 21);
            _averageTrueRange = Indicators.GetIndicator<AverageTrueRange>(100);
        }
        
        double calcPrevTrue( int index )
        {
            double dblSum  = MarketSeries.Close[ index + 1] * 21;
               double dblSumw = 21;
               int jnx, knx;
   
               dblSum  += MarketSeries.Close[ index ] * 20;
               dblSumw += 20;

               for ( jnx = 1, knx = 20; jnx <= 20; jnx++, knx-- )
               {
                  dblSum  += MarketSeries.Close[index + 1 + jnx ] * knx;
                  dblSumw += knx;
               }
           return ( dblSum / dblSumw );
        }


        public override void Calculate(int index)
        {
               double dblTma, dblPrev;
               double atr = _averageTrueRange.Result[index + 10] / 10;
               double gadblSlope = 0.0;

               if ( atr != 0 )
               {
                  dblTma = _triangularMovingAverage.Result[index];
                  dblPrev = calcPrevTrue( index );
                  gadblSlope = ( dblTma - dblPrev ) / atr;
               }
               Result[index] = gadblSlope ;
        }
    }
}


@DomnikInvest

DomnikInvest
04 Jun 2013, 17:39

Now I add reference and now is withaut errors but dont work 

//#reference: C:\Users\ToTo\Documents\cAlgo\Sources\Indicators\AverageTrueRange.algo
// -------------------------------------------------------------------------------
//
//    This is a Template used as a guideline to build your own Robot. 
//    Please use the “Feedback” tab to provide us with your suggestions about cAlgo’s API.
//
// -------------------------------------------------------------------------------

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false)]
    public class TMASlope : Indicator
    {
		private AverageTrueRange _averageTrueRange;
		private TriangularMovingAverage _triangularMovingAverage;
		
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main", Color = Colors.Turquoise, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries Result { get; set; }


        protected override void Initialize()
        {
            _triangularMovingAverage = Indicators.TriangularMovingAverage(MarketSeries.Close, 21);
            _averageTrueRange = Indicators.GetIndicator<AverageTrueRange>(100);
        }
        
		double calcPrevTrue( int index )
		{
			double dblSum  = MarketSeries.Close[ index + 1] * 21;
   			double dblSumw = 21;
   			int jnx, knx;
   
   			dblSum  += MarketSeries.Close[ index ] * 20;
   			dblSumw += 20;

   			for ( jnx = 1, knx = 20; jnx <= 20; jnx++, knx-- )
   			{
      			dblSum  += MarketSeries.Close[index + 1 + jnx ] * knx;
      			dblSumw += knx;
   			}
   		return ( dblSum / dblSumw );
		}


        public override void Calculate(int index)
        {
   			double dblTma, dblPrev;
   			double atr =_averageTrueRange.Result[index + 10] / 10;
   			double gadblSlope = 0.0;

   			if ( atr != 0 )
   			{
      			dblTma = _triangularMovingAverage.Result[index];
      			dblPrev = calcPrevTrue( index );
      			gadblSlope = ( dblTma - dblPrev ) / atr;
   			}
   			Result[index] = gadblSlope ;
        }
    }
}




@DomnikInvest

cAlgo_Fanatic
04 Jun 2013, 18:09

In the Calculate method index is the last index of the series at the moment. Therefore any value such as:

MarketSeries.Close[index + 1 + jnx ]

does not exist yet.

You want to calculate backwards by subtracting from the last index instead of adding.


@cAlgo_Fanatic

DomnikInvest
04 Jun 2013, 18:42

Please can you help me to fix the code its my first time with C# and I am stil confused  - If you can show me how to fix I can learn from this for the future....THX


@DomnikInvest

cAlgo_Fanatic
05 Jun 2013, 09:53

Start by changing  the lines where you are adding to the index to subtract instead and see if this is the desired outcome of the indicator like so:

double dblSum  = MarketSeries.Close[ index - 1] * 21;
//...
dblSum += MarketSeries.Close[index - 1 - jnx] * knx;
//...
double atr = _averageTrueRange.Result[index - 10] / 10;

 


@cAlgo_Fanatic

DomnikInvest
05 Jun 2013, 14:17

Thanks I already see where was the problem.... THX anyway


@DomnikInvest

DomnikInvest
05 Jun 2013, 15:38

Add levels

Hi,

how can I add 2 levels 0.5 and -0.5, 0.8 and -0.8 for example?

 //#reference: C:\Users\ToTo\Documents\cAlgo\Sources\Indicators\AverageTrueRange.algo
    // -------------------------------------------------------------------------------
    //
    //    This is a Template used as a guideline to build your own Robot.
    //    Please use the “Feedback” tab to provide us with your suggestions about cAlgo’s API.
    //
    // -------------------------------------------------------------------------------

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

    namespace cAlgo.Indicators
    {
        [Indicator(IsOverlay = false)]
        public class TMASlope : Indicator
        {
          private AverageTrueRange _averageTrueRange;
          private TriangularMovingAverage _triangularMovingAverage;
          
            [Parameter(DefaultValue = 0.0)]
            public double Parameter { get; set; }

            [Output("Main", Color = Colors.Turquoise, PlotType = PlotType.Histogram)]
            public IndicatorDataSeries Result { get; set; }


            protected override void Initialize()
            {
                _triangularMovingAverage = Indicators.TriangularMovingAverage(MarketSeries.Close, 21);
                _averageTrueRange = Indicators.GetIndicator<AverageTrueRange>(100);
            }
           
          double calcPrevTrue( int index )
          {
             double dblSum  = MarketSeries.Close[ index - 1] * 21;
                double dblSumw = 21;
                int jnx, knx;
       
                dblSum  -= MarketSeries.Close[ index ] * 20;
                dblSumw -= 20;

                for ( jnx = 1, knx = 20; jnx <= 20; jnx++, knx-- )
                {
                   dblSum  -= MarketSeries.Close[index - 1 - jnx ] * knx;
                   dblSumw -= knx;
                }
             return ( dblSum / dblSumw );
          }


            public override void Calculate(int index)
            {
                double dblTma, dblPrev;
                double atr =_averageTrueRange.Result[index - 10] / 10;
                double gadblSlope = 0.0;

                if ( atr != 0 )
                {
                   dblTma = _triangularMovingAverage.Result[index];
                   dblPrev = calcPrevTrue( index );
                   gadblSlope = ( dblPrev - dblTma ) / atr;
                }
                Result[index] = gadblSlope ;
            }
        }
    }




@DomnikInvest

DomnikInvest
05 Jun 2013, 15:40

right code!!!
 //#reference: C:\Users\ToTo\Documents\cAlgo\Sources\Indicators\AverageTrueRange.algo
    // -------------------------------------------------------------------------------
    //
    //    This is a Template used as a guideline to build your own Robot.
    //    Please use the “Feedback” tab to provide us with your suggestions about cAlgo’s API.
    //
    // -------------------------------------------------------------------------------

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

    namespace cAlgo.Indicators
    {
        [Indicator(IsOverlay = false)]
        public class TMASlope_11 : Indicator
        {
          private AverageTrueRange _averageTrueRange;
          private WeightedMovingAverage _weightedMovingAverage;
          
            [Parameter(DefaultValue = 0.0)]
            public double Parameter { get; set; }

            [Output("Main", Color = Colors.Turquoise, PlotType = PlotType.Histogram)]
            public IndicatorDataSeries Result { get; set; }


            protected override void Initialize()
            {
                _weightedMovingAverage = Indicators.WeightedMovingAverage(MarketSeries.Close, 21);
                _averageTrueRange = Indicators.GetIndicator<AverageTrueRange>(100);
            }
           
          double calcPrevTrue( int index )
          {
             double dblSum  = MarketSeries.Close[ index - 1] * 21;
                double dblSumw = 21;
                int jnx, knx;
       
                dblSum  += MarketSeries.Close[ index ] * 20;
                dblSumw += 20;

                for ( jnx = 1, knx = 20; jnx <= 20; jnx++, knx-- )
                {
                   dblSum  += MarketSeries.Close[index - 1 - jnx ] * knx;
                   dblSumw += knx;
                }
             return ( dblSum / dblSumw );
          }


            public override void Calculate(int index)
            {
                double dblTma, dblPrev;
                double atr =_averageTrueRange.Result[index - 10] / 10;
                double gadblSlope = 0.0;

                if ( atr != 0 )
                {
                   dblTma =_weightedMovingAverage.Result[index];
                   dblPrev = calcPrevTrue( index );
                   gadblSlope = ( dblTma-dblPrev ) / atr;
                }
                Result[index] = gadblSlope ;
            }
        }
    }

 


@DomnikInvest