Hi,
I have tried two formulas for slope but they produce wrong results. Any help is appreciated. Below are the two formulas and attached is a screen shot of the two studies on a graph. Thanks.
(I left out the preMain for space purposes but will send full script if needed)
///Start of 1st Formula
function main(S1K, S1KSmooth, S1D, S1FastColor, S1SlowColor,Multi,AdjustmentU,AdjustmentD,vDegrees ,vDegrFontSize,vDegrTextOffset,vDegrTopOffset) {
var myVar1;
var myVar1_1;
var sDisplayPrice1;
if ( bInit == false ) {
myStudy1 = stochK( S1K, S1KSmooth, S1D );
bInit = true;
}
setBarFgColor(S1FastColor, 0); //(0,238,0), 0);//lime
setBarFgColor(S1SlowColor, 1); //(0,238,0), 0);//lime
//retrieve the current values
myVar1 = myStudy1.getValue(0);
myVar1_1 = myStudy1.getValue(-1);
var Slope1 = ((myVar1/myVar1_1)-1)* Multi;
if(myVar1==null||myVar1_1==null) return;
var adjustedmyVar1 = myVar1+AdjustmentU;
var angle1 = (Math.atan(Slope1) / (Math.PI/180 ));
var FinalDegrees1 = (angle1 );
sDisplayPrice1 = Math.round(FinalDegrees1);
sDisplayPrice1 = Math.round((Math.atan(((myStudy1.getValue(0)/myStudy1.getValue(-1))-1)* Multi) / (Math.PI/180 )));
if ( vDegrees == true){
drawTextRelative(vDegrTextOffset,vDegrTopOffset, " " + sDisplayPrice1 + " deg_%K" , Color.black, null, Text.RELATIVETOTOP|Text.ONTOP|Text.FRAME|Text.BOLD ,"Arial",vDegrFontSize,"Degrees");
}
return new Array( myVar1, myVar2 );
}
///End of 1st Formula
///Start of 2d Formula
function main(S1K, S1KSmooth, S1D, S1FastColor, S1SlowColor,vDegrees,vDegrFontSize,vDegrTextOffset ,vDegrTopOffset,BarsforSlope,Multi) {
if ( bInit == false ) {
myStudy1 = stochK( S1K, S1KSmooth, S1D );
myStudy2 = stochD( S1K, S1KSmooth, S1D );
bInit = true;
}
setBarFgColor(S1FastColor, 0); //(0,238,0), 0);//lime
setBarFgColor(S1SlowColor, 1); //(0,238,0), 0);//lime
myVar1 = myStudy1.getValue(0);
myVar2 = myStudy2.getValue(0);
Slope1 = CheckSlope(myStudy1,BarsforSlope); // sending the CheckSlope function a series
angle1 = (Math.atan(Slope1)/(Math.PI/180 ));
FinalDegrees1 = (angle1);
sDisplayPrice1 = Math.round(FinalDegrees1*Multi);
vCursorDeg = " " + sDisplayPrice1;
vCursorSlope = " " + Math.round(Slope1*Multi);
if ( vDegrees == true){
drawTextRelative(vDegrTextOffset,vDegrTopOffset, " " + sDisplayPrice1 + " deg_%K", Color.black, null, Text.RELATIVETOTOP|Text.ONTOP|Text.FRAME|Text.BOLD ,"Arial",vDegrFontSize,"Degrees");
}
return new Array( myVar1, myVar2,vCursorDeg,vCursorSlope);
}
function CheckSlope(tmpX,NoOfBars){// CheckSlope function S. Hare 8 17 2006
var tmp = tmpX.getValue(-NoOfBars);
if (tmp == null || tmp == 0){
return null;
}
var time = rawtime(-NoOfBars)-rawtime(0);
if (time ==null || time == 0){ // if the check for zero is not made, there is a potential for over-run when dividing by this number
return null;
}
var slope = (tmp - tmpX.getValue(0))/time; // time base seconds, therefore the slope is price change per second
return slope;
}
///End of 2d Formula
I have tried two formulas for slope but they produce wrong results. Any help is appreciated. Below are the two formulas and attached is a screen shot of the two studies on a graph. Thanks.
(I left out the preMain for space purposes but will send full script if needed)
///Start of 1st Formula
function main(S1K, S1KSmooth, S1D, S1FastColor, S1SlowColor,Multi,AdjustmentU,AdjustmentD,vDegrees ,vDegrFontSize,vDegrTextOffset,vDegrTopOffset) {
var myVar1;
var myVar1_1;
var sDisplayPrice1;
if ( bInit == false ) {
myStudy1 = stochK( S1K, S1KSmooth, S1D );
bInit = true;
}
setBarFgColor(S1FastColor, 0); //(0,238,0), 0);//lime
setBarFgColor(S1SlowColor, 1); //(0,238,0), 0);//lime
//retrieve the current values
myVar1 = myStudy1.getValue(0);
myVar1_1 = myStudy1.getValue(-1);
var Slope1 = ((myVar1/myVar1_1)-1)* Multi;
if(myVar1==null||myVar1_1==null) return;
var adjustedmyVar1 = myVar1+AdjustmentU;
var angle1 = (Math.atan(Slope1) / (Math.PI/180 ));
var FinalDegrees1 = (angle1 );
sDisplayPrice1 = Math.round(FinalDegrees1);
sDisplayPrice1 = Math.round((Math.atan(((myStudy1.getValue(0)/myStudy1.getValue(-1))-1)* Multi) / (Math.PI/180 )));
if ( vDegrees == true){
drawTextRelative(vDegrTextOffset,vDegrTopOffset, " " + sDisplayPrice1 + " deg_%K" , Color.black, null, Text.RELATIVETOTOP|Text.ONTOP|Text.FRAME|Text.BOLD ,"Arial",vDegrFontSize,"Degrees");
}
return new Array( myVar1, myVar2 );
}
///End of 1st Formula
///Start of 2d Formula
function main(S1K, S1KSmooth, S1D, S1FastColor, S1SlowColor,vDegrees,vDegrFontSize,vDegrTextOffset ,vDegrTopOffset,BarsforSlope,Multi) {
if ( bInit == false ) {
myStudy1 = stochK( S1K, S1KSmooth, S1D );
myStudy2 = stochD( S1K, S1KSmooth, S1D );
bInit = true;
}
setBarFgColor(S1FastColor, 0); //(0,238,0), 0);//lime
setBarFgColor(S1SlowColor, 1); //(0,238,0), 0);//lime
myVar1 = myStudy1.getValue(0);
myVar2 = myStudy2.getValue(0);
Slope1 = CheckSlope(myStudy1,BarsforSlope); // sending the CheckSlope function a series
angle1 = (Math.atan(Slope1)/(Math.PI/180 ));
FinalDegrees1 = (angle1);
sDisplayPrice1 = Math.round(FinalDegrees1*Multi);
vCursorDeg = " " + sDisplayPrice1;
vCursorSlope = " " + Math.round(Slope1*Multi);
if ( vDegrees == true){
drawTextRelative(vDegrTextOffset,vDegrTopOffset, " " + sDisplayPrice1 + " deg_%K", Color.black, null, Text.RELATIVETOTOP|Text.ONTOP|Text.FRAME|Text.BOLD ,"Arial",vDegrFontSize,"Degrees");
}
return new Array( myVar1, myVar2,vCursorDeg,vCursorSlope);
}
function CheckSlope(tmpX,NoOfBars){// CheckSlope function S. Hare 8 17 2006
var tmp = tmpX.getValue(-NoOfBars);
if (tmp == null || tmp == 0){
return null;
}
var time = rawtime(-NoOfBars)-rawtime(0);
if (time ==null || time == 0){ // if the check for zero is not made, there is a potential for over-run when dividing by this number
return null;
}
var slope = (tmp - tmpX.getValue(0))/time; // time base seconds, therefore the slope is price change per second
return slope;
}
///End of 2d Formula
Comment