Self-regressive Median Coefficient - page 6

 

Hi people,

After two months or so in MQL5, what I meant back then was something like the following. And sorry about posting whole code, it's better for search engines. :)

//+------------------------------------------------------------------+
//|                                                 Expert Indicator |
//+------------------------------------------------------------------+
MqlTick _MqlTick;
long   TimeArray[5];
double PriceArray[5],StatsPriceArray[5],VolumeRealArray[5];
ulong  LastCount=0;

// indicators array
double _iMD[5],_iMax[5],_iMin[5],_iMDSlope[5];
// calculations array
double _iMDSlopeArray[10];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   ArrayInitialize(TimeArray,0);
   ArrayInitialize(PriceArray,0.0);
   ArrayInitialize(VolumeRealArray,0.0);
   ArrayInitialize(StatsPriceArray,0.0);
   ArrayInitialize(_iMD,0.0);
   ArrayInitialize(_iMax,0.0);
   ArrayInitialize(_iMin,0.0);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   SymbolInfoTick(_Symbol,_MqlTick);

   if(_MqlTick.time_msc!=TimeArray[0])
     {
      LastCount+=1;
      for(int i=4;i>0;i--)
        {
         TimeArray[i]       = TimeArray[i-1];
         PriceArray[i]      = PriceArray[i-1];
         VolumeRealArray[i] = VolumeRealArray[i-1];
        };
      TimeArray[0]       = _MqlTick.time_msc;
      PriceArray[0]      = _MqlTick.last;
      VolumeRealArray[0] = _MqlTick.volume_real;

      // indicator calculation
      ArrayCopy(StatsPriceArray,PriceArray,0,0,WHOLE_ARRAY);
      ArraySort(StatsPriceArray);
      // position shifting of indicators
      for(int i=4;i>0;i--)
        {
         _iMin[i]     = _iMin[i-1];
         _iMD[i]      = _iMD[i-1];
         _iMin[i]     = _iMax[i-1];
         _iMDSlope[i] = _iMDSlope[i-1];
        };
      _iMin[0] = StatsPriceArray[0];
      _iMD[0]  = StatsPriceArray[2];
      _iMax[0] = StatsPriceArray[4];
     }
      // median slope
      int n=0;
      for(int i=0;i<(5-1);i++)
      {
         for(int k=i+1;k<5;k++)
         {
            _iMDSlopeArray[n]=(PriceArray[k]-PriceArray[i])/((TimeArray[k]-TimeArray[i])==0 ? 1 : (TimeArray[k]-TimeArray[i]));
            n++;
         };
      };
      ArraySort(_iMDSlopeArray);
      _iMDSlope[0]=(_iMDSlopeArray[6]+_iMDSlopeArray[5])/2;
      _iMDSlope[0]*=60*1000; //(rate per minute);
      

   Comment(
           "Time: "+(string)_MqlTick.time+"\n",
           "Time: "+(string)_MqlTick.time_msc+" [ms]\n",
           "Ask: " +(string)_MqlTick.ask+"\n",
           "Last: "+(string)_MqlTick.last+"\n",
           "Bid: " +(string)_MqlTick.bid+"\n",
           "Volume [real]: "+(string)_MqlTick.volume_real+"\n",
           "Flag: "+(string)_MqlTick.flags+"\n",
           "Lasts: "+(string)LastCount+"\n",
           "Price[-4]: "+(string)TimeArray[4]+" [ms]; "+(string)PriceArray[4]+" ("+(string)VolumeRealArray[4]+")\n",
           "Price[-3]: "+(string)TimeArray[3]+" [ms]; "+(string)PriceArray[3]+" ("+(string)VolumeRealArray[3]+")\n",
           "Price[-2]: "+(string)TimeArray[2]+" [ms]; "+(string)PriceArray[2]+" ("+(string)VolumeRealArray[2]+")\n",
           "Price[-1]: "+(string)TimeArray[1]+" [ms]; "+(string)PriceArray[1]+" ("+(string)VolumeRealArray[1]+")\n",
           "Price[ 0]: "+(string)TimeArray[0]+" [ms]; "+(string)PriceArray[0]+" ("+(string)VolumeRealArray[0]+")\n\n",
           "Max Price: "+(string)_iMax[0]+"\n",
           "Med Price: "+(string)_iMD[0]+"\n",
           "Min Price: "+(string)_iMin[0]+"\n",
           "Med Slope: "+(string)NormalizeDouble(_iMDSlope[0],_Digits)+"\n"
           );
  }
//+------------------------------------------------------------------+
Files:
 
Arthur Albano:

Hi people,

After two months or so in MQL5, what I meant back then was something like the following. And sorry about posting whole code, it's better for search engines. :)

Hi Arthur

  • At first I would like to know what you want to achieve with this code. I can't see any possibilities to use it in trading but I might be wrong. Maybe this could be used in high frequency trading but I'm sure that high frequency trading can't be done by Metatrader.Or is it just a mathematical exercise?
  • It's a little bit confusing if you call your code as an indicator (it's an EA). BTW Is there a reason why it isn't an indicator?
  • You could consider some improving in your code. Static variables instead of global variables, string formatting, array shifting, on deinit cleaning…
  • What is the thousand in this line: _iMDSlope[0]*=60*1000; ?
  • I guess you have a typo in this line: _iMin[i]     = _iMax[i-1];

 
Petr Nosek:

Hi Arthur

  • At first I would like to know what you want to achieve with this code. I can't see any possibilities to use it in trading but I might be wrong. Maybe this could be used in high frequency trading but I'm sure that high frequency trading can't be done by Metatrader.Or is it just a mathematical exercise?
  • It's a little bit confusing if you call your code as an indicator (it's an EA). BTW Is there a reason why it isn't an indicator?
  • You could consider some improving in your code. Static variables instead of global variables, string formatting, array shifting, on deinit cleaning…
  • What is the thousand in this line: _iMDSlope[0]*=60*1000; ?
  • I guess you have a typo in this line: _iMin[i]     = _iMax[i-1];

Petr Nosek:

Hi Arthur

  • At first I would like to know what you want to achieve with this code. I can't see any possibilities to use it in trading but I might be wrong. Maybe this could be used in high frequency trading but I'm sure that high frequency trading can't be done by Metatrader.Or is it just a mathematical exercise?
  • It's a little bit confusing if you call your code as an indicator (it's an EA). BTW Is there a reason why it isn't an indicator?
  • You could consider some improving in your code. Static variables instead of global variables, string formatting, array shifting, on deinit cleaning…
  • What is the thousand in this line: _iMDSlope[0]*=60*1000; ?
  • I guess you have a typo in this line: _iMin[i]     = _iMax[i-1];


I worked for a decade measuring industrial process output through signal processing. Then I switched jobs and a new colleague, who is a daytrader, said I could use my knowledge and experience assembling an EA for the Brazilian derivative market (USDBRL).

So, out of nowhere, I downloaded MT5 and opened a demo account. Before even knowing about any indicators, I tried to use my knowledge to predict direction. The first thing I did was to download stocks data and do quantile regression, which proved to predict values better than averages and linear regression.

Why? Because quantiles swipe out all the noise. Is far more robust than moving averages. I used quantile regression at industrial signal processing. I had chills in my spine when I found the definition of "median price". Although I use HLC as candle price while testing strategies, I know it is not representative to the candle.

In signal processing we have time, not bars. A millions of signals processed per second. And that means physics time. When market is flat, using time instead of bars can give higher predictability for a fact.

So, one can calculate speed, acceleration by filtering noise. But it has to be done using time. And then comes SymbolInfoTick(_Symbol,_MqlTick) in hand. _MqlTick.time_msc gives time in milliseconds (ms). So multiplying it by 1000 gives seconds and by 60 gives us minutes. So, if I calculate speed, the number gives me a very straightforward answer: it is going up or down x monetary units per minute. So I can easily expect the price in one minute time just adding it to last (when available) or bid/ask. Just place (Bid+_MDSlope[0]) and check the results 1 minute later.

At signalprocessing, there is no such a thing as timeframe. Basic time unit is 1 second. And there is no other time unit.

And yes! The code is dirty! :D I only did care about some statics. :) I am not a programmer myself.

If I remember Fourier well, I would say 50% of all indicators are, inf fact, a polynomial equations trying to be converted into a sinusoid. :P For instance, rewritting iAO and optimizing both fast and slow periods gives better results than 5 and 34. If it used the median of 3 would make more sense for smoothing. You can include in it MACD and Alligator.

So, rewritting speed, acceleration using quantiles for smoothing always produces better results in signal processing. I still didn't test comparison for prediction. I lack the skill to do so, or even write an article.

 
Petr Nosek:

