Announcement

Collapse
No announcement yet.

coding question # 2

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • coding question # 2

    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

    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);
        
    setShowTitleParametersfalse );


        
        
    //initialize formula parameters
        
    x=0
        
    aFPArray[x] = new FunctionParameter"fSlow"FunctionParameter.NUMBER);
        
    withaFPArray[x] ) {
            
    setName"Slow Period to use for SMA" );
            
    setLowerLimit);
            
    setDefault34 );
        } 
        
    x++; 
        
    aFPArray[x] = new FunctionParameter"fFast"FunctionParameter.NUMBER);
        
    withaFPArray[x] ) {
            
    setName"Fast Period to use for SMA" );
            
    setDefault15 );
        }

             
        
    x++;     
        
    aFPArray[x] = new FunctionParameter"fExactlyWhen"FunctionParameter.NUMBER);
        
    withaFPArray[x] ) {
            
    setName"How many bars to use to Slow above/ bleow threshold" );
            
    setDefault40 );
        }     
        
    x++;     
        
    aFPArray[x] = new FunctionParameter"fExactlyHowMuch"FunctionParameter.NUMBER);
        
    withaFPArray[x] ) {
            
    setName" How much above/below theshold needed for condition # 3" );
            
    setDefault(4);
        } 



    }

    //== Main processing function
    function mainfSlow,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(myFlagxFastxSlow);


  • #2
    just at a glance,

    If by "won't update" you mean won't plot, use getSeries() for the sma studies by replacing lines78-83 with:
    PHP Code:
    if(getBarState() ==BARSTATE_ALLBARS)
    {
    xFast getSeries(sma(fFast));  
    xSlow getSeries(sma(fSlow));


    Though you declare booleans globaly as false, in main() once their value is changed to true, there is no opposite condition making them false again so they stay true from then on. For this, if appropriate, you can add an "else boolvar = false;" as in:
    PHP Code:
    if(conditionboolvar true;
    else 
    boolvar false
    or change the scope of the boolvar to local instead of global so it starts out as false for every iteration of main() and will be true only upon a condition that changes it to true. To make the variable local delete the global variable declaration from outside of main() and put it inside main(){}.

    Wayne

    Comment


    • #3
      thanks wayne. I realized the problem was where I was declaring the variables.

      all of my bools had to be declared outside the main function, otherwise they weren't changing bynamically with the data, they would always update as true/fasle

      but thanks for the help

      Comment

      Working...
      X