I have tested it in real time in a demo account and when the condition is met, it close all long orders (or short orders).

What do you mean about "trade function"?

If I have a lot of orders opened and I don't have set the variable "TRADE* my_order" for each order; is there a way to retriev it for the order I am checking?