First, please review this post for ideas on how to find the error in your coding: How do I debug my formula?. Since you didn't show us all your code, there's really no way for anybody to guess where your mistake is.
Second, AmiBroker's default behavior is to force all your trade prices to be within the Low-High range of prices for that bar on which you enter or exit. That means if you specify a SellPrice which is above the High then AmiBroker will use the High price to exit the trade. If you specify a SellPrice which is below the Low, then AmiBroker will use the Low price to exit the trade. That may provide you with some clues about where to look for the problem.
Third, it seems likely that you could implement your logic with array formulas instead of using a loop. That would be much more efficient than the looping approach.
Fist, I am using _TRACE to debug this for a week. And since I don´t have a clue about why the SellPrice is being ignored inside the loop, I am here asking for help. I will not show all my code, beceuse the problem is inside an if statement, and it´s just a question why the SellPrice is being ignored...
Second, you said:
That means if you specify a SellPrice which is above the High then AmiBroker will use the High price to exit the trade. If you specify a SellPrice which is below the Low, then AmiBroker will use the Low price to exit the trade
This is not the case. If you read what i wrote you will see that my SellPrice is BETWEEN high and low of the bar/candle. Not above the high, not below the low.
In fact I DID read what you wrote, and I looked at the code that you posted. It does not appear to match up with the results you are getting, which suggests there's something else going on with the code which you chose NOT to post. So you have three choices:
Post your code as-is so that people can help you
Post a simplified version of your code with all your top-secret trading ideas and other unnecessary complexity removed so that people can help you
Use other tools like the built-in debugger to find and resolve the problem yourself