SplitPosition endless loop
Author: 07nioe
Creation Date: 5/8/2015 5:39 PM
profile picture

07nioe

#1
Hi Eugene, I need your help again.

I would like to split a position for the exits. Compiling runs will. But If I execute the strategy computing will not come to an end. I don't know the error. Here is the relevant part of the strategy:

CODE:
Please log in to see this code.


I am sure you will realize the problem. Thank you.

Best
Niels
profile picture

Eugene

#2
Hi Niels,

Your code is splitting positions in an uncontrolled fashion. Nothing seems to be limiting it from creating more and more. That's why the code hangs. Please take a look at this Knowledge Base article and a couple of downloadable strategies:

WealthScript Techniques | Splitting a Position into Two

ActiveTrader 2011-01 | Trading Hybrid Indicators
ActiveTrader 2010-02 | RSI scale-out system (Rev.C)

Notice the "canSplit" logic preventing from the "viral replication" of splitted positions going on and on and on. Hope this helps.
profile picture

07nioe

#3
Hi Eugene,

the endless loop problem is resolved. But with the strategy below there are two other problems emerged:

1. The strategy should exit 50 percent of the position when the close is above the entry price and sells the rest after certain days. If you look at the trade's list the strategy does something else. And I don't know how to fix it.

2. The MaxDD varies depending on the backtesting period whilst the date of MaxDD remains always the same. Maybe there is an synchronization problem.

Do you see an error in the code?

Thank you for help.

Here is the strategy:

CODE:
Please log in to see this code.
profile picture

Eugene

#4
Niels,

QUOTE:
1. The strategy should exit 50 percent of the position when the close is above the entry price and sells the rest after certain days. If you look at the trade's list the strategy does something else. And I don't know how to fix it.

Fix what? As there are no written rules, can you explain what else is the strategy doing, for example?

P.S. Did you intend to buy every dip or is your code missing an "else"?

CODE:
Please log in to see this code.
profile picture

07nioe

#5
Hi,

here is the rule: the strategy should buy stocks with an RSI below 50. If the price rises and the close is higher than the entry price, it should sell 50% of the stocks. The other 50% should be sold after 10 days. That's it!. The code above however sometimes doesn't sell 50% of the position after it is in profit. Sometimes the trade's list shows that the position is splited in three. I don't know what's going on there. Sufficient Explanation?

Best
Niels
profile picture

Eugene

#6
I believe you can avoid using SplitPosition and make it simpler:

CODE:
Please log in to see this code.
profile picture

07nioe

#7
Thank you for the code. Now the strategy does what it should do. The variation of the DD figures by changing the period is still to observe. For example, if you change the period 1.1.2008 until 31.12.2013 to 1.1.2008 until 31.12.2014 the DD at exact the same date rises. I don't know why. Thank you for help.

Best
Niels
profile picture

Eugene

#8
I don't know either. Probably because I'm overlooking a) what instruments (bar scale, data provider, dataset, position sizing etc.) you're applying this code to, and since seeing is believing, b) a couple of attached screenshots illustrating what you're observing?
profile picture

07nioe

#9
Attached two screenshots:

first one shows the test done with S&P100, 10 pct of equity, period 2008 until 2014. Second: same portfolio, same pct of equity but period from 2008 until 2013. As you can see DD is different. I synchronized Indicators.

CODE:
Please log in to see this code.


profile picture

Eugene

#10
So what gives? There's been a bubble blowing in U.S. markets for half a decade. No wonder [for many a system] that the drawdown happened in the dawn of the backtest period (2009) soon after the market crash (2008). The market hasn't looked back since then so the max DD date doesn't change.
profile picture

07nioe

#11
I don't know if I understand you in the right way. But the date is not the problem. It is the different values.
profile picture

Eugene

#12
Why should the values be the same if it's a pretty different backtest? There's no contradiction with the identic dates (explained above). I'm not worried about it at all.
profile picture

07nioe

#13
I don't see this big difference in the backtests. The only difference is in the time period. One backtest ends at 2013, the other at 2014. The MaxDD is for both at 09.03.2009. I am absolutly fine with identical dates, but I am not with the fact that it shows two different figures. From 2008 until 2013 the backtests are completely the same. Thus, also the trades must be same as well as the MaxDD and its date. My other strategies show exactly this behavior. I can not understand why this strategy follows a different logic.
profile picture

Eugene

#14
To eliminate guesswork, consider using the Portfolio Inspector (or Trades tab) to find out where the difference is coming from.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).