- ago
I have been struggling with occasional auto-trade erroneous orders (IB and TDA) and have finally come across following needing your help - with thanks!

The following code runs a 1-Min dataset of 5 symbols and attempts to trade (long) just 1 symbol at a time (similar to Max Open Pos. 1 in Position Sizing) on auto-trade. The position is closed after at least 25 bars count - or stop loss.

The strategy was designed in building blocks, then switched to C# with WL's C# generator and slightly simplified.

Question 1: what needs changing in the code so that only 1 symbol is open at any point in time?

Question 2: is below a) an error or what is causing the excess orders and wrong symbol/# bars in the Exit Signal text?

In the code there are 2 versions of closing orders; a) CloseAtTrailingStop (by WL's C# generator) and b) Placetrade (Sell with Stop). The Signal Name contains the Symbol being processed. Here are the results for a) and b):

a) using CloseAtTrailingStop, WL generates 5 selling stop orders for 3 open positions - this is resulting in Rejects by TDA and occasional unreconciled open orders, with no further closing. IB ends up with occasional unintended short positions




b) using Placetrade WL generates 2 correct selling stop orders for 2 open positions (I still wish it was capped at 1 open position as in Question 1)





Here is the strategy code:
CODE:
public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPositionAllSymbols(0);          if (foundPosition0 == null)          {             if (Indicator[index] >= Parameters[1].AsInt)             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, $"Buy At Market ({bars.Symbol})");             }          }          else          {             Backtester.CancelationCode = 150;             if (idx - foundPosition0.EntryBar + 1 >= Parameters[2].AsInt)             {                if (Indicator[index] < Parameters[1].AsInt)                {                   PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0, 0,                      $"Sell ({bars.Symbol}) after {idx - foundPosition0.EntryBar + 1} bars"); //                  ClosePosition(foundPosition0, OrderType.Market, 0, //                     $"Sell ({bars.Symbol}) after {idx - foundPosition0.EntryBar + 1} bars");                }             }             else             {                PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, foundPosition0.EntryPrice * (1-Parameters[3].AsDouble/100), 0,                   $"Sell ({bars.Symbol}) at {Parameters[3].AsDouble}% stop loss"); //               CloseAtTrailingStop(foundPosition0, TrailingStopType.PercentHL, Parameters[3].AsDouble, //                   $"Sell ({bars.Symbol}) at {Parameters[3].AsDouble}% trailing stop loss");             }          } }


Thank you so much
0
308
11 Replies

Reply

Bookmark

Sort
Cone8
 ( 28.25% )
- ago
#1
QUOTE:
Question 1: what needs changing in the code so that only 1 symbol is open at any point in time?
All of the logic.

Generally if a strategy needs to know what external symbols are doing, you need to code the logic for all trading in a single class. There's always more ways than one, and maybe (see next question) if you used unique positionTags for each symbol, you'd be to determine if another was already open.

QUOTE:
Question 2: is below a) an error or what is causing the excess orders and wrong symbol/# bars in the Exit Signal text?
You're using FindOpenPositionAllSymbols and looking for/using the same positionTag for all symbols. That's bad logic if you're trying to trade just one symbol in a class.
0
Cone8
 ( 28.25% )
- ago
#2
After thinking about a little more, since you're entering at market and you only want to trade 1 symbol, you MUST determine (in a single strategy class that process all symbols) which symbol the strategy will trade in case the entry rule triggers simultaneously for multiple symbols.
0
- ago
#3
can you suggest code edits that do what you say Cone. still learning c# and lots to catch up . thank you
0
- ago
#4
QUOTE:
After thinking about a little more, since you're entering at market and you only want to trade 1 symbol, you MUST determine (in a single strategy class that process all symbols) which symbol the strategy will trade in case the entry rule triggers simultaneously for multiple symbols.


the indicator will tell when a symbol can be traded, provided there is no other open position. after that the strategy should not let other symbols trade until there is no open position again. basically trying to implement the Position Sizer ability to limit to 1 Max Open Pos. I'd appreciate any code edits or concrete suggestions on how to achieve it under auto-trade. thanks again
0
- ago
#5
You would want to vote up this feature request:
https://www.wealth-lab.com/Discussion/New-trading-threshold-for-maximum-number-of-open-positions-8006
0
Cone8
 ( 28.25% )
- ago
#6
It's related, but that feature request for the "number of fills threshold" for wouldn't solve this case of controlling how many orders are placed simultaneously.

QUOTE:
basically trying to implement the Position Sizer ability to limit to 1 Max Open Pos.
Actually that would work. The "Max Entries per Bar" sizer gets you halfway there.

Edit - Right now there's a problem using a sizer for this because Transaction.Weight isn't used for sizing *Signals*. So you wouldn't be able to select a specific symbol for a trading signal if there were multiple candidates. We'll see if that can be changed.
0
- ago
#7
Thank you Cone - by the way really appreciate your rapid responses beyond workdays :) - did certainly not mean to cause that extra work for you.

Ideally Strategy Monitor would support Position Sizer functionality. Then Max Open Pos = 1 would solve the issue - just as it does in backtest mode.
However as I understand from a prior thread Pos Sizer is limited to backtesting. Is this still the case ? Any reason for not letting it work in SM (auto-trade) as well ?



Pos Sizer deals with the issue of more than 1 simultaneous candidate for signal by making additional symbols beyond 1 NSF Positions. Really just perfect to resolve this issue in auto-trade mode, if working.

0
- ago
#8
QUOTE:
However as I understand from a prior thread Pos Sizer is limited to backtesting. Is this still the case ? Any reason for not letting it work in SM (auto-trade) as well ?

Hasn't this question been answered in a topic you created earlier this month?
https://www.wealth-lab.com/Discussion/Strategy-Monitor-in-B7-not-respects-Max-Open-Positions-8089
0
- ago
#9
NO

The question is can we activate Pos Sizer in SM - any issues or downside for WL? what do you think? Should this become a feature request? Tx
0
- ago
#10
My question in post above was rhetorical. You have already asked exactly this question in Post #2 and received a definitive answer from Cone in Post #3 here:
https://www.wealth-lab.com/Discussion/Strategy-Monitor-in-B7-not-respects-Max-Open-Positions-8089
0
- ago
#11
EUGENE I'll stop here further miss-communications and send in a feature request.
The team can then do whatever is appropriate. I would have appreciated a more wholistic understanding of why/why not making pos sizer available in strategy monitor, as I think it enhances the value of WL. Thanks for your time
0

Reply

Bookmark

Sort