Comparing Dollar Volume to potential position size as a buy filter

Hi there,

I've been trying to create a buy filter that checks if a stock has a higher dollar volume than the position size that I would set. The idea is to avoid buying a stock if the dollar volume is lower than my potential position size, and that the $ amount of the position size would also vary depending on whether my equity goes up or down.

After some searching and reading, I think the only way to do this is to perform some basic calculations on bo.equity to inside the backtester object to work out my position size and compare it to a DollarVolume array that I created.

If that works, I would write an if statement that checks if DollarVolume is greater than my position size. If so it would then run bo.ProcessTradeSignals( bar );

At this stage I'm trying to build the code and seeing if the backtester object can read the DollarVolume array values and Trace it to console, however the DollarVolume array is writing out 0.0 for its values.

Is it possible to get DollarVolume recognised in the backtester object? Or if this isn't the way it's meant to work, I would appreciate being pointed in the right direction.

I've included a simplified version of what I've coded so far for reference.

Thanks

dt = DateTime();

SetOption("MaxOpenPositions", 20);
SetPositionSize(5, spsPercentOfEquity);


DollarVolume = V*C;

Plot(DollarVolume, "dollar Volume", colorLightBlue, styleLine);

SetCustomBacktestProc(""); 
if( Status( "action" ) == actionPortfolio ) {
	bo = GetBacktesterObject();
	bo.PreProcess(); // Initialize backtester
	
	for( bar = 0; bar < BarCount; bar++ ) 
	{
		
		_TRACE(NumToStr(bo.Equity) + " " + DateTimeFormat("%Y/%m/%d", dt[bar]) + " " + NumToStr(DollarVolume[bar]));

		bo.ProcessTradeSignals( bar );
		
		
	}
	bo.PostProcess();

}


FastMA = MA(C, 50 );
SlowMA = MA(C, 10 );

Buy = Cross(FastMA, SlowMA);
Sell = Cross(SlowMA, FastMA);

You need to pass arrays from 1st phase to 2nd phase via static variables.

So in your case

DollarVolume = V*C;
StaticVarSet( "MktVol_" + Name(), DollarVolume );

And inside signal loop

mkt_vol = StaticVarGet( "MktVol_" + sig.Symbol ); 
//etc

// NOTE: mkt_vol is array!
// so use subscript -> mkt_vol[i] 

Generally there is trade size limit setting in analysis settings:

26

Limit trade size as % of entry bar volume

This prevents from entering the trades greater than given percentage of entry bar's volume. For example if backtesting daily data and today's volume for thinly traded stock is 177,000 shares, setting this to 10% will limit the maximum trade size to 17,700 shares (10% of total daily volume). This prevents from 'affecting the market' by huge orders.

IMPORTANT NOTE:
Some instruments like MUTUAL FUNDS come without VOLUME data. To backtest such instruments please set this field to ZERO (0) or check "Disable trade size limit weh bar volume is zero" box. This effectively turns OFF this feature. Otherwise you won't be able to enter any trade at all.

https://www.amibroker.com/guide/w_settings.html

2 Likes

Great. Thanks again for your help

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.