Been working on this for wayyyyy to long and just plain stuck-- do i use a switch or a enum or something else? Neither switch nor making a emum seem willing to use a integer like below...
What im trying to do is cycle through a group of numbers using gap++.. So in the example below we would start out with gap=1 or gap=1157.. I would gap++ so the next time gap was called-- gap=2 or gap=3365
Use an array ... also please use the SRC button to post code ...
// Please note that this code is untested and serves only as an example int GapList[] = { 1157, 3365, 7758, 8655 }; // An array int GapCount = ArraySize( GapList ); // For arrays, the first position is identified as 0 and not 1for( int GapIndex = 0; GapIndex < GapCount; GapIndex++ ) { Print( GapList[ GapIndex ] ); }
What im trying to do is cycle through a group of numbers using gap++.. So in the example below we would start out with gap=1 or gap=1157.. I would gap++ so the next time gap was called-- gap=2 or gap=3365
So, I would totally use Fernando's methodology myself.
But if you really, really, really wanted to use an enum, you could do something like this:
enum GAPS { _1157 = 1, _3365, _7758, _8655 }; GAPS gap = _1157; for ( ; gap <= _8655; gap++ ) { long lngGap = StringToInteger(StringSubstr(EnumToString(gap),1,4)); Print(lngGap); }
Hi Fernando-- thanks for your help...
So i tried the code and the problem i got was at the start of each new candle it would just print all 4 numbers of the array in the journal... So changed it and made it comment and got rid of the "for" loop... It works now except-- once we hit the end number 8665-- the tester stops.. How can we make it cycle through and then start again after we comment the last number?
//+------------------------------------------------------------------+ //| line1EA.mq4 | //| Copyright 2017, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2017, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict int GapList[] = { 1157, 3365, 7758, 8655 }; // An array int GapCount = ArraySize( GapList ); int GapIndex; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { static datetime candletime = 0; if(candletime != Time[0]) CheckForSignal(); candletime=Time[0]; } //+------------------------------------------------------------------+ // void CheckForSignal() { Comment( GapList[ GapIndex ] ); GapIndex++; }
Hi Anthony... naaa im not stuck on enum.. I just thought that might work to cycle through a group of numbers...But Fernando's array is the way to go.. ............... (noob here)(but trying to learn)..
Looking at your enum function-- id have the same question for you.. Once we hit the last number-- how do we get it to cycle back to the first number and start again?
or seeing as how you would do it the same way Fernando did-- how would you cycle back after the last number and start the sequence again?
The code I posted was as I stated just to "serve only as an example" so you could understand how arrays work and because you requested and I quote:
... What im trying to do is cycle through a group of numbers ...
It is up to you now, to take the example and apply the knowledge as you see fit, but please note, that the way you are cycling you will only be checking one signal per tick and not all possible signals each time a new tick arrives. That is OK as long as that it is exactly the behavior you want.
However, using the ArraySize() function in the global scope may not be the best usage. It might work here because you are defined a "static" array, but when using dynamically set arrays, you cannot use it that way.
Take some time to read the documentation on arrays and array function in order to improve your code.
To cycle back, to the start, use the same logic as in the "for" loop - that is, check for the Maximum size of the array and then, restart. Also, always initialise your variables and use the "strict" format.
#property strict ... int GapIndex = 0; ... void CheckForSignal() { Comment( GapList[ GapIndex ] ); GapIndex++; if( GapIndex >= GapCount ) GapIndex = 0; }
I also recommend reading a book on the subject of coding ...
Forum on trading, automated trading systems and testing trading strategies
Sergey Golubev, 2017.09.16 05:40
Expert Advisor Programming for MetaTrader 4
This book will teach you the following concepts:
- The basic of the MLQ4 language, including variables and data types, operations, conditional and loop operators, functions, classes and objects, event handlers and more.
- Place, modify and close market and pending orders.
- Add a stop loss and/or take profit price to an individual order, or to multiple orders.
- Close orders individually or by order type.
- Get a total of all currently opened orders.
- Work with OHLC bar data and locate basic candlestick patterns.
- Find the highest high and lowest low of recent bars.
- Work with MetaTrader’s built-in indicators, as well as custom indicators.
- Add a trailing stop or break even stop feature to an expert advisor.
- Use money management and lot size verification techniques.
- Add a flexible trading timer to an expert advisor.
- Construct several types of trading systems, including trend, counter-trend and breakout systems.
- Add alert, emails, sounds and other notifications.
- Add and manipulate chart objects.
- Read and write to CSV files.
- Construct basic indicators, scripts and libraries.
- Learn how to effective debug your programs, and use the Strategy Tester to test your strategies.
All of the source code in this book is available for download, including an expert advisor framework that allows you to build robust and fully-featured expert advisors with minimal effort.
When in doubt, think.
int GapList[] = { 1157, 3365, 7758, 8655 }; // An array int GapCount = ArraySize( GapList ); int GapIndex; : GapIndex = (GapIndex+1)%GapCount; // GapIndex++;
GapIndex = (GapIndex+1)%GapCount; // GapIndex++;
Although I agree that this implementation is more concise and "visually appealing", it may be more difficult for a "newby coder" to understand.
I also have a suspicion that it will be more CPU intensive than the use of a "if" statement, especially since we don't know how MetaQuotes implemented it under the hood.
Here are some independent observations: Efficient C Tip #13 – use the modulus (%) operator with caution
- embeddedgurus.com
" when in doubt , think"... haha if you guys had any idea how long i actually worked on this before asking for help---- i was determined to figure it out myself... I wasnt looking for a free ride -- but i just was stuck on the best way to be able to sequence though a group of numbers....and no matter what i searched i couldnt find the answer or where to find the answer to read.....
This thread really helped me to understand that arrays handle a group of numbers very well.... I actually learned a lot.... So Thank You guys very much...
Whroeder1-- i promise to always do my best to figure(and think) it out before asking for help...
Fernando-- ill read the book you posted...
btw--this is what i came up with before i saw your input.. So i was close-- but yours is cleaner.. As a noob-- doing it the cleanest way is often what i miss...
if(GapIndex<=2) GapIndex++; else(GapIndex=0);
I just sometimes get lost in the what is the best took to use for whatever problem im trying to overcome... But im learning by the day ...
Again.. Thanks to all ..
So got to thinking... Using the array above worked well for constant int's.. But what if i wanted to have user definable integers? Such as extern gap1 = nnn/extern gap2 =yyy ..... I read i cant have a variable used in the array.. So what would be the best way to have a list of user definable variables(externs)-- which could then be cycled through in the same way the Array was done above..
I guess my question is how to have a group of variables that can then be cycled through and when the last one is used it starts over again.. Would that be with a array that can use variables or another method.. I have read all day trying to figure this out... But i feel like im looking in the wrong areas..
So got to thinking... Using the array above worked well for constant int's.. But what if i wanted to have user definable integers? Such as extern gap1 = nnn/extern gap2 =yyy ..... I read i cant have a variable used in the array.. So what would be the best way to have a list of user definable variables(externs)-- which could then be cycled through in the same way the Array was done above..
I guess my question is how to have a group of variables that can then be cycled through and when the last one is used it starts over again.. Would that be with a array that can use variables or another method.. I have read all day trying to figure this out... But i feel like im looking in the wrong areas..
Either you have multiple user inputs for each value, which you then assign to an array individually, or you have a single input string of delimited values that you then convert into an array by splitting it up.
Another alternative, is an external file of data, which you can then read, process and assign the values to an array at start-up.
PS! Try reading the book first, then come back to your project! You will learn many of the basic techniques you need for your project that way.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Been working on this for wayyyyy to long and just plain stuck-- do i use a switch or a enum or something else? Neither switch nor making a emum seem willing to use a integer like below...
What im trying to do is cycle through a group of numbers using gap++.. So in the example below we would start out with gap=1 or gap=1157.. I would gap++ so the next time gap was called-- gap=2 or gap=3365
gap=1;
SomeWayToHaveGapValue=below(gap)
{
1=1157;
2=3365;
3=7758;
4=8655;
};
Appreciate any help..