- ago
I am running my strategy using the Alpaca extension, but I am seeing behavior that I don't understand and appears to be a bug:

1) Sell signal is not executed (no apparent error message)
2) Multiple long positions are created, despite setting "Max Open Pos: 1"

The following images show a "Sell 24 at 202.26" signal, but if you look at the position history in Alpaca, you can see that no sell ever took place. However, the subsequent "Buy" signal was executed, causing the creation of 2 long positions (24 shares * 2).

There is no warning or error in the WealthLab log, so I'm not sure how to troubleshoot this. Any help would be appreciated!



0
428
Solved
6 Replies

Reply

Bookmark

Sort
- ago
#1
The following is my "Execute(...)" loop, which was generated from a very basic "Indicator crosses value" design block strategy. I just added a few lines at the top to make sure the position is only held during normal market hours:

CODE:
public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          DateTime currentDay = bars.GetDate(index);          TimeSpan currentTime = bars.GetTime(index);          DateTime currentDateTime = currentDay + currentTime;          // If we have an open position and the market is closing soon, close that position          if (foundPosition0 != null && !_market.IsOpenMarketTime(currentDateTime.AddMinutes(5)))          {             ClosePosition(foundPosition0, OrderType.Market, 0, "MARKET_CLOSING");             return;          }          // If the market is currently closed, don't open any new positions          if (!_market.IsOpenMarketTime(currentDateTime))          {             return;          }                    bool condition0;          if (foundPosition0 == null)          {             condition0 = false;             {                if (indicator.CrossesOver(Parameters[1].AsDouble, index))                {                   condition0 = true;                }             }             if (condition0)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }          }          else          {             condition0 = false;             {                if (indicator2.CrossesUnder(Parameters[2].AsDouble, index))                {                   condition0 = true;                }             }             if (condition0)             {                ClosePosition(foundPosition0, OrderType.Market, 0, "Sell At Market (1)");             }          } }
0
Glitch8
 ( 7.81% )
- ago
#2
The chart is showing you backtest results. It doesn't correspond to what's happening with your trading account.
1
Best Answer
- ago
#3
I took the following steps: opened a chart, enabled Streaming, dragged a strategy onto the chart, and then enabled "Auto-Place" orders. If I understand correctly, you're saying that the actual buy/sell signals sent to Alpaca may not exactly match the signals displayed in the backtest chart, is that correct?

If so, then I guess that's not a bug and I can live with that. That would just leave the question of why it created 2 long positions, despite the strategy settings to create at most 1 long position.
0
- ago
#4
I just tried running the strategy again, but using the "Strategy Monitor" rather than dropping the strategy onto a chart. This time, it generated two consecutive "Sell" orders, causing me to go "Short", despite the strategy being configured with "Max Open Short: -1". As before, there's no sign of an error occurring in the WealthLab log, or in the Strategy Monitor "Log Pane".
0
- ago
#5
QUOTE:
That would just leave the question of why it created 2 long positions, despite the strategy settings to create at most 1 long position.

I'm not seeing 2 long positions on the chart of your backtest? But if you're starting to trade a system that already has an open position in backtest, you'd want to avoid sending a sell order. For example, by setting StartIndex in the strategy code to the bar that corresponds to the bar when it goes flat.
1
Glitch8
 ( 7.81% )
- ago
#6
Also leverage Portfolio Sync in Trading Preferences to help sync signals with a live broker account.
1

Reply

Bookmark

Sort