SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Thu Oct 01, 2020 10:06 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Aug 21, 2012 7:31 pm 
Offline

Joined: Wed Aug 08, 2007 6:32 pm
Posts: 236
Hi,

I have built a user indicator but I getting the following exception. It seems to work fine as long as the data set is small enough but I cant figure out why it is running out of memory because it should be that intensive. Also i have already tweaked the data manager trade array and bar array length with no avail. Would appreciate any help i can get on this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OpenQuant.API;
using OpenQuant.API.Indicators;
using OpenQuant.API.Plugins;

namespace Indicators
{
public class TRNDR : UserIndicator
{
int ematrPeriod, emampPeriod, smdPeriod, deviationFactor;

public TRNDR(BarSeries input, int ematrPeriod, int emampPeriod, int smdPeriod, int deviationFactor)
: base(input)
{
Name = "TRNDR";
this.ematrPeriod = ematrPeriod;
this.emampPeriod = emampPeriod;
this.smdPeriod = smdPeriod;
this.deviationFactor = deviationFactor;
}

EMA ematr;
TR tr;
EMA emamp;
SMD smd;
TimeSeries midPointSeries = new TimeSeries();
TimeSeries trndrSeries = new TimeSeries();
Double trndr = 0;

public override double Calculate(int index)
{
if (index >= 0)
{
//Input is a barseries
BarSeries bars = Input as BarSeries;

//Calculate the midpoint and add it to the series for use laterin calculating the EMAMP
Double midPoint = (Input[index, BarData.High] + Input[index, BarData.Low]) / 2;

midPointSeries.Add(bars.Last.DateTime, midPoint);

//make sure we have enough bars before we start calculating
if (index > (ematrPeriod-1) && index > (emampPeriod -1))
{
//build a new EMA of the midpoints
emamp = new EMA(midPointSeries, emampPeriod);
//create a new true range series
tr = new TR(bars);
//calculate the EMA of the true range
ematr = new EMA(tr, ematrPeriod);

//make sure we have enough bars to find the standard deviation
if (index > (ematrPeriod + smdPeriod - 1))
{
//standard the deviation of the EMA
smd = new SMD(ematr, smdPeriod);

//heart of the indicator this is the equation to solve
Double trndrl = emamp.Last - ematr.Last / 2 - deviationFactor * smd.Last;
Double trndrh = emamp.Last + ematr.Last / 2 + deviationFactor * smd.Last;

//make sure that the previous value of trndr is not 0
// ReSharper disable CompareOfFloatsByEqualityOperator
if (trndr != 0)
// ReSharper restore CompareOfFloatsByEqualityOperator
{
//price stays below trndr
if (Input[index, BarData.Close] <= trndr && Input[index - 1, BarData.Close] <= trndrSeries.Last)
{
//trndr high is less than the last trndr point lower the trndr to the new trndr high point otherwise leave it the same
if (trndrh <= trndrSeries.Last)
{
trndr = trndrh;
trndrSeries.Add(bars.Last.DateTime, trndr);
}
else
{
trndr = trndrSeries.Last;
trndrSeries.Add(bars.Last.DateTime, trndr);
}
}
//price stays above trndr
else if (Input[index, BarData.Close] >= trndr && Input[index - 1, BarData.Close] >= trndrSeries.Last)
{
//if trndr low is higher than the last trndr point raise it to the new value else leave it the same
if (trndrl >= trndrSeries.Last)
{
trndr = trndrl;
trndrSeries.Add(bars.Last.DateTime, trndr);
}
else
{
trndr = trndrSeries.Last;
trndrSeries.Add(bars.Last.DateTime, trndr);
}
}
//price crosses below trndr
else if (Input[index, BarData.Close] < trndr && Input[index - 1, BarData.Close] > trndrSeries.Last)
{
trndr = trndrh;
trndrSeries.Add(bars.Last.DateTime, trndr);
}
//price crosses above trndr
else
{
trndr = trndrl;
trndrSeries.Add(bars.Last.DateTime, trndr);
}
return trndr;
}
// if first calculation
// if the close is below the trendr low
if (Input[index, BarData.Close] < trndrl)
{
//trndr is short so is using the high stop
trndr = trndrh;
trndrSeries.Add(bars.Last.DateTime, trndr);
}
// if close is greater than trendr high or trendr low for that matter
else
{
//trndr is in the middle somewhere so use the low stop to get things going. This could be superfluous code
trndr = trndrl;
trndrSeries.Add(bars.Last.DateTime, trndr);
}
return trndr;
}
return Double.NaN;
}
return Double.NaN;
}
return Double.NaN;
}
}
}


System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.Collections.Generic.SortedList`2.set_Capacity(Int32 value)
at System.Collections.Generic.SortedList`2.EnsureCapacity(Int32 min)
at System.Collections.Generic.SortedList`2.Insert(Int32 index, TKey key, TValue value)
at System.Collections.Generic.SortedList`2.set_Item(TKey key, TValue value)
at SmartQuant.Data.MemorySeries`1.Add(DateTime datetime, Object obj)
at SmartQuant.Indicators.Indicator.Add(DateTime DateTime, Double Data)
at SmartQuant.Indicators.EMA.Calculate(Int32 index)
at SmartQuant.Indicators.EMA.OnInputItemAdded(Object sender, DateTimeEventArgs EventArgs)
at SmartQuant.Indicators.Indicator.OnInputItemAdded2(Object sender, DateTimeEventArgs EventArgs)
at SmartQuant.Series.ItemAddedEventHandler.Invoke(Object sender, DateTimeEventArgs e)
at SmartQuant.Series.TimeSeries.EmitItemAdded(DateTime dateTime)
at SmartQuant.Series.DoubleSeries.Add(DateTime DateTime, Double Data)
at OpenQuant.API.TimeSeries.Add(DateTime dateTime, Double data)
at Indicators.TRNDR.Calculate(Int32 index)
at OpenQuant.API.Plugins.UserIndicator.OnInputItemAdded(Object sender, DateTimeEventArgs args)
at SmartQuant.Series.ItemAddedEventHandler.Invoke(Object sender, DateTimeEventArgs e)
at SmartQuant.Series.TimeSeries.EmitItemAdded(DateTime dateTime)
at SmartQuant.Series.BarSeries.Add(Bar bar)
at SmartQuant.Instruments.BarSeriesList.Syt7KTetTb(Instrument , Bar )
at SmartQuant.Instruments.DataManager.x5D42Jcqo(Object , BarEventArgs )
at SmartQuant.Providers.ProviderManager.NQB6efMVLi(Object , BarEventArgs )
at SmartQuant.Providers.BarEventHandler.Invoke(Object sender, BarEventArgs args)
at SmartQuant.Simulation.SimulationDataProvider.BPX1AQ4a8p(Object , BarEventArgs )
at SmartQuant.Providers.BarFactory.PYfT4AgTa3(Bar , IFIXInstrument )
at SmartQuant.Providers.BarFactory.t9xTXRWvGh(ReminderEventArgs )
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at SmartQuant.Clock.cGIlD5d71h(DateTime )


Top
 Profile  
 
PostPosted: Wed Aug 22, 2012 1:56 pm 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi,

It seems that you create several indicators on each bar. Why can't you just initialize them in the constructor of your indicator class and simply use them in the Calculate method? Each time you add some value to the midPointSeries series - the emamp indicator should be recalculated automatically, so no need to create a new one each time the Calculate method is called.

Regards,
Sergey.


Top
 Profile  
 
PostPosted: Thu Aug 23, 2012 6:25 pm 
Offline

Joined: Wed Aug 08, 2007 6:32 pm
Posts: 236
Thanks Baraz, it has fixed the problem. Silly mistake to make


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group