Strategy – Dollar Cost Average


Category:
0
0

Hi Pete,

Been following you for a while, watching your videos multiple times.

Wanted to pick you brain.

Looking to create a strategy for Dollar Cost Averaging (buying a certain number of stocks every so often) but couldn’t find a simple way of doing this.

I tried to code something myself and came up with a not-so-elegant solution.

Upper Strategy:

input tradeSize = 1;
def signal = Getday() % 25 == 1;
addOrder(OrderType.BUY_TO_OPEN, signal, open[-1], tradeSize, Color.CYAN, Color.CYAN);
#def exit = 0;
#addOrder(OrderType.SELL_TO_CLOSE, exit, open[-1], tradeSize, Color.MAGENTA, Color.MAGENTA);

 

Lower Study:

plot signal = Getday() % 25 == 1;

 

It seems to be partly doing what I want it to do (buy a certain number of stocks every so often), but it is skipping certain months  (highlighted in the attached image, circled in red) and looks a bit sporadic.

I would prefer to buy once a period (once a month), but can’t figure out how to do it.

Really appreciate it!

Attachments:
RESOLVED
Marked as spam
Posted by (Questions: 2, Answers: 2)
Asked on May 9, 2020 10:38 am
67 views
1
Private answer

Each month of trading has only 21 days on average. You can never rely on the first trading day of each month being the 1st day of the calendar month. So we need to write a statement that is true on the first trading day of each month, not the 1st calendar day of each month:

plot signal = GetMonth() <> GetMonth()[1];

Marked as spam
Posted by (Questions: 37, Answers: 4079)
Answered on May 9, 2020 11:11 am