Trend noise balance (from tos)

 
I am moving here from thinkorswim (trying to anyways).  I wrote a "strategy" I used pretty successfully there which I would eventually try to get set up as an EA over here, 
but first I obviously need my indicators set up.  Below is what I have for trendnoisebalance indicator thus far, and below the ### signs is the thinkorswim code for (just for your reference).
I have not finished adding in the portion where the value resets when the EMA's cross yet because there is a simple error in here somewhere I can't get past.  I have tried plotting all of the variables
individually and all work w/ exception to the "trend".  Please let me know if where I am making my error (or errors,  in all likelihood). 

TNB is a great indicator to keep you out of choppy areas, feel free to try it out.

Thanks




//+------------------------------------------------------------------+
//|                                          Trend Noise Balance.mq4 |
//|                                                              TCC |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "TCC"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//--- input parameters
extern int    fastlength=7;
extern int    slowlength=15;
extern int    trendlength=7;
extern int    noiselength=250;
extern int    correctionfactor=2;
//--- buffers
double   TNB[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0,TNB);   
   SetIndexStyle(0,DRAW_LINE);
   string short_name = "TrendNoiseBalance";
   IndicatorShortName(short_name);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
  int start()
    {
     int limit;
     int counted_bars=IndicatorCounted();
  //---- check for possible errors
     if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted
     if(counted_bars>0) counted_bars--;
     limit=Bars-counted_bars;
  //---- main loop
     double cpc=0;
     double smf = 2 / (1 + trendlength);
     for(int i=0; i<limit; i++)
     {
      double rel=Close[i]-Close[i+1];
      cpc=cpc+rel;
      double trend=cpc*smf;
      trend=trend*(1 - smf)+cpc*smf;     
      double   diff=MathAbs(cpc-trend);
      double   noise = correctionfactor * (diff+noiselength)/2;
      double   denom = MathAbs(trend) + MathAbs(noise);
      if(denom==0)
         {
         TNB[i]=0;
         }else
         {
         TNB[i]=  100*(MathAbs(trend)/denom);
         }
      } 

##############################################
thinkorswim code:

input fastLength = 7;
input slowLength = 15;
input trendLength = 4;
input noiseType = {default linear, squared};
input noiseLength = 250;
input correctionFactor = 2;

assert(trendLength > 0, "'trend length' must be positive: " + trendLength);
assert(correctionFactor > 0, "'correction factor' must be positive: " + correctionFactor);

def smf = 2 / (1 + trendLength);

def reversal = TrendPeriods(fastLength, slowLength);

def cpc = if isNaN(reversal[1]) then 0 else if reversal[1] != reversal then 0 else cpc[1] + close - close[1];

def trend = if isNaN(reversal[1]) then 0 else if reversal[1] != reversal then 0 else trend[1] * (1 - smf) + cpc * smf;

def noise;
def diff = AbsValue(cpc - trend);
switch(noiseType) {
case linear:
    noise = correctionFactor * Average(diff, noiseLength);
case squared:
    noise = correctionFactor * Sqrt(Average(diff*diff, noiseLength));
}

def denom = AbsValue(trend) + AbsValue(noise);
plot TNB = if denom == 0 then 0 else 100 * AbsValue(trend) / denom;
plot HalfLine = 50;

TNB.SetDefaultColor(GetColor(9));
HalfLine.SetDefaultColor(GetColor(5));
 
tcc123:

I am moving here from thinkorswim (trying to anyways).  I wrote a "strategy" I used pretty successfully there which I would eventually try to get set up as an EA over here, 
but first I obviously need my indicators set up.  Below is what I have for trendnoisebalance indicator thus far, and below the ### signs is the thinkorswim code for (just for your reference).
I have not finished adding in the portion where the value resets when the EMA's cross yet because there is a simple error in here somewhere I can't get past.  I have tried plotting all of the variables
individually and all work w/ exception to the "trend".  Please let me know if where I am making my error (or errors,  in all likelihood). 

Please read some other posts before posting . . .

Please   edit   your post . . .    please use the   SRC   button to post code: How to use the   SRC   button. 

 
tcc123:

Watch out for type casting  for example . . . 

double smf = 2 / (1 + trendlength);

  . . .   2 / 8 will be typecast to 0 as you have int / int . . .  instead do . . .

double smf = 2.0 / (1.0 + trendlength);
 
  1. SRC
  2. On MT4 v434, division quotient don't give floating point values(Bug??) - MQL4 forum
  3. Contradictory information on IndicatorCounted() - MQL4 forum
  4. double cpc=0;
         double smf = 2 / (1 + trendlength);
         for(int i=0; i<limit; i++)
         {
          double rel=Close[i]-Close[i+1];
          cpc=cpc+rel;
    Always count down. You are using future values to create older lines.
  5. After the first time, limit will mostly be zero, so cpc will always be the same as rel, and trend will always be the same as rel *2/(1+trendlength). Count down and create other buffers so you can use the previous value of cpc and trend.





 
thanks for the help guys, i will try to keep these things in mind if i post again.
 
tcc123:
thanks for the help guys, i will try to keep these things in mind if i post again.
Could you please do as I asked and edit your first post . . .
 
i would like to edit it but i do not see how to edit an existing post...or delete one for that matter.  could you tell me where to do so...i apologize for the ignorance
 
tcc123:
i would like to edit it but i do not see how to edit an existing post...or delete one for that matter.  could you tell me where to do so...i apologize for the ignorance

Hover your mouse in the area marked . . . but on the post you wish to edit or delete.

 

 
tcc123:
i would like to edit it but i do not see how to edit an existing post...or delete one for that matter.  could you tell me where to do so...i apologize for the ignorance
Thank you for editing  
Reason: