Hi
I thought this would be easy, but after a whole day trying different options, i don't seem to be any closer :(
I just want to have an external variable where I enter a list of the hours to exclude from trading. (just based on server time)
I have created a string array variable, and found some code that I thought might help, and modified that, but I don't seem to be able to pull the values out of the array to compare to TimeHour(TimeCurrent()).
I put the split string code in the init () function as i didn't want this to calculate every tick.
How can I access the values in the array from another function (CheckSessionTime()) which is called in Start()?
As a workaround I created a whole bunch of variables that read from the array, but that seems messy and didn't work anyway.
Declare your array globally not local to int() then you can access it from anywhere . . . while you are at it, change the name of your array, char is a type in the new mql4 . . .
Thanks, made those changes but I still can't seem to get the values.
string stri[]; int NTI; int AR[]; int pos; int hournow; int init() { for(NTI=0;NTI<StringLen(NonTradingHours);NTI++) ///NTI - NonTradingIndex { if(StringGetChar(NonTradingHours,NTI)==44)//code "," { pos++; ArrayResize(AR,pos); AR[pos-1]=NTI; //Print (i); } } ArrayResize(stri,pos+1); for(NTI=0;NTI<=pos;NTI++) { if(NTI==0)stri[0]=StringSubstr(NonTradingHours,0,AR[NTI]); else if(NTI==pos)stri[NTI]=StringSubstr(NonTradingHours,AR[NTI-1]+1); else stri[NTI]=StringSubstr(NonTradingHours,AR[NTI-1]+1,AR[NTI]-AR[NTI-1]-1); NTHvalue1 = StrToInteger(stri[0]); NTHvalue2 = StrToInteger(stri[1]); NTHvalue3 = StrToInteger(stri[2]); NTHvalue4 = StrToInteger(stri[3]); NTHvalue5 = StrToInteger(stri[4]); NTHvalue6 = StrToInteger(stri[5]); NTHvalue7 = StrToInteger(stri[6]); NTHvalue8 = StrToInteger(stri[7]); }
I tried to amend my function...
/// CheckSession time function - checks the current time to the start and end time extern variables. bool CheckSessionTime () { hournow = TimeHour(TimeCurrent()); if (BusinessDay()==true) if (hournow != StrToInteger(stri[NTI])) //(NTHvalue1 != hournow || NTHvalue2 != hournow || NTHvalue3 != hournow || NTHvalue4 != hournow || // NTHvalue5 != hournow || NTHvalue6 != hournow || NTHvalue7 != hournow || NTHvalue8 != hournow) return(1); }
which didn't work and i also added Print statements to the start, but it doesn't output the value from the array.
Print("i - ",NTI," str - ",stri[NTI], "hournow = ", hournow);
Thanks, made those changes but I still can't seem to get the values.
I tried to amend my function...
which didn't work and i also added Print statements to the start, but it doesn't output the value from the array.
Can't you do something like this . . .
/// TradeThisHour function - returns true if it's OK to trade during this hour bool TradeThisHour() { hournow = TimeHour(TimeCurrent()); if( BusinessDay() ) if( StringFind( NonTradingHours, DoubleToStr(hournow, 0) ) == -1 ) return(true); // hour not found return(false); }
Can't you do something like this . . .
that looks a lot cleaner, but haven't managed to get it to work yet.
i integrated that function into mine...
///Allow trading function - if the extern variable is true then the times are checked otherwise trading is allowed anyway. bool AllowTrading() { bool TradeAllowed=false; if (SessionTime==true) { if(TradeThisHour()==true)//if (CheckSessionTime()==true) { TradeAllowed=true; } else TradeAllowed=false; } else TradeAllowed=true; return(TradeAllowed); //Print(CheckSessionTime()); }
interestingly i still get trades during the nontrading hours, but i don't get any print messages for those hours.
if (AllowTrading()==true) Print( "hournow = ", hournow," TradeThisHour =",TradeThisHour());
that looks a lot cleaner, but haven't managed to get it to work yet.
i integrated that function into mine...
interestingly i still get trades during the nontrading hours, but i don't get any print messages for those hours.
Each time you place a trade also print the current hour, the NonTradingHours string, the value of TradeAllowed and then when you have a trade that shouldn't have happened check the variables printed and find out what is going wrong.
Thanks mate - it worked a treat!
No idea what was preventing it from running - just added the print statements, and removed my coding attempt, and it worked fine this morning. I'll just put it down to a late night!
thanks for your help - this is a much cleaner solution than mine.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi
I thought this would be easy, but after a whole day trying different options, i don't seem to be any closer :(
I just want to have an external variable where I enter a list of the hours to exclude from trading. (just based on server time)
I have created a string array variable, and found some code that I thought might help, and modified that, but I don't seem to be able to pull the values out of the array to compare to TimeHour(TimeCurrent()).
I put the split string code in the init () function as i didn't want this to calculate every tick.
How can I access the values in the array from another function (CheckSessionTime()) which is called in Start()?
As a workaround I created a whole bunch of variables that read from the array, but that seems messy and didn't work anyway.