Hi everyone,
My purpose: Determine The Best Moving Average Crossover Trading Strategy.(Moving average is calculated on the closing price)
Buy and sell signal:
About data:
My code: I know how to use -tsegen- to calculate moving average:
However, I don't know how to use -forevalue- to do it, and how to calculate the holding-period returns and chose the highest one.
I hope that you could help me. Thanks in advance.
Best
My purpose: Determine The Best Moving Average Crossover Trading Strategy.(Moving average is calculated on the closing price)
Buy and sell signal:
- A buy signal: when a shorter-term moving average crosses above a longer-term moving average.
- A sell signal: when a short moving average crosses below a long moving average.
- If a buy signal appears on date t, we will buy a stock at the opening price on date t+1
- After buying a stock, we will wait for the appearance of a sell signal (e.g. on date t+10) to sell the stock at the opening price on the following date (t+11).
- The holding-period returns is calculated as (selling price - buying price)/buy price.
- The new round begins. Wait for the buy signal and so on...
- After buying a stock, the stock needs 3 days to be credited to our account, then we can sell it. For example, we buy a stock on date t, we can't sell the stock on date t+1, t+2. We only can sell the stock from date t+3.
About data:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float date long firm double(open_price close_price) 22446 1 83.8 83.2 22447 1 83.2 83.1 22448 1 82.1 82.6 22449 1 83.1 83.8 22452 1 83.6 85.6 22453 1 86.1 85.4 22454 1 85.3 84.9 22455 1 84.3 84.7 22456 1 84.7 85.2 22459 1 86.1 85.7 22460 1 86.6 88.6 22461 1 89 88 22462 1 88.1 88.9 22463 1 89.3 91.9 22466 1 95 92.7 22467 1 92.5 87 22468 1 87 90.5 22469 1 90.4 89.8 22470 1 89.6 89.7 22473 1 88.8 86.1 22474 1 86.9 87 22475 1 87.6 85.2 22476 1 84.7 85.4 22477 1 85.9 86.8 22480 1 85.2 84.7 22481 1 84.8 86.8 22482 1 86.8 86.6 22483 1 86.2 89.4 22446 2 102.1 102.8 22447 2 102.5 104 22448 2 104 104.2 22449 2 104.5 108.5 22452 2 108 106.6 22453 2 107.1 107.6 22454 2 108 109.7 22455 2 110 110.7 22456 2 110.7 112.6 22459 2 112.6 112.6 22460 2 112 114 22461 2 114 116.4 22462 2 116.7 116.4 22463 2 116.4 114.9 22466 2 113.9 113.6 22467 2 112.5 112 22468 2 112 113.4 22469 2 111.2 111 22470 2 110 109.8 22473 2 109.5 107.1 22474 2 106.9 103.4 22475 2 103.4 101.8 22476 2 100.2 102.5 22477 2 102.5 102.5 22480 2 100 97 22481 2 97.8 100.2 22482 2 99.5 99 22483 2 98.9 100.3 end format %td date label values firm firm label def firm 1 "FPT", modify label def firm 2 "VCB", modify
Code:
tsegen ma2=rowmean(L(0/1).close_price) // moving average for 2 days tsegen ma3=rowmean(L(0/2).close_price) // moving average for 3 days
I hope that you could help me. Thanks in advance.
Best
Comment