Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/06/23 11:29
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
7 registered members (3run, miwok, AndrewAMD, Quad, TipmyPip, fairtrader, 1 invisible), 637 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 2 1 2
Re: Using OptimalF to help pick the best combos [Re: dusktrader] #427245
08/05/13 14:30
08/05/13 14:30
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
One thing I noticed... it says the simulation period starts in 2010 now, instead of 2002 as I requested. I assume this is because one of the pairs does not have data until 2010 (ie, lowest common denominator?) If this is the case, I should probably eliminate that pair to get a longer test.

EDIT: that does seem to be the case. I swapped out 3 pairs with the next 3 best-performers... now all have data back to 2002. That gave me less desirable results, so I want to keep poking at this. Maybe I'll try to add equity-curve trading next.

Last edited by dusktrader; 08/05/13 14:58.
Re: Using OptimalF to help pick the best combos [Re: dusktrader] #427257
08/05/13 20:55
08/05/13 20:55
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
One question I do have is... why is Profit Factor and OptimalF different when training with only 1 pair vs. several in a loop? I'm sure there is a reason -- I'd just like to understand it better.

My understanding is that -- the way this loop is written -- when training, it should calculate parameters and OptimalF for each pair individually. Therefore, why do the stats appear differently (ie, Profit Factor and OptimalF) when comparing the individual train vs. the group train?

I individually trained each pair, then selected these top-6 performers. Then I put them together in a loop and trained again:

Quote:
Walk-Forward Test Workshop5_3cream_dusktrader portfolio - performance report

Simulation period 25.04.2002-10.06.2013
Test period 04.11.2008-10.06.2013
WFO test cycles 4 x 1843 bars (63 weeks)
Training cycles 5 x 10443 bars (360 weeks)
Lookback time 500 bars (17 weeks)
Assumed slippage 10.0 sec

Gross win/loss 7786$ / -3523$ (+44413p)
Average profit 927$/year, 77$/month, 3.57$/day
Max drawdown -244$ 6% (MAE -473$ 11%)
Total down time 30% (TAE 94%)
Max down time 35 weeks from Aug 2012
Largest margin 310$
Trade volume 362564$ (78864$/year)
Transaction costs -107$ spr, -18$ slp, 336$ rol
Capital required 507$

Number of trades 322 (71/year, 2/week, 1/day)
Percent winning 48%
Max win/loss 331$ / -127$
Avg trade profit 13$ 137.9p (+50$ / -21$)
Avg trade slippage -0.06$ -0.6p (+0.03$ / -0.14$)
Avg trade bars 138 (+224 / -57)
Max trade bars 1397 (48 weeks)
Time in market 603%
Max open trades 12
Max loss streak 10 (uncorrelated 9)

Annual return 470%
Profit factor 2.21 (PRR 1.89)
Sharpe ratio 1.53
Kelly criterion 1.27
OptimalF .185
Ulcer index 4%
Prediction error 31%

Portfolio analysis OptF ProF Win/Loss Cycles

AUDJPY avg .193 2.87 11/20 /\\/
CHFJPY avg .189 1.63 68/52 X//X
EURAUD avg .206 2.67 14/39 //\\
EURNZD avg .049 1.57 3/8 ///\
EURUSD avg .233 2.68 43/27 ///\
GBPAUD avg .191 1.98 17/20 \/\/

AUDJPY:L .193 2.87 11/20 /\\/
AUDJPY:S .000 ---- 0/0 ....
CHFJPY:L .123 1.40 32/30 \///
CHFJPY:S .255 1.94 36/22 ///\
EURAUD:L .000 ---- 0/0 ....
EURAUD:S .206 2.67 14/39 //\\
EURNZD:L .000 ---- 0/0 ....
EURNZD:S .049 1.57 3/8 ///\
EURUSD:L .131 2.26 15/13 ///\
EURUSD:S .334 3.19 28/14 ///\
GBPAUD:L .000 ---- 0/0 ....
GBPAUD:S .191 1.98 17/20 \/\/


Code:
// Workshop 5: Counter trend trading, optimized ////////////////

function reverseShort(int MaxTrades)
{
	// update the stops and profit targets of open trades
	if(Stop > 0) exitShort(0,priceClose()+Stop);
	if(TakeProfit > 0) exitShort(0,-(priceClose()-TakeProfit));
 
	// if MaxTrades is not reached, open a new trade
	if(NumOpenShort < MaxTrades)
	{
		// reinvest the square root of profits
		//var MarginShort = sqrt(WinShort-LossShort)*OptimalFShort;
		//Margin = clamp(MarginShort, 5, 300); //max $300 per trade
		enterShort();
	}

	// otherwise, close all opposite trades
	else if(!mode(HEDGING)) exitLong();
	return 0;
}

function reverseLong(int MaxTrades)
{
	// update the stops and profit targets of open trades
	if(Stop > 0) exitLong(0,priceClose()-Stop);
	if(TakeProfit > 0) exitLong(0,-(priceClose()+TakeProfit));
 
	// if MaxTrades is not reached, open a new trade
	if(NumOpenLong < MaxTrades)
	{
		// reinvest the square root of profits
		//var MarginLong = sqrt(WinLong-LossLong)*OptimalFLong;
		//Margin = clamp(MarginLong, 5, 300); //max $300 per trade
		enterLong(); }

	//otherwise, close all opposite trades
	else if(!mode(HEDGING)) exitShort();
	return 0;
}

function run()
{
	set(PARAMETERS+FACTORS+NFA);  // generate and use optimized parameters
	BarPeriod = 240;	// 4 hour bars
	LookBack = 500;
	StartDate = 2002;
	//EndDate = 2009;
	NumWFOCycles = 5; // activate WFO

	//if(ReTrain) {
	//	UpdateDays = -1;	// update price data from the server 
	//	SelectWFO = -1;	// select the last cycle for re-optimization
	//}

// portfolio loop
	while(asset(loop("EURUSD","AUDJPY","CHFJPY","GBPAUD","EURAUD","EURNZD")))
	{	
		// calculate the buy/sell signal
		vars Price = series(price());
		vars DomPeriod = series(DominantPeriod(Price,30));
		var LowPeriod = LowPass(DomPeriod,500);
		vars HP = series(HighPass(Price,LowPeriod*optimize(1,0.5,2)));
		vars Signal = series(Fisher(HP,500));
		var Threshold = optimize(1,0.5,2,0.1);

		Stop = optimize(4,2,8) * ATR(100);
		Trail = 4*ATR(100);

		//when training, force all trades so we can see what would be profitable via OptimalF
		if(is(TRAINMODE))
		{
			if(crossUnder(Signal,-Threshold)) reverseLong(1);
			else if(crossOver(Signal,Threshold)) reverseShort(1);
		}

		//when testing/trading only trade those known profitable via OptimalF
		if(is(TESTMODE) || (TRADEMODE)) 
		{
			//Allow more long trades for known-more-profitable
			if(crossUnder(Signal,-Threshold) && (OptimalFLong >= .15)) reverseLong(3); //known very big win
			else if(crossUnder(Signal,-Threshold) && (OptimalFLong >= .1)) reverseLong(2); //known big win
			else if(crossUnder(Signal,-Threshold) && (OptimalFLong >= .001)) reverseLong(1); //known win
	
			//Allow more short trades for known-more-profitable
			else if(crossOver(Signal,Threshold) && (OptimalFShort >= .15)) reverseShort(3); //known very big win
			else if(crossOver(Signal,Threshold) && (OptimalFShort >= .1)) reverseShort(2); //known big win
			else if(crossOver(Signal,Threshold) && (OptimalFShort >= .001)) reverseShort(1); //known win
		}

	} //while loop

	PlotWidth = 1000;
	PlotHeight1 = 600;
}


Re: Using OptimalF to help pick the best combos [Re: dusktrader] #427260
08/05/13 22:16
08/05/13 22:16

A
acidburn
Unregistered
acidburn
Unregistered
A



I'm still learning this money management stuff, and should've probably read lots of documentation before jumping on here with my own ideas, but I'll try my luck anyway.

From what I understand, Optimal F's function is to determine the ideal fraction of the money to allocate per trade based on past performance, given in percentage of the money available. [http://www.dummies.com/how-to/content/the-optimal-f-money-management-style.html]

So, in a way, it's already a complete and usable formula for position sizing. You use it simply by multiplying it with your available margin, and probably with some other factor (less than 1) depending on your risk appetite. But, that's it. I believe Z's work like that.

What doesn't feel right to me in your source, is that you first invent artificial thresholds in OptF (.0001, .1, .15), and then once again, completely artificially assign 1, 2, or 3 open trades to them, in the process destroying all that OptF has given you, a precise measure of how much to invest, in that particular instrument, and depending on the direction of the trade you plan to take.

I might be completely off with the above, but that's how I see it.

What I think you could've done, is feed ALL you instruments, and let OptF sort it out, and then also use the raw OptF value to decide if to take the trade (or not), and especially how large position to take. If you look closely, some of the OptF factors are 0, so your strategy could just skip those trades (0 lots = no trade). Once again, I believe all Z's work just like that. With, of course, some additional stuff.

Do I make any sense?

Re: Using OptimalF to help pick the best combos [Re: ] #427279
08/06/13 10:11
08/06/13 10:11
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
Originally Posted By: acidburn
What doesn't feel right to me in your source, is that you first invent artificial thresholds in OptF (.0001, .1, .15), and then once again, completely artificially assign 1, 2, or 3 open trades to them, in the process destroying all that OptF has given you, a precise measure of how much to invest, in that particular instrument, and depending on the direction of the trade you plan to take.

Yes, I believe you are right in all that you have said. I'm mostly just looking at using it in some different ways, and TRYING not to break its purpose.

I do understand the purpose is for finding the optimal weight to apply on an individual trade.

I also understand that choosing which pair to trade based on something like OptimalF represents (at least) a selection bias. But then, we've already agreed that OptimalF itself breaks the OOS rules in a sense.

