Working with lists - please help....

Created at 29 Oct 2013, 00:31
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!
jhtrader's avatar

jhtrader

Joined 15.10.2013 Blocked

Working with lists - please help....
29 Oct 2013, 00:31


Hi,

I am looking for some help on a problem.

I would like to compare the current range to the cumulative range of the last 10 bars.

While

current range x > Range(x-1)

current range x > Range(x-1) + Range (x-2)

current range x > Range(x-1) + Range (x-2) + Range (x-3)

....

when the condition is false it returns the last index. so if the current range is greater than the cumulative range of the last 4 bars it returns 5.

Issues

The Highs that are reported are not the same as on the screen

private readonly List<double> _Cumm5 = new List<double>(10);

min5 = MarketData.GetSeries(Symbol.Code, TimeFrame.Minute5);

   for (int i = 1; i < 11; i++)
            {
                // _Cumm5[i - 1] = min5.High[min5.High.Count - i] - min5.Low[min5.Low.Count - i] + _Cumm5[i];
                //Print("Val H {0}", min5.High[min5.High.Count - i]);
                Print("{0}", _Cumm5.Count);
                double H = min5.High[min5.High.Count - i];
                double L = min5.Low[min5.Low.Count - i];
                double Ct = min5.High.Sum(i);
                // Print("Val L{0}", min5.Low[min5.Low.Count - i]);
               // _Cumm5.Add(H - L);
                //Print("Cumm: {0}", _Cumm5[i].ToString());
                Print("H {0}", H);

                Print("L {0}", L);

                Print("Rng {0}", H - L);

            }

I cannot seem to get the list working it loops infinitely

 

 

 

 

 

 

 

 

 


Replies

jhtrader
29 Oct 2013, 01:18

RE:

Question 1.

Access the last 10 highs - no matter what I do I cant seem to get the real highs and lows as on the chart..

            for (int i = MarketSeries.High.Count - 1; i > MarketSeries.High.Count - 11; i--)
            {
                Print("Val H {0}", min5.High[i]);

           }

I also just tried to get the last high.. in the following way.. and it too didnt work.

