Announcement

Collapse
No announcement yet.

My First Code - MAs added to Bid/Ask Vol.efs

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

  • My First Code - MAs added to Bid/Ask Vol.efs

    Hi, I am just getting my feet wet by modifying the bidaskvolume study. Basically, I want this to remain a non-price study that plots the bid/ask volume(this part is exactly the same as bidaskvolume.efs, except in thin lines, hence the line thickness of 1), and the moving averages of the bid/ask vol on the same study(except in thick lines, hence line thickness of 3).

    Basically, to acheive this, I attempted to merge the MA part of volume with MA code together with the bidaskvol efs.

    Here is the resulting code. It appears to output correctly however I am not sure if the moving averages are working right. From the looks of the code used in the vol with MA study, these are SMAs with a period of 10. Alex or Jason, will you please confirm and verify that the code is working as I meant it to? Also, if you can suggest a simpler or more efficient alternative it would be greatly appreciated.

    Here is the code:

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

    Version: 2.0

    Notes:
    2.0
    - Added validation for 0 bid/ask data.
    ************************************************** ***************/

    var study = null;

    function preMain() {
    setStudyTitle("Bid\/Ask Volume with MA");
    setCursorLabelName("Ask Vol", 0);
    setCursorLabelName("Inside Vol", 1);
    setCursorLabelName("Bid Vol", 2);
    setCursorLabelName("Ask MA",3);
    setCursorLabelName("Bid MA",4);
    setDefaultBarFgColor(Color.lime, 0);
    setDefaultBarFgColor(Color.white, 1);
    setDefaultBarFgColor(Color.red, 2);
    setDefaultBarFgColor(Color.lime, 3);
    setDefaultBarFgColor(Color.red, 4);
    setDefaultBarThickness(1, 0);
    setDefaultBarThickness(1, 1);
    setDefaultBarThickness(1, 2);
    setDefaultBarThickness(3, 3);
    setDefaultBarThickness(3, 4);
    setPlotType(PLOTTYPE_LINE, 0);
    setPlotType(PLOTTYPE_LINE, 1);
    setPlotType(PLOTTYPE_LINE, 2);
    }

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

    var vBidMAArray = new Array();
    var vAskMAArray = new Array();

    function main(nAskMALength, nBidMALength) {
    if (nAskMALength == null) {
    nAskMALength = 10;
    }
    else {
    nAskMALength = Math.round(nAskMALength);
    }
    if (nBidMALength == null) {
    nBidMALength = 10;
    }
    else {
    nBidMALength = Math.round(nBidMALength);
    }
    if (getCurrentBarIndex() < 0) return;

    var nState = getBarState();
    if (nState == BARSTATE_NEWBAR) {
    nBidVol = 0;
    nInsideVol = 0;
    nAskVol = 0;
    vVol = 0;
    vBidMAArray.unshift(nBidVol); //inserts array element to the front of the array
    vAskMAArray.unshift(nAskVol); //inserts array element to the front of the array
    }

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

    var nTempAsk = getMostRecentAsk();
    var nTempBid = getMostRecentBid();
    if (nTempAsk != null && nTempAsk != 0) nAsk = nTempAsk;
    if (nTempBid != null && nTempBid != 0) nBid = nTempBid;

    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;
    }
    }
    vBidMAArray[0] = nBidVol;

    if (vBidMAArray[nBidMALength-1] != null) {

    var vBidSum = 0;

    for (i=0; i <= nBidMALength-1; i++) {
    vBidSum += vBidMAArray[i];
    }
    var vBidMA = vBidSum / nBidMALength;

    } else return;

    vAskMAArray[0] = nAskVol;

    if (vAskMAArray[nAskMALength-1] != null) {

    var vAskSum = 0;

    for (i=0; i <= nAskMALength-1; i++) {
    vAskSum += vAskMAArray[i];
    }
    var vAskMA = vAskSum / nAskMALength;

    } else return;

    return new Array(nAskVol, nInsideVol, nBidVol, vAskMA, vBidMA);
    }

  • #2
    tokyotrader
    As far as I can see both averages appear to be calculating correctly.
    FWIW here is what I did to test them. I saved your efs as test1.efs, then created the following EFS2 script as test2.efs and ran both in Tick Replay

    PHP Code:
    function preMain(){
        
    setCursorLabelName("AskMA",0);
        
    setCursorLabelName("BidMA",1);
        
    setDefaultBarFgColor(Color.lime,0);
        
    setDefaultBarFgColor(Color.red,1);
    }

    function 
    main(){

        var 
    tmp efsExternal("test1.efs");
        var 
    AskMA sma(10,getSeries(tmp,0));
        var 
    BidMA sma(10,getSeries(tmp,2));

        return new Array (
    AskMA,BidMA);

    The result can be seen in the image enclosed below.
    For more information on the efsExternal() function I used in the script see this thread.
    Alex

    Comment

    Working...
    X