Dollar Cost Averaging
Author: rmpwealth
Creation Date: 7/3/2010 8:48 AM
profile picture

rmpwealth

#1
I would like to compare the results of my strategies with the results of a simple Dollar Cost Averaging strategy using the same equity over the same time period, but do not know how to program a DCA strategy. Can anyone help?

NOTE: While it would be nice to have DCA results superimposed onto the same charts & tables as the other strategies (i.e. -- the way W-L shows Buy & Hold results), this is not necessary! I am willing to run DCA strategies separately and make the comparisons manually, provided that I know how to setup the DCA strategy parameters to get valid "apples to apples" comparisons with the strategies to which I want to compare it. Particulary confusing to me is position sizing -- a DCA strategy adds regular amounts of money at regular time intervals, whereas other strategies add money in amounts, and at dates, that are determined by the trades that are implemented by the strategies.

Thanks in advance for your help.
profile picture

Eugene

#2
QUOTE:
but do not know how to program a DCA strategy.

Something like that (on-the-fly coding)?
CODE:
Please log in to see this code.
profile picture

Ninozah

#3
Hello Eugene,
Fidelity told me that you are the best expert on this forum. Is there a complete "cut and paste" Dollar Cost Average algorithm? So it is ready to run. With maybe 2 variables on the input screen: the day of the month and the dollar amount.
Thank you so much.
Nino
profile picture

Eugene

#4
Hi Nino,

It's natural that certain ideas require programming and customization. Being just a technique, a tool, Dollar Cost Averaging is one of them. Motivated trading system authors need to add a DCA logic (like on example above) in their own code-based strategies.

A pure DCA strategy with three parameters (entry date, the fixed amount of money to invest, and the investment horizon) found in Wikipedia can be coded like this:

CODE:
Please log in to see this code.
profile picture

Ninozah

#5
Thank you so much Eugene
I realy appreciate. This works great as you wrote it.
PS. In my calculations I would need another parameter. Initial price.
How is that written in this programing language please?
Here is my logic: IF Bar = 1 then InitialPrice = Close[bar]
PS. I haven't programmed in many years, at that time we used FORTRAN and BASIC
profile picture

Ninozah

#6
Ahh, here comes the fun part. The first code you sent me works fine but skips trades. It buys on 3/1/2012, 5/1, 6/1, 8/1, 10/1, 11/1, 2/1/2013, 3/1/2013.
It skips in 2012 April 1st, July 1st, September 1st, December 1st and January 1st 2013. I use Scale: Daily, Data Range 280 bars, Position size $5000. I ran on all Dow 300 dataset.
Ideas please?
profile picture

Eugene

#7
2012/04/01 and the rest were holidays.

The old logic of buying when the month changes was left commented in the code for you to experiment and play with. Edit the code in the following way to always purchase on first trading day of month:

Was (fixed date):
CODE:
Please log in to see this code.


Edited (first of month):
CODE:
Please log in to see this code.

profile picture

Ninozah

#8
Thank you again so much Eugene. I do not to monopolize your time, I wish I could understand the code better. Is there a pdf file which explains all the syntax and variables?
For example this code now buys everything correctly every first of the month. But the share size does not correspond to my manual calculations. Also with 13 correct buys, there are also 10 exits! First 10 have exits and just last 3 are still open, whereas all should be still open since there is no expicit "SellAtClose" anywhere in the code. I suspect it may have something to do with these:

CODE:
Please log in to see this code.


Thank you.
profile picture

Eugene

#9
QUOTE:
Also with 13 correct buys, there are also 10 exits!


Correct. If you haven't already played with the "Exit after" slider, I urge you to do so. By default, the exit is set to 60 days whereas entries happen exactly every month.
profile picture

Ninozah

#10
Thank you again Eugene. Finding my way around.
I am stuck with one error:

CODE:
Please log in to see this code.



If X is positive I want to buy SetShareSize od stock and this part works fine.
If X is negative I want to sell SetShareSize od stock and this part does not compile.
I get an error "No overload for method 'SellAtClose' takes 1 argument.
profile picture

