/******************************************************************* Provided By : TS Support, LLC for eSignal. (c) Copyright 2002 ********************************************************************/ // Tweaked to fix drift - Garth Doverspike 8/3/03 function preMain() { setStudyTitle("Demand Index - tweaked(b)"); setCursorLabelName("Demand Index", 0); setDefaultBarFgColor(Color.red, 0); addBand(0, PS_SOLID, 1, Color.black); setDefaultBarThickness(1, 0); setDefaultBarThickness(1, 1); setPlotType(PLOTTYPE_LINE,0); setPlotType(PLOTTYPE_HISTOGRAM,1); } vAvgTR = new Array(); var VolAvg_1 = 0; var WghtClose_1 = 0; var BuyPres_1 = 0; var SellPres_1 = 0; //Start Tweak [Garth] - make global so that we can get the values var WghtClose = 0; var BuyPres = 1; var SellPres = 1; var VolAvg = 0; //End Tweak var DMI = 1;//added by ACM var DMI_1 = null;//added by ACM //var DMI_2 = null; var vFlag=0; function main(Length) { if(Length == null) Length = 5; var i; var vSum = 0.0; var hh2, ll2; // Start Tweak - moved up from end of study so that end of bar // values are used. if (getBarState() == BARSTATE_NEWBAR){ WghtClose_1 = WghtClose; DMI_2 = DMI_1; DMI_1 = DMI;//added by ACM vFlag=0; if(getCurrentBarIndex() - getOldestBarIndex() > 5){ BuyPres_1 = BuyPres; SellPres_1 = SellPres; } VolAvg_1 = VolAvg; } VolAvg = volume(0); WghtClose = 0; BuyPres = 1; SellPres = 1; // End Tweak var WtCRatio = 1, VolRatio = 1, BuyP = 1, SellP = 1, Sign = 1, Constant = 1, TempDI = 1, AvgTR = high(0) - low(0); if(getCurrentBarIndex() - getOldestBarIndex() == 1){ for(i = -Length + 1; i >= 0; i++) vSum += volume(i); VolAvg = vSum / Length; } WghtClose = (high(0) + low() + 2 * close()) * .25; if(high() > high(-1)) hh2 = high(0); else hh2 = high(-1); if(low(0) < low(-1)) ll2 = low(0); else ll2 = low(-1); if (getBarState() == BARSTATE_NEWBAR){ for(i = Length - 2; i >= 0; i--) vAvgTR[i + 1] = vAvgTR[i] vAvgTR[0] = hh2 - ll2; } vSum = 0; for(i = 0; i < Length; i++) vSum += vAvgTR[i] AvgTR = vSum / Length; VolAvg = ((VolAvg_1 * (Length - 1)) + volume(0)) / Length; if(WghtClose != 0 && WghtClose_1 != 0 && AvgTR != 0 && VolAvg != 0){ WtCRatio = (WghtClose - WghtClose_1) / Math.min(WghtClose, WghtClose_1) ; VolRatio = volume(0) / VolAvg; Constant = ((WghtClose * 3) / AvgTR) * Math.abs(WtCRatio); if(Constant > 88) Constant = 88; Constant = VolRatio / Math.exp(Constant); if(WtCRatio > 0){ BuyP = VolRatio; SellP = Constant; } else{ BuyP = Constant; SellP = VolRatio; } BuyPres = ((BuyPres_1 * (Length - 1)) + BuyP) / Length; SellPres = ((SellPres_1 * (Length - 1)) + SellP) / Length; TempDI = +1; if(SellPres > BuyPres){ Sign = -1; if(SellPres != 0) TempDI = BuyPres / SellPres; } else{ Sign = +1; if(BuyPres != 0) TempDI = SellPres / BuyPres; } TempDI = TempDI * Sign; if(TempDI < 0) DMI = -1 - TempDI; else DMI = +1 - TempDI; } if(vFlag==0){ if(DMI_1<0.5 && DMI>=0.5||DMI_1>0.5 && DMI<=0.5){ Alert.playSound("ding.wav"); vFlag=1; } } return new Array(DMI,DMI); }