Announcement

Collapse
No announcement yet.

Moving Average and Price Study

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

  • Moving Average and Price Study

    I got the efs from page 63 04/07/2004. It is called bidaskvolumetotalasline. I made it a histogram and changed it to cumulative askvolume minus bidvolume. It works fine. When I tried to make it a Price study and a Moving average, I ran into a brick wall. If it can be modified by a java speaker I would appreciate it.(It is a modified BidAskVolume.efs)

    /************************************************** ***************
    Provided By : eSignal. (c) Copyright 2003

    Jason K. posted this info 7/18/03:

    File Name: BidAskVolume.efs

    Description:
    Tracks volume traded at the Bid, Ask and inside the bid/ask spread. Displays the volume in three histograms,
    green (ask), black (inside), and red (bid).

    Formula Parameters:
    None.

    Notes:
    When the formula is first applied to the chart, there will not be any volume information displayed. The formula will
    then begin collecting data as trades occur and display the data in real time going forward.

    ################## My Notes ################
    crjaq1 posted this request on 4/7/04:
    How can I merge all three types of data in the BID/ASKVOLUME.EFS study to form a total sum value: line chart per
    one minute interval? Can someone edit the code in order to make this plot. Or is there something similar?
    THANKS

    ************************************************** ***************/

    function preMain() {
    /*************************************
    setStudyTitle("Bid\/Ask Volume");
    setCursorLabelName("Ask Vol", 0);
    setCursorLabelName("Inside Vol", 1);
    setCursorLabelName("Bid Vol", 2);
    setDefaultBarFgColor(Color.green, 0);
    setDefaultBarFgColor(Color.black, 1);
    setDefaultBarFgColor(Color.red, 2);
    setDefaultBarThickness(8, 0);
    setDefaultBarThickness(6, 1);
    setDefaultBarThickness(4, 2);
    setPlotType(PLOTTYPE_HISTOGRAM, 0);
    setPlotType(PLOTTYPE_HISTOGRAM, 1);
    setPlotType(PLOTTYPE_HISTOGRAM, 2);
    *****************************************/

    //Added the following 4 lines
    setStudyTitle("Bid\/Ask Volume Total");
    setCursorLabelName("BidAskInside", 0);
    setPlotType(PLOTTYPE_HISTOGRAM, 0);
    setDefaultBarFgColor(Color.black, 0);

    }

    var nBidVol = 0;
    var nInsideVol = 0;
    var nAskVol = 0;
    var vVol = null;
    var bPrimed = false;

    function main() {
    if (getCurrentBarIndex() < 0) return;

    var nState = getBarState();
    if (nState == BARSTATE_NEWBAR) {
    // nBidVol = 0;
    nInsideVol = 0;
    // nAskVol = 0;
    vVol = 0;
    }

    var vPrevVol = null;
    if (vVol != null && bPrimed == true) vPrevVol = vVol;

    var nAsk = getMostRecentAsk();
    var nBid = getMostRecentBid();
    var vClose = close();
    vVol = volume();

    var vTradeVol = vVol - vPrevVol;

    if (bPrimed == false && vVol != null) {
    bPrimed = true;
    return;
    } else {
    if (vClose <= nBid) {
    nBidVol += vTradeVol;
    } else if (vClose >= nAsk) {
    nAskVol += vTradeVol;
    } else {
    nInsideVol += vTradeVol;
    }
    }

    //Added the following two lines
    var vTotal = nAskVol - nBidVol ;
    return vTotal;

    //return new Array(nAskVol, nInsideVol, nBidVol);
    }

  • #2
    Hello samui,

    I would leave your study as a non-price study and just merge it with the price pane to create the overlay. You have a scaling problem if you make this a price study because the return values of the study are based on the volume scale and not the price scale.

    Adding a moving average of the return value to this study can be quickly accomplished using EFS2 in version 7.9. Here's what you need to do.

    1) Rename main() to something else. Let's use calc() for this example.
    2) Then create a new function called main() and move the first line from the calc function to the new main(). Everything else in calc() can remain unchanged.
    3) Add a "nLength" parameter to main(). This will be used for the moving average periods. Check for a null value of nLength and set it to some default value in that case. Main should now look like this up to this point.

    PHP Code:
    function main(nLength) {
        if (
    getCurrentBarIndex() < 0) return;

        if (
    nLength == nullnLength 10;
        ... 

    4) The calc() function contains the code that creates your volume return series. The reason we are putting this into a separate function is so that we can create a series based on this code that can be passed to the built-in moving average functions. To turn calc() into a series we call this function from main() using efsInternal().

    var myVolCalc = efsInternal("calc");

    5) We now have a variable (or Series Object) that we can use to return the volume series to the chart and it can also be used as the data source for the moving average function. In order to use a custom calculation as the data source for a built-in study, that source must be a Series Object. The efsInternal() function takes care of that for us. Now your main function just needs to call the moving average built-in study and return both the custom series and the moving average series to the chart like below.

    PHP Code:
    function main(nLength) {
        if (
    getCurrentBarIndex() < 0) return;
        
        if (
    nLength == nullnLength 10;
        var 
    myVolCalc efsInternal("calc");
        var 
    myMA sma(nLengthmyVolCalc);
        
        return new Array(
    myVolCalcmyMA);

    6) You'll probably want to add the preMain() settings to format the second return element as you desire.

    PHP Code:
    setCursorLabelName("MA"1);
    setPlotType(PLOTTYPE_LINE1);
    setDefaultBarFgColor(Color.red1); 
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment

    Working...
    X