Replies

herculesone
12 Sep 2021, 15:40

Solved

I was able to solve this as I finally understood how the index and time are related with these functions and I have chosen to do the following:

        private void EMAAnalysis(int index)
        {

            Bars shortChart = MarketData.GetBars(ShortMATimeFrame);
            Bars longChart = MarketData.GetBars(LongMATimeFrame);

            int shortIndex = shortChart.OpenTimes.GetIndexByExactTime(Bars.OpenTimes.Last(index));
            int longIndex = longChart.OpenTimes.GetIndexByExactTime(Bars.OpenTimes.Last(index));

            MovingAverage shortEMA = Indicators.MovingAverage(shortChart.ClosePrices, ShortMALength, ShortMAType);
            MovingAverage longEMA = Indicators.MovingAverage(longChart.ClosePrices, LongMALength, LongMAType);

            double shortResult = 0;
            double longResult = 0;

            for (int i = index; i < 100; i++)
            {
                shortResult = shortEMA.Result.Last(i);

                if (!double.IsNaN(shortResult) && shortResult != 0)
                {
                    Print("Short EMA = ", shortResult);
                    Print("Short Index = ", i);
                    break;
                }
            }

            for (int i = index; i < 100; i++)
            {
                longResult = longEMA.Result.Last(i);

                if (!double.IsNaN(longResult) && longResult != 0)
                {
                    Print("Long EMA = ", longResult);
                    Print("Long Index = ", i);
                    break;
                }
            }

            if (shortResult > longResult)
            {
                _longtrend = true;
            }

            if (shortResult < longResult)
            {
                _longtrend = false;
            }

        }

 

This way we look back at the real index of each bar until we get a number that is valid. Due to the way tick data is collected it seems that the NaN check needs to be there all the time, if not the values are sometimes all over the place as we get a lot of NaN and actual values.

Solved. Thank you!


@herculesone

herculesone
11 Sep 2021, 18:18

RE: RE:

Thank you very much for your response, I indeed discovered this property and it helped get the time I need still the readings are way off sometimes, I made another post about this today with some example code here:

cTDN Forum - Multi time frame data (ctrader.com)

 

Thank you again for your help!

 


@herculesone

herculesone
10 Aug 2021, 16:31

RE:

amusleh said:

Hi,

Not sure what's your exact problem, but you can try this method to get volume in percentage:

        /// <summary>
        /// Returns the amount of volume based on your provided risk percentage and stop loss
        /// </summary>
        /// <param name="symbol">The symbol</param>
        /// <param name="riskPercentage">Risk percentage amount</param>
        /// <param name="accountBalance">The account balance</param>
        /// <param name="stopLossInPips">Stop loss amount in Pips</param>
        /// <returns>double</returns>
        public double GetVolume(Symbol symbol, double riskPercentage, double accountBalance, double stopLossInPips)
        {
            return symbol.NormalizeVolumeInUnits(riskPercentage / (Math.Abs(stopLossInPips) * symbol.PipValue / accountBalance * 100));
        }

 

 

Thank you for your response! :) 

 

I will give this a try.

 

The problem is that on cTrader you have 2 different values for trade volume.

Quantity (lots)

Volume (in units)

On XAUUSD specifically, the conversion from Quantity to Volume does not work correctly.

 


@herculesone