Click here to Login





                                                   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...
Return per Bar Stop - Money Management Strategy
Long strategy created with the money management tool
Money Management System to Split Order Into Smaller Pieces
Averaging Down Money Management Strategy

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

Related Forum Threads
Money Management Script variables and more
Backtesting: Using lookback money management feature
How do I reduce a position size from within the Money Management
example of advanced money management tool Stop Order
Money Management vs. Trading System

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 the Percentage Scale









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.