sine wave

 
Hello everyone. I would like to draw a sine wave with specific frequency and amplitude on the graph. I have created an indicator but it has three problems.
1) When changing timeframe the frequency changes.
2) The wave never exceeds the last candle.
Instead of creating an indicator, perhaps it would be better to draw the wave.
3) The indicator does not work when new bars are created.
If you want to try the code it works on EURUSD monthly timeframe.
In my opinion instead of creating an inidicatore the sinusoid should be designed with ObjectCreate.
Thank you.
//+------------------------------------------------------------------+
//|                                                waveindicator.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

#property indicator_buffers 4

#property indicator_color1 Red
#property indicator_color2 Red
#property indicator_color3 Red
#property indicator_color4 Blue

double extern mediapartenza=1.31896;
double extern ampiezza0=0.09;
double extern fase0=65;
double extern frequenza0=0.005;
double extern ampiezza1=0.09;
double extern fase1=65;
double extern frequenza1=0.005;
double extern ampiezza2=0.05;
double extern fase2=65;
double extern frequenza2=0.005;

double seno0[];
double seno1[];
double seno2[];
double seno3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,seno0);
   SetIndexLabel(0,"frequenza lunga");
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,seno1);
   SetIndexLabel(1,"frequenza media");
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(2,seno2);
   SetIndexLabel(2,"frequenza corta");
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(3,seno3);
   SetIndexLabel(3,"somma onda");





//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
  double pi=3.14159265;
 int    counted_bars=IndicatorCounted();
//----
   for(int i =Bars-counted_bars-1;i>=0;i--){
  seno0[i]=mediapartenza+ampiezza0*MathSin(2*pi*(i-fase0)*frequenza0);
  seno1[i]=mediapartenza+ampiezza1*MathSin(2*pi*(i-fase1)*frequenza1);
  seno2[i]=mediapartenza+ampiezza2*MathSin(2*pi*(i-fase2)*frequenza2);
  seno3[i]=mediapartenza+ampiezza0*MathSin(2*pi*(i-fase0)*frequenza0)+ampiezza1*MathSin(2*pi*(i-fase1)*frequenza1)+ampiezza2*MathSin(2*pi*(i-fase2)*frequenza2);
  
  
  
  
  
   }
   
//--- return value of prev_calculated for next call
   return(0);
  }
//+------------------------------------------------------------------+
 
Not updating last bar is probably because:
i =Bars-counted_bars-1

You should also have the calculation outside the loop, maybe:

   int counted_bars=MathMin(Bars-IndicatorCounted(),Bars-1);
   for(int i=counted_bars; i>=0; i--)

Frequency changes with timeframe because it's based on timeframe index, not time itself.

If you are going to draw more complicated things (you should, sine waves solely by themselves are stupid) you may want to build a template with this:

https://www.mql5.com/en/docs/standardlibrary/canvasgraphics/ccanvas

Documentation on MQL5: Standard Library / Custom Graphics / CCanvas
Documentation on MQL5: Standard Library / Custom Graphics / CCanvas
  • www.mql5.com
Standard Library / Custom Graphics / CCanvas - Reference on algorithmic/automated trading language for MetaTrader 5
 

You've updated from init/start to the new event handlers.

Now update from IndicatorCounted to the values in OnCalculate.
          How to do your lookbacks correctly.

 
Thanks for the interest. I would just like to create sinusoidal waves that do not change frequency based on the timeframe. If I change the timeframe the sine wave must remain the same. I have not found in CCanvas the possibility of creating sonusoidal waves. Could someone give me some examples?
 
Hi guys, I could calculate my sine wave. Now I should graph it. I would like the wave to be drawn only once at boot time. Is it better to create an EA, an indicator or a script? Which Event Handling Functions should I use?
Thank you.
int start()
{

double radianti;
int gradi;
double y;
for (gradi=0;gradi<=360;gradi++)
{
radianti=gradi*(M_PI/180.0);
y=MathArcsin(radianti);

bool  grafico=ObjectCreate(0,"seno",OBJ_ARROW,0,radianti,y);
ObjectSetInteger(0,"seno",OBJPROP_ARROWCODE,158);
}
return(0);
}
 
I can not get what I want. I would like an indicator where you can insert sine waves and then add them with the possibility to change phase and amplitude frequency.
If you specify a sine wave in timeframe H1 if I move in m5 the sine wave must not change frequency. Am I willing to pay for this how much could it cost me?
Thank you
 
88nicola88:

Try maybe by storing data value into a text file and when you change TF, try recreate the graph using the stored values from the text file, that can help you solve your problem.

 
88nicola88:

hello  

did you get your sine wave indicator 

Reason: