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
(458)
Projects
902
77%
Arbitration
25
16% / 68%
Overdue
100
11%
Free
Published: 1 article, 6 codes
Similar orders
Trend ScreenerPRO MT5 30 - 40 USD
I found your indicator in the marketplace. It seems to be exactly what I'm looking for. I would like to buy it and to be personalized for my own use. I'm just trading XAU/USD and follow a very good signal on Telegram, GTMO. I'm very successful with him and all of his followers earning a lot of money. I want to get my own Indikator to confirm his signals for my trades before entering. Can you offer me your indicator
I have two trading view indicators that I need combined and backtested if you do that? I have alerts already in place and the win rate looks good I just can't backtest that far. I need a few minor tweaks to the code as well. Some lable fixes, time frames for trading hours and possibly extending the lines for more entries. It's a basic code based off fibonacci numbers with a break and retest. I can send you both the
Hello, I attached the design of a swap cost calculator I need to be programmed for MT4. It should be visually appealing like a dashboard. I need to enter 28 pairs. The inputs are "Balance" or "Equity" and the Lot Amount tradet. The balance or equity can be input by the user manually. The lots tradet as well. The output is the dashboard. The Swap Costs in EUR (or account currency) need to be calculated by the programm
The ea should include indicators and should be able to spot entries on a 1 minute /15 minutes time frame, it has to scalp and automatically add take profit and stop loss the bot must trade small accounts from 12USD
Hope you're well. I have two trading view indicators that I need combined and backtested if you do that? I have alerts already in place and the win rate looks good I just can't backtest that far. I need a few minor tweaks to the code as well. Some lable fixes, time frames for trading hours and possibly extending the lines for more entries. It's a basic code based off fibonacci numbers with a break and retest. I can
Hello, I want to create an MT4 indicator identical to the one shown in the attached image. What I need: – Red arrows = sell signals at reversal zones – Green arrows = buy signals at reversal zones – Arrows appearing at candle closes for confirmation – It should work best on Forex and cryptocurrency pairs on the M1 timeframe. – A high win rate (90% or more). – There should be no repainting or other fraudulent
Hi developers I'm looking for someone who can create an indicatore like the image I'm attaching. The second phase to create a bot out of the indicatore. Waiting to hear from legit programmers. I'll want tk see the indicatore in visual mode before delivery to confirm that I doesn't repaint or place at a delay after market as moved on. The emphasis should be on accuracy without too much lag
How to Make Money in MetaTrader 5 (MT5) Using Scripts and Indicators One of the world's most powerful and widely used trading platforms is MetaTrader 5 (MT5), developed by MetaQuotes Software. Its adaptability enables traders to trade forex, stocks, commodities, cryptocurrencies, and a variety of other financial instruments, in addition to providing robust support for algorithmic trading via custom indicators and
GMG bot 50 - 100 USD
I am seeking the development of a custom, non-repainting technical indicator for use in the forex market, specifically designed to integrate seamlessly with a trading robot (Expert Advisor) on the MetaTrader 5 (MT5) platform. The indicator should combine both trend and momentum analysis to generate high-quality buy and sell signals for automated trading strategies. It must work efficiently across major forex pairs
I want an experienced developer who can work on my existing EA and add additional indicators to make it profitable in trading gold, forex, commodities, and other instruments. The trading robot will detect the market move and place a trade once it meets the requirements. I want a profitable trading robot, which I will test very well before the payment

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)