Need Programming Help with My Code

 


So I'm trying to create an indicator, here's the basic process

inputs:

buyzone (Stochastic level)

sellzone(stochastic level)

Fast MA

Slow MA

Sell Signal: 

IF (Stochastics>= buyzone) AND (Fast MA < Slow MA)

THEN (Display Red down arrow on chart)

Buy Signal:

IF(Stochastics=<sellzone) AND (Fast MA > Slow MA)

THEN (Display Green up arrow on chart)


Code is below. Please forgive any basic errors as I'm still very new to programming. 

Thanks



#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrGreen
#property indicator_color1 clrRed
#property indicator_width1 3
#property indicator_width2 3

input       int     MAFastPeriod;
input       int     MASlowPeriod;
input       int     StochasticPeriod;    
input       int     StochasticSell;
input       int     StochasticBuy;
double              up[];
double              down[];

int buyorsellMA;
int buyorsellStochastic;



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,up);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0, 233);
   
   
   SetIndexBuffer(1,down);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1, 234);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void OnTick()
   {
      
         
         
  
   }

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 SlowMovingAverage = iMA(NULL, 0, MASlowPeriod, 0, MODE_EMA, PRICE_CLOSE, 0);
      double FastMovingAverage = iMA(NULL, 0, MAFastPeriod, 0, MODE_EMA, PRICE_CLOSE, 0);
      
      if(SlowMovingAverage > FastMovingAverage)
         buyorsellMA = -1;
         
      if(SlowMovingAverage < FastMovingAverage)
         buyorsellMA = 1;
 //+------------------------------------------------------------------+
//| making the stochastic                           |
//+------------------------------------------------------------------+        
      string signal="";
      
      double KArray[];
      double DArray[];
      
      ArraySetAsSeries(KArray,true);
      ArraySetAsSeries(DArray,true);
      
      int StochasticDefinition=iStochastic(NULL,0,StochasticPeriod,0,1,MODE_SMA,0,0,0);
      
     
      double KValue0=KArray[0];
      double DValue0=DArray[0];
      
      if(KValue0>=StochasticBuy)
         buyorsellStochastic = 1;
      
      if(KValue0<=StochasticSell)
         buyorsellStochastic = -1;
         
  //+------------------------------------------------------------------+
//| making MAKING THE trade signal
//+------------------------------------------------------------------+    
      if((buyorsellMA == 1)&&(buyorsellStochastic==1))
         up[1]=Low[1];
         
         
     else if((buyorsellMA == -1)&&(buyorsellStochastic==-1))
         down[1]=High[1];
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 

Use the </> code button to insert your code.


 
mindeet: So I'm trying to create an indicator, here's the basic process...
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. No question asked, no problem stated, no reply required.
 
whroeder1:
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. No question asked, no problem stated, no reply required.

okay i fixed it. 

regarding point 2, My question is simply "Whats wrong with my code?!"

 
  1. How should we know, you haven't stated a problem.

  2. Do you expect to see arrows in older bars? Unlike most indicators that loop through older bars and displays there also, your code only puts an arrow on bar one, you'll have to let it run for a while. If you change symbols or TF, you loose existing dots.

  3.       double KArray[];
          double DArray[];
         
          double KValue0=KArray[0];
          double DValue0=DArray[0];
    The arrays have no size, so your variables have random values. If you had used strict, you would have know that.
  4. After fixing № 3,You never assign values to your arrays, so your variables have random values.
  5. input       int     MAFastPeriod;
    input       int     MASlowPeriod;
    input       int     StochasticPeriod;    
    input       int     StochasticSell;
    input       int     StochasticBuy;
    
    if(KValue0>=StochasticBuy)
    Your variables have random values. So your condition may never be true.
 
whroeder1:
  1. How should we know, you haven't stated a problem.

  2. Do you expect to see arrows in older bars? Unlike most indicators that loop through older bars and displays there also, your code only puts an arrow on bar one, you'll have to let it run for a while. If you change symbols or TF, you loose existing dots.

  3. The arrays have no size, so your variables have random values. If you had used strict, you would have know that.
  4. After fixing № 3,You never assign values to your arrays, so your variables have random values.
  5. Your variables have random values. So your condition may never be true.

1. The problem is that there are no arrows appearing on screen at any point.

2.  ^

3. Can you explain in more detail or link me to a resource for correcting this error?

4. I don't even understand this one

5. Variables are input by the user before the insdicator runs. 


Thanks for the reply, does my code make sense to you aside from the errors that have been pointed out? 

 

Add #property strict to your code and errors are going to be shown

Starting from a fact that you parameters and arrays are not initialized at all, but since there is much more errors, use the strict statement to help you pinpoint all the errors you have in your code

 
Mladen Rakic:

Add #property strict to your code and errors are going to be shown

Starting from a fact that you parameters and arrays are not initialized at all, but since there is much more errors, use the strict statement to help you pinpoint all the errors you have in your code

I have added that line of code at the top. No errors, just one warning about a data loss due to type conversion.

Reason: