PL.NET question

Questions about MultiCharts .NET and user contributed studies.
jarym
Posts: 58
Joined: 16 Feb 2015
Has thanked: 14 times
Been thanked: 6 times

PL.NET question

Postby jarym » 21 Feb 2015

Hi,

Attempting to port some of my code from EL to PL.NET and I need a pointer on how best to achieve something very simple.

In EL I can do the following:
Value21 = MEDIAN( DMIPlus/2 ,7);
Where DMIPlus is the result of the DirMovement function.
Value21 is evaluated to be the 7 period median value of half of DMIPlus for each period.

In PL.NET I have m_DirMovement.DMIPlus which is of the type ISeries. I can't see whether there's an 'easy' way to get half of each period's value and then get a median for it.

Is there an easy way to do this?

jarym
Posts: 58
Joined: 16 Feb 2015
Has thanked: 14 times
Been thanked: 6 times

Re: PL.NET question

Postby jarym » 22 Feb 2015

Slight update, I have hacked together an IEnumerable implementation for ISeries and then used a LINQ sample I found on the internet to create a Median function.

Warning: I have been living in the land of Java for the last few years, the last time I touched C# was in the days when .NET 2.0 was considered bleeding edge. I would appreciate any quick review from more seasoned developers!

Code: Select all

using System;
using System.Collections.Generic;
using System.Collections;
using System.Drawing;
using System.Linq;


namespace PowerLanguage.Jarym
{
public static class ExtensionMethods
{
public static ISeriesEnumerable AsEnumerable(this ISeries<double> series, int length)
{
return new ISeriesEnumerable(series, length);
}

public static double Median(this ISeries<double> series, int length)
{
IEnumerable<double> list = ExtensionMethods.AsEnumerable(series, length);
List<double> orderedList = list
.OrderBy(numbers => numbers)
.ToList();

int listSize = orderedList.Count;
double result;

if (listSize%2 == 0) // even
{
int midIndex = listSize/2;
result = ((orderedList.ElementAt(midIndex - 1) +
orderedList.ElementAt(midIndex))/2);
}
else // odd
{
double element = (double) listSize/2;
element = Math.Round(element, MidpointRounding.AwayFromZero);

result = orderedList.ElementAt((int) (element - 1));
}

return result;
}
}

public class ISeriesEnumerable : IEnumerable<double>
{
private ISeries<double> _series;
private int limit = int.MaxValue;

public ISeriesEnumerable(ISeries<double> series, int max)
{
_series = series;
limit = max;
}

public IEnumerator<double> GetEnumerator()
{
return new ISeriesEnumerator(_series, limit);
}


private IEnumerator GetEnumerator1()
{
return this.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator1();
}

}


public class ISeriesEnumerator : IEnumerator<double>
{
private ISeries<double> _series;

// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
int limit = int.MaxValue;

public ISeriesEnumerator(ISeries<double> series)
{
_series = series;
}

public ISeriesEnumerator(ISeries<double> series, int max) : this(series)
{
limit = max;
}

public bool MoveNext()
{
position++;
return (position < limit && _series[position] != null);
}

public void Reset()
{
position = -1;
}

private object Current1
{
get { return Current; }
}

object IEnumerator.Current
{
get { return Current1; }
}

public double Current
{
get
{
return _series[position];
}
}


private bool disposedValue = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
{
// Dispose of managed resources.
}
}

this.disposedValue = true;
}

~ISeriesEnumerator()
{
Dispose(false);
}
}

}

Now, I need an XAverage function that works on a VariableSeries<double> (in fact, I need to do an XAverage on a few instances) and I see that there is no extension method provided for it. Can someone tell me if there's an easy way to call the built-in XAverage for VariableSeries instances?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: PL.NET question

Postby Henry MultiСharts » 26 Feb 2015

Now, I need an XAverage function that works on a VariableSeries<double> (in fact, I need to do an XAverage on a few instances) and I see that there is no extension method provided for it. Can someone tell me if there's an easy way to call the built-in XAverage for VariableSeries instances?
Hello jarym,

You can use the prebuilt Function.XAverage.


Return to “MultiCharts .NET”