Plot Sine Wave with length x days

Hi,

I'm stuck on how to create a sine wave of varying length in days. I want length to be start of the wave to start of the next wave (daily timeframe, so 1 bar=1 day).

ln = param("Length (days)", 250, 10,500,1);
//CODE HERE
//I CAN SEE sin(x) --- but it's not cycle length
plot(sinewave, "Sin", colorblue);

Thanks for your help :slight_smile:

Starting point:

bi = BarIndex(); // bar counter
TwoPi = 2 * 3.14159;  // trig functions use radians, 1 cycle is 2 * Pi radians
Length = 250; // your cycle length in bars
Plot( sin( TwoPi * bi / Length ), "", colorBlue, styleNoRescale );

Didnt work, just gave some vertical lines.

I put it into Claude, and this came out - and works:

lnn = Param("Length (days)", 250, 10, 500, 1);

sinewave = sin(2 * 3.14159 * BarIndex() / lnn);

Plot(sinewave, "Sin", colorBlue);

It is a breeze single-sentence work for AFL Assistant within AmiBroker:

// Plot a sinewave with definable length and amplitude

// Define parameters for the sine wave
Length = Param( "Length", 50, 10, 200, 1 );
Amplitude = Param( "Amplitude", 10, 1, 100, 1 );

// Calculate the phase for each bar
// BarIndex() gives the 0-based index of the current bar
// We want the phase to go from 0 to 2 * PI over the specified Length
Phase = 2 * 3.1415926535 * BarIndex() / Length;

// Calculate the sine wave value
SineWave = Amplitude * sin( Phase );

// Plot the sine wave
Plot( SineWave, "Sine Wave", colorBlue, styleLine );

// Add a commentary to the chart
printf( "Sine Wave (Length: %g, Amplitude: %g)", Length, Amplitude );

This is Gemini 2.5 Flash which is essentially free (unlike Claude).

3 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.