Code Errors In Channel Code

 
Errors
1) When placed on mT4 chart the last bar the lines go all weird
2) I think the TureHigh and TrueLow calculations are not correct, please help
3) How does when debug Mt4 code, output variables, during calculations etc...


******Code*****
//+------------------------------------------------------------------+
//| Omega Tradestation Keltner Channels.mq4 |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 White
#property indicator_color2 White
#property indicator_color3 Yellow
#property indicator_color4 White
#property indicator_color5 White


double upper2[], upper1[], middle[],lower1[], lower2[];
extern int period = 50;
extern double factor1 = 1.5;
extern double factor2 = 4.0;

int init()
{
SetIndexStyle(0,DRAW_LINE);
SetIndexShift(0,0);
SetIndexDrawBegin(0,0);
SetIndexBuffer(0,upper2);

SetIndexStyle(1,DRAW_LINE);
SetIndexShift(1,0);
SetIndexDrawBegin(1,0);
SetIndexBuffer(1,upper1);

SetIndexStyle(2,DRAW_LINE);
SetIndexShift(2,0);
SetIndexDrawBegin(2,0);
SetIndexBuffer(2,middle);

SetIndexStyle(3,DRAW_LINE);
SetIndexShift(3,0);
SetIndexDrawBegin(3,0);
SetIndexBuffer(3,lower1);

SetIndexStyle(4,DRAW_LINE);
SetIndexShift(4,0);
SetIndexDrawBegin(4,0);
SetIndexBuffer(4,lower2);

//---- indicators
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;

double avg;

for(int x=0; x<limit; x++) {
middle[x] = iMA(NULL, 0, period, 0, MODE_SMA, PRICE_TYPICAL, x);
avg = AvgTrueRange (period, x);
upper1[x] = middle[x] + (avg * factor1);
upper2[x] = middle[x] + (avg * factor2);
lower1[x] = middle[x] - (avg * factor1);
lower2[x] = middle[x] - (avg * factor2);
}

return(0);
}
//+------------------------------------------------------------------+

//AvgTrueRange = Average(TrueHigh - TrueLow, Length);
//{Average is simple moving average}
double AvgTrueRange (int period, int shift) {
double sum=0;
for (int x=shift;x<(shift+period);x++) {
sum += TrueHigh(x)-TrueLow(x);
}
sum = sum/period;
return (sum);
}
/*
***TrueHigh
If close(Previous) > High(current) then
TrueHigh = Close(previous)
else
Truehigh = high;

***TrueLow
If close(previous) < Low(current) then
TrueLow = Low(Previous)
else
TrueLow = low;
*/

double TrueHigh(int shift){
double k=0;
for (int x=shift;x<=(x+1);x++) {
if (Close[x-1] > High[x])
k=Close[x-1];
else
k=High[x];
return (k);
}
}

double TrueLow(int shift){
double k=0;
for (int x=shift;x<=(x+1);x++) {
if (Close[x-1] < Low[x])
k=Close[x-1];
else
k=Low[x];
return (k);
}
}
 
I think I fized, below is the Omega tradestation Ts200i Keltner Channel, better than Mt4

Also inlcuded my method for debugging, print variable contents to a Csv file ( yuk, why no text file)

****************************************************************************

//+------------------------------------------------------------------+
//| Keltner Channels.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 White
#property indicator_color2 White
#property indicator_color3 Yellow
#property indicator_color4 White
#property indicator_color5 White


double upper2[], upper1[], middle[],lower1[], lower2[];
//int hnd;
extern int period = 50;
extern double factor1 = 1.5;
extern double factor2 = 4.0;

int init()
{
SetIndexStyle(0,DRAW_LINE,STYLE_DOT,1);
SetIndexShift(0,0);
SetIndexDrawBegin(0,period+1);
SetIndexBuffer(0,upper2);

SetIndexStyle(1,DRAW_LINE,STYLE_DOT,1);
SetIndexShift(1,0);
SetIndexDrawBegin(1,period+1);
SetIndexBuffer(1,upper1);

SetIndexStyle(2,DRAW_LINE,STYLE_DOT,1);
SetIndexShift(2,0);
SetIndexDrawBegin(2,period+1);
SetIndexBuffer(2,middle);

SetIndexStyle(3,DRAW_LINE,STYLE_DOT,1);
SetIndexShift(3,0);
SetIndexDrawBegin(3,period+1);
SetIndexBuffer(3,lower1);

SetIndexStyle(4,DRAW_LINE,STYLE_DOT,1);
SetIndexShift(4,0);
SetIndexDrawBegin(4,period+1);
SetIndexBuffer(4,lower2);

//---- indicators
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;

double avg;
// hnd = xOpenFile();//Debug
for(int x=0; x<limit; x++) {
middle[x] = iMA(NULL, 0, period, 0, MODE_SMA, PRICE_CLOSE, x);
avg = AvgTrueRange (period, x);
upper1[x] = middle[x] + avg * factor1;
upper2[x] = middle[x] + avg * factor2;
lower1[x] = middle[x] - avg * factor1;
lower2[x] = middle[x] - avg * factor2;
}
//xCloseFile (hnd);
return(0);
}
//+------------------------------------------------------------------+

//AvgTrueRange = Average(TrueHigh - TrueLow, Length);
//{Average is simple moving average}
double AvgTrueRange (int period, int shift) {
double sum=0;
for (int x=shift;x<(shift+period);x++) {
sum += TrueHigh(x)-TrueLow(x);
}
sum = sum/period;
return (sum);
}
/*
***TrueHigh
If close(Previous) > High(current) then
TrueHigh = Close(previous)
else
Truehigh = high;

***TrueLow
If close(previous) < Low(current) then
TrueLow = Low(Previous)
else
TrueLow = low;
*/

double TrueHigh(int shift){
double k=0;
if (shift==0) return (High[shift]);
for (int x=shift-1;x<=(x+1);x++) {
if (x == (shift + 1)) break;
if (Close[x+1] > High[x])
k=Close[x+1];
else
k=High[x];
return (k);
}
}

double TrueLow(int shift){
double k=0;
//bool m=false;
if (shift==0) return (Low[shift]);
for (int x=shift-1;x<=(x+1);x++) {
if (x == (shift + 1)) break;
if (Close[x+1] < Low[x])
k=Close[x+1];
else
k=Low[x];
/*
if (k==Close[x+1])
m = true;
else
m=false;

xPrint(hnd,Symbol()+" "+Time[x]+" "+x+" "+Close[x]+" "+Low[x+1]+" "+k+" "+m);
*/
return (k);
}
}
/*
int xOpenFile(){
hnd=FileOpen("mydata.csv", FILE_CSV|FILE_WRITE," ");
if(hnd<0)
{
Comment("Unable to open file my_data.csv");
return(0);
}
return(hnd);
}

int xPrint(int hnd, string data){
FileWrite(hnd, data);
return(0);
}

int xCloseFile(int hnd){
FileClose(hnd);
return(0);
}
*/
//----