Announcement

Collapse
No announcement yet.

Single EFS looking at two Bar Lengths

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

  • #16
    Reading my last post - it wasn't very clear - please accept my apologies !

    I am trying to code the buy and sell signals to work as follows:

    If the latest weekly MA figure is higher than the last one, only allow long positions.

    If the latest weekly MA value is lower than the last, only allow short positions.

    I am a little stuck with the coding, as I am still learning to code in EFS. I tried the following, but it has not worked, and I am not sure why.................but I think it is probably a basic error:

    //grab the MA value for the bar interval we are viewing - this is a daily MA
    nPlot1 = nStudy1.getValue( MAStudy.MA, 0 );
    //grab the MA value for the Weekly bar interval
    nPlot2 = calcAlternateMA( Math.round( fPeriod2 ), nBarTime, sSymbol );

    var Plot2 = getValue(nPlot2, 0); // i.e. get the latest value of the weekly MA
    var Plot2last = getValue(nPlot2, -1); // i.e. get the value of the previous weekly MA
    if (Plot2 == null) return;
    if (Plot2last == null) return;

    //[I THINK THE ERROR IS IN THE ABOVE FOUR LINES]

    I was then adding, as part of my buy condition, the following

    (Plot2 > Plot2Last) to give the condition under which only longs would be allowed, and
    Plot2 < Plot2Last) to give the condition under which only shorts would be allowed.

    To fill it in a bit more, the calcAlternateMA function is a separate one as follows:

    function calcAlternateMA( nBars, fRTime, fSymbol ) {
    var x=0;
    var y=0;
    var fTime = null;
    var nTmp = 0;

    //find our starting offset in the daily bar interval
    while( true ) {
    fTime = getValueAbsolute( "rawtime", -x, fSymbol );
    if ( fTime==null ) return( null );
    if ( fTime<=fRTime ) break;
    x++;
    }

    //calculate the MA
    y=0;
    while( y<nBars ) {
    nTmp += getValueAbsolute( "close", -(y+x), fSymbol );
    y++;
    }

    return( nTmp/nBars );

    }


    I think the error is in the getValue function for the plot2 variable, and I am not sure how to write this part of the code. I am quite sure the rest of the code is accurate, because Chris wrote it ! All I want to do is take the value of Plot2 (the weekly MA value) and compare it with the previous value of PLot2 - as determined by the calcAlternateMA function above. Please could you tell me where I am wrong?

    Many thanks in advance for your help

    Kindest regards

    Neil.

    Comment


    • #17
      Neil:

      Take a look at the attached version of the script. It will set the background color to red when the weekly MA is declining and will set the background color to green when the weekly MA is rising.

      Chris
      Attached Files

      Comment


      • #18
        Chris - you are a star! That is exactly what I wanted. Thanks very muchly.

        Neil.

        Comment

        Working...
        X