MarketSeries.Close array analogy to MQL4 CopyClose

Created at 24 Jul 2019, 15:14
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!
GU

gutygs

Joined 24.07.2019

MarketSeries.Close array analogy to MQL4 CopyClose
24 Jul 2019, 15:14


Hi everyone!

I am new to this forum and cTrader for the matter! I come from using MQL4 but I-m now switching to cTrader for it's many features!

I'm sorry if this has been solved before. I've looked for it without any luck; I'm trying to build an array of close prices beginning from the last candle up until a term iCandle. When I call MarketSeries.Close[index] outside a for loop, the data is displayed nicely. Nonetheless, upon calling the method inside a for loop, I don't get any value back. I am also new to C# as I've been coding in Python for the last 2 years, so I'm guessing there must be something I'm doing wrong.

Any help will be much appreciated! Thank you!

Code:


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


namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Strategy5 : Robot
    {
        [Parameter("iCandle", DefaultValue = 7)]
        public int iCandle { get; set; }

        protected override void OnStart()
        {

            Print("initializing console");

        }

        protected override void OnBar()
        {

            //lets print the last 7 candle closes
            Print("Printing the last 7 candles");
            var lastIndex = MarketSeries.Close.Count - 1;

            string data = "";
    
            for (int i = lastIndex; i <= lastIndex-iCandle; i--)
            {
            string strclose = MarketSeries.Close[i].ToString();
            data += strclose + ", ";
            }

            Print("last prices are {0}", data);


        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 


@gutygs
Replies

firemyst
17 Aug 2019, 18:37

RE:

gutygs said:

Hi everyone!

I am new to this forum and cTrader for the matter! I come from using MQL4 but I-m now switching to cTrader for it's many features!

I'm sorry if this has been solved before. I've looked for it without any luck; I'm trying to build an array of close prices beginning from the last candle up until a term iCandle. When I call MarketSeries.Close[index] outside a for loop, the data is displayed nicely. Nonetheless, upon calling the method inside a for loop, I don't get any value back. I am also new to C# as I've been coding in Python for the last 2 years, so I'm guessing there must be something I'm doing wrong.

Any help will be much appreciated! Thank you!

Code:


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


namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Strategy5 : Robot
    {
        [Parameter("iCandle", DefaultValue = 7)]
        public int iCandle { get; set; }

        protected override void OnStart()
        {

            Print("initializing console");

        }

        protected override void OnBar()
        {

            //lets print the last 7 candle closes
            Print("Printing the last 7 candles");
            var lastIndex = MarketSeries.Close.Count - 1;

            string data = "";
    
            for (int i = lastIndex; i <= lastIndex-iCandle; i--)
            {
            string strclose = MarketSeries.Close[i].ToString();
            data += strclose + ", ";
            }

            Print("last prices are {0}", data);


        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 

Of course you won't get any values inside the for loop. Look at the conditions of your for loop.

 for (int i = lastIndex; i <= lastIndex-iCandle; i--)

lastIndex is set to the last index.

i is set to this value.

So how is "i" going to be less than the "lastIndex - iCandle"?

Say lastIndex is 3000. So i starts off at 3000.

how is 3000 going to be <= (3000 - 5)?

It never will be.

So it never enters your for-loop.

And thus nothing is printed.


@firemyst