- ago
Strategy opened multiple positions and kept opening new positions when if-condition was set to stop open new positions. Checking, FoundPosition0 was full but strategy was receiving value as null. Can you re-validate the TDA API integration is working correctly with the following methods ?

CODE:
   if (foundPosition0 == null)             {                if (FindOpenPosition(0) == null)                {                   if (!HasOpenPosition(bars, PositionType.Long))                   {                      if (foundPosition0?.PositionType != PositionType.Long)                      {                         _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.AveragePriceOHLC[idx], "Long At Limit (1)");                                               }                   }                }             }
1
991
Solved
14 Replies

Reply

Bookmark

Sort
- ago
#1
I don't understand the above code at all. If the Positions variable(s) is(are) "null," then there's nothing to test against in the first place. Right? So why are you trying to test for something that's not even there? Please explain your logic.

What does make sense is to test the Position variables if they contain something to test for (i.e. does not equal "null"); otherwise, you're going to get undefined behavior. If this doesn't not make sense, then you need a local programmer (or instructor) to explain this better than the help forum can.

You can't expect an undefined variable (i.e. a variable that's "null") to steer the program flow of your program. It will only result in undefined program behavior.

Please tell us under what circumstances (conditions) you want to execute a Buy-Long order. We can write a code example for that.
0
Cone8
 ( 6.32% )
- ago
#2
Crash, your code for PlaceTrade() does not set the PositionTag, i.e., 0, that you're testing for. Click on FindOpenPosition and study the example.
0
Best Answer
- ago
#3
//Studied the reference. Tried to fix the code but it is not defining the data in foundPosition0, which is still returning null.
//The TDA API Integration is working fine. Right ?. Please see 2 if statement lines below. Where is the error?

CODE:
////Studied the reference. Tried to fix the code but it is not defining the data in foundPosition0, which is still returning null. //The TDA API Integration is working fine. Right ?. Please see 2 if statement lines below. Where is the error? public class MyStrategy : UserStrategyBase { Position foundPosition0; private Transaction _transaction; public MyStrategy() : base() { } public override void Execute(BarHistory bars, int idx) {    foundPosition0 = FindOpenPosition(1); if (foundPosition0 == null && FindOpenPosition(1) == null) // This line is not working //       {          if (!HasOpenPosition(bars, PositionType.Long) || foundPosition0?.PositionType != PositionType.Long) // This line is not working //          {             _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.AveragePriceOHLC[idx], 1);             _transaction.AutoProfitTargetPrice = bars.AveragePriceOHLC[idx] + 0.02;             _transaction = PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, bars.AveragePriceOHLC[idx] + 0.02, 2);                       }       } }
0
MIH8
- ago
#4
Hi Crash,

can you please describe in "words" what you are trying to achieve?

The !HasOpenPosition(bars, PositionType.Long) will already prevent you from opening a second position, at the same time, for the same symbol.

Now I can imagine different scenarios. One is that you only want one position open for all symbols at the same time for your strategy.

Another would be that you do not want to open a second position for the same symbol after you have already closed a position for the same symbol (in a period of time, for example, daily)

Well, there can be other scenarios as well. It will be easier to provide help if the goal is clear.
2
- ago
#5
Using, TDA API, The following statement was returning false and it is supposed to be true. How do I fix this ?


CODE:
if (FindOpenPosition(PositionType.Long) == null){    _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.AveragePriceOHLC[idx], 2);       _transaction.AutoProfitTargetPrice = bars.AveragePriceOHLC[idx] + 0.02;    }
0
- ago
#6
QUOTE:
Using the TDA API,... FindOpenPosition

That's because FindOpenPosition has nothing to do with what's on the broker's side or the TDA API.

FindOpenPosition is part of the WL framework and returns information about the WL simulation your WL strategy is running. And we know that has nothing to do with your real world portfolio on the broker's side (although you should try to keep both in sync so there's some correspondence between the two).