Eugene

#11
You will find the answer to what a "Position pos" is in the WealthScript Programming Guide > Programming Trading Strategies > Trading Signals.
profile picture

Ninozah

#12
I am simply asking how to sell precalculated position size. Not Last position size, not all positions. But the size of position equal to the number calculated few lines above using function SetShareSize.
I do not think I will become an expert C# programmer, I appreciate help (the only help I got so far) but I feel your help is directed at me becoming an expert programer. I am not sure I can become one. I am willing to pay someone to help me, I tried to find experts in C and WealthLab using google search, you mentioned that WealthLab canceled the forum to meet and match programmers, Fidelity says they cannot help with programming. They said I am not the only one needing help of the similar type. I looked at other prewritten examples but did not find any that calculates position size within the code, only thru position size panel.
The Quick Ref may be very explanatory to you, but it is not to me. The SetShareSize function calculates the size I need. Any subsequent BuyAtClose implies this share size, but any subsequent SellAtClose does not. To me that is illogical. So my question is direct:
Can you please write the subsequent line of code where SellAtClose will sell the number of shares calculated previously by "SetShareSize" ie.
SetShareSize( investmentAmount / Close[bar] );
Thank you
profile picture

Eugene

#13
Have you switched to "WealthScript Override (SetShareSize)" mode? You could find in the QuickRef that this is a prerequisite to making SetShareSize function like intended.

QUOTE:
Any subsequent BuyAtClose implies this share size, but any subsequent SellAtClose does not. To me that is illogical.


Any subsequent ExitAtMarket in my strategy exits precisely the size that BuyAtClose took. The strategy above was written to allow to take multiple positions in the same security, as inspired by Wikipedia's description of DCA - maybe it confused you. Set a monthly schedule by typing in a number less than 30 bars in "Exit after", and only a single position will be taken.

Alternatively, here's single-position DCA strategy that holds strictly for one month after entering on 1st trading day of month and exits at close on the last trading day of month:

CODE:
Please log in to see this code.


Note: requires installed Community Components (will error if not; install the library and restart WLP before using).

Hope that helps.
profile picture

Ninozah

#14
QUOTE:
Have you switched to "WealthScript Override (SetShareSize)" mode?

Yes I did. And as I said it works perfectly with BuyAtClose.

So when I looked at Quick ref guide I noticed that BuyAtClose requires only one argument (int bar)
"BuyAtClose(int bar);" this is from the quick ref
but the SellAtClose requires TWO arguments (int bar, Position pos)
SellAtClose(int bar, Position pos); this is from the quick ref

So this second argument "Position pos" is what I am struggling with. Position size needs to be specified, it is not taken automatically from SetPosSize as it is with BuyAtClose.

So my question is, how will the code look like if I want to "SellAtClose" instead of "BuyAtClose":
Replacing the line "BuyAtClose( bar );" with "SellAtClose( bar );" creates the compilation error
CODE:
Please log in to see this code.
profile picture

Eugene

#15
You specify a position size to an exit method only if you wish this to become a partial exit. You can not pass an arbitrary size to exit from a position - it works in a different manner in Wealth-Lab: WealthScript Techniques | Splitting a Position into Two. If you're after making partial exits to a position, this would have required a design beyond the quick help I had intended to provide with Dollar Cost Averaging strategy as described in Wikipedia/Investopedia.

If it's not what you want, then you have to tell me why do you think the code from my post #13 does not produce the result you're looking for. You have to explain why do you think that "position size is not taken automatically", because this single-position strategy will always exit the exact size used to enter the position.
profile picture

Ninozah

#16
The code you sent me #13 is perfect! It calculates the size and buys few shares per formula "investmentAmount / Close[bar]".

What I want to accomplish is identical, just instead of buying few shares, I want to sell few shares based on similar formula. I suppose, you could think of it as a Dollar Cost SELLING over a period of time, rather than buying. So I thought I would use the same code, just replacing BuyAtClose with SellAtClose.