# Dollar Cost Averaging, but with a filter question

I am writing a dollar cost averaging formula using this:

``````MonthBegin = Month() != Ref( Month(), -1 );
PositionSize = 200;					// \$200 per month
Buy = IIf( MonthBegin, sigScaleIn, 0 );
``````

I would like to amend this so that if C<MA(20) on the monthbegin, store that month's \$200 in cash - and then when the C>MA(20) on monthbegin, buy all the missed months of cash:

Jan-March : C<MA20 - so this is 3x\$200 = \$600 stored as cash, not in shares
April 1st C>MA20: so buy the \$200 for this month + \$600 stored in cash = \$800 purchase
May 1st C>MA20: just buy \$200
etc...

`Buy = IIf( MonthBegin, IIf(C>MA(C,20), sigScaleIn {{{AND BUY NUMBER OF MISSED MONTHS}}}, {{{STORE IN CASH}}}), 0 );`

Thank you as always

You would have to write CBT mid level code
https://www.amibroker.com/guide/a_custombacktest.html

Also see `bo.Cash`, `sig.PosSize`, ...

Here is example output (via `bo.RawTextOutput()`) of what you seem to try to achieve.
"No Entry,..." rows below are the ones where `C < MA` so just adding up \$200 each time at those occasions.

So answer is... yes, it's possible to do that.

Thanks fxshrat.

As a relative newbie, any chance you could post the code you used... I'm a bit lost with the CBT

Hi fxshart, would you be prepared to sell the code you created for this? If so please let me know how best to sort that out. Thanks Matt