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);
}
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);
}
Comment