Print("Val H {0}", min5.High[min5.High.Count - 1]);

 Print("Val H {0}", min5.High[MarketSeries.Close.Count - 2]

Print("Val H {0}", min5.High[MarketSeries.High.Count - 2]

Question 2.

How to put these values into a list. so that the values are cumulative

Question 3.

loop through and compare until currRange < cummulative range and return the index.

 

 

 

 

 

 

 

jhtrader said:

Hi,

I am looking for some help on a problem.

I would like to compare the current range to the cumulative range of the last 10 bars.

While

current range x > Range(x-1)

current range x > Range(x-1) + Range (x-2)

current range x > Range(x-1) + Range (x-2) + Range (x-3)

....

when the condition is false it returns the last index. so if the current range is greater than the cumulative range of the last 4 bars it returns 5.

Issues

The Highs that are reported are not the same as on the screen

private readonly List<double> _Cumm5 = new List<double>(10);

min5 = MarketData.GetSeries(Symbol.Code, TimeFrame.Minute5);

   for (int i = 1; i < 11; i++)
            {
                // _Cumm5[i - 1] = min5.High[min5.High.Count - i] - min5.Low[min5.Low.Count - i] + _Cumm5[i];
                //Print("Val H {0}", min5.High[min5.High.Count - i]);
                Print("{0}", _Cumm5.Count);
                double H = min5.High[min5.High.Count - i];
                double L = min5.Low[min5.Low.Count - i];
                double Ct = min5.High.Sum(i);
                // Print("Val L{0}", min5.Low[min5.Low.Count - i]);
               // _Cumm5.Add(H - L);
                //Print("Cumm: {0}", _Cumm5[i].ToString());
                Print("H {0}", H);

                Print("L {0}", L);

                Print("Rng {0}", H - L);

            }

I cannot seem to get the list working it loops infinitely

 

 

 

 

 

 

 

 

 

 


Spotware
29 Oct 2013, 11:07

RE:

jhtrader said:

Hi,

I am looking for some help on a problem.

I would like to compare the current range to the cumulative range of the last 10 bars.

While

current range x > Range(x-1)

current range x > Range(x-1) + Range (x-2)

current range x > Range(x-1) + Range (x-2) + Range (x-3)

....

when the condition is false it returns the last index. so if the current range is greater than the cumulative range of the last 4 bars it returns 5.

Issues

The Highs that are reported are not the same as on the screen

Make sure you choose min 5 Timeframe for the robot instance.

private readonly List<double> _Cumm5 = new List<double>(10);

min5 = MarketData.GetSeries(Symbol.Code, TimeFrame.Minute5);

   for (int i = 1; i < 11; i++)
            {
                // _Cumm5[i - 1] = min5.High[min5.High.Count - i] - min5.Low[min5.Low.Count - i] + _Cumm5[i];
                //Print("Val H {0}", min5.High[min5.High.Count - i]);
                Print("{0}", _Cumm5.Count);
                double H = min5.High[min5.High.Count - i];
                double L = min5.Low[min5.Low.Count - i];
                double Ct = min5.High.Sum(i);
                // Print("Val L{0}", min5.Low[min5.Low.Count - i]);
               // _Cumm5.Add(H - L);
                //Print("Cumm: {0}", _Cumm5[i].ToString());
                Print("H {0}", H);

                Print("L {0}", L);

                Print("Rng {0}", H - L);

            }

I cannot seem to get the list working it loops infinitely

It is not clear what you mean by loops infinitely. If you are referring to the for loop, the syntax is correct, i.e. it does not create an infinite loop.
If you are adding this code in the OnTick method it will be called on each tick.
If you mean it does not break out of the loop when the condition is met, then you need to add the condition inside the loop and if it is met you will break out of the loop.

Try this code:

        protected override void OnBar()
        {
            double cummulativeRange = 0;
            int min5Count = min5.High.Count; 

            for (int i = 1; i < 11; i++)
            {
                double H = min5.High[min5Count - i];
                double L = min5.Low[min5Count - i];

                double range = H - L;
                cummulativeRange += range;

                if (range < cummulativeRange)
                {
                    Print("min5Count {0}", min5Count);
                    Print("min5Count - i {0}, i {1}, range {2}, cumRange {3}", min5Count - i, i, range, cummulativeRange);
                    break;
                }
            }            
        }

 


@Spotware

Spotware
29 Oct 2013, 11:32

RE: RE:

jhtrader said:

Question 1.

Access the last 10 highs - no matter what I do I cant seem to get the real highs and lows as on the chart..

            for (int i = MarketSeries.High.Count - 1; i > MarketSeries.High.Count - 11; i--)
            {
                Print("Val H {0}", min5.High[i]);

           }

See the example for GetIndexByDate here: /forum/whats-new/1463#3

The code above prints the High of the min 5 timeframe. The chart will have the prices of the timeframe chosen when you add the instance of the robot.

I also just tried to get the last high.. in the following way.. and it too didnt work.

Print("Val H {0}", min5.High[min5.High.Count - 1]);

min5.High.LastValue

Question 2.

How to put these values into a list. so that the values are cumulative

List<double> cummulativeHighs = new List<double>();
//...
cummulativeHighs.Add(min5.High.LastValue + cummulativeHighs.LastOrDefault());

Question 3.

loop through and compare until currRange < cummulative range and return the index.

See the previous reply post.


@Spotware

jhtrader
29 Oct 2013, 22:24

Thanks for the answer.. I am sure that this will be alot of help to newbie programmers like myself.

Is this the correct and most efficient way to write this function

The following function

- gets the last 5 min range

- creates a 15 min range list with cummulative values from the last bar to the 10th most recent bar  (i dont use this list this is just to learn how to add and retrieve)

- compare the last 5min range to the list created -> and return the index (number of bars that the current range is larger than)

 

        public int check2minBar()
        {

            int result = 0;
            var lastIndex = MarketSeries.Close.Count - 1;
            var index1 = GetIndexByDate(series1m, MarketSeries.OpenTime[lastIndex]);
            var index2 = GetIndexByDate(series2m, MarketSeries.OpenTime[lastIndex]);

            //The current unformed bar of the larger timeframe is no included as it incudes the formed bar of the lower timeframe


            double Rng1 = Math.Round((series1m.High[index1 - 1] - series1m.Low[index1 - 1]) / Symbol.PipSize, 2);

            Print("The Current 1min bar has H:{0}, L:{1} and Rng = {2}", series1m.High[index1 - 1], series1m.Low[index1 - 1], Rng1);

            double cummRng2min = 0;
            int min2Count = series2m.High.Count - 1;

            Print("The curr 2 min count = {0}", min2Count);

            for (int i = 1; i < Periods + 1; i++)
            {

                double H = series2m.High[min2Count - i];
                double L = series2m.Low[min2Count - i];
                double range = Math.Round((H - L) / Symbol.PipSize, 2);
                cummRng2min += range;

                Print("H: {0}", H);
                Print("L: {0}", L);
                Print("Rng: {0}", range);
                Print("CR: {0}", cummRng2min);

                //Put in CummRange List
                // _Cumm2.Add(range + _Cumm2.LastOrDefault());
                //print list value
                // Print("Value added to list {0}", _Cumm2[i].ToString());
                //check if the current 5 min Rng 

                if (Rng1 <= cummRng2min)
                {
                    Print("min2Count {0}", min2Count);
                    Print("(min2Count - i) = {0}, i {1}, range {2}, cumRange {3}", min2Count - i, i, range, cummRng2min);
                    result = min2Count - (min2Count - i);
                    break;

                }

            }

            return result;

        }

QUESTION 2 - Using A List Option where I store the values to a list and then comparing the values

List<double> cummulativeHighs = new List<double>();

//...

cummulativeHighs.Add(min5.High.LastValue + cummulativeHighs.LastOrDefault()); <-- this crashed on the first itteration

Please show me how I could have modified the function above to add the ranges to a list that is cummulative and then to loop through the list and compare the value to the Curr1minrange and return the index

int index = 0;

for(int i =0; i<_cummulativeHighs.Count; i++)

{

     if(Rng5 < _cummulativeHighs[i])

         index = i;

        break;

}

Many thanks..

 

 

 

 

 


atrader
30 Oct 2013, 12:32

RE:

jhtrader said:

Thanks for the answer.. I am sure that this will be alot of help to newbie programmers like myself.

Is this the correct and most efficient way to write this function

The following function

- gets the last 5 min range

            double Rng1 = Math.Round((series1m.High[index1 - 1] - series1m.Low[index1 - 1]) / Symbol.PipSize, 2);

You mean series1m is the min 5 market series? I suggest you use better naming.

- creates a 15 min range list with cummulative values from the last bar to the 10th most recent bar  (i dont use this list this is just to learn how to add and retrieve)

That is also confusing. You mean the sum of ranges of the 15 min market series? Give example with numbers.
To learn how to use lists you can see some tutorials e.g. http://www.dotnetperls.com/list


@atrader

jhtrader
30 Oct 2013, 19:51

RE: RE:

Sorry there was a typo.. I changed the code to 1 and 2 min timeframes to speed up testing...

The code uses the correct naming the comments may be a little confusing but just substitute the timeframes... 5min changed to 1min and 15min changed to 2min.

In relation to the confusion on the list I provided the example at the top

I would like to compare the current range to the cumulative range of the last 10 bars.

While

current range x > Range(x-1)

current range x > Range(x-1) + Range (x-2)

current range x > Range(x-1) + Range (x-2) + Range (x-3)

so the list is a list of ranges the first element has the current range the next the sum of the 1st and 2nd and the third is the sum of 1st 2nd and 3rd and so on..

Its ok.. I have solved this now.

 for (int i = 0; i < arraySize; i++)
            {

                double H = series2m.High[min2Count - i];
                double L = series2m.Low[min2Count - i];
                double range = Math.Round((H - L) / Symbol.PipSize, 2);

                // Print("High = {0} Low = {1} and Rng: {2}", H, L, range);

                // _Cumm2.Add(range + _Cumm2.LastOrDefault());  The list option works fine too
                _Cumm2[i] = _Cumm2[i] + series2m.High[min2Count - i] - series2m.Low[min2Count - i];

            }

 

 

 

jhtrader said:

Thanks for the answer.. I am sure that this will be alot of help to newbie programmers like myself.

Is this the correct and most efficient way to write this function

The following function

- gets the last 5 min range

            double Rng1 = Math.Round((series1m.High[index1 - 1] - series1m.Low[index1 - 1]) / Symbol.PipSize, 2);

You mean series1m is the min 5 market series? I suggest you use better naming.

- creates a 15 min range list with cummulative values from the last bar to the 10th most recent bar  (i dont use this list this is just to learn how to add and retrieve)

That is also confusing. You mean the sum of ranges of the 15 min market series? Give example with numbers.
To learn how to use lists you can see some tutorials e.g. http://www.dotnetperls.com/list

 


walshregal
16 Dec 2013, 10:41

c# list sample programs http://csharp.net-informations.com/collection/list.htm c# list

walsh


@walshregal