Main (trading) loop and live trading
Author: jalalfeghhi1
Creation Date: 1/17/2011 12:46 PM
profile picture

jalalfeghhi1

#1
Eugene,
I have made a lot of progress but there is an issue that I can't quite figure out. All trading strategies that I have seen have a main loop that iterates through all the bars of the chart to look for signals. I understand how this main loop is useful for screens, where we look for after the market close sigals for the next day. We run the script once and look for alerts.

I am writing a system for live (day) trading that deals with intraday (1 minute) bars, the system runs in real-time streaming data mode and generates alerts. My understanding is that whenever wlp completes a tick (bar), say every minute, it calls my strategy. With this methodology, I really do not need a main loop that goes through all the previous bars from 0 to Count-1. All I care about are the conditions at the very last bar, say if my indicator has crossed my signal at the time instand Bar.Count-1. If I keep the main for loop, I may found out that in a moment in past there was a trade signal. This signal, however, may not be useful to my LIVE trading system. Keeping this loop will result in the slowing down of the entire system I imagine.

I can understand one case where the main for loop might be useful: if I run my system at, say 7:00 a.m. and have missed the first 1/2 hour of the market. Perhaps there was a trading signal a few minutes prior to time that I launched my system, in this case, the main for loop can catch the signal. However, once I run this main loop, I should not iterate through main loop anymore and should continue with the last bar only, I guess.

I feel I have missed a key concept here. Perhaps the main loop is useful for back testing and I should keep it. Perhaps for day trading I should only deal with the last intrday bars enough for my indicators to work and it is not a big deal to keep the for loop.

Could you please shed some light on this?

-Best, J
profile picture

Cone

#2
QUOTE:
With this methodology, I really do not need a main loop that goes through all the previous bars from 0 to Count-1.
If the strategy doesn't care about exiting any Positions, then you don't need the loop; otherwise you do. You can't exit Position that the Strategy doesn't know about.

QUOTE:
If I keep the main for loop, I may found out that in a moment in past there was a trade signal. This signal, however, may not be useful to my LIVE trading system. Keeping this loop will result in the slowing down of the entire system I imagine.
You've got the right idea, but keep in mind what I just said above: the script must enter position in order to exit them. You can keep the post-processing to a minimum when trading live by:

1. deselecting any Visualizer that you don't need to reference while trading, and,
2. initializing the data range with the minimum number of bars that ensures the script is aware of past entries that it needs to exit.

In summary, unless you're just using a "screen", you need the trading loop.
profile picture

jalalfeghhi1

#3
Cone,
Thanks so much for your reply. I am still confused. Are you saying that I need the main for loop because I need to find out if I had entered a position before (in time) in order that I can exit it? I thought that I could simply use the positions object to determine if my Strategy had opened any positions before?

To be specific, when Strategy Monitor calls my strategy, I calculate bullish/bearish signals as follows:

CODE:
Please log in to see this code.

As the above code sample shows, all I care about when WLP calls my strategy is the macd and macd signal at the lastest bar. In this case, why would I need to iterate through all the past bars for a LIVE trading system?
profile picture

Cone

#4
QUOTE:
Are you saying that I need the main for loop because I need to find out if I had entered a position before (in time) in order that I can exit it?
In a nutshell, yes.

QUOTE:
I thought that I could simply use the positions object to determine if my Strategy had opened any positions before?
Exactly. But how do you propose doing that if you the Strategy is not running in a loop?

QUOTE:
all I care about when WLP calls my strategy is the macd and macd signal at the lastest bar. In this case, why would I need to iterate through all the past bars for a LIVE trading system?
Of course you only care about the latest bar! This is how strategies are run, bar-by-bar, always processing the most recent one and determining if an action is required. Past actions (ones that occurred on previous bars) create (and exit) Positions. Bar-by-bar the Strategy trades in and out of Positions.

This is how you trade in real life.
profile picture

jalalfeghhi1

#5
Cone,
I think I am beginning to understand my source of confusion. I used to work with another trading system that had the capability to load the current positions directly from the account. With that system, you would be able to determine all your positions (regardless of which strategy created them), cash, other account features. This represents the most DIRECT and RELIABLE way for an automated trading system to determine current positions as the data comes directly from the actual data (positions over in Fidelity).

Based on you comments, it appears to me that WLP does not have this capability. In order to determine the current positions, one needs to loop back through previous bars to determine if the strategy had generated a trade, and now the current bar can be used to exit from it. This is how I understand your remarks.

