Regression + Sine wave - fit an indicator and return the period of the sine as ouput

Indicators

Specification

This project is only suited for programmers with a very sound background in mathematics.

I have an indicator, it's an oscillator. I want to plot a “sine curve” on it that fits its shape closely: for this it needs to calculate a sine wave equation y(t)=A*sin(wt+theta) with amplitude, phase and period (frequency). One of the buffers in the code should be the period of the sine wave: this is what I am interested in as an input for other indicators, so I should be able to use that: comment in the code which buffer it is in, and the code should be written that it is easy for me to take that value as an input for another indicator, or other code that will be added by me.

 So the idea is to break down an existing indicator into sine waves.

 The transition from the first sine wave (orange) with the first period (40) into the next sine wave (blue) with the next period (50) can be smooth if the difference in periods is not too large and the phase of the next wave is chosen to smoothly continue the previous one: the key thing is that the phase will have a value as if it was continuing the same wave: only the period and amplitude change: if you look at the red rectangle, you see that the blue curve is almost a continuation of the orange curve: in sine wave terminology this means that the phase of the combined wave only changed a little more than what one would expect if the orange wave would continue. 

 s1

The important element here is that the shape needs to be continuous: no sudden changes unless the input signal (indicator it’s calculated on) changes really suddenly.

s2 

So above the picture is somewhat wrong because it thinks of this task as trying to fit segments of sines to the input signal: this is not really the case: you need to use the trick with the phase (use previous’ bar sine’s phase) to create a continuous output signal.

s3 

On this image above, if you look at the blue curve in Figure 1, you see that it first goes up, it accelerates its increase, then it reaches a maximum and then it goes down: first a bit quicker, then a bit slower. Now if you cut the blue line where the small vertical red lines are drawn you get pieces A, B & C as in Figure 2: green, yellow and gray respectively. Now if I look at the sine wave indicator that I attached, and leave the phase at 0 and the period at 1, and I play a bit with the period variable to get a sine wave from which part A could be cut out of, I see, after a bit of trial and error, that part A could be a part from a sine wave with a period of 40 (approximately: I did this manually, so I'm probably off by a more than a few digits): as you see in Figure 3: the red curve (sine wave with period 40) can pretty much follow the green curve (part A) without any interruption. The same goes for Figure 4 where the yellow curve (more or less: remember I drew it manually) is the start for a continuously flowing sine wave with period 50. In Figure 5 we see again that part C fits right into a part of a sine wave with period 120.

 So in this example, the blue line is a combination of 3 sine waves: one with period 40, that turns into one with period 50, that gradually transforms into one with period 120. The interesting element is that we can see this in real time: if the period of the wave would be a constant 40,like in part A, there would be no fit any more. In fact there is probably an increase between part A and B: I should have put another piece in there because the blue line keeps increasing too long: this is where the period should be increased, and maybe even the amplitude also. So you are actually continuously fitting pieces of sine waves onto your input data.

 Please use this type of loop:

 int start()

  {

   int limit;

   //double ...;

   int counted_bars=IndicatorCounted();

   if(counted_bars<0) return(-1);

   if(counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;

    for(int i=0; i<limit; i++)

   {   

    // here comes your code

    }

   return(0);

  }

I can give you a primitive sine wave indicator in mql4 (that doesn't change phase, frequency and amplitude) and a linear regression indicator. 

Take a look at the image below: the upper panel is candlestick chart, middle panel is a stochastic taken as input signal, and bottom panel is the result of the indicator (a previous attempt). It doesn't matter that price and indicator are not maximally correlated all the time, what matters is the very strong correlation between the input (middle panel) and the output (bottom panel); You see the shape is almost the same:

s4 

I am not interested in the values of the Amplitude or the Phase, only in the buffer that contains the period for each bar of the best-fitting sine wave (the one that is closest to the input signal).

The only problem with the screenshot above is that the output of the indicator (bottom panel) was not as smooth as the input: it looked at the input signal and then tried to fit a number of separate, individual sine waves. While if you use the trick with taking phase as a simple variable that increases step-wise and you only vary amplitude and period, this can lead to a smooth shape that fits the input signal exactly and can give me what I need (the period of the wave).

As the indicator to calculate it on you can just use iStochastic(0,0,15,3,3,0,0,i); The code should work on other indicators that do not have this particular limit of 0 at the bottom and 100 at the top so use iCustom. If you use the regression method there should be very little difference: the smaller the difference between signal and source, the better.

 

To illustrate the concept of a continuous curve with varying period: keep in mind it's done manual, so it's only an approximation: if the period would have been constant (with also constant amplitude), then the thick blue line would have continued along the light blue dots, but we didn't, so something changed: either amplitude or period or phase or all 3: instead of the blue dots, the best fitting sine curve continued on the yellow dots, so here the period from the beginning of the yellow dots to the end of the yellow dots didn’t change! So this is an example where the period didn’t change much, but the amplitude changed dramatically (resulting in the yellow dots, not the light blue dots).

 s5

The good part about this technique is that you can adjust the period instantaneously, and you don’t have to worry about the lag of Fourier-based techniques: if you look at this image:

s6 

You can see that the ideal period of the blue line is changed instantaneously, without delay, in the bar where it occurs. From point A to B it’s pretty much constant, but then you see that the period grows dramatically in point B, and it stays almost constant from B to C, and then in C it drops a lot because there the blue curve starts to wiggle in short moves. Here is a bigger example of what the period variable should look like (again it is done manually, so it is an approximation): 

ideal period 2 

Don't mind the values, just look how they are relative to each other and at what point they change.  Within 1 green rectangle the period is constant. Between 2 rectangles, where I drew the vertical green lines, is where the period changes (more or less). Do you understand what I mean by this? Why it changes only at those periods? 

Here is another example, I drew it manually to show my logic or the way of thinking when the period changes. I don't need to see the rectangles in the indicator, but the algorithm needs to decide in the same way as my explanation: 

s7 

If you look at this screenshot, the period only changes in point E (the middle of box 6). Why? Because if wouldn't have changed, we would have continued along the yellow dots. Do you see that it now becomes a different period?

To take an abstract example:

some simple code in R (a statistics program) shows when you get a smooth transition from 1 period to the next:

theta<-0

t<-200

pvector<-c(rep(50,t/2),rep(100,t/2))

waves<-(1*sin(2*pi*1/pvector*(1:t)+theta))

plot(waves,type="l")

lines((1*sin(2*pi*1/50*(1:t)+theta)),lty=3,col="red")

lines(waves,lwd=2)

so first I create a vector for the period: pvector: that contains 200 observations: 100 times 50 and 100 times 100, I then use this pvector as the period for the black line: a combination of 2 sine waves. Now the question is: when does the period of the black line change? Answer: in bar number 100, you see that the dotted line and the black line are no longer on top of each other: they start to separate. So in bar number 100, the period needs to be adjusted: 

s8 

Take a look at this image: It is a curve with non-constant amplitude, but constant period:

s9 

And here it is the other way around: amplitude is constant (50), period is variable: 

 s10

Now what algorithm would get the result of the first image for period? And what algorithm would get the result of the image above? And then finally, combine these 2 algorithms and we have our solution. 

 

If you have read all the way to here I know you are serious about this, (or you want to program it for yourself...) so I invite you to bid. If the price is too low, contact me, maybe I can change it. But this really is not a very hard project for the right person. 

Responded

1
Developer 1
Rating
(461)
Projects
902
77%
Arbitration
25
16% / 68%
Overdue
100
11%
Free
Similar orders
I need a developer that can fix a indicator i have, I have a table that looks at emas and display bullish/bearish, I have spy on the table but its not always updating and need help fixing this I have this table and the first 6 rows looks at the chart symbol and if price is above 12 ema it will sows bullish under the 5/12 ema coulmn and bullish if its above, and same under the 34/50 colmun, if price is below 50 ema
I need an EA in MT5 that trades based on volatility with market orders. It must check and correctly process possible errors in trading operations. Entry and exits based on volatility threshold
Modificar EA 30+ USD
Es posible descompilar un EA de mt4? Tengo uno y me gustaría descompilarlo y poder tener el archivo haber como funciona de verdad. Saludos amigos programadores. espero tenga solución este problema
Hello, I am looking to have two custom indicators developed for integration into an Expert Advisor (EA): 1. Malaysian SNR (Support and Resistance) Indicator : This indicator should accurately identify and plot support and resistance levels based on Malaysian trading principles and methodologies. 2. Break of Structure Indicator : This indicator should detect and signal breaks in market structure, indicating potential
Hello Everyone, Herewith sending the existing Trading view Indicator, and required the same Indicator with all display into MT5. Trading view Indicator Screen shot attached for more reference. Price is negotiable
Hello there, I need a professional mt4 developer who can perfectly help me to backtest my mt4 trading strategy on these trading pairs, XAUUSD H1 PEPPERSTONE 1:200 LEVERAGE. The strategy would be provided once you bid to this task
GIOVA 30 - 50 USD
I need a skillful programmer that can Automate my trading indicator into an EA . System has 2 indicators. One of the Indicator is to know the Direction of Trend on H1 and the second indicator is to be use to enter trade on M5 in line with the H1 Trend The EA must have 1. TP and SL, 2. Trailing Stop Loss, 3. ability to add more trade when there is signal in line with H1
Hello, I am looking for a professional who can assist me with the following tasks: Create a used replica of my trading setup from TradingView. Copy and configure this setup in my Tradovate account. This is exactly what I need. Your assistance would be greatly appreciated this is exactly what i want https://youtu.be/WOMA8Ift9r8?si=PPf0CHNrZkZ0InHN
Hello, i need professionalthat can help me handle this, I want used replica to of my trading From trading view And copy to my tradovate this is exactly i need https://youtu.be/WOMA8Ift9r8?si=PPf0CHNrZkZ0InHN
need a skilled programmer to input parameters in existing indicator. Adding timeframes in the indicator. I have source code file for this indicator. More details will be provided upon selection of job

Project information

Budget
100 - 150 USD
VAT (21%): 21 - 31.5 USD
Total: 121 - 181.5 USD
For the developer
90 - 135 USD
Deadline
from 1 to 7 day(s)