Error in cTrader native MovingAverage indicator.

Created at 15 Dec 2020, 07:39
SH

Shares4us

Joined 01.04.2020

Error in cTrader native MovingAverage indicator.
15 Dec 2020, 07:39


In an MultiThreaded environment we sometimes get the following error while  running the CheckSMA function

An item with the same key has already been added. 

at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)    
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)    
at cTrader.Automate.Small.V1.Indicator.SmallIndicatorWrapperProvider.GetNested(ISmallChartSource smallChartSource, Type clrIndicatorType, Dictionary`2 parameterValues, AlgoContext algoContext)  
at cTrader.Automate.Small.V1.Indicator.SmallIndicatorAccessor.GetInternal[TIndicator](ISmallChartSource chartSource, Object[] parameterValues)
at cTrader.Automate.Small.V1.Indicator.SmallIndicatorAccessor.SimpleMovingAverage(DataSeries source, Int32 periods)    
at cAlgo.TU_TV.CheckSMA(Int32 period) in c:\Users\Edward\Documents\cAlgo\Sources\Indicators\........ 

It could be an error on our part or at Spotware. But as MA is one of the most commonly used indicators it is of the utmost importance to get it straight.

private int CurrentSmaPeriod = -1;

private void CheckSMA(int period)
{
    if (period <= 0 || CurrentSmaPeriod == period) return;
    i_SMAt = Indicators.MovingAverage(Bars.MedianPrices, period, MovingAverageType.Simple);
    CurrentSmaPeriod = period;
}

Hope it gets solved!

 


@Shares4us
Replies

... Deleted by UFO ...

PanagiotisCharalampous
16 Dec 2020, 11:04

Hi Shares4us,

The API is not thread safe. You should have that in mind in case you are developing multi-threaded code.

Best Regards,

Panagiotis 

Join us on Telegram 


@PanagiotisCharalampous