Click here to Login





                                                   Pairs trading

  1

0
Peter Gum
2011-03-12 07:44:55


I know I am doing something wrong, but what? I am trying to get the 'Pairs Trading Strategy' (783) to work. It makes no trades (simulated) for me using the default symbols. No other pairs of symbols I pick work either.
Thanks for some guidance.
~Pete



QuantShare
2011-03-12 08:34:40

  0

Make sure that you have EOD or intraday data (depending on your time frame) for the symbols you are using in this pairs trading system.

Also, make sure that the symbol specified in "Symbols & Dates" tab (Update a trading system) exists.



Peter Gum
2011-03-12 09:39:10

  0

Thanks. (Your 'Symbols & Dates' point was the answer.)
Two more questions:
- How would I plot/graph the difference in the closes of two prices (stocks)?
- How can I view the script for this strategy? For example, I am contemplating taking a position not when the the difference crosses over 2 deviations but when it crosses back over on the way down.
Thanks.
~Pete



Peter Gum
2011-03-12 13:54:48

  -1

I figured out the answer to the second question. However, this code confuses me:
--------------
string formula = "ratio = close / GetSeries('" + pair2 + "', close);sd1=Stddev(ratio, " + period + ");avg1=sma(ratio, " + period + ");";
MMParser parser = Data.ParseFormula(formula);
TimeSeries ratio = parser.GetTimeSeries(pair1, "ratio");
TimeSeries sd = parser.GetTimeSeries(pair1, "sd1");
TimeSeries mean = parser.GetTimeSeries(pair1, "avg1");

string ident = pair1 + "-" + pair2;
if(Variables.IsVariableExists(ident))
{
// Pair Found - Exit if ratio lower than mean + one standard deviation
if(ratio[0] < mean[0] + sd[0] && ratio[0] > mean[0] - sd[0])
------------------
Let me have a try at understanding:
- 'pair2' in the formula (line 1) is just a place holder.
- the next four lines apply the forumla to pair1, to obtain ratio, sd1 and avg1 for pair1.
-Why define ident followed by if it exists? Is that an error test?
-why ratio[0] (indexed) and not when returned from parsing? Has that got to do with 'vectors'?
(It also seems to mean the behavoir of pair1, not the behavior of the spread, controls buying and selling?)

(FYI, my interest in this is inspired by this article: http://www.ljmu.ac.uk/Images_Everyone/Jozef_1st(1).pdf )

I thank you for your patience on this. I'm trying to learn . . .
~Pete



Peter Gum
2011-03-12 14:07:48

  0

I think I have one of the answers: ratio is a timeseries which is why ratio[0] is used in the if statement. (And why sd and mean are indexed.) ~Pete


QuantShare
2011-03-12 15:30:49

  0

The "parser.GetTimeSeries" function returns a time-series. ratio[0] references the ratio at the current bar while ratio[1] references the ratio one bar ago.

The ratio, sd and mean are calculated using the pair1 symbol and also pair2 (GetSeries function inside the formula).

---> close / GetSeries('" + pair2 + "', close)

Close references pair1 and GetSeries('" + pair2 + "', close) references the close price of pair2

I will answer the other questions on monday. Thank you.



Peter Gum
2011-03-12 16:05:22

  0

Thank you! That helps. The non-obvious-to-me part is that the first close refers to pair1. That means the formula computes ratio, sd1 and avg1 for the *ratio* of the respective closes. Aha. But the remaining steps then are unclear. (Perhaps a brief outline of the mathematics being computed would help.) This is progress . . .
~Pete



Peter Gum
2011-03-13 07:52:31

  0

I think I got it! Very clever. This statement:
TimeSeries ratio = parser.GetTimeSeries(pair1, "ratio");
supplies the pair1 close while executing the formula; the value of the named variable ("ratio"; a series) is returned (as your note said).
The 2 statements following are redundant but there is no other way to capture all three series (ratio, sd1 and avg1) at once.
How am I doing?

The open questions then are what is ident for and how to graph the ratio?

Again, thank you for your patience.
~Pete



QuantShare
2011-03-14 06:46:12

  0

- The function "Data.ParseFormula" parses the formula and therefore "parser.GetTimeSeries" function is executed very quickly.

- "ident" is used to recognize whether a pair is active or not. If it is active (entered) then we evaluate for the exit rule and if it is not then we evaluate the entry rule (buy a security and sell the other one).

- To plot the difference in the closes of two prices (stocks), right click on a chart and select "Edit Formula". Type the following formula:

a = GetSeries("AA", close);
b = close - a;
plot(b, "Diff");

Click on "Update Graph". (Replace AA by a symbol name)

By clicking on "Update Indicator" (third icon on the top of a chart pane) and selecting the latter formula, you can quickly update the symbol name "AA".



Peter Gum
2011-03-14 08:30:25

  0

Thank you very much. That is very helpful. This object, which you created, has allowed me to rather quickly and efficiently uncover a few promising pairs.
'Rapid prototyping' is a valuable attribute of Quantshare.
Let me mention some items that came to my attention while working with this:
- Under 'Symbols & Dates' "Period" is not sticky. It kept reverting when I made any other changes to a strategy. It should be sticky.
- It would have been convenient if there were a built-in control for limiting the per-trade commitment, perhaps a percent of available equity. I could not figure out how the default that was used was arrived at.
- In a live account a broker may in fact not have shares available to short. Thus the short position would be taken first and the long position not entered until the short is filled. Can you outline how this might be done?
- I am currently contemplating a handful of pairs. It would be convenient if there were a script that formed a timeseries from the pair and allowed that to be treated like a symbol. (Although the implications of that seem like an awful lot of work!) (I did not see how to make "Composites" handle this, but maybe I missed something?)

As my questions reflect, Quantshare is enabling me to uncover practical and useful strategic opportunities.
That has made it worth the effort to dig into the details.

~Pete



QuantShare
2011-03-14 14:58:46

  0

Best Answer
- Thank you for reporting this. We will fix it.

- If you do not specify a number of shares in the "Functions.AddLongPosition" function then the portfolio will take a position that represents (Total Equity / Maximum number of positions).
You can update this programmatically using "UpdateCategorySettings" function. Example:
Portfolio.UpdateCategorySettings("long", 50, 4, null);
Portfolio.UpdateCategorySettings("short", 50, 4, null);

- By saying "not have shares available to short", do you mean that no cash is available?

- You can create a composite symbol. Example: difference between stock prices and then references that composite.
Check Help -> QuantShare Lessons for more info on how to create a composite.
You can also download composites from the sharing server to see how they are implemented.



Peter Gum
2011-03-14 15:49:23

  0

Thank you! I will look into those suggestions.

Sometimes a broker (e.g., IB) will reject an order for lack of current shortable inventory (error code 201 I believe). IB even provides an API to check for inventory currently available for shorting (tick value 46).

~Pete



Peter Gum
2011-03-14 16:24:45

  0

I put this in the 'Composite' window:
composite = GetSeries("trow",close) - GetSeries("qqqq",close);
The available calcuations are aggregates that I don' want; I want 'value of composite'; actually I just want to plot composite.
How do I do that?
Thanks.
~Pete



QuantShare
2011-03-15 07:35:04

  0

- There is no way to know if there is a lack in shortable inventory (unless you have a database of shortable inventory per day). This could be done later when the realtime + broker API version is released.

- You must reference a single symbol in "Select Symbols" control. Example: trow
Then type the following formula:
composite = close - GetSeries("qqqq",close);



Peter Gum
2011-03-15 09:41:46

  0

Thank you.
- On point one, mainly I was observing that there are often considerations that affect live trading that are not pertinent when backtesting, meaning only that backtested scripts often need to be enhanced to deal with additional administrative and risk realities for live trading.
- I made the changes. Sorry I am being so slow 'catching on' as to how things work. I appreciate the help.

~Pete



QuantShare
2011-03-15 10:16:34

  0

- That is true. Some risks you encounter in live trading cannot be reproduced during backtests.

- No problem Pete.

If you have some suggestions on how to improve usability or have some feature suggestions. Please don't hesitate to send them to me at support@quantshare.com



Peter Gum
2011-03-15 11:05:38

  0

The third parameter of "AddLongPosition" is '_Trading Order order'. What is it? I don't know what to set it to (intellisense does not tell me)?

"UpdateCategorySettings' does provide for limiting individual trades, which I think would be useful. (It can be useful to enforce diversification.)

~Pete



QuantShare
2011-03-15 11:36:54

  0

The third parameter specify the type of order (market or limit) to use to enter the position. You can set it to null (use simulator default settings) or you can use the "Orders" class.


No more messages
0




Reply:

No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"










QuantShare

Trading Items
Pairs Trading Strategy
31 Forex Pairs Historical Data
Forex Trading Indicator - Better than expected Economic Calendar ...
Currency Trading News from Yahoo - Forex Market
One-Minute Intraday Data for Currency Pairs

How-to Lessons
How to create a trading system
How to detect stocks trading near all time highs
How to import trading items from other accounts
How to debug a trading system using the money management tool
How to import trading data from CSV files

Related Forum Threads
Cycle Trading Rule
QuantShare Trading Software: New Version 2.9.2
Trading strategy over a socket connection
How to read the number of positions in trading system
Reinvest Dividends in Your Trading System - Script

Blog Posts
Trading Items: Data Download using .Net Scripts
Buy the best/top rated stocks or how to create powerful rank base...
How to create buy and sell trading rules based on News Data
How to create a trading system, screen and composite using earnin...
QS Trading Software: Global Script









QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.