Circular Buffer Issue MQL5

 

Hello everyone,

I am trying to record some values in an array and I want them to be in a sequence such that the latest update should be on top and it should hold 10 such messages.

I have figured out the use of the mod operator to implement a kind of circular buffer stuff but I don't know how to go about with the array to implement the desired effect.

string test[10]; // This is the array that will hold the messages/strings
void OnStart()
{
   for(int j=0;j<50;j++) // I have chosen 50 as a number just to show that the index passes beyond the array size 
     {
         test[j%ArraySize(test)]=IntegerToString(j); 
         ArrayPrint(test); 
     }
}

Output Generated is as follows:

"0"  null null null null null null null null null
"0"  "1"  null null null null null null null null
"0"  "1"  "2"  null null null null null null null
"0"  "1"  "2"  "3"  null null null null null null
"0"  "1"  "2"  "3"  "4"  null null null null null
"0"  "1"  "2"  "3"  "4"  "5"  null null null null
"0"  "1"  "2"  "3"  "4"  "5"  "6"  null null null
"0"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  null null
"0"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  null
"0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
"10" "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"
"10" "11" "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"
"10" "11" "12" "3"  "4"  "5"  "6"  "7"  "8"  "9"
"10" "11" "12" "13" "4"  "5"  "6"  "7"  "8"  "9"
"10" "11" "12" "13" "14" "5"  "6"  "7"  "8"  "9"
"10" "11" "12" "13" "14" "15" "6"  "7"  "8"  "9"
"10" "11" "12" "13" "14" "15" "16" "7"  "8"  "9"
"10" "11" "12" "13" "14" "15" "16" "17" "8"  "9"
"10" "11" "12" "13" "14" "15" "16" "17" "18" "9"
"10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
"20" "11" "12" "13" "14" "15" "16" "17" "18" "19"
"20" "21" "12" "13" "14" "15" "16" "17" "18" "19"
"20" "21" "22" "13" "14" "15" "16" "17" "18" "19"
"20" "21" "22" "23" "14" "15" "16" "17" "18" "19"
"20" "21" "22" "23" "24" "15" "16" "17" "18" "19"
"20" "21" "22" "23" "24" "25" "16" "17" "18" "19"
"20" "21" "22" "23" "24" "25" "26" "17" "18" "19"
"20" "21" "22" "23" "24" "25" "26" "27" "18" "19"
"20" "21" "22" "23" "24" "25" "26" "27" "28" "19"
"20" "21" "22" "23" "24" "25" "26" "27" "28" "29"
"30" "21" "22" "23" "24" "25" "26" "27" "28" "29"
"30" "31" "22" "23" "24" "25" "26" "27" "28" "29"
"30" "31" "32" "23" "24" "25" "26" "27" "28" "29"
"30" "31" "32" "33" "24" "25" "26" "27" "28" "29"
"30" "31" "32" "33" "34" "25" "26" "27" "28" "29"
"30" "31" "32" "33" "34" "35" "26" "27" "28" "29"
"30" "31" "32" "33" "34" "35" "36" "27" "28" "29"
"30" "31" "32" "33" "34" "35" "36" "37" "28" "29"
"30" "31" "32" "33" "34" "35" "36" "37" "38" "29"
"30" "31" "32" "33" "34" "35" "36" "37" "38" "39"
"40" "31" "32" "33" "34" "35" "36" "37" "38" "39"
"40" "41" "32" "33" "34" "35" "36" "37" "38" "39"
"40" "41" "42" "33" "34" "35" "36" "37" "38" "39"
"40" "41" "42" "43" "34" "35" "36" "37" "38" "39"
"40" "41" "42" "43" "44" "35" "36" "37" "38" "39"
"40" "41" "42" "43" "44" "45" "36" "37" "38" "39"
"40" "41" "42" "43" "44" "45" "46" "37" "38" "39"
"40" "41" "42" "43" "44" "45" "46" "47" "38" "39"
"40" "41" "42" "43" "44" "45" "46" "47" "48" "39"
"40" "41" "42" "43" "44" "45" "46" "47" "48" "49"

So in this case the higher number is basically the latest message that I want at the top and the oldest one should always be at the bottom, how can that be achieved ?

 
If your buffer is a display buffer, set it to as_series true and the system will do it for you