Remember, WL knows nothing about your real world portfolio on the broker's side. If you find this a problem, then you need to vote for this feature request. https://www.wealth-lab.com/Discussion/Access-Accounts-data-from-broker-programmatically-7914
2
- ago
#7
Crash2022, was this really necessary to ask same question in the new topic (leaving @MIH's question unanswered)? Let's append it to your last known topic then (here).
0
- ago
#8
@MIH. Thanks for the feedback.
@SuperTicker and @Eugene. Thanks for the good answers.

Important questions for you and any suggestions would be appreciated:

1. Passing the tag = '2' Does the method FindOpenPosition(2) know if an entry position is opened or closed ?
2. Passing the tag = '2' from an exit transaction, TransactionType.Sell, it means, it will always show that a completed closed exit transaction is true. Correct ?


CODE:
Position foundPosition0; foundPosition0 = FindOpenPosition(2); //long    if (FindOpenPosition(PositionType.Long) == null)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.AveragePriceOHLC[idx], 2);                _transaction.AutoProfitTargetPrice = bars.AveragePriceOHLC[idx] + 0.02;                _transaction = PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, bars.AveragePriceOHLC[idx] + 0.02, 2);                             }




3. The exit limit order get canceled automatically after n bars. I need the strategy to allow the exit limit order to stand-still or re-submit the order. Logic flow as follows: If there is an exit limit order open, allow it or resubmit it. How do I code this out correctly?

CODE:
   if (HasOpenPosition(bars, PositionType.Long) || foundPosition0?.PositionType == PositionType.Long)             {                _transaction.AutoProfitTargetPrice = foundPosition0.EntryPrice;                //ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice- 0.02 * (foundPosition0.Bars.SymbolInfo?.PointValue ?? 1), "Cover at 0.03 profit target");                _transaction = PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, foundPosition0.EntryPrice);             }
1
- ago
#9
I think we got started on the wrong foot here. Let's consider the first part of your code
CODE:
if (FindOpenPosition(PositionType.Long) == null) PlaceTrade(....)
This code UNconditionally buys a stock regardless of how well it's doing if no position is currently owned. Why on earth would you want to UNconditionally buy any random stock without judging its merits first? Is the stock going up, or not?

To go one step further, none of my WL strategies use the FindOpenPosition call. I didn't even know it existed, and I've been using WL for a couple years.

Moreover, I can't find any WL sample strategies that use the FindOpenPosition call either (although I might have missed one). So why are we using the FindOpenPosition call in the first place? It's a total mystery to me.

---
What is done in a WL strategy is to evaluation a stock on its merits using WL indicators, and if those indicators suggest that stock is going up in the future, then we place a trade to Buy it. And when the WL indicators suggest that stock is going down, then we sell it. (And we don't need FindOpenPosition to do that!)

I would suggest you take a sample WL strategy that's known to work and start with that. Then ask, "What are its shortcomings and how can I modify and enhance that strategy to fix those shortcomings?" And if you have specific questions, then start a new topic and raise them.

Finally, I would try avoiding the FindOpenPosition call altogether. If you're getting errors, try fixing those "program flow errors" without using FindOpenPosition. If I and the sample WL strategies don't need this call, you probably don't need it either.
2
Glitch8
 ( 12.10% )
- ago
#10
Good advice! I believe the use of FindOpenPosition is a consequence of converting a BB strategy to C# code. The code generator uses it because the BB strategy can manage multiple entry blocks, so it’s a way to track that. It’s not something that’s required when dealing with strategies that don’t do this.
0
- ago
#11
QUOTE:
FindOpenPosition ... The code generator uses it because the BB strategy can manage multiple entry blocks,

Interesting. Thanks for that clarification. I have multiple entry branches in my strategies too, but none of those entries are tide to any particular exit condition. Each exit condition is independent of the entry used.

In my mind, if any exit condition is met, the stock Sells (regardless of the condition used to enter the trade). But now I understand the purpose of the FindOpenPosition call (and why I never use it).
0
- ago
#12
The position data that I need would not be available and compiler is running into null issues. The method is very important to access position properties such as foundPosition0.EntryPrice. It is a way to track EntryPrice , CostPrice and others. If you can update foundPosition.members values without using FindOpenPosition(), let me know.



0
- ago
#13
@Glitch
My code is not working to force the exit order to stand-still or re-submit it and need to determine the problem with it.
3. The exit limit order get canceled automatically after n bars. The strategy needs to allow the exit limit order to stand-still or re-submit the order. Logic flow as follows: If there is an exit limit order open, Allow it to stand or resubmit it at price = foundPosition.entryPrice.


Does anyone know the solution code ?

0
- ago
#14
QUOTE:
important to access position properties such as foundPosition0.EntryPrice.

Use statements like
CODE:
LastOpenPostion.EntryPrice // -or- LastPostion.EntryPrice
to access this information.

Why do I think you're making this strategy much more complicated than necessary? Get something simple running correctly first, then add complexity to it.

QUOTE:
compiler is running into null issues

Your program is flowing into statements it shouldn't be encountering in the first place (and you don't realize it). Insert the if...else construct
CODE:
   if (HasOpenPosition(bars, PositionType.Long))    {       //code your sell conditions here    }    else    {       //code your buy conditions here    }
to prevent your code from erroneously querying for positions that don't exist yet. Can you get a local programmer (and partner) to fix the logic and flow of your program?

---
I still think you should start with a WL sample strategy that's already working until you get more familiar with what's going on.
2

Reply

Bookmark

Sort