Announcement

Collapse
No announcement yet.

pls help

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

  • pls help

    I hv written below script so that the background will display below colors when below conditions happen..
    orange: lsma above ema AND RSI>=50
    pink: lsma above ema AND RSI<=50
    lime: lsma below ema AND RSI>=50
    green: lsma below ema AND RSI<=50

    However, it doesn't display show the correct background color.... For example, at ~8:30, the background color should be lime since RSI<=50 and lsma>=ema but it shows pink color instead.

    BTW, I take the 10min data and put it on a 5min chart deliberately for some reasons.

    pls advise. thx



    var amLib = addLibrary("amStudies.efsLib");

    // parameters
    var RSILength = 14;
    var RSI_Higher_Threshold = 50;
    var RSI_Lower_Threshold = 50;
    var StrongUp_Bg_Color = Color.green;
    var Up_Bg_Color = Color.RGB(255,200,150);
    var No_Direction_Bg_Color = Color.yellow;
    var Down_Bg_Color = Color.RGB(128,255,128);
    var StrongDown_Bg_Color = Color.RGB(255,128,0);
    var nLSMALength = 20;
    var nEMALength = 20;

    var bRSIUP = false;
    var bRSIDown = false;

    function preMain() {
    var x=0;
    setPriceStudy(true);
    setStudyTitle("RSI background (10min)");
    setShowCursorLabel(false);
    setComputeOnClose(true);
    }

    function main() {

    // initialization

    if (getBarState() == BARSTATE_NEWBAR) {
    bRSIUp = false;
    bRSIDown = false;
    }

    vRSI = rsi(RSILength,inv(10));
    if (vRSI == null ) return;

    dThisEMA = ema(nEMALength,inv(10));
    dLastEMA = ema(nEMALength,inv(10),-1);
    if (dThisEMA == null || dLastEMA == null ) return;
    dThisLSMA = amLib.amLSMA(nLSMALength,inv(10));
    dLastLSMA = amLib.amLSMA(nLSMALength,inv(10),-1);
    if (dThisLSMA == null || dLastLSMA == null) return;

    if(vRSI >= RSI_Higher_Threshold) {
    if (dThisLSMA >= dThisEMA) {
    setDefaultBarBgColor(StrongUp_Bg_Color);
    } else {
    setDefaultBarBgColor(Up_Bg_Color);
    }
    Trend = "RSI Up";
    BgColor=Color.lime;
    } else if(vRSI <= RSI_Lower_Threshold) {
    if (dThisLSMA <= dThisEMA) {
    setDefaultBarBgColor(StrongDown_Bg_Color);
    } else {
    setDefaultBarBgColor(Down_Bg_Color);
    }
    Trend = "RSI Dn";
    BgColor=Color.red;
    } else {
    setDefaultBarBgColor(Color.yellow);
    BgColor=Color.yellow;
    Trend = "****"
    }
    drawTextAbsolute(2, 22, close(0).toFixed(2), Color.black, Color.white,
    Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATI VETORIGHT, null, 12, "PP");
    // drawTextAbsolute(2, 4, Trend, Color.black, BgColor,
    // Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATI VETORIGHT, null, 12, "RSITrend");
    drawTextAbsolute(2, 4, "RSI:"+vRSI.toFixed(0), Color.black, BgColor,
    Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATI VETORIGHT, null, 12, "RSITrend");

    return;
    }
    Last edited by Richard Yu; 10-10-2005, 06:42 AM.

  • #2
    Richard
    You may want to read through this post that explains some of the issues related to using multiple time frames when generating signals, etc for real time and back testing
    Alex

    Comment


    • #3
      I hv read the article but frankly speaking I am still confused since I am quite lousy in programming.

      Anyway, there seems to be many combinations:
      i) compute on close or not
      ii) use one of below ways to get the data
      - Avg = sma(3,inv(15))
      - Avg.getValue(0)
      - getSeries(Avg)

      So if I want to satisfy below requirements, which combination should I choose?
      - update at bar end only
      - the 5min chart (with 10min MA and RSI data) is in sync with real values at the 5min intervals
      - the charts will look correct when I first load it

      I am quite a lazy student and want to fix it first and then think about the reasons later...

      thx.

      Comment


      • #4
        Richard
        Remove setComputeOnClose() and all instances of dThisXXX (since you will not be needing them) and create instead a dLastXXX for each of the studies as in the following example)
        dLastRSI = rsi(RSILength,inv(10),-1)
        Then enclose all your conditions that color the bars or execute other commands inside a
        if(getBarStateInterval("10")==BARSTATE_NEWBAR){
        which will force the efs to evaluate those conditions only when the 10 min interval is completed.
        For more information and examples on getBarStateInterval() see this article in the EFS KnowledgeBase
        Alex

        Comment


        • #5
          Will it cause error if I change 10 to 5 for below eqt? I would like to show the 10min MA on a 5min chart and get the update every 5 min.... thx.

          if(getBarStateInterval("5")==BARSTATE_NEWBAR)

          Comment


          • #6
            Richard
            If you only want to display the 10min MA on the 5 min chart and are not using it in any of the conditions then simply create the study ie var myStudy = sma(10,inv(10)); and return it to the chart ie return myStudy;
            The conditions should always be evaluated IMO at the completion of the higher interval (hence the 10 min) as explained in the post I linked earlier
            Alex

            Comment


            • #7
              Actually I need to get the 10min RSI and MA value to evaluate some conditions every 5 minute. I hv another efs that needs to do this too.

              How should I write the codes in this case? thx.
              Last edited by Richard Yu; 10-10-2005, 10:21 AM.

              Comment


              • #8
                Richard
                You would enclose those conditions in a separate getBarStateInterval("5") condition. Note however that because the study is based on 10 min data the condition that evaluates to true at the 5 min mark may no longer be true at the completion of the interval.
                Alex

                Comment

                Working...
                X