This code is pretty problematic to say the least:
1. First and foremost, it suffers from two "peeking into the future" errors:
A. You can't check for "High[bar]" and issue a BuyAtStop[bar]/ShortAtStop[bar]. It's simply impossible with regard to how Wealth-Lab operates. If you checked for a price on "bar", the stop/limit order should be for "bar+1".
B. You're checking for
!IsLastPositionActive on the same bar for which you're triggering exits. (WealthScript Programming Guide > Programming Trading Strategies > Peeking >
Trading based on Position Active Status).
[On the bright side, you're avoiding entries and exits using stop orders on the same bar with that
"bar!=StopBar" because as you seem to know, one
can't realistically backtest with stop/limit orders on the same bar.]
2. Defining a class-level variable (here: EntryPrice) sometimes becomes a recipe for disaster. Usually it's done unintentionally but has a big effect on how a Strategy operates. In such cases, the Strategy behavior might be different depending on whether you clicked "Execute" or "Go". Since storing EntryPrice is not something one'd want to keep between strategy executions, don't define a variable as class level unless you really intended to do it.
3. No entry alerts due to wide usage of "bar".
4. At the risk of something like existing trades disappearing in the beginning of the data range because indicator value suddenly changes, you can't use
GetTradingLoopStartBar(58) with an 57-period EMA. The main loop should start at least on bar 57*3. For more, see the WealthScript Programming Guide > Indicators >
Stability of Indicators.
5.
NumberOfActivePositions is superfluous. Just call ActivePositions.Count instead.
What are its entry and exit rules in plain English? It may benefit from rewriting from scratch.