So far I have failed to code a kind of ETF savings plan simulation. A savings plan works like this… on the 1st or last day of each month you buy a share of an ETF, for example SP500. So that means cumulative orders true. That would be the standard. But I want to know whether it is really better to only buy when the price is above a certain MA, the standard here is usually the SMA200 in Daily. And then whether it is better to sell all shares if the price slips below this SMA or to simply hold and not buy any new shares. What I’m having trouble with is that the code only buys once a month, cumulate Orders=true
Can someone please code this simulation for me?
This is the code:
ONCE LastDay = 0
IF Month <> Month[1] THEN
BUY 1 CONTRACT AT MARKET
LastDay = 31
IF OpenMonth = 4 OR OpenMonth = 6 OR OpenMonth = 9 OR OpenMonth = 11 THEN
LastDay = 30
ELSIF OpenMonth = 2 THEN
YY = OpenYear
LastDay = 28
IF YY MOD 4 = 0 THEN
IF YY MOD 100 = 0 THEN
IF YY MOD 400 = 0 THEN
LastDay = 29
ENDIF
ELSE
LastDay = 29
ENDIF
ENDIF
ENDIF
DoW = DayOfWeek - 1
FOR i = Day TO LastDay
Dow = Dow + 1
IF Dow = 7 THEN
Dow = 0
ENDIF
NEXT
IF Dow = 0 THEN
LastDay = LastDay - 2
ELSIF DoW = 6 THEN
LastDay = LastDay - 1
ENDIF
ENDIF
IF Day = LastDay THEN
BUY 1 CONTRACT AT MARKET
ENDIF
you may add your conditions to the entries, apart from the day.
That looks good. Thanks Roberto.