MA Histogramm only show for a limited range

Created at 30 Aug 2018, 15:42
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!
swingfish's avatar

swingfish

Joined 25.06.2016

MA Histogramm only show for a limited range
30 Aug 2018, 15:42


is it possible to display (as example) a Moving average only for the last 10 candles?

but not impacting the formular how its calculated .. 

 

something like put NaN in the values? 

is there some example out there on how to do this? 

 

 

 


@swingfish
Replies

PanagiotisCharalampous
30 Aug 2018, 15:52

Hi swingfish,

Here it is

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API example.
//    
//    All changes to this file will be lost on next application start.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
// -------------------------------------------------------------------------------------------------

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
    public class SampleSMA : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter(DefaultValue = 14)]
        public int Periods { get; set; }

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

        public override void Calculate(int index)
        {
            double sum = 0.0;

            for (int i = index - Periods + 1; i <= index; i++)
            {
                sum += Source[i];
            }
            Result[index] = sum / Periods;
            Result[index - 10] = double.NaN;
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

swingfish
06 Sep 2018, 16:13

thank you very much, this did help a lot .. but for some strange reason the indicator does not delete the rest of the day after the reset.

my special version does "reset" to 0 at the Tokyo opening.. and for some reason the previous 15 dots are note being deleted after 

i can't figure out where the bug is. as you see the code is really simple

the part 

    if (i < index - 15)
    {
     VWAP[i] = double.NaN;
    }

does remove the dots perfectly from the current day, but leaves the last 15 points of the previos day staying .. 

is there a way to get rid of them ? 

here is a screenshoot https://prnt.sc/krb16s

i'm clueless on what i do wrong 

 

private int start_bar = o;
if (ShowVwap)
{
 int end_bar = index;
 int CurrentDay = MarketSeries.OpenTime[end_bar].DayOfYear;
 double highest = 0;
 double lowest = 999999;
 double close = MarketSeries.Close[index];

 if (CurrentDay == oldCurrentDay)
 {
  double sum = 0.0;
  for (int i = start_bar; i <= end_bar; i++)
  {
   sum += MarketSeries.Close[i];
   VWAP[i] = TotalPV / TotalVolume;
   for (int k = start_bar; k <= i; k++)
   {
    double HLC = (MarketSeries.High[k] + MarketSeries.Low[k] + MarketSeries.Close[k]) / 3;
    double OHLC = (MarketSeries.High[k] + MarketSeries.Low[k] + MarketSeries.Open[k] + MarketSeries.Close[k]) / 4;
    double avg = HLC;
    double diff = avg - VWAP[i];
   }

   // cut vwap short

   if (!ShowHistoricalvWap)
   {
    if (i < index - 15)
    {
     VWAP[i] = double.NaN;
    }

    // this does not work (supposed to delete the old days data
    /*
    if (i > MarketSeries.OpenTime[end_bar].DayOfYear)
    {
     VWAP[i] = double.NaN;
    }
   */
   }
  }
 }
 else
 {
  oldCurrentDay = MarketSeries.OpenTime[end_bar].DayOfYear;
  start_bar = end_bar - TimeOffset;
 }
}

 


@swingfish

PanagiotisCharalampous
06 Sep 2018, 16:16

Hi swingfish,

It seems that you are running that part only for the last day because of the following statement

 if (CurrentDay == oldCurrentDay)
 {

Best Regards,

Panagiotis


@PanagiotisCharalampous

swingfish
06 Sep 2018, 16:28

oh yes, that's right ... so I have to make another loop to do the cleanup I guess

how do I loop through all the history without blowing the memory? to clean it up ? 

 

 

 


@swingfish

swingfish
06 Sep 2018, 16:55

thanks again for pointing that out 

i just added a "manual removal" in the else of if (CurrentDay == oldCurrentDay)

                    if (!ShowHistoricalvWap)
                    {
                        for (int i = index - 16; i <= index; i++)
                        {
                            VWAP[i] = double.NaN;
                        }

 

not really a "clean" way of doing it but that saves me the loop through all the history .. right? 

 


@swingfish

PanagiotisCharalampous
06 Sep 2018, 17:02

Hi swingfish,

You dont need to make a loop. just keep the code outside the if-else statement at the botton of the calculate funtion. See below

 if (CurrentDay == oldCurrentDay)
            {
                double sum = 0.0;
                for (int i = start_bar; i <= end_bar; i++)
                {
                    sum += MarketSeries.Close[i];
                    VWAP[i] = TotalPV / TotalVolume;
                    for (int k = start_bar; k <= i; k++)
                    {
                        double HLC = (MarketSeries.High[k] + MarketSeries.Low[k] + MarketSeries.Close[k]) / 3;
                        double OHLC = (MarketSeries.High[k] + MarketSeries.Low[k] + MarketSeries.Open[k] + MarketSeries.Close[k]) / 4;
                        double avg = HLC;
                        double diff = avg - VWAP[i];
                    }

                    // cut vwap short



                }
            }
            else
            {
                oldCurrentDay = MarketSeries.OpenTime[end_bar].DayOfYear;
                start_bar = end_bar - TimeOffset;
            }
            if (!ShowHistoricalvWap)
            {
                if (i < index - 15)
                {
                    VWAP[i] = double.NaN;
                }
                // this does not work (supposed to delete the old days data
            }

Best Regards,

Panagiotis


@PanagiotisCharalampous