so I think I have my simple script almost working. Though now it wont "update" , and my bools all return true.
I think it might have something to do with the allbars?
any ideas
I think it might have something to do with the allbars?
any ideas
PHP Code:
/*****
Avis script written Feb 23 2011
versio 1.1
will alert if moving averages (to be use on a fast chart (ie 30 seconds, 1min) is
showing a trending market
**/
var xFast = null;
var xSlow = null;
var above_lows = false;
var above_fast = false;
var above_previous_sma = false;
var below_highs = false;
var below_fast = false;
var below_previous_sma = false;
var aFPArray = new Array();
function preMain() {
var x;
setPriceStudy(true);
setShowTitleParameters( false );
//initialize formula parameters
x=0;
aFPArray[x] = new FunctionParameter( "fSlow", FunctionParameter.NUMBER);
with( aFPArray[x] ) {
setName( "Slow Period to use for SMA" );
setLowerLimit( 1 );
setDefault( 34 );
}
x++;
aFPArray[x] = new FunctionParameter( "fFast", FunctionParameter.NUMBER);
with( aFPArray[x] ) {
setName( "Fast Period to use for SMA" );
setDefault( 15 );
}
x++;
aFPArray[x] = new FunctionParameter( "fExactlyWhen", FunctionParameter.NUMBER);
with( aFPArray[x] ) {
setName( "How many bars to use to Slow above/ bleow threshold" );
setDefault( 40 );
}
x++;
aFPArray[x] = new FunctionParameter( "fExactlyHowMuch", FunctionParameter.NUMBER);
with( aFPArray[x] ) {
setName( " How much above/below theshold needed for condition # 3" );
setDefault(4);
}
}
//== Main processing function
function main( fSlow,fFast,
fExactlyWhen,
fExactlyHowMuch,
fTickSize)
{
var exactly_when = fExactlyWhen;
var exactly_howmuch = fExactlyHowMuch
if(getBarState() ==BARSTATE_ALLBARS)
{
xFast = sma(fFast);
xSlow = sma(fSlow);
}
var nFast_0 = xFast.getValue(0)
var nFast_1 = xFast.getValue(-1)
var nFast_2 = xFast.getValue(-2)
var nFast_3 = xFast.getValue(-3)
var nFast_4 = xFast.getValue(-4)
var nFast_5 = xFast.getValue(-5)
var nSlow_0 = xSlow.getValue(0)
var nSlow_1 = xSlow.getValue(-1)
var nSlow_2 = xSlow.getValue(-2)
var nSlow_3 = xSlow.getValue(-3)
var nSlow_4 = xSlow.getValue(-4)
var nSlow_5 = xSlow.getValue(-5)
var myCondtion_1_h = low(0) > nSlow_0;
var myCondtion_2_h = low(-1) > nSlow_1;
var myCondtion_3_h = low(-2) > nSlow_2;
var myCondtion_4_h = low(-3) > nSlow_3;
var myCondtion_5_h = low(-4) > nSlow_4;
var myCondtion_6_h = low(-5) > nSlow_5;
var myCondtion_1_l = high(0) < nSlow_0;
var myCondtion_2_l = high(-1) < nSlow_1;
var myCondtion_3_l = high(-2) < nSlow_2;
var myCondtion_4_l = high(-3) < nSlow_3;
var myCondtion_5_l = high(-4) < nSlow_4;
var myCondtion_6_l = high(-5) < nSlow_5;
// condtion 1, are prices above or below slow SMA. Ie has it been trending
// need to figure out how to make function parameter to allow user to specify the time period back for which high/low must be aboe SMA
if(myCondtion_1_h==true && myCondtion_2_h==true && myCondtion_3_h==true && myCondtion_4_h==true && myCondtion_5_h==true)
above_lows = true;
if(myCondtion_1_l==true && myCondtion_2_l==true && myCondtion_3_l==true && myCondtion_4_l==true && myCondtion_5_l==true)
below_highs = true;
//condtion 2 ...is the fast above the slow or vice versa
if ( nFast_0 > nSlow_0 )
above_fast = true;
if (nFast_0 < nSlow_0 )
below_fast = true;
// Condition 3... the current SMA must be significantly higher than it was before. how high and exactly when before are user defined
// time period offset for when to measure how much higher the sma has to be
var ex_when = xSlow.getValue(-exactly_when)
// how much higher the current sma must be comparted to an older sma
if( nSlow_0 > ( ex_when + exactly_howmuch))
above_previous_sma = true;
if( nSlow_0 < (ex_when - exactly_howmuch))
below_previous_sma = true;
var myFlag = "Neutral"
if (above_lows==true && above_fast==true && above_previous_sma==true)
// change background color
// put in label
{
myFlag = "UP"
setBarFgColor(Color.black,0)
setBarBgColor(Color.RGB(192,255,160))
}
else if ( below_highs ==true && below_fast==true && below_previous_sma==true)
{
myFlag = "Down"
setBarFgColor(Color.white,0)
setBarBgColor(Color.RGB(255,160,160))
}
else
{
myFlag = "Neutral"
setBarFgColor(Color.black,0)
setBarBgColor(Color.lightgrey,0)
}
// to add on in the future. If UP or down was true in the recent past, and now a condtion is not (price pierced moving average, or fast above, etc, comment on that.. save it as an object? last one
return new Array(myFlag, xFast, xSlow);
}
Comment