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
(509)
Projects
977
74%
Arbitration
27
19% / 67%
Overdue
101
10%
Free
Published: 1 article, 6 codes
Similar orders
Pip Scalper Bot 60+ USD
i want a trading bot that is aleast 98% sure,and cam also do scalping 99% correct using smart money concept, ICT, ALL technical analysis on it and also put risk management on it
Greetings I need MT5 developer that has expertise in developing a custom indicator for mt5 boom and crash based on my exact details and requirements which would be discuss later. Kindly bid for this project if it is something you can handle for me
Hello, I have a Ctrader indicator with the source code, I was wondering if this possible to convert it to Quantower. Hello, I have a Ctrader indicator with the source code, I was wondering if tis possible to convert it to Quantower., i need an expert who can convert it perfectly
I’m looking for an experienced NinjaTrader developer to complete an existing custom indicator. The project is already partially built and is well organized, completely functional, and well documented. The former developer experienced some personal difficulties and unfortunately cannot continue. Key Requirement (Read Carefully): You MUST have direct, hands-on experience with NinjaTrader and NinjaScript (C#) . This is
I already have a fully developed MT5 Expert Advisor with all required prop firm features, including: Risk management Daily loss & max drawdown limits Spread & slippage filters News filter Trade management system The EA structure is complete. 👉 What I need is a professional developer to replace ONLY the entry logic with a high-quality, rule-based trading strategy. 🚨 STRICT REQUIREMENT (READ CAREFULLY): I am NOT
Hello, I’m looking for an experienced developer who can help convert an existing cTrader indicator into a fully functional Quantower indicator . I already have the complete source code for the cTrader indicator (written in C#) , and I would like the same logic, behavior, and visual output to be accurately replicated in Quantower
Hello Developers I have a Project to get done! i have a simple strategy can you please create the automated forex ea to execute my trading strategy? i need custom ea for tradingview and mt4/mt5 correction: i need a tradingview indicator created that tells me when to buy or sell. and ea in mt4/mt5
Mam kody EA Bot. Chciałbym je dokończyć, dopracować i ukończyć projekty. Chciałbym otrzymać pliki SET po ukończeniu EA. Jeśli jesteś zainteresowany, skontaktuj się ze mną. Szukam doświadczonego programisty do stworzenia dedykowanego doradcy eksperckiego (EA) do tradingu. Programista powinien posiadać solidną wiedzę z zakresu MT5, logiki strategii, wskaźników, zarządzania ryzykiem i backtestingu. Doświadczenie w
Salary ₹25,000 – ₹75,000 per month (based on capability, not years) Performance bonuses based on execution quality & system edge Project-based incentives for high-impact deliveries Annual bonus for consistent performers Perks & Benefits 4 months leave/year (designed for deep work cycles) 15 casual leaves Work on real trading engines, not basic bots Qualification 2–5+ years in MQL5 / MT5 development (preferred) Must
I am looking for an experienced MQL5 developer to create a custom technical indicator for MetaTrader 5. The indicator should combine Supertrend logic with Support/Resistance pullback levels to generate high-probability entry signals. 1. Core Logic The indicator must follow a two-step confirmation process: Trend Identification: Use the Supertrend indicator to determine the primary trend. Entry Signal (The

Project information

Budget
100 - 150 USD
Deadline
from 1 to 7 day(s)