I do plan to eventually use OptimalF for its intended purposes, which is to get the optimal allocation factor for a given individual trade (ie, trade weighting). (But at this stage I'm still trading with minimum 1 Lots -- strategy design phase, so factors are not applied yet to margin.)

I was trying to use OptimalF in a way that somewhat mirrors my discretionary trading... where I keep track of those assets that "work" with a trade logic, and those that don't. Because I've found that one-size-does-NOT-fit-all.

Since OptimalF takes a global look at all trades of a given strategy logic (across all assets and the entire testing timeframe)... it knows which ones COULD work, and the degree to which they could work, as compared to all the rest.

I would argue that taking multiple trades is not the same as a weighting factor that you would apply to margin. I'd like if someone could poke a hole in this theory ... if so, it would invalidate the entire reason I'm using OptimalF this way, I think.

So the intent here would be:
#1: identify which assets and trade direction have worked historically (and how well they've worked)
#2: allow MORE trades of those that we know work well, eliminate those that don't have a good track record
#3: ultimately, use OptimalF to weight those trades that we do end up using

Re: Using OptimalF to help pick the best combos [Re: dusktrader] #427280
08/06/13 10:35
08/06/13 10:35

A
acidburn
Unregistered
acidburn
Unregistered
A



OK, I'm certainly not going to stand in your way, especially when you're completely aware that your way of using OptF is slightly controversial. Not to mention that we like thinking outside the box on this forum, we agreed earlier. wink Thank you for a nice explanation and definitely good luck!

I'd very much appreciate jcl's view on your approach, but it seems that he has already found the beach Sundance and I were talking about few days ago. Oh, well... grin

Re: Using OptimalF to help pick the best combos [Re: ] #427292
08/06/13 13:10
08/06/13 13:10
Joined: May 2013
Posts: 627
Bonn
Sundance Offline
User
Sundance  Offline
User

Joined: May 2013
Posts: 627
Bonn
Next summer we will visit jcl on his beach and check it out if its a cool place to be...

@dusktrader: nice theorie of using OptimalF. When i have time i will read your thoughts again. Always good to be open minded...

Last edited by Sundance; 08/06/13 13:20.
Re: Using OptimalF to help pick the best combos [Re: Sundance] #427295
08/06/13 13:55
08/06/13 13:55
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
One other thing I'm just noticing ... the numbers can REALLY play games in your head. I was trying to figure out why the last code I posted "seemed worse" than the second-to-last code (only variation was a few instruments).

In the 2nd-to-last, we see AR 509%, whereas the last was AR 470%.

But, in looking closer... you can spot the improvement in other metrics:

2nd-to-last:
Quote:
Annual return 509%
Profit factor 2.00 (PRR 1.65)
Sharpe ratio 1.82
Kelly criterion 2.11
OptimalF .344
Ulcer index 8%
Prediction error 37%


Last (should be "best so far"):
Quote:
Annual return 470%
Profit factor 2.21 (PRR 1.89)
Sharpe ratio 1.53
Kelly criterion 1.27
OptimalF .185
Ulcer index 4%
Prediction error 31%


I think the Annual Return can almost be ignored, because right now it is all based on using only flat 1 Lot trades. This would become dramatically different when you apply OptimalF factors to the per-trade margin.

Profit Factor is a truer sign, I believe. Specifically, I really like the Pessimistic version.

Sharpe is worse on the latter. I believe this measures consistency of the returns.

Ulcer index is better, as well as Prediction error. I haven't studied Prediction error and don't fully understand it yet. But I've observed that it seems to go down as more trades are taken.

Re: Using OptimalF to help pick the best combos [Re: dusktrader] #427301
08/06/13 14:26
08/06/13 14:26
Joined: May 2013
Posts: 627
Bonn
Sundance Offline
User
Sundance  Offline
User

Joined: May 2013
Posts: 627
Bonn
Thanks for your thoughts dusktrader. The Sharpe ratio is one of the most valuable numbers i think. The greater the value the better. I don't wan't a strategy that makes no earnings 10month in a row and only pays it back on two month. That would be psychologically an overkill for me. Better 100% Annual return with a high Sharpe ratio then 300% with a ugly Sharpe ratio.

Re: Using OptimalF to help pick the best combos [Re: Sundance] #427303
08/06/13 14:39
08/06/13 14:39

A
acidburn
Unregistered
acidburn
Unregistered
A



I must agree with Sundance on this one. At the moment, I believe Sharpe is the most important number of them all.

I'm yet to do some math homework, but from what I read elsewhere, Sharpe seems to be king.

Of course, if pressed with enough arguments, I'm willing to change my mind.

Re: Using OptimalF to help pick the best combos [Re: ] #427305
08/06/13 14:50
08/06/13 14:50
Joined: May 2013
Posts: 627
Bonn
Sundance Offline
User
Sundance  Offline
User

Joined: May 2013
Posts: 627
Bonn
In my opinion the Sharpe ration is a sign of consistency and therefore a sign of robustness against the market volatility.

Page 2 of 2 1 2

Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1