Click here to Login





                                                   Simulator

  0

0 1 - Next
F Mazandarany
2010-11-23 18:05:57


I want to use 2 positions in a simulation study, where I use position 1 (say symbol SPY) to take long positions using a rule I define and for position 2(say symbol MSFT) I want to take short positions using a different rule. I don't know how to do this. I have done it where the long and short rules use the same symbol, but can't figure out how to do this for two symbols as I described. Please advise



QuantShare
2010-11-24 04:17:12

  0

There are several ways to do this. The simplest way is to filter the symbol name in the buy and short rules:

buy = YOUR_RULES and StringEqual(name(), "SPY");
short = YOUR_RULES and StringEqual(name(), "MSFT");



F Mazandarany
2010-11-24 14:36:01

  0

Thank you. I did not explain my problem clearly. What I want to do is when, say, I buy SPY using a buy rule ( close>sma100), sell it when sma<100, and at this time (upon selling SPY), enter a short or a long of a different smybol, say MSFT until the SPY buy rule is satisfied again when MSFT trade is exited. In other words, I want to be able to enter a trade using a different symbol exactly when I exit SPY, and exit this trade exactly when I enter a new trade with SPY. This will be the strategy for the simulation over a period of time to test.I should add that as it moves from one trade (say after it exitrs SPY and enters MSFT), it should invest the entire avaiable equity in the MSFT trade, and so on. So all the avaiable equity is in one trade or the other ( SPY or MSFT) depending on which rule is in effect


QuantShare
2010-11-25 05:41:03

  0

Here is a money management script that allows you to create this strategy:
- OnEndPeriod event
- Do not specify buy or sell rules

string symbolLong = " SPY";
string symbolShort = "MSFT";
string rule = "a = close > sma(100);";

TimeSeries t = Data.ParseFormula(rule).GetTimeSeries(symbolLong, "a");
if(t[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(!Portfolio.IsInPortfolio(symbolShort, false))
{
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("long", 0, 1, null);
Portfolio.UpdateCategorySettings("short", 100, 1, null);

Functions.AddShortPosition(symbolShort, null);
}
}




F Mazandarany
2010-11-26 12:56:41

  0

This only enters a MSFT short at the beginning of the defined peroid which it holds through the entire period. There must be a conditional rule that is incorrect/missing. This problem occurs regardless of beginnind date I select or the symbols I select, it always enters and holds the short symbol.
Also I would like to make the following as varables:
For entering Long: sma(a)*(1+b); for exitnig Long sma(c)*(1+d)
For entering Short sma(e)*(1+f); for exiting short sma(g)*(1+h)
so a,b,c,d,e,f,g,h would be varables in the optimization.
Thanks alot for your help.



QuantShare
2010-11-26 13:34:43

  0

I will send you the parameterized script on Monday.

The above script is correct. There is simply an extra white space in the first line (" SPY")
The first line should be:
string symbolLong = "SPY";



QuantShare
2010-11-29 04:33:04

  0

Regarding the above parameterized entry and exit rules, do you want to apply them for several stocks or for only two as with the previous script?


F Mazandarany
2010-11-30 09:48:30

  0

yes, I would like several stocks and the ability to have all longs as well as longs and shorts;ie, say I start with one or two longs and upon exiting these two, I would enter two new longs or one long and one short etc.
-



QuantShare
2010-12-01 04:16:29

  0

To do this:

- Create a simple trading system with the appropriate buy, sell, short and cover rules.
Example of buy rule: sma(a)*(1+b), then in the optimize grid, set the "a" and "b" variables start, end and step values.

- Select a Long/Short system type. The simulator will assign 50% equity to long positions and 50% equity to short positions

- Depending on how you want to change this percentage. Create a money management script that simply call the function (Portfolio.UpdateCategorySettings)



F Mazandarany
2010-12-01 13:21:38

  0

my problem is I don't know how to create separate buy/sell rules for each equity in the same strategy. Say I want to buy/sell SPY using sma of SPY>100 to buy and sma of spy<100 to sell. In the same strategy I want to buy MSTFT when MSFT sma>150 and sell when MSFT sma<150. I don'nt know how to tie sma values to specific symbols for buy, sell, short and cover rules. Please show me how to do this. Thanks for you help


QuantShare
2010-12-02 09:06:49

  0

Rules are automatically executed for each symbol.

If you specify close > sma(100) as a buy rule then the simulator will execute that rule for each symbol and enter the symbols (if there are free positions) where the buy rule condition is true.



F Mazandarany
2010-12-04 16:39:53

  0

I used the script you suggested, it works except in one aspect. It enters a long ( ILF) using the rule ILF close >SMA(200)*1.035 and exits this position when ILF close<sma(200)*1.035. It enters and exits shorts in SPY in opposite direction to ILF which is what I want. The aspect that I want to change is to exit the long ILF when its close is less than sma(200)*0.965 ( not < sma(200)*1.035). The short in SPY should occur when the ILF long is exited( close<sma(200)*0.965) and covered when the ILF long is entered( ILF close>sma(200)*1.35). Please show me how to modify the OnEndPeriod code, shown below, to accomplish this.

string symbolLong = "ILF";
string symbolShort = "SPY";
string rule1 = "a = close > sma(200)*1.035;";
TimeSeries t = Data.ParseFormula(rule1).GetTimeSeries(symbolLong, "a");
if(t[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(!Portfolio.IsInPortfolio(symbolShort, false))
{
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("long", 0, 1, null);
Portfolio.UpdateCategorySettings("short", 100, 1, null);

Functions.AddShortPosition(symbolShort, null);
}
}



QuantShare
2010-12-06 04:59:14

  0

Here is your formula: (Added a sell rule)

string symbolLong = "ILF";
string symbolShort = "SPY";
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);
}
}



F Mazandarany
2010-12-06 10:35:23

  0

Thank you. I made the change you suggested, but to my surprise, the output remains absolutely unchanged. The sames exact trades, etc.? I'm guessing that the new rule is not getting invoked in the "IF" statements!


F Mazandarany
2010-12-06 12:25:42

  0

Sorry, my mistake, your suggested change is working correctly. Thank you agaain!


F Mazandarany
2010-12-06 16:26:39

  0

Using the above MM script I would like to get an output that tabulates the bar by bar return; i.e.; the rate of change of equity per bar, in a format that can be exported/copied to an excel file. Please show me how to do this. Thanks.


QuantShare
2010-12-07 05:46:32

  0

Just before the "Functions.CloseAllPositions(0);" lines, add the following code:

MMPosition[] positions = Portfolio.GetOpenPositions();
for(int i=0;i < positions.Length;i++)
{
positions[i].AddTradeMetric("ROC Per Bar", positions[i].Performance / positions[i].BarsSinceEntry);
}

In the simulator report and under "Trades" tab, click on "Export" to export the data to CSV format.





F Mazandarany
2010-12-07 11:20:04

  0

Thanks. I added the extra lines. What I get is the normal info under "Trades' tab, plus an extra column for ROC/bar (see attached picture). What I am looking for is the actual daily (bar by bar) ROC or the actual equity value for every day from which I would calculate the ROC for every day.I know this data is there since the system plots the equity curve based on daily vales . I also noticed under the D.W.M.Y tab, there is a "Daily Return" plot, I just need this data in a tabular form. I'm including the entire script below. Thanks again for you patience in helping me.

string symbolLong = "spy";
string symbolShort = "SPY";
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))
{
MMPosition[] positions = Portfolio.GetOpenPositions();
for(int i=0;i<positions.Length;i++)
{
positions[i].AddTradeMetric("ROC Per Bar", positions[i].Performance / positions[i].BarsSinceEntry);
}
Functions.CloseAllPositions(0);
Portfolio.UpdateCategorySettings("long", 0, 1, null);
Portfolio.UpdateCategorySettings("short", 100, 1, null);

Functions.AddShortPosition(symbolShort, null);
}
}



QuantShare
2010-12-07 13:47:15

  0

Here is an example on how to do this:

- Copy the following code at the end of the previous script:

if(Variables.IsVariableExists("equity"))
{
double equity = (double)Variables.GetVariable("equity");
double roc = Math.Round(((Portfolio.Equity / equity) - 1) * 100, 4);
Global.Trace(Divers.CurrentDate.ToString() + (char)Keys.Tab + roc);
}

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

- Run the Simulation
- Select View->Output (The data will be copied there)



F Mazandarany
2010-12-08 12:48:22

  0


Thanks, it works. However; once I run the Simulator with different inputs, like changing dates(simulation period), the output window returns the same starting date as I ran the first time. Even when I change the symbols, and use entirely different dates with the new symbol, the output window starts with dates that I did not specify in the strategy. The end dates appear to be always correct. This is not a problem with the strategy report itself, the dates and the simulation are correct in the simulation report. The problem is in the output window. I'm reproducing the entire script for you to test yourself.

string symbolLong = "SPY";
string symbolShort = "SPY";
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);
Global.Trace(Divers.CurrentDate.ToString() + (char)Keys.Tab + roc);
}

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



No more messages
0 1 - Next




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
Choose function
Conditional Trailing Stop Exit 2013-10-18
Portfolio vs Stock Return
Scale-in Trading Strategy
Currency Mgmt v 0.9

How-to Lessons
How to optimize an indicator in your trading system
How to copy a trading item
How to create a formula
How to optimize the stop limit of a trading system
How to add a metric in the trading system simulation report

Related Forum Threads
Simulator question
Access the equity value of the portfolio from the simulator
Ranking System Manager and Simulator yield very different results
Simulator symbols
Create a Metric in Simulator Report

Blog Posts
3 ways to rank stocks in a trading system - Simulator and Potfoli...
QuantShare - review by the Stock Trading Software Reviews
How to Backtest Each Stock or Asset Individually
10 masks to create thousands of rules to use into your trading sy...
Trading Forex with the commitments of traders report









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.