How can we get the exact daily gain or match the index DJIA (or SPX)? I tried various ways with and without using the ratios of Wikipedia's entry (and DIA) for the percentage of each equity. Weighting all 30 stocks, only using the gains, etc. There's no way I found a way to replicate the exact daily Open/Close movement (or at least the close from a known Open). DIA and it's options don't match it at all. I tried something similar with the SPX (using Spy, options of both SPY and SPX and /ES) and still wasn't able to reproduce (without having to program 500 stocks in Thinkscript...).
Note: The DIA has exactly the same amount of shares of each equity (5 447 036) and doesn't match the DJIA index on an Open/Close (even at a ratio). Some days will be green on DJIA and red on DIA.
I believe you are having this this problem, because DJIA is a price-weighted index (i.e., the higher the component company stock price, the higher weighting it carries in the index). Maybe try the DIA etf as some sort of proxy instead.
Not all stocks open at the exact same time each and every day. And also order imbalances on the close delay end of day fixed closing prices for many.
Here's the answer: You can't. Indexes aren't real and can't be traded. Make your strategy on an actual financial instrument instead.