Click here to Login





                                                   Long-Long Pair trading

  0

0
F Mazandarany
2010-12-22 14:58:16


Thanks to your help, I have been using the MMscript below to test strategies that use buy/sell rules to go long one symbol and go short a second symbol when the long is exited, like a two-symbol pair. In the example below , I go long SPY when the buy rule1 is satisfied, and exit when the sell rule2 is true, and simultaneously go short ^gspc., and cover the short when the SPY rule1 is true again. What I would like to change is to go long a second symbol when I exit the first Long symbol. So I would go long SPY using rule1, exit SPY using rule2, and simultaneously enter a long with another symbol, say IEL. In summary, I would have a 2- symbol Long-Long pair where I would be fully invested in one long or the other depending on the buy/sell rules set for symbol 1.

You help in changing the script below to accomplish this is very appreciated.


string symbolLong = "spy";
string symbolShort = "^GSPC";
string rule1 = "a = close > sma(200)*1.035;";
string rule2 = "a = close < sma(200)*0.965;";
TimeSeries buy = Data.ParseFormula(rule1).GetTimeSeries(symbolLong, "a");
TimeSeries sell = Data.ParseFormula(rule2).GetTimeSeries(symbolLong, "a");

if(buy[0] > 0)
{
if(!Portfolio.IsInPortfolio(symbolLong, true))
{
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("short", 0, 1, null);
Portfolio.UpdateCategorySettings("long", 100, 1, null);

Functions.AddLongPosition(symbolLong, null);
}
}
else if(sell[0] > 0)
{
if(!Portfolio.IsInPortfolio(symbolShort, false))
{
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("long", 0, 1, null);
Portfolio.UpdateCategorySettings("short", 100, 1, null);

Functions.AddShortPosition(symbolShort, null);
}
}

if(Variables.IsVariableExists("equity"))
{
double equity = (double)Variables.GetVariable("equity");
double roc = Math.Round(((Portfolio.Equity / equity) - 1) * 100, 4);
string text = Divers.CurrentDate.ToString() + (char)Keys.Tab + roc;
Variables.SetVariable("output", Variables.GetVariable("output") + Environment.NewLine + text);
}
else
{
Variables.SetVariable("output", "");
}

Variables.SetVariable("equity", Portfolio.Equity);



QuantShare
2010-12-23 04:30:33

  0

To do this, you must add the following line after (Functions.AddShortPosition(symbolShort, null))

Functions.AddLongPosition("IEL", null);

And update the percentage to invest in the long and short categories.

Example: (100% Long and 50% Margin Short)
Portfolio.UpdateCategorySettings("long", 100, 1, null);
Portfolio.UpdateCategorySettings("short", 50, 1, null);



F Mazandarany
2010-12-23 11:54:28

  0

Thanks, this works. The only problem is that once it enters the second long it trades it every day (it buys and sells the second long every day) until the first long is entered. In the script below, I used SPY for the first long symbol, and IAU for the second long symbol. I have included a picture for you to see: http://img145.imageshack.us/i/qs3h.jpg/. It shifts between the two longs on the correct dates as shown by the arrows in the picture, but it trades the IAU daily in between .. The Simulation was run from 1/1/2010-12/23/2010.

string symbolLong = "spy";
string symbolShort = "^GSPC";
string rule1 = "a = close > sma(200)*1.035;";
string rule2 = "a = close < sma(200)*0.965;";
TimeSeries buy = Data.ParseFormula(rule1).GetTimeSeries(symbolLong, "a");
TimeSeries sell = Data.ParseFormula(rule2).GetTimeSeries(symbolLong, "a");

if(buy[0] > 0)
{
if(!Portfolio.IsInPortfolio(symbolLong, true))
{
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("short", 0, 1, null);
Portfolio.UpdateCategorySettings("long", 100, 1, null);
Functions.AddLongPosition(symbolLong, null);
}
}
else if(sell[0] > 0)
{
if(!Portfolio.IsInPortfolio(symbolShort, false))
{
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("long", 100, 1, null);
Portfolio.UpdateCategorySettings("short", 0, 1, null);

Functions.AddShortPosition(symbolShort, null);
Functions.AddLongPosition("IAU", null);
}
}
if(Variables.IsVariableExists("equity"))
{
double equity = (double)Variables.GetVariable("equity");
double roc = Math.Round(((Portfolio.Equity / equity) - 1) * 100, 4);
string text = Divers.CurrentDate.ToString() + (char)Keys.Tab + roc;
Variables.SetVariable("output", Variables.GetVariable("output") + Environment.NewLine + text);
}
else
{
Variables.SetVariable("output", "");
}

Variables.SetVariable("equity", Portfolio.Equity);



QuantShare
2010-12-23 12:21:40

  0

This is because "^GSPC" is never sold (You have set the short category to 0) and therefore the following rule is always TRUE:
if(!Portfolio.IsInPortfolio(symbolShort, false))

You can change this line by:
if(!Portfolio.IsInPortfolio(symbolShort, false) && !Portfolio.IsInPortfolio("IAU", true) )



F Mazandarany
2010-12-23 21:23:15

  0

When I make the change you suggested as shown below in line 21 of the code, I get errors that overwhem my limited ability to fix. Once again, I appreciate your patience

string symbolLong = "spy";
string symbolShort = "^GSPC";
string rule1 = "a = close > sma(200)*1.035;";
string rule2 = "a = close < sma(200)*0.965;";
TimeSeries buy = Data.ParseFormula(rule1).GetTimeSeries(symbolLong, "a");
TimeSeries sell = Data.ParseFormula(rule2).GetTimeSeries(symbolLong, "a");

if(buy[0] > 0)
{

if(!Portfolio.IsInPortfolio(symbolLong, true))

{
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("short", 0, 1, null);
Portfolio.UpdateCategorySettings("long", 100, 1, null);
Functions.AddLongPosition(symbolLong, null);
}
}
else if(sell[0] > 0)
{

if(!Portfolio.IsInPortfolio(symbolShort, false) &&; !Portfolio.IsInPortfolio("IAU";, true) )
{
Functions.CloseAllPositions(0);

Portfolio.UpdateCategorySettings("long", 100, 1, null);
Portfolio.UpdateCategorySettings("short", 0, 1, null);

Functions.AddShortPosition(symbolShort, null);
Functions.AddLongPosition("IAU", null);
}
}
if(Variables.IsVariableExists("equity"))
{
double equity = (double)Variables.GetVariable("equity");
double roc = Math.Round(((Portfolio.Equity / equity) - 1) * 100, 4);
string text = Divers.CurrentDate.ToString() + (char)Keys.Tab + roc;
Variables.SetVariable("output", Variables.GetVariable("output") + Environment.NewLine + text);
}
else
{
Variables.SetVariable("output", "");
}

Variables.SetVariable("equity", Portfolio.Equity);





QuantShare
2010-12-24 05:19:27

  0

Replace
if(!Portfolio.IsInPortfolio(symbolShort, false) &&; !Portfolio.IsInPortfolio("IAU";, true) )

with

if(!Portfolio.IsInPortfolio(symbolShort, false) && !Portfolio.IsInPortfolio("IAU", true) )

(Remove semi-colons)



F Mazandarany
2010-12-24 10:55:06

  0

After I made this change It now does the opposite, trades the first long symbol (SPY) every day, and also does not apply the rules correctly. Appreciate your patience.


QuantShare
2010-12-24 11:14:28

  0

Are you sure SPY is traded every day?

Please make sure you have quotes data for IAU and ^GSPC symbols.

The above script works for me.



F Mazandarany
2010-12-27 10:52:32

  0

Yes, actually both SPY and IAU are traded every day, and the rules are not applied correctly. See picture at: http://img29.imageshack.us/i/qs4vf.jpg/.
Here is the script directly from the MM screen.

string symbolLong = "spy";
string symbolShort = "^GSPC";
string rule1 = "a = close > sma(200)*1.035;";
string rule2 = "a = close < sma(200)*0.965;";
TimeSeries buy = Data.ParseFormula(rule1).GetTimeSeries(symbolLong, "a");
TimeSeries sell = Data.ParseFormula(rule2).GetTimeSeries(symbolLong, "a");

if(buy[0] > 0)
{
if(!Portfolio.IsInPortfolio(symbolShort, false) && !Portfolio.IsInPortfolio("IAU", true) )
{
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("short", 0, 1, null);
Portfolio.UpdateCategorySettings("long", 100, 1, null);
Functions.AddLongPosition(symbolLong, null);
}
}
else if(sell[0] > 0)
{
if(!Portfolio.IsInPortfolio(symbolShort, false))

{
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("long", 100, 1, null);
Portfolio.UpdateCategorySettings("short", 0, 1, null);

Functions.AddShortPosition(symbolShort, null);
Functions.AddLongPosition("IAU", null);
}
}
if(Variables.IsVariableExists("equity"))
{
double equity = (double)Variables.GetVariable("equity");
double roc = Math.Round(((Portfolio.Equity / equity) - 1) * 100, 4);
string text = Divers.CurrentDate.ToString() + (char)Keys.Tab + roc;
Variables.SetVariable("output", Variables.GetVariable("output") + Environment.NewLine + text);
}
else
{
Variables.SetVariable("output", "");
}

Variables.SetVariable("equity", Portfolio.Equity);



QuantShare
2010-12-27 11:31:47

  0

Best Answer
You have made a mistake in the above formula. Please read again my second response.

You have replaced
if(!Portfolio.IsInPortfolio(symbolLong, true))
by
if(!Portfolio.IsInPortfolio(symbolShort, false) && !Portfolio.IsInPortfolio("IAU", true) )

While you should replace
if(!Portfolio.IsInPortfolio(symbolShort, false))
by
if(!Portfolio.IsInPortfolio(symbolShort, false) && !Portfolio.IsInPortfolio("IAU", true) )



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
Forex Trading Indicator - Better than expected Economic Calendar ...
Pairs Trading Strategy
Strategy Indicator - Percent winning trades for a trading rule
Transform any order into a limit order selectively based on a tra...
Ichimoku Kinko Hyo - Trading Indicator

How-to Lessons
How to create a trading system
How to get trading orders from a portfolio programmatically
How to optimize the number of positions in a trading system
How to export trading data to a CSV file
How to set order type of a trading system programmatically

Related Forum Threads
Pair Trading Strategy would be nice!
Create a trading system
How to read the number of positions in trading system
Reinvest Dividends in Your Trading System - Script
How to edit a trading system i've downloaded from shared server?

Blog Posts
Synchronize Buy/Sell List of Rules in the Trading System Optimize...
True portfolio simulation applied to trading systems
Charting, Backtesting and Trading using Fundamental Data
4 indicators to create adaptive trading systems
Trading System: Buy Stocks based on their Sharpe Ratio Rank









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.