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");
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");
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) )
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");
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.
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");
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) )
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.