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
Fixed the error. MQL does not like swaping of two number that occupy the same position on an array. It writes ZERO. Now Median is faster, I believe, as it is quickselect, and it's worst case scenario is quicksort, the method used with ArraySort(). With help from https://www.mql5.com/en/users/fxsaber https://www.mql5.com/en/forum/275881/page2
#define SWAP(a, b) { if (a != b) a ^= b ^= a ^= b; } #define SORT(a, b, c) {if(a > b) SWAP(a,b); if(a > c) SWAP(a,c); if (b>c) SWAP(b,c)} // Partition using Lomuto partition scheme int partition(int &list[],int left,int right,int pivotIndex) { int pivotValue = list[pivotIndex]; SWAP(list[pivotIndex],list[right]); int storeIndex = left; for(int i=left; i<right; i++) { if(list[i] < pivotValue) { SWAP(list[storeIndex],list[i]); storeIndex++; } } SWAP(list[right],list[storeIndex]); return storeIndex; } int quickselect(int &list[], int left, int right, int k) { if(left==right) return list[left]; int pivotIndex=left+rand()%(right-left+1); pivotIndex=partition(list,left,right,pivotIndex); // The pivot is in its final sorted position if(k==pivotIndex) return list[k]; else if(k<pivotIndex) return quickselect(list, left, pivotIndex - 1, k); else return quickselect(list, pivotIndex + 1, right, k); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnStart() { int A[]={ 7,4,6,3,9,1 }; int size=ArraySize(A); int medianRank=(size-1)/2; int med = (size%2==1)?quickselect(A,0,size-1,medianRank):(quickselect(A,0,size-1,medianRank)+quickselect(A,0,size-1,medianRank+1))/2; return; } //+------------------------------------------------------------------+
So at my surprise, I didn't succeed. The main optimization idea I had was to reduce the number of division operations needed :
As this operation is done in a loop for every candle i, there are a lot of repetitions of the exact same operation. So the idea was to do all the operations once and to memorize them (see attached how I did). However it doesn't improve this speed, even while the numbers of operations was reduced by a factor 16 !
From 64 millions to 4 millions division operations, but no change in execution time. I didn't expect that. That means double arithmetic CPU is very efficient and cached very well all the results.
Also, though this imbricated loop with division operations is time consuming, the main bottleneck in the ArraySort(), the speed impact is more than 3 times the one of the loops. So even if the "division" optimization had worked that global impact would have been low (~20% max).
That was an interesting exercise, even if it failed.
Attached the code (as we are on week-end I didn't pay attention to "live" update).
MLaden's code performance with ArraySort()
MLadens's code using quickselect for median:
So, if the newbie here doing nothing (much!) wrong, it seems quickselect is loosing to ArraySort(). My bad...
MLaden's code performance with ArraySort()
MLadens's code using quickselect for median:
So, if the newbie here doing nothing (much!) wrong, it seems quickselect is loosing to ArraySort(). My bad...
There is very little chance to have mql code to be faster than ArraySort(), even with a better algorithm. MQL5 is a managed language, dynamic arrays indexing needs to be checked at run time. You would need to use static array with constant indexing to hope for an improvement, which is not really useful for most usage.
There is very little chance to have mql code to be faster than ArraySort(), even with a better algorithm. MQL5 is a managed language, dynamic arrays indexing needs to be checked at run time. You would need to use static array with constant indexing to hope for an improvement, which is not really useful for most usage.
I hoped for more slicing, picking and copying in arrays, such as object oriented programming languages, such as Array.Sort(), Array.Median(), Array.Percent(PercentRank, C={0..1}), etc. Hard-coding selection in unsorted arrays should be faster than sorting and selecting, specially because of medians of median. And implementing a med(array) should be done using two-numbers selection algorithm, which is also faster than averaging two selections. But that's for MQL5 developers.
HI all!
I have news! I will be traveling for a week and away from my laptop. So, enough of newbie code for you! :) But I already coded a percentile value based on percent rank. Bu adding the coefficients to price, I have a price projection. But also, the next logical step is to calculate Bowley's (Interquartile) Skewness, which is non-dimensional, but only uses numbers and no model equation (not Pearson), to compare different equities.
Hi people!
After a week at the ocens, I am back!
So, I have news. I decided do calculate the distance from a value to an underlying straight line. So, I did it in a few steps:
1) Use MLaden code;
2) Calculate the Intercept for that line;
3) Calculate the shortest distance (perhaps could be optmized for less loops);
4) Divide it by the last price (I have not decided my mind on last price or the point at the median line);
5) Divide by 3600 (seconds -> minutes);
6) Multiply by 100 to get percent.
I had to change the colors to mach SELL/BUY buttons at MT5. I miss these buttons so much. Sorry Mladen :)
I am not sure also if I get the distance at current time or the perpendicular distance (like a spread), in step 3.
But what is really anoyng me is to get time wright.
So, I have a problem getting distance for any period (step 5). The distance should not change too much, but it seems to change more than it should by changing time period. I have tried using PeriodSeconds(), PERIOD_CURRENT and _Period, but it didn't work as expected.
There goes the code:
Hi people!
I created also an oscillator that calculates the following, which is the quadratic coefficient (second derivative), calculating the acceleration:
Enjoy! :)
Hi people!
I created also an oscillator that calculates the following, which is the quadratic coefficient (second derivative), calculating the acceleration:
Enjoy! :)
But what is really anoyng me is to get time wright.
So, I have a problem getting distance for any period (step 5). The distance should not change too much, but it seems to change more than it should by changing time period. I have tried using PeriodSeconds(), PERIOD_CURRENT and _Period, but it didn't work as expected.
There goes the code:
Why do you use time in your calculation? As I said you'll get in trouble with using time. The time axis isn't linear one. There aren't weekends, closed times for some symbols... on the time axis. Better way is using just candle numbers IMHO. As you can see in my code.
Why do you use time in your calculation? As I said you'll get in trouble with using time. The time axis isn't linear one. There aren't weekends, closed times for some symbols... on the time axis. Better way is using just candle numbers IMHO. As you can see in my code.
The simplification of time could lead to a different t/p. I haven't traded with both. I still do not know how to build a bot to test both versions. But most important of all, your indicator displays when prices come out from the buy zone (slope -) and enters sell zone (slope +). When this happens, one should buy and sell with a calculated t/p. At the other hand, when the slope comes from the sell zone (-) and crosses to the other side (+), one should buy and put a t/p. Both could be, theoretically, calculated with one indicator. Like a sinusoid.
I work many years with signal processing using OSI Soft PI products (ProcessBook, alerts, etc) and others. There is a server side median function. I miss having a moving (mathematical) median. That's why I have difficulties forgetting time. And median is far more robust. So even with many errors in data, they are filtered out too.
And I do not know how to assemble a bot. I have started with Metatrader just a month ago.