I use a piece of code that will close all my positions (long or short) when the price is above the average (positionprice):
For example, if you have 4 LONG positions open, and the average price is becoming positive, then you can close them at market. The extrapip is the amount of pips you want to be positive (x number of contracts). If you want a conservative approach, you can set extrapip=0
Warning: Trading may expose you to risk of loss greater than your deposits and is only suitable for experienced clients who have sufficient financial means to bear such risk. The articles, codes and content on this website only contain general information. They are not personal or investment advice nor a solicitation to buy or sell any financial instrument. Each investor must make their own judgement about the appropriateness of trading a financial instrument to their own financial, fiscal and legal situation.