If my understanding is correct, scenarios can be thought of when by looping over past bars to determine the current positions can be inaccurate, for example, perhaps the strategy generated a buy signal via an alert but the user decided not to trade that alert. Now the next day the user restarts his system and the strategy thinks that there is an open position that needs exiting, but in fact there is none. Furthermore. Or perhaps a position had been opened up many, many bars ago outside of the current bar range, the strategy would never find out about this position. Also what if the user has manually done a trade (not based on a signal) but now wants a strategy to take over and close the position at a appropriate time? Looping through previous bars would never reveal this open position. Another issue might that if a strategy open a position, another strategy cannot take over this position as it might never see this position.

If all my understanding is correct and I am not missing any points or key concepts, it seems to me that the best way to determine the current positions is not to loop through previous bars but to DIRECTLY query the Fidelity Account data. This way, when the system (strategy) starts, most accurate and up-to-date data can be loaded into the system. Furthermore, this gives you all the positions regardless of which strategy created them.

Unless I can have access to my live Fidelity ACCOUNT data (i.e. to determine various accounts, choose an account, load positions into my account, load the available cash, etc.), I really cannot see how I can have a LIVE INTRADAY trading system that works with most accurate data. For example, after a trading day, my system is turned off, next day when the market opens, the system starts, loads the latest data, and starts calculating. If the next day, after I restart my system, I cannot load the latest account/position data from Fidelity, I just cannot see how my system can work reliably and even looping through previous bars to determine possible positions may not be reliable as I mentioned above.

I have to assume there is a way to access Real Time Fidelty Account Data, as WLP has the capability to log into accounts in Fidelity and display all the positions. Can we programmaticaly access this data as well?

I do appreciate all your thoughts, sorry for the long post, wanted to make sure you understand my thinking progess.

-Best, J
profile picture

Cone

#6
QUOTE:
it appears to me that WLP does not have this capability.
WLP is capable if that were the design, which it isn't. The design is that WLP runs strategies in theoretical mode. There's a large discussion about this in the User Guide: Orders chapter.

QUOTE:
one needs to loop back through previous bars to determine if the strategy had generated a trade, and now the current bar can be used to exit from it.
Sort of. You don't really need a loop to create a Position. A loop is just a way to change the bar numbers. You could do it like this:

CODE:
Please log in to see this code.


QUOTE:
scenarios can be thought of when by looping over past bars to determine the current positions can be inaccurate,
Well, sure. If you're not following the Strategy, then it might not match your account Positions. But if you were supposed to buy, but didn't, the strategy will keep doing what you programmed it to do... sell the position that you were supposed to have bought. When it tries to sell, the order will either be filtered (due to Portfolio Synch) or will error out (Port. Synch Disabled).

Furthermore, even if you follow your strategy, there's a small possibility of getting out of synch when using limit orders (and even a smaller possibility when using stop orders). Described in the User Guide > Orders

QUOTE:
best way to determine the current positions is not to loop through previous bars but to DIRECTLY query the Fidelity Account data.
That's another design, not Wealth-Lab's. With your design, Strategy trades that should have occurred won't show up in the chart or Trades list. Then you'll have customers telling you that they don't like the design because the strategy should always tell you when it should buy and sell.

And this is the crux of the matter. You won't be able to please everyone. The only way you'll get everything that you want, the way you want it, is by designing and writing your own trading platform.

QUOTE:
I have to assume there is a way to access Real Time Fidelty Account Data, as WLP has the capability to log into accounts in Fidelity and display all the positions. Can we programmaticaly access this data as well?
You can see your Account Positions in the Accounts tool. WLP has a Portfolio SYnch function that works as previously described. It will keep orders to match the shares that you have in your account, and, it will filter exit orders for Positions that you don't own. That's the design. A strategy cannot access Account Positions (but if you're a hacker, you can probably find a way to do it deep in the Windows API, since as you say WLP does get this information).
profile picture

Eugene

#7
If actual positions could be exported somehow from Fidelity's site or from AT Pro to a text file, just wondering if this could then be of some help?

Import real (historical) trades
profile picture

jalalfeghhi1

#8
Cone,
I am reading the chapter on Orders and going through all your comments, do appreciate your remarks, will get back with you when I have digested all that :-)

-best, J
profile picture

jalalfeghhi1

#9
Eugene,
I took a quick look at the link and your idea to sync up WLP with Fidelity Actual Data sounds extremely exciting. I will explore that as soon as I have had a chance to better understand the WLP architecture and design philosophy that Cone has been trying to explain to me. That will probably be a few days :-)

-Regardas, J
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).