Pair trade at sub-minute intervals
Author: doccanter
Creation Date: 4/7/2020 5:31 PM
profile picture

doccanter

#1
Hi. I'm completely new to WealthLab. I have a trading strategy that is not based on technical indicators or charting and I'm wondering before I get too deep if WealthLab is a good fit for what I want to do. I would call it a paired trade strategy in which I would need the following capabilities: 1) streaming quotes for 2 securities, 2) loop type programming which would process the price data and automatically execute quick response buy/sell market orders when the system parameters dictated. Ideally I would like the loop to process every 5 or 10 seconds but at intervals no longer than 1 minute. Is that feasible? Any response /feedback would be appreciated. Thank you
profile picture

Eugene

#2
Hi,

The minimum bar scale for Fidelity data in WLP is 1 Minute. It can deliver the streaming data for 2 instruments which should work in general in a streaming Strategy window. Our IQFeed data provider, on the other hand, supports sub-minute bar scales (Tick and Second). Again, it may work out depending on your strategy's complexity and data loading range. If you provide more insight into your algo and data requirements, we should be able to tell for sure.
profile picture

doccanter

#3
Thanks Eugene. I'm going to take a shot at it and see what I can get going. I may try to circle back once i know more.
profile picture

Eugene

#4
Please don't hesitate to ask if you have followup questions.
profile picture

doccanter

#5
So I have made a good first attempt at the code.but I have a few holes remaining and hoping you might shed some light. If you would rather I email you outside the forum just let me know.

the first section of code I'm using is to set some initial variables and pass through once without any loops. as part of that, I wanted to pull the open price for two ticker symbols for bar 0. I was messing with 2 lines - int bar = 0 and Sopen = GetSessionOpen ("Ticker Symbol") Either by concept or by syntax it won't compile so do you have any suggestions on how best to accomplish?

the second section of code is my big processing loop where I would hope all the bar data for the day would be processed. I found the following statement in the guidance which appears to compile OK, am I using the right one to initiate the loop so to speak?

for (int bar = GetTradingLoopStartBar(1); bar < Bars.Count; bar++)

also, after it runs through the code once in this section, how does it know to loop back? Do I need to add a command?
Within this section, I am trying to "pull" pricing (similar to above) for 2 securities at the close of each bar and I just can't figure it out. Any suggestions? Also within this section I am trying at different times to buy and sell each of the two securities and I found a few commands but again either by concept of syntax I can't get them to compile:
{ BuyAtMarket(Sshares,"ticker"); } and { SellAtMarket(Sshares,"ticker"); } Any suggestions on the best way to do this? I ultimately want these to execute on a fully automated basis. Thank you in advance
profile picture

Eugene

#6
Before anything else, please create a support ticket for us to make sure we're supporting a Fidelity customer. Thanks.

Then I'd suggest that you share your trading rules in plain English here. Let technicians try and find an optimal solution first.

P.S. For your future record: when followup questions are specific and substantially different from the original post, ideally you would 1) break them up and 2) start a new topic for each problem. But first please search the forum by entering a few keywords in the "Search Forum..." text box. Chances are you'll find an answer instantly.
profile picture

doccanter

#7
In the WS programming guide I found the sample script for pairs trading including the classes "PairsBarsLoader" and "PairsScript". Within my code both of these classes are now red underlined with the message that the namespace already contains a definition for them. Any thoughts on what I've done wrong? I can't see anywhere elsewhere that these classes are defined.
profile picture

Eugene

#8
I have no idea what you might have done wrong. These specific scripts are for live trading. Let's focus on the backtesting side of it. Strike Ctrl-O ("Open Strategy"), click "Download", check "Download public strategies" and uncheck "Published since". Now, "Begin download" will retrieve all publicly available strategies. Among them you'll find some pairs trading scripts if you enter "pair" in Strategy Name field (the same dialog window). This should do for a start.
profile picture

doccanter

#9
ok, I'll try that out, thanks
profile picture

doccanter

#10
so, i feel like i have made great progress on my pairs trade program and it appears to do well with backtesting etc. but now I am having trouble getting the strategy to generate alerts/paper trades. In the strategy monitor, the trades are indicated on the chart so I am not sure what is going on. I have read the "no alerts" FAQ you referenced in other threads but I am still not seeing what is wrong. Any suggestions? Would you mind taking a glance at my code? There is one other strange issue also where the sell trade indicator keeps bumping over to the last bar sort of like a phantom indicator. So something is also wrong there.
profile picture

Eugene

#11
Apparently, something's wrong with the code. Sure, let's have a look at it. It will help if you also include the trading rules.
profile picture

doccanter

#12
CODE:
Please log in to see this code.
profile picture

Eugene

#13
Although the script is long enough to follow without a description, clearly this part of it will not trigger Alerts in SM or Strategy window b/c it uses "bar" instead of "bar+1":

CODE:
Please log in to see this code.
profile picture

doccanter

#14
ok thank you. I was trying to sell at the open of the last bar of the day.
profile picture

Eugene

#15
You're welcome.
profile picture

doccanter

#16
can you please make a suggestion as to what script I could use to close my open positions prior to the end of the day? I would be happy for the easiest fix, meaning I just need the positions closed - getting a certain close or open price on the last bar or next to last bar is not important to me. Maybe if I could pass a normal SellAtMarket bar +1 triggered by the next to last bar? something like that?
profile picture

Eugene

#17
profile picture

doccanter

#18
thanks i will check it out
profile picture

doccanter

#19
OK, I was able to work this out, thanks again. now that I have this issue worked out and my bar+1 issues worked out hopefully my strategy will generate alerts today
profile picture

Eugene

#20
Glad to hear you've made a good progress on this!
profile picture

doccanter

#21
so one step forward , two back....
so I now have alerts generating for my sell orders but not my buy orders. the buys are being indicated on the strategy window charts. what generates the alerts? I thought the buy and sell command lines did?
profile picture

doccanter

#22
also, the sell alerts that do generate in the strategy monitor, don't auto-stage into the orders tool even though i have that option selected. I can manually select the alerts and select stage orders and they will stage at that point but the auto feature does not seem to work. any thoughts?
profile picture

doccanter

#23
never mind on my last question - I found the answer in the User Guide:

QUOTE:
Alerts to exit positions that are not owned in your account are not automatically transmitted to the Orders tool when Auto-Stage or Auto-Place is enabled. These Alerts will still be visible in the Alerts views, however


I think my problems are stemming from the buying not generating alerts and orders
profile picture

Eugene

#24
Make sure to always load enough bars. Specifying too few bars in the Data Range control may lead to trades/alerts getting out of sync.
profile picture

doccanter

#25
so when you say load enough bars, I see this a lot in the examples:

CODE:
Please log in to see this code.


is the 20 count for bar the loading you're talking about, as opposed to mine starting at zero?
profile picture

doccanter

#26
if you're talking about the historical data range, I'm including at least one day prior, which when I'm using one minute bars is starting with at least 390 bars loaded, if I'm understanding the terminology you're using. thanks in advance I appreciate your help
profile picture

Eugene

#27
The historical data range is what I'm talking about. If your trades ever span more than a day than "at least one day prior" is positively not enough.
profile picture

doccanter

#28
My trading program is only designed for intraday so in light of that what do you think?

Also does it have anything to do when I open the strategies/turn on streaming with respect to when the market is open or closed?
profile picture

Eugene

#29
OK let's assume the strategy now closes positions before the end of day. Alerts from the buy side should be fine if you load whatever it required for them to trigger e.g. several days of data. Try to load even more data. If still no go, make the entry condition(s) less restrictive and see if it would improve the buy alerts.
profile picture

doccanter

#30
OK, so I've gone back and am streaming the intraday 1-minute pair trade strategy (including the PairsBarLoader) with 10 days of data. The strategy, at least according to the strategy window, appears to be working as intended. However, the entry alerts are not generated and passed to the orders tool even though they show up on the strategy window's "trade" log (see attached photo). When applicable, the exit alerts are generated and passed to the orders tool but they error out because it says the shares are not there to sell. Then despite the order tool problems, the strategy window will ultimately show that the pair trade closed out.

You had suggested making the entry condition less restrictive but it already doesn't seem very restrictive as a large number of buys are made per day.so I am not sure what else to try with respect to that tactic. I've listed the buy section of the code below for your reference. Any help you can provide would be greatly appreciated. I feel like I'm right near the end but just have to get over this last obstacle. Thanks in advance.

CODE:
Please log in to see this code.
profile picture

doccanter

#31
lets try this again
profile picture

Eugene

#32
That screenshot is really fugly. ;)

To take a screenshot, please strike "Prt Scrn" key on your keyboard. (Make sure the entire WLP window is visible.) Then start MS Paint (which is bundled with Windows). Press Ctrl-V (or choose Paste from menu). Now save as PNG format. Voila!
profile picture

doccanter

#33
OK see attached
profile picture

Eugene

#34
Looks good now. Here's the problem in your code:
CODE:
Please log in to see this code.

When it comes to generate an Alert (i.e. bar == Bars.Count), your code throws an IOOB (index out of bounds) exception and then the loop has to continue. Never a chance to issue a buy Alert.
profile picture

doccanter

#35
That did the trick! Thank you for all your help!!!
profile picture

Eugene

#36
Glad to help you.
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).