This is a big project to be reviewed. There is no immediate solution for what you want. What do you exactly mean by 'If the Martingale position generates profit'? Do you mean profit with the open trades at this moment of considering the closed ones too?
F