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

Indicadores

Termos de Referência

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. 

Respondido

1
Desenvolvedor 1
Classificação
(501)
Projetos
968
74%
Arbitragem
27
19% / 67%
Expirado
100
10%
Carregado
Publicou: 1 artigo, 6 códigos
Pedidos semelhantes
I need help in modifying an amibroker AFL indicator the indicator already works but I need per symbol static variable isolation, parameters persistence per symbol after restart, non declining trailing stop logic, parameter auto restore when switching symbols and a global reset function for static variables. For better understanding As discussed, this is the official offer for restructuring my RAD Chandelier stop loss
Hi , I have some indicators that I want set up on my TV chart and want to create one chart for some and another chart for some others. Plus I want to set up the brackets orders so I can trade from the chart. I have these set up somewhat but need it cleaned up and the way I want them. how much would something like this cost to do? I'm in California and would like you to show me so I can learn to do this when I want to
I would love to see most of my trades going well being in profits! & keep consistent trading without having to blow any accounts be able to identify right trades and have good trading strategy
hi, code me indicator based on 3 candles rejection - need the sourcecode - need to work on all timeframe - non repaint - alert and draws arrows on next candle - draw lines on previous candles - set time window (allowed time to show arrows+alert) - someone with good reputation and able to provide proof of the past works (devs with more than 100 jobs preferably) - you will need to provide sample before accepting u as
Martingale Strategy 50 - 70 USD
I need a gambling bot that implements the Martingale Strategy for betting on roulette. It will be used on platforms like Betcity and Unibet to manage bets effectively. Scope of work - Develop a bot that implements the Martingale Strategy for roulette. - Ensure compatibility with Betcity and Unibet platforms. - Include functionalities for adjusting bet size and managing losses. - Integrate platform-specific features
I need a hft bot that works well on live market. I have tested some bot and they only perform on demo market. If you have any one that is profitable in the live market, pls send a message. I will need to test it before paying. Pls make sure the bot is profitable in LIVE MARKET
Hello there Hpe you are doing good I am in search of a pine script expert developer who can build strategy in apudFlow in pinescript. Kinldy bid on this project if you can do this
European Central Bank (ECB) Interest Rate Decision The European Central Bank left interest rates unchanged at its first policy meeting of 2026, in line with expectations. source: https://www.mql5.com/en/economic-calendar/european-union/ecb-interest-rate-decision '407332776' : added order #481999464 sell 0.01 BTCUSDm at market
I am in search of a profitable bot for scalping Gold. The bot should be ready as at now to trade in a live market with good consistency. It should have a low drawdown. No martingale or grid system. The developer should be able to send a demo so I can test. If you have any profitable EA, pls holla
I need a reliable, clean-coded Expert Advisor built for both MetaTrader 4 and MetaTrader 5 platforms. Main trading behavior: The EA follows buy and sell arrows produced by my custom indicator. Whenever a buy arrow shows up on the chart: if a sell position is currently open → close that sell immediately and enter a buy trade in its place. Whenever a sell arrow appears: if a buy position exists → close the buy and

Informações sobre o projeto

Orçamento
100 - 150 USD
Prazo
de 1 para 7 dias