Is there way to access my portfolio

I want to access my portfolio through the code such that, I can use my last buy quantity, value etc for taking decisions on averaging out etc.

For example,

If my portfolio is having ONGC 25QTY @ 360/share, I want to write an algorithm when the ONGC price goes below 5% of my Portfolio price of 360 and recommend a buy to average out the value.

I hope my question is clear.

Your question is very clear, what isn’t is whether this will make money or not?

It is not about immediate money but a strategy based on the portfolio value etc. Also, this will be useful for a long term investor to make profit or average the holdings.