Stocks bought second time in auto trading despite IsLastPositionActive
Author: rajeshu
Creation Date: 8/27/2020 1:14 AM
profile picture

rajeshu

#1
Hi Cone/Eugene

I see that during execution of auto trade the stocks were bought second time (screenshot attached) eventhough I have IsLastPositionActive check to false as shown below.

CODE:
Please log in to see this code.


Is there any other check in code I need to add apart from this to avoid buying new position until last open position is closed.

Do you recommend adding Preferences->Trading->Trading Thresholds will avoid buying more than one position based on the input $$

Thank,
Rajesh
profile picture

Eugene

#2
Hi Rajesh,

By this short snippet I can tell that it diverges from the standard single-position strategy template. Apart from that it reverses the order of checking for Position existence, it gives room for different scenarios including peeking into the future. On what I mean, check out the WealthScript Programming Guide > Programming Trading Strategies > Peeking > Trading based on Position Active Status.

With more code revealed it might be possible to say for sure.
profile picture

rajeshu

#3
Thanks Eugene, I changed the code as per the directions and the skeleton looks like this now:

CODE:
Please log in to see this code.


Today it still bought 2 times like it did before.
profile picture

Cone

#4
You need to be more specific - what were the exit limit orders, what symbol, what timeframe, and at what time of day?

Anyway, we've discussed this before on the entry side. If your limit trigger is reached your live position may not be sold but the script will always exit the Position "hypothetically" and continue with the entry logic, which can put on another position. Again, it's the same issue that you solved with the entries by using AtMarket entries. There is no difference. The only way to guard against this is to be monitoring the trades and take over manually when your limit order is not filled.
profile picture

rajeshu

#5
Thanks Cone, I have attached the trading data and you can see the 2nd and 3rd row where the stocks have been bought. I read the documentation on Trading Thresholds and confused with 2 options Enable Cash Threshold & Enable Buying Power Threshold.

I want to limit my stock buying to 5k and after selling I should buy again. Does any of 2 options mentioned above help me out apart from manually monitoring the trade for duplicate positions.

And whats the real life scenario for IsLastPositionActive function if I have to monitor the trade manually. Will you suggest something like this for more control to block multiple positions
CODE:
Please log in to see this code.


Thanks again,
Rajesh
profile picture

Cone

#6
QUOTE:
Does any of 2 options mentioned above help me out apart from manually monitoring the trade for duplicate positions.
No, those options have an "account basis", not per position.

The image you posted only tells me the same thing you said. But remember that the strategy is running hypothetically. The exercise is to look at the strategy trades! You'll see where your limit order "could have" filled but didn't.

QUOTE:
And whats the real life scenario for IsLastPositionActive function
There is no "real life". A Strategy runs hypothetically ONLY. I can't stress that enough. You can add logic based on more inputs to synchronize with live trading, but that's on you.

For example, find the file where IB saves its fills. Have the strategy parse and "synchronize" with it. For example, for the case that we're discussing, you'd have to make sure that the strategy does not execute the sell signal on the bar on which the limit order did not fill. I'm not talking about the Alert - always send the Alerts - but the strategy history needs to be synch'd with the live trades. This is not an easy task, but can be done with some good programming.
profile picture

rajeshu

#7
Thanks Cone, I will dig into more!
profile picture

rajeshu

#8
I checked the Orders window and see the sell was cancelled. Does this mean that even though Sell was cancelled the stock was bought again considering the Sell executed successfully? is there any function to check the lastorder status?
profile picture

Cone

#9
Are we still on the same topic since we were discussing something that happened last week? The image is showing trading from 9/2/2020.

If you want to discuss a particular trade, please post an image of the chart with the trading system executions. You must realize by now that if you're using limit orders, there's a good possibility you won't be filled even if the limit price is attained - especially for orders that only last a few minutes.

The only solutions are to either closely monitor your trading and takeover manually when an out-of-sync condition occurs, or, create an add-on that synchronizes live trading with the hypothetical strategy.
profile picture

rajeshu

#10
Yeah I am talking about the same topic. I work on this if I get some time from my full time job.

I checked the forum and looks like Orders.xml is the file that stores live trading data transmitted from IB? So the idea will be to parse the Orders.xml and before creating buy order check if the message is filled for the last sell trade?

Or I am going in completely different direction for the solution.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).