Hi Arthur

  • At first I would like to know what you want to achieve with this code. I can't see any possibilities to use it in trading but I might be wrong. Maybe this could be used in high frequency trading but I'm sure that high frequency trading can't be done by Metatrader.Or is it just a mathematical exercise?
  • It's a little bit confusing if you call your code as an indicator (it's an EA). BTW Is there a reason why it isn't an indicator?
  • You could consider some improving in your code. Static variables instead of global variables, string formatting, array shifting, on deinit cleaning…
  • What is the thousand in this line: _iMDSlope[0]*=60*1000; ?
  • I guess you have a typo in this line: _iMin[i]     = _iMax[i-1];

Thanks for warning about the typo!

 

I will add a price mode later to it, so one can choose ask, last or bid. Currently uses only last prices, so it won't run on forex.

Files:
 
Arthur Albano:


I worked for a decade measuring industrial process output through signal processing. Then I switched jobs and a new colleague, who is a daytrader, said I could use my knowledge and experience assembling an EA for the Brazilian derivative market (USDBRL).

So, out of nowhere, I downloaded MT5 and opened a demo account. Before even knowing about any indicators, I tried to use my knowledge to predict direction. The first thing I did was to download stocks data and do quantile regression, which proved to predict values better than averages and linear regression.

Why? Because quantiles swipe out all the noise. Is far more robust than moving averages. I used quantile regression at industrial signal processing. I had chills in my spine when I found the definition of "median price". Although I use HLC as candle price while testing strategies, I know it is not representative to the candle.

In signal processing we have time, not bars. A millions of signals processed per second. And that means physics time. When market is flat, using time instead of bars can give higher predictability for a fact.

So, one can calculate speed, acceleration by filtering noise. But it has to be done using time. And then comes SymbolInfoTick(_Symbol,_MqlTick) in hand. _MqlTick.time_msc gives time in milliseconds (ms). So multiplying it by 1000 gives seconds and by 60 gives us minutes. So, if I calculate speed, the number gives me a very straightforward answer: it is going up or down x monetary units per minute. So I can easily expect the price in one minute time just adding it to last (when available) or bid/ask. Just place (Bid+_MDSlope[0]) and check the results 1 minute later.

At signalprocessing, there is no such a thing as timeframe. Basic time unit is 1 second. And there is no other time unit.

And yes! The code is dirty! :D I only did care about some statics. :) I am not a programmer myself.

If I remember Fourier well, I would say 50% of all indicators are, inf fact, a polynomial equations trying to be converted into a sinusoid. :P For instance, rewritting iAO and optimizing both fast and slow periods gives better results than 5 and 34. If it used the median of 3 would make more sense for smoothing. You can include in it MACD and Alligator.

So, rewritting speed, acceleration using quantiles for smoothing always produces better results in signal processing. I still didn't test comparison for prediction. I lack the skill to do so, or even write an article.

Hi Arthur,
thank you for your story. I would like to split this into two parts.

The first part is using quantile regression in your calculation instead of linear regression or averages. I have no problem with that and it seems to me that quantile regression could be a better choice for the prediction.

The second part is the prediction. What do you want to predict? Do you really believe that you can predict where the price will be in one minute from a ms frame? Each tick changes your median slope dramatically. As I said in my previous post I can imagine using this kind of prediction in high frequency trading but I can't see any possibilities in trading by Metatrader. There is too much noise in lower timeframes like M1 (random walk) and you want to use a ms frame.

I appreciate your approach (something else than MA crosses) although I don't believe in practical use (at least not in this form). I'll try to watch your progress.

Good luck.

 
Petr Nosek:

Hi Arthur,
thank you for your story. I would like to split this into two parts.

The first part is using quantile regression in your calculation instead of linear regression or averages. I have no problem with that and it seems to me that quantile regression could be a better choice for the prediction.

The second part is the prediction. What do you want to predict? Do you really believe that you can predict where the price will be in one minute from a ms frame? Each tick changes your median slope dramatically. As I said in my previous post I can imagine using this kind of prediction in high frequency trading but I can't see any possibilities in trading by Metatrader. There is too much noise in lower timeframes like M1 (random walk) and you want to use a ms frame.

I appreciate your approach (something else than MA crosses) although I don't believe in practical use (at least not in this form). I'll try to watch your progress.

Good luck.

Added a price mode, as FOREX doesn't have last price. I am checking the indicators results. It seems I am calculating them wrong.
 
Next logical step: calculate median time difference, so next predicted price will be last + median time difference * slope.
 
I found the critical error at the code (((InpTicks%1)==1)). Fixed it. :P And added the timelag indicator.
 
Seems I got the median slope calculation correct, finally... :D
Files:
Reason: