Click here to Login

                                                   Backtesting: Using lookback money management feature


2011-08-20 18:15:45

Hello, I am trying to backtest the money management functuionality that rejects non profitable positions. I am using the code (see below) but get the exact same results whether I use this code or I don't

My questions are...
1) Why is it not rejecting any trades?
2) How can I change this to base the rejection on the Sharpe ratio?
3) Is there a document of other ratios (Sortino, Ulcer, etc) that I can use to reject other trades if I wish.

Thank you

======= Code ==================

double pastdays = (double)Variables.GetVariable("Past Days");
double nbtrades = (double)Variables.GetVariable("Number of Trades");

MMPosition[] positions = Portfolio.GetClosedPositions(Divers.CurrentDate.AddDays(-pastdays));
int neg = 0;
for(int i=0;i<positions.Length;i++)
if(positions[i].Symbol == NewPosition.Symbol && positions[i].Performance < 0)
if(neg >= nbtrades)
Divers.Output("Position Rejected: " + NewPosition.Symbol);

2011-08-22 09:29:12


The above code rejects a position (trade) if there were at least "nbtrades" non profitable trades (Previous trades for the same stocks).
Is that what you want to do?

2011-08-22 11:19:31


Yes, That is what I wish to do in my question 1. I am doing this to test the logic of this money management routine. In addition to getting the same results (using the lookback logic vs not using), I do not see any increase in the time for the backtesting to finish using the lookback logic vs not using.

Eventually I would like to substitute a different variable than "non profitable trades" like "Sharpe". That is the reason for my question 2 and 3 in the original post.

2011-08-23 07:22:16


Try setting a low value for "nbtrades" (Example: 1)

The reason you see no difference is maybe because each stock is traded only once.

2011-08-23 11:19:21


I tried a lower number of trades and no change. I then looked at the trade history for both backtesting runs (with lookback and without). Both trade histories were the same. I then ran another backtest (non lookback) using only 1 stock taken from the trade history from the lookback backtest. I found many stocks this way that had a negative profit and also less trades than the required criteria.

Please help.

Also, back to my question 2 and 3 in the original post...

2) How can I change this to base the rejection on the Sharpe ratio? Please give example.
3) Is there a document of other ratios (Sortino, Ulcer, etc) that I can use to reject other trades if I wish. If not, can you give example?

2011-08-24 08:00:52


Best Answer
1) The script works for me.

I have tried a simple trading system with the following settings:
- Past Days = 100 (Look for the past 100 days)
- Nb Trades = 1

2) You should replace "positions[i].Performance < 0":

You can change the previous line by:

TimeSeries ts = Data.ParseFormula("a = sharpe(close, 30);").GetTimeSeries(NewPosition.Symbol, "a");
if(positions[i].Symbol == NewPosition.Symbol && rs[0] > 1)

// Sharpe based on the previous 30 bars

3) You should create your own implementation of these ratios.

You can also hire one of our developers. If you are interested, please send me an email to

No more messages


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"


Trading Items
Investing in stocks using the Kelly criterion money management st...
Withdraw a Fixed Amount Every Month - Money Management
Correlation Filter Money Management Strategy
Return per Bar Stop - Money Management Strategy
Averaging Down 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 draw an horizontal segment line using QS language
How to plot a stock using different periods in the same chart
How to get stocks for a particular index using the global script

Related Forum Threads
Place a profit stop through Money management strategy
Money Management script and exit options in the wizard
Get the Previous N bar Date/DateTime in Money Management script
Advanced Money Management
Money management - C#

Blog Posts
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 ...
Several money management strategies in a trading system
Money Management: Scale-in Trading Strategy

Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2021
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
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.