There are several problems with this:
1. Close[bar] of which stock? You're not using SetContext there, so Close[bar] will refer to whichever stock you left as the Context.
2. You're looping through Positions with an index p, but you used
LastPosition instead of p in the Close/EntryPrice comparison.
Correction (untested):
CODE:
Please log in to see this code.