- You can't create an array of functions. Create a helper function:
int SellStopsTotal(int iWhich){ switch(iWhich){ case 0: return SellStopsTotalTF0(); case 1: return SellStopsTotalTF1(); } Print(...); return EMPTY; }
- The only thing different between TF0() and TF1() is the timeframe. Instead, pass it as an argument.
int SellStopsTotalTF(ENUM_TIMEFRAMES tf=PERIOD_CURRENT) { if(tf == PERIOD_CURRENT) tf = (ENUM_TIMEFRAMES)_Period; int SellStopsTotal=0; for(int x=0;x<OrdersTotal();x++) { if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==true && OrderType()==OP_SELLSTOP && _Period==tf) { ++SellStopsTotal; } } return SellStopsTotal; }
OrderType()==OP_SELLSTOP && _Period==TimeFrame[x])
An order does not have a timeframe. Period is the current chart's. That is bogus. If you really want to find orders per TF, open them with a magic number MNbase+TF and then you can filter by MN.
Well, you could have an array of class instances which feature some function. Like so:
class Agent { public: virtual void DriveToHeadQuarters()=0; }; class AgentMulder : public Agent { public: void DriveToHeadQuarters() { // cool stuff going on } }; class AgentScully : public Agent { public: void DriveToHeadQuarters() { // other stuff going on } }; Agent *SpecialAgents[2]; int OnInit() { SpecialAgents[0]=new AgentMulder; SpecialAgents[1]=new AgentScully; for(int i=0; i<2; i++) SpecialAgents[i].DriveToHeadQuarters(); }
Albeit that @whroeder1 is spot on with this advice, there's no need to do it that complex.
FWIW, you actually can have an array of function pointers in MQL. The functions need to have the same return types and params, but it can be done.
void p1() { Print("func 1"); } void p2() { Print("func 2"); } void OnStart() { typedef void (*TFunc)(void); TFunc funcs[2]; funcs[0] = p1; funcs[1] = p2; for (int i=0; i<ArraySize(funcs); i++) { funcs[i](); } }
That being said, I've never had a real-life practical use-case for this pattern in MQL.
whroeder1:
- You can't create an array of functions. Create a helper function:
- The only thing different between TF0() and TF1() is the timeframe. Instead, pass it as an argument.
- An order does not have a timeframe. Period is the current chart's. That is bogus. If you really want to find orders per TF, open them with a magic number MNbase+TF and then you can filter by MN.
Thanks I got it sorted out
lippmaje:
Well, you could have an array of class instances which feature some function. Like so:
Albeit that @whroeder1 is spot on with this advice, there's no need to do it that complex.
Excellent!!!
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Is it possible to use function arrays in mql4? As in an array that holds a function in every index so that the following 2 functions can be placed inside an array:
so that its instead
Would also need to be able to place a defined array's values (the TimeFrame[x] array's values) inside the order select section as shown above.
Is this or an equivalent at all possible? Any ideas as to how to accomplish this? So its actually 2 questions here:
1. Can we use functions as array values in any way at all?
2. Can we insert an array's index (actual value) as a variable for a function used inside a function array or in any way at all?
Tried starting the EA with
When trying to compile returns that [x] is an invalid array index and that SellStopsTotal function can only be declared globally; both things which imply that what I'm trying to do is just not supported.