How do you add an item to an Array in MQL4?










0














I'm trying to create an Array in MQL4, and on every tick I want to add a new value to array[0] and push the rest of the items back, so the old array[0] becomes array[1] and array[1] becomes array[2] and so on. I want to be able to do this an unlimited number of times, once per tick. Then I can access the value from the previous tick using array[1], but I can also access its current value array[0]. Thanks!










share|improve this question


























    0














    I'm trying to create an Array in MQL4, and on every tick I want to add a new value to array[0] and push the rest of the items back, so the old array[0] becomes array[1] and array[1] becomes array[2] and so on. I want to be able to do this an unlimited number of times, once per tick. Then I can access the value from the previous tick using array[1], but I can also access its current value array[0]. Thanks!










    share|improve this question
























      0












      0








      0







      I'm trying to create an Array in MQL4, and on every tick I want to add a new value to array[0] and push the rest of the items back, so the old array[0] becomes array[1] and array[1] becomes array[2] and so on. I want to be able to do this an unlimited number of times, once per tick. Then I can access the value from the previous tick using array[1], but I can also access its current value array[0]. Thanks!










      share|improve this question













      I'm trying to create an Array in MQL4, and on every tick I want to add a new value to array[0] and push the rest of the items back, so the old array[0] becomes array[1] and array[1] becomes array[2] and so on. I want to be able to do this an unlimited number of times, once per tick. Then I can access the value from the previous tick using array[1], but I can also access its current value array[0]. Thanks!







      arrays mql4






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 18:55









      Wayne Filkins

      937




      937






















          1 Answer
          1






          active

          oldest

          votes


















          0














          It is possible to have an array of primitives in MQL4, the problem here is that you will have to copy them all each tick which is waste of time and resources. If you want to save some more advanced data (let's say MqlTick), you have to store it as the structure or create a class and save classes. It should be really senseless to copy all objects, that is why you should think of applying CList or CArrayObj and adding objects to the end. Simple magic would help to call element with index 0:



          #include <ArraysArrayObj.mqh>
          class CTick : public CObject
          public: double m_bid, m_ask;
          CTick(const double bid,const double ask):m_bid(bid),m_ask(ask)
          ;
          CArrayObj *listOfTicks;
          CTick *getTickByShift(const int shift=0)

          return listOfTicks.At(listOfTicks.Total()-1-shift));

          int OnInit()
          listOfTicks=new CArrayObj();
          return(1);

          void OnTick()
          listOfTicks.Add(new CTick(Bid,Ask));
          //example of accessing ticks 0 and 3, keep in mind you do not have 3 at start!
          double bid0=getTickByShift(0).m_bid;
          double ask3=getTickByShift(3).m_bid;

          void OnDeinit(const int reason)delete listOfTicks;


          If you need really unlimited number of operations - probably 2^31-1 would be enough for you, to speed up you may think of listOfTicks.Step(1000); to have 1000 empty slots and do not resize(=copy all to the new location) the array every tick.






          share|improve this answer






















          • I don't really need "infinite", just one for every bar, so for long back-tests it will require a lot. It's actually running on every bar, not on every tick, but I already have the code for that so I didn't include it in the info. Your answer looks very promising and I shall try it :)
            – Wayne Filkins
            Nov 12 '18 at 20:54











          • 2^31 should do the trick
            – Wayne Filkins
            Nov 12 '18 at 20:55










          • What i'm really trying to do is add the equivalent of how high and low work. For high you can go high[5] or high[400] to grab previous candle highs, and i'm trying to basically make my own variables that do that. I have one that changes constantly based on different conditions, and I need to be able to go var[5] or var[50] at any given time. So like a series in tradingview (converting my tradingview script to mql4)
            – Wayne Filkins
            Nov 12 '18 at 20:58










          • This is what I got with that code: i.imgur.com/RTrihke.png
            – Wayne Filkins
            Nov 12 '18 at 21:21










          • add void OnDeInit(const int reason)delete(listOfTicks); in order to kill remaining objects at end
            – Daniel Kniaz
            Nov 12 '18 at 22:07










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268400%2fhow-do-you-add-an-item-to-an-array-in-mql4%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          It is possible to have an array of primitives in MQL4, the problem here is that you will have to copy them all each tick which is waste of time and resources. If you want to save some more advanced data (let's say MqlTick), you have to store it as the structure or create a class and save classes. It should be really senseless to copy all objects, that is why you should think of applying CList or CArrayObj and adding objects to the end. Simple magic would help to call element with index 0:



          #include <ArraysArrayObj.mqh>
          class CTick : public CObject
          public: double m_bid, m_ask;
          CTick(const double bid,const double ask):m_bid(bid),m_ask(ask)
          ;
          CArrayObj *listOfTicks;
          CTick *getTickByShift(const int shift=0)

          return listOfTicks.At(listOfTicks.Total()-1-shift));

          int OnInit()
          listOfTicks=new CArrayObj();
          return(1);

          void OnTick()
          listOfTicks.Add(new CTick(Bid,Ask));
          //example of accessing ticks 0 and 3, keep in mind you do not have 3 at start!
          double bid0=getTickByShift(0).m_bid;
          double ask3=getTickByShift(3).m_bid;

          void OnDeinit(const int reason)delete listOfTicks;


          If you need really unlimited number of operations - probably 2^31-1 would be enough for you, to speed up you may think of listOfTicks.Step(1000); to have 1000 empty slots and do not resize(=copy all to the new location) the array every tick.






          share|improve this answer






















          • I don't really need "infinite", just one for every bar, so for long back-tests it will require a lot. It's actually running on every bar, not on every tick, but I already have the code for that so I didn't include it in the info. Your answer looks very promising and I shall try it :)
            – Wayne Filkins
            Nov 12 '18 at 20:54











          • 2^31 should do the trick
            – Wayne Filkins
            Nov 12 '18 at 20:55










          • What i'm really trying to do is add the equivalent of how high and low work. For high you can go high[5] or high[400] to grab previous candle highs, and i'm trying to basically make my own variables that do that. I have one that changes constantly based on different conditions, and I need to be able to go var[5] or var[50] at any given time. So like a series in tradingview (converting my tradingview script to mql4)
            – Wayne Filkins
            Nov 12 '18 at 20:58










          • This is what I got with that code: i.imgur.com/RTrihke.png
            – Wayne Filkins
            Nov 12 '18 at 21:21










          • add void OnDeInit(const int reason)delete(listOfTicks); in order to kill remaining objects at end
            – Daniel Kniaz
            Nov 12 '18 at 22:07















          0














          It is possible to have an array of primitives in MQL4, the problem here is that you will have to copy them all each tick which is waste of time and resources. If you want to save some more advanced data (let's say MqlTick), you have to store it as the structure or create a class and save classes. It should be really senseless to copy all objects, that is why you should think of applying CList or CArrayObj and adding objects to the end. Simple magic would help to call element with index 0:



          #include <ArraysArrayObj.mqh>
          class CTick : public CObject
          public: double m_bid, m_ask;
          CTick(const double bid,const double ask):m_bid(bid),m_ask(ask)
          ;
          CArrayObj *listOfTicks;
          CTick *getTickByShift(const int shift=0)

          return listOfTicks.At(listOfTicks.Total()-1-shift));

          int OnInit()
          listOfTicks=new CArrayObj();
          return(1);

          void OnTick()
          listOfTicks.Add(new CTick(Bid,Ask));
          //example of accessing ticks 0 and 3, keep in mind you do not have 3 at start!
          double bid0=getTickByShift(0).m_bid;
          double ask3=getTickByShift(3).m_bid;

          void OnDeinit(const int reason)delete listOfTicks;


          If you need really unlimited number of operations - probably 2^31-1 would be enough for you, to speed up you may think of listOfTicks.Step(1000); to have 1000 empty slots and do not resize(=copy all to the new location) the array every tick.






          share|improve this answer






















          • I don't really need "infinite", just one for every bar, so for long back-tests it will require a lot. It's actually running on every bar, not on every tick, but I already have the code for that so I didn't include it in the info. Your answer looks very promising and I shall try it :)
            – Wayne Filkins
            Nov 12 '18 at 20:54











          • 2^31 should do the trick
            – Wayne Filkins
            Nov 12 '18 at 20:55










          • What i'm really trying to do is add the equivalent of how high and low work. For high you can go high[5] or high[400] to grab previous candle highs, and i'm trying to basically make my own variables that do that. I have one that changes constantly based on different conditions, and I need to be able to go var[5] or var[50] at any given time. So like a series in tradingview (converting my tradingview script to mql4)
            – Wayne Filkins
            Nov 12 '18 at 20:58










          • This is what I got with that code: i.imgur.com/RTrihke.png
            – Wayne Filkins
            Nov 12 '18 at 21:21










          • add void OnDeInit(const int reason)delete(listOfTicks); in order to kill remaining objects at end
            – Daniel Kniaz
            Nov 12 '18 at 22:07













          0












          0








          0






          It is possible to have an array of primitives in MQL4, the problem here is that you will have to copy them all each tick which is waste of time and resources. If you want to save some more advanced data (let's say MqlTick), you have to store it as the structure or create a class and save classes. It should be really senseless to copy all objects, that is why you should think of applying CList or CArrayObj and adding objects to the end. Simple magic would help to call element with index 0:



          #include <ArraysArrayObj.mqh>
          class CTick : public CObject
          public: double m_bid, m_ask;
          CTick(const double bid,const double ask):m_bid(bid),m_ask(ask)
          ;
          CArrayObj *listOfTicks;
          CTick *getTickByShift(const int shift=0)

          return listOfTicks.At(listOfTicks.Total()-1-shift));

          int OnInit()
          listOfTicks=new CArrayObj();
          return(1);

          void OnTick()
          listOfTicks.Add(new CTick(Bid,Ask));
          //example of accessing ticks 0 and 3, keep in mind you do not have 3 at start!
          double bid0=getTickByShift(0).m_bid;
          double ask3=getTickByShift(3).m_bid;

          void OnDeinit(const int reason)delete listOfTicks;


          If you need really unlimited number of operations - probably 2^31-1 would be enough for you, to speed up you may think of listOfTicks.Step(1000); to have 1000 empty slots and do not resize(=copy all to the new location) the array every tick.






          share|improve this answer














          It is possible to have an array of primitives in MQL4, the problem here is that you will have to copy them all each tick which is waste of time and resources. If you want to save some more advanced data (let's say MqlTick), you have to store it as the structure or create a class and save classes. It should be really senseless to copy all objects, that is why you should think of applying CList or CArrayObj and adding objects to the end. Simple magic would help to call element with index 0:



          #include <ArraysArrayObj.mqh>
          class CTick : public CObject
          public: double m_bid, m_ask;
          CTick(const double bid,const double ask):m_bid(bid),m_ask(ask)
          ;
          CArrayObj *listOfTicks;
          CTick *getTickByShift(const int shift=0)

          return listOfTicks.At(listOfTicks.Total()-1-shift));

          int OnInit()
          listOfTicks=new CArrayObj();
          return(1);

          void OnTick()
          listOfTicks.Add(new CTick(Bid,Ask));
          //example of accessing ticks 0 and 3, keep in mind you do not have 3 at start!
          double bid0=getTickByShift(0).m_bid;
          double ask3=getTickByShift(3).m_bid;

          void OnDeinit(const int reason)delete listOfTicks;


          If you need really unlimited number of operations - probably 2^31-1 would be enough for you, to speed up you may think of listOfTicks.Step(1000); to have 1000 empty slots and do not resize(=copy all to the new location) the array every tick.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 22:07

























          answered Nov 12 '18 at 20:16









          Daniel Kniaz

          2,2502715




          2,2502715











          • I don't really need "infinite", just one for every bar, so for long back-tests it will require a lot. It's actually running on every bar, not on every tick, but I already have the code for that so I didn't include it in the info. Your answer looks very promising and I shall try it :)
            – Wayne Filkins
            Nov 12 '18 at 20:54











          • 2^31 should do the trick
            – Wayne Filkins
            Nov 12 '18 at 20:55










          • What i'm really trying to do is add the equivalent of how high and low work. For high you can go high[5] or high[400] to grab previous candle highs, and i'm trying to basically make my own variables that do that. I have one that changes constantly based on different conditions, and I need to be able to go var[5] or var[50] at any given time. So like a series in tradingview (converting my tradingview script to mql4)
            – Wayne Filkins
            Nov 12 '18 at 20:58










          • This is what I got with that code: i.imgur.com/RTrihke.png
            – Wayne Filkins
            Nov 12 '18 at 21:21










          • add void OnDeInit(const int reason)delete(listOfTicks); in order to kill remaining objects at end
            – Daniel Kniaz
            Nov 12 '18 at 22:07
















          • I don't really need "infinite", just one for every bar, so for long back-tests it will require a lot. It's actually running on every bar, not on every tick, but I already have the code for that so I didn't include it in the info. Your answer looks very promising and I shall try it :)
            – Wayne Filkins
            Nov 12 '18 at 20:54











          • 2^31 should do the trick
            – Wayne Filkins
            Nov 12 '18 at 20:55










          • What i'm really trying to do is add the equivalent of how high and low work. For high you can go high[5] or high[400] to grab previous candle highs, and i'm trying to basically make my own variables that do that. I have one that changes constantly based on different conditions, and I need to be able to go var[5] or var[50] at any given time. So like a series in tradingview (converting my tradingview script to mql4)
            – Wayne Filkins
            Nov 12 '18 at 20:58










          • This is what I got with that code: i.imgur.com/RTrihke.png
            – Wayne Filkins
            Nov 12 '18 at 21:21










          • add void OnDeInit(const int reason)delete(listOfTicks); in order to kill remaining objects at end
            – Daniel Kniaz
            Nov 12 '18 at 22:07















          I don't really need "infinite", just one for every bar, so for long back-tests it will require a lot. It's actually running on every bar, not on every tick, but I already have the code for that so I didn't include it in the info. Your answer looks very promising and I shall try it :)
          – Wayne Filkins
          Nov 12 '18 at 20:54





          I don't really need "infinite", just one for every bar, so for long back-tests it will require a lot. It's actually running on every bar, not on every tick, but I already have the code for that so I didn't include it in the info. Your answer looks very promising and I shall try it :)
          – Wayne Filkins
          Nov 12 '18 at 20:54













          2^31 should do the trick
          – Wayne Filkins
          Nov 12 '18 at 20:55




          2^31 should do the trick
          – Wayne Filkins
          Nov 12 '18 at 20:55












          What i'm really trying to do is add the equivalent of how high and low work. For high you can go high[5] or high[400] to grab previous candle highs, and i'm trying to basically make my own variables that do that. I have one that changes constantly based on different conditions, and I need to be able to go var[5] or var[50] at any given time. So like a series in tradingview (converting my tradingview script to mql4)
          – Wayne Filkins
          Nov 12 '18 at 20:58




          What i'm really trying to do is add the equivalent of how high and low work. For high you can go high[5] or high[400] to grab previous candle highs, and i'm trying to basically make my own variables that do that. I have one that changes constantly based on different conditions, and I need to be able to go var[5] or var[50] at any given time. So like a series in tradingview (converting my tradingview script to mql4)
          – Wayne Filkins
          Nov 12 '18 at 20:58












          This is what I got with that code: i.imgur.com/RTrihke.png
          – Wayne Filkins
          Nov 12 '18 at 21:21




          This is what I got with that code: i.imgur.com/RTrihke.png
          – Wayne Filkins
          Nov 12 '18 at 21:21












          add void OnDeInit(const int reason)delete(listOfTicks); in order to kill remaining objects at end
          – Daniel Kniaz
          Nov 12 '18 at 22:07




          add void OnDeInit(const int reason)delete(listOfTicks); in order to kill remaining objects at end
          – Daniel Kniaz
          Nov 12 '18 at 22:07

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268400%2fhow-do-you-add-an-item-to-an-array-in-mql4%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto