Click here to Login

Trading Software Trading objects Features How-to Blog Search


                                                   How do I use variables in Money Management Scripts?

  0

0
Russ
2010-09-28 22:13:28


I am trying to write a script that will close a position when a position closes below a trailing variable. I want to set (or update if it has been set already) my stop if:


1) sma(close, 20) < sma(close, 50) (20 day sma is less than the 50 day sma on the current bar)
AND
2) ref((sma(close,20),1) > ref((sma(close,50),1) (yesterday's 20 day sma is greater than yesterday's 50 day sma)


If these two conditions are true I want to set a variable (I use "stop") equal to the close of the current bar * 0.95 (stop = close*0.95)


Last of all, if the close of any future bar is less than "stop", I would like to close the position. Here is my first attempt at writing a script, I am fairly certain I butchered it.
If someone could post a working script for this and/or point out where my code is wrong I would greatly appreciate it. I only run a simulation on 1 symbol (^DJI) in order to
simplify the setting of variables since with multiple symbols I believe you might have to make an array of the variable "stop"??? Listed below is my code:


OnNewPosition:

Variables.SetVariable("stop", 0);




OnEndPeriod:

if (Portfolio.GetNumberOfPositions("long") > 0) // this code doesn't need to run if there are no active positions.
{

MMPosition[] positions = Portfolio.GetOpenPositions();
for(int i=0;i<positions.Length;i++)
{
MMPosition pos = positions[i];

MMParser parser = Data.ParseFormula("b = close");
TimeSeries b = parser.GetTimeSeries(pos.Symbol, "b");

MMParser sma20parser = Data.ParseFormula("sma20 = sma(close,20)");
TimeSeries sma20 = sma20parser.GetTimeSeries(pos.Symbol, "sma20");

MMParser sma50parser = Data.ParseFormula("sma50 = sma(close,50");
TimeSeries sma50 = sma50parser.GetTimeSeries(pos.Symbol, "sma50");



if (sma20[0] < sma50[0])
{
if (sma20[-1] > sma50[-1])
{

Variables.SetVariable("stop", b[0] * 0.95);

}
}


if (b[0] < (double)Variables.GetVariable("stop"))
{

Functions.SellPosition(pos.Symbol, Orders.OpenMarketOrder());

}


} // end for
} // end inital if



When I run a simulation with this script it does not execute and I get the following error message:

Error: OnEndPeriod : Event 'OnEndPeriod': Specified cast is not valid.



Also runs really slow, takes more than 1 minute to run on 1 symbol...


Please help! :)



QuantShare
2010-09-29 05:58:43

  0

Best Answer
Hi,

Five important things first:

- sma20[-1] refers to a future bar. You should use sma20[1] instead to refer to yesterday.

- You get the "Specified cast is not valid" error because variable "stop" is of type Integer.
In OnNewPosition you should use: Variables.SetVariable("stop", (double)0);

- You forgot the closing parenthesis "sma(close,50" in the following line:
MMParser sma50parser = Data.ParseFormula("sma50 = sma(close,50");

- You forgot the semi-colon at the end of each formula inside the "ParseFormula" function.

- Use the "Divers.Output" function to debug a money management script. Output data will be displayed under the "Details" tab in the simulation report.

Here is a modified version of your script:
if (Portfolio.GetNumberOfPositions("long") > 0)
{
MMPosition[] positions = Portfolio.GetOpenPositions();
for(int i=0;i<positions.Length;i++)
{
MMPosition pos = positions[i];
if(pos.Var1 == null) // Otherwise the stop is already set
{
MMParser parser = Data.ParseFormula("sma20 = sma(close,20);sma50 = sma(close,50);");
TimeSeries sma20 = parser.GetTimeSeries(pos.Symbol, "sma20");
TimeSeries sma50 = parser.GetTimeSeries(pos.Symbol, "sma50");
TimeSeries close = Data.GetPriceSeries(pos.Symbol, "close");

if (sma20[0] < sma50[0] && sma20[1] > sma50[1])
{
pos.Var1 = close[0] * 0.95;
}
}

// Check the stop
if(pos.Var1 != null)
{
double stop = (double)pos.Var1;
TimeSeries close = Data.GetPriceSeries(pos.Symbol, "close");
Divers.Output("Check Stop: " + close[0] + " - Stop: " + stop);
if(close[0] < stop)
{
Functions.SellPosition(pos.Symbol, Orders.OpenMarketOrder());
}
}

} // end for
} // end inital if


Note that I have executed your script and the above one on 1 symbol and it took less than 3 seconds.
Note also that you can use the "Trailing Stop" option on the "Update Trading System" form.

Please let me know if you have any questions.



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
Investing in stocks using the Kelly criterion money management st...
Averaging Down Money Management Strategy
Long strategy created with the money management tool
Money Management System to Split Order Into Smaller Pieces
Correlation Filter Money Management Strategy

How-to Lessons
How to calculate the average of a time series using the money man...
How to debug a trading system using the money management tool
How to download and use U.S. stocks earnings data
How to use the Fibonacci retracement drawing tool
How to use a custom drawing tool

Related Forum Threads
Money Management Script variables and more
Backtesting: Using lookback money management feature
Example of Money Management
Money Management Scripting
Advanced Money Management

Blog Posts
Several money management strategies in a trading system
Basic trading system implemented using the money management tool
Create a trading strategy using the money management tool - Part ...
Create a trading strategy using the money management tool - Part ...
How to use date components in your trading rules









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 © 2012 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items