Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
2 registered members (Quad, AndrewAMD), 1,007 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Need some help with equity-curve trading #427562
08/09/13 13:57
08/09/13 13:57
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
I'm trying to assemble a simple proof-of-concept strategy that uses equity-curve trading with phantom trades. Part of my issue, I think, is that I don't know where phantom trades are reflected (ie, should they be in the testtrades.csv log??) I can't tell if the phantom trades are working or not.

Can you please give some pointers what I'm doing wrong here? This is my script and the result image:
Code:
// phantom trade study
#include <profile.c>

// equity curve trading: switch to phantom mode when the equity 
// curve goes down and is below its own lowpass filtered value
function checkEquity()
{
	var EquityLong, EquityShort;
	vars EquityCurve = series(EquityLong+EquityShort);
	vars EquityLP = series(LowPass(EquityCurve,10));
	if(EquityLP[0] < LowPass(EquityLP,100) && falling(EquityLP))
		Lots = -1; // drawdown -> phantom trading
	else
		Lots = 1; // profitable -> normal trading
}

function run()
{
	set(NFA+LOGFILE);
	StartDate = 2002;
	vars Price = series(price());
	vars Trend = series(LowPass(Price,1000));
	vars Signals = series(0);
	
	Stop = 4*ATR(100);
	checkEquity();

	// reinvest a portion of profits
	var MarginLong = sqrt(WinLong-LossLong);
	var MarginShort = sqrt(WinShort-LossShort);
	Margin = clamp((MarginLong+MarginShort)/2, 5, 1000);

	if(valley(Trend)) {
		Signals[0] = 1;
		//exitShort();
		if(Sum(Signals+1,3) == 0)  // no signals in the previous 3 bars?
			reverseLong(1);
	} else if(peak(Trend)) {
		Signals[0] = 1;
		//exitLong();
		if(Sum(Signals+1,3) == 0)
			reverseShort(1);
	}
	

	PlotWidth = 800;
	PlotHeight1 = 320;
	plot("Margin",Margin,1,RED);
	plot("Lots",Lots,1,BLUE);
//	plot("Trend",Trend[0],0,RED);
//	plotTradeProfile(50);
}



Attached Files dt-phantom_EURUSD.png
Re: Need some help with equity-curve trading [Re: dusktrader] #427564
08/09/13 14:15
08/09/13 14:15
Joined: Apr 2013
Posts: 57
3
3DCat Offline
Junior Member
3DCat  Offline
Junior Member
3

Joined: Apr 2013
Posts: 57
why do you define EquityLong and EquityShort?
Also since Lots is always 1, your script doesn't enter any PhantomTrades.

Re: Need some help with equity-curve trading [Re: 3DCat] #427566
08/09/13 14:49
08/09/13 14:49
Joined: Jul 2000
Posts: 27,935
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,935
Frankfurt
Only real trades are in the .csv. Phantom trades are in the log and in the trade list.

Re: Need some help with equity-curve trading [Re: jcl] #427568
08/09/13 15:09
08/09/13 15:09
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
Woohoo! It's working! Thanks for the tips 3DCat and jcl. Sometimes it takes poking my brain a little to get to that Aha! moment.

3D, the answer to your question is "I don't know" LOL. I copied that segment from the manual. After reading what you said (defining EquityLong/EquityShort), I realized that was just an example that maybe didn't apply to my strategy. I switched it now to just "Equity" and it seems to do what I want. I can verify that it is switching to and from Lots=1 and Lots=-1.

I will try to spot this in the logs as well because I don't know how to do that yet. Here is my current script revision:

Code:
// phantom trade study
#include <profile.c>

// equity curve trading: switch to phantom mode when the equity 
// curve goes down and is below its own lowpass filtered value
function checkEquity()
{
	vars EquityCurve = series(Equity);
	vars EquityLP = series(LowPass(EquityCurve,10));
	if(EquityLP[0] < LowPass(EquityLP,100) && falling(EquityLP))
		Lots = -1; // drawdown -> phantom trading
	else
		Lots = 1; // profitable -> normal trading
}

function run()
{
	set(NFA+LOGFILE);
	StartDate = 2002;
	vars Price = series(price());
	vars Trend = series(LowPass(Price,1000));
	vars Signals = series(0);
	
	Stop = 4*ATR(100);
	checkEquity();

	// reinvest a portion of profits
	var MarginLong = sqrt(WinLong-LossLong);
	var MarginShort = sqrt(WinShort-LossShort);
	Margin = clamp((MarginLong+MarginShort)/2, 5, 1000);

	if(valley(Trend)) {
		Signals[0] = 1;
		//exitShort();
		if(Sum(Signals+1,3) == 0)  // no signals in the previous 3 bars?
			reverseLong(1);
	} else if(peak(Trend)) {
		Signals[0] = 1;
		//exitLong();
		if(Sum(Signals+1,3) == 0)
			reverseShort(1);
	}
	

	PlotWidth = 800;
	PlotHeight1 = 320;
	plot("Margin",Margin,1,RED);
	plot("Lots",Lots,1,BLUE);
	plot("Equity",Equity,1,GREEN);
//	plot("Trend",Trend[0],0,RED);
//	plotTradeProfile(50);
}


Attached Files dt-phantom_EURUSD.png
Re: Need some help with equity-curve trading [Re: dusktrader] #427613
08/10/13 17:35
08/10/13 17:35
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
Ok, I have a philosophy question now. I've been playing around with the equity-curve trading, and it's working well IMO. I really like this concept and how easy it is to implement with Zorro.

My question is... should the crossover of the 2 LowPass-filtered Equity curves be tighter or wider, generally speaking?

For example, when studying the result charts, I played around with the optimizer, forcing the fast/slow curves to be spread apart, or closer. Regardless of what optimizer thinks is best from a profit angle... I would like to know if these lines should be tight/wide from the philosophy angle.

What I mean is... normally when I think of crossovers, I'm thinking in terms of entry/exit signals. And this is not necessarily the same purpose I think. I believe that I would want the curves to be somewhat far apart, to further filter noise. When they are tighter, I am seeing a lot of flipping between Lots=1 and Lots=-1. While that does still work, I wonder if it makes good sense?

Generally speaking, I want the trades to have some flexibility to stretch and develop, before Zorro would punish the strategy by putting it in phantom mode. Therefore, it seems I would want a wider set of curves.

Could someone who uses equity-curve trading comment on their findings or philosophy with this please? I am getting nice results with optimizer regardless of how I force the width of the curves.

THANKS... and long live Zorro!

Re: Need some help with equity-curve trading [Re: dusktrader] #427618
08/10/13 20:29
08/10/13 20:29

A
acidburn
Unregistered
acidburn
Unregistered
A



I'm far from being an expert on the topic, actually I have very recently learned what equity curve trading is, and your script is first hands on experience for me. So, a bloody amateur speaking...

From what I learned past hour or two playing with your idea, the equity curve seems to be just another curve, not unlike the price curve. So whatever problems we have while taming the price curve, they're still there while trying to cope with the equity curve. In a way, nothing's new, all the same problems. What period to use, how to avoid overfit, what stop loss to use, yadda, yadda...

So, sorry my friend, no good answer to your questions.

But, I have something else, I have a very simple strategy that exhibits a very interesting equity curve. Test it, and you will see. This is where I spent an hour or two in vain, trying to trade the equity curve, and that's how I learned the above facts. I think if equity curve is worth it, it should be able to convert this simple strategy into a winning one. Wildly winning one (at least on that time period, which is chosen for obvious reasons). Unless it can be applied to this simplest of all examples, I just won't be convinced there's money in it. Yeah, consider this a bait. wink

Code:
function run()
{
	set(PLOTNOW|PLOTPRICE);
	StartDate = 20040610;
	EndDate = 20060801;
	BarPeriod = 240;
	asset("EUR/USD");

	vars Price = series(price());
	vars LRS = series(LinearRegSlope(Price, 9));

	if (rising(LRS)) {
		if (!NumOpenLong)
			enterLong();
	} else if (falling(LRS)) {
		if (!NumOpenShort)
			enterShort();
	}
}


Re: Need some help with equity-curve trading [Re: ] #427622
08/10/13 21:17
08/10/13 21:17

A
acidburn
Unregistered
acidburn
Unregistered
A



In case it's not immediately obvious what I wanted to do, here's a slight modification of the strategy from my last post. I start trading it in mean reversion mode, and roughly in the middle of the testing period I switch it to trend following mode. Voila, our simple strategy is now a respectable 191% annual profit, 1.69 Sharpe. Of course, it would be great if we could automate that switch by peeking at the equity curve. So far, I failed... frown

This example is also interesting because it shows how fast and quickly the market turns our winning strategy into a losing one, and actually requires a complete 180° turn if we're to continue winning. Of course, I stumbled upon it accidentally, but I still like it. cool

Code:
function run()
{
	set(PLOTNOW|PLOTPRICE);
	StartDate = 20040610;
	EndDate = 20060801;
	BarPeriod = 240;
	asset("EUR/USD");

	static int dir, reversed;

	if (is(INITRUN)) {
		dir = -1; // start in mean-reversion mode
		reversed = 0;
	}

	vars Price = series(price());
	vars LRS = series(LinearRegSlope(Price, 9));

	if (!reversed && Bar > NumBars / 2) {
		dir = -1 * dir; // switch to trend-following mode
		reversed = 1;
	}

	if (dir == 1 && rising(LRS) || dir == -1 && falling(LRS)) {
		if (!NumOpenLong)
			enterLong();
	} else if (dir == 1 && falling(LRS) || dir == -1 && rising(LRS)) {
		if (!NumOpenShort)
			enterShort();
	}
}


Re: Need some help with equity-curve trading [Re: ] #427628
08/10/13 23:42
08/10/13 23:42
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
Hey Acid, nice to see someone else poking at this concept! I don't have time at the moment to play with your script (although I like your strategy idea here and will definitely play around with it). My sole intention right now is to figure out the equity-curve trading. As such, I just needed a very basic script that was "marginally profitable" to work with. Yours could definitely work too if you are seeing a positive return without applying the phantom trades.

Phantom trades are something I already do in my manual trading. Basically I follow 27 pairs and apply the same strategy rules on all of them. However, by experience I've learned that not all pairs work as well. Since I don't know which ones are good/bad, I keep a spreadsheet of past trade history. I have a scoring system that I came up with that is basically the same as phantom trading: if the ratio of wins:losses is 0.6 or better, than I flip the switch to full-weight trading. If it falls below 0.6 (or never achieves that level), then I flip the switch to flat lot 0.01 trading (essentially pennies only). This enables me to build a realtime track record of each pair and identify those that are working. It also enables me to switch (with some degree of lag) between pairs that fall out of favor and those that emerge as new champions.

Overall, I am essentially using a rudimentary method of manual equity-curve trading.

In your scripts above, I am not seeing anywhere that you set Lots=1 and Lots=-1. This is necessary to flip between phantom and normal mode.

The scripts I've posted above may have had some issues so I'll post some code below that I'm working on so far:

This is the magic code that filters your equity through the LowPass filters and looks for crossovers. You need to apply this code before you set margin, and before entering the market.
Code:
function checkEquity()
{
	vars EquityCurve = series(Equity);
	
	var fast=optimize(10,1,25,1);
	var slow=optimize(100,100,300,1);
	vars EquityLP = series(LowPass(EquityCurve,fast));
	if(EquityLP[0] < LowPass(EquityLP,slow) && falling(EquityLP))
		Lots = -1; // drawdown -> phantom trading
	else
		Lots = 1; // profitable -> normal trading

	plot("EquityLPslow",LowPass(EquityLP,slow),1,BLUE);
	plot("EquityLPfast",LowPass(EquityLP,fast),0,GREEN);
}



After calling the above code, you can set:
Margin = (reinvest code here)
If (long entry condition) reverseLong();
else if (short entry condition) reverseShort();

and then for study purposes this helps:
Code:
PlotWidth = 6000;
PlotHeight1 = 1000;
plot("Margin",Margin,1,RED);
plot("Lots",Lots,1,BLUE);



* Note I love the reverseShort/reverseLong helper functions that were introduced in v1.14 ... these are superior to enterLong/enterShort IMO. Even if you only use them for 1 trade at a time, at the very least they move your stop up with every new signal in the same direction... that alone makes them smarter.

Re: Need some help with equity-curve trading [Re: dusktrader] #427629
08/10/13 23:55
08/10/13 23:55
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
Ok check this out... I really don't have time to poke at it right now, but just popping in that code above: wink
Code:
function checkEquity()
{
	vars EquityCurve = series(Equity);
	
	var fast=optimize(10,1,25,1);
	var slow=optimize(100,100,300,1);
	vars EquityLP = series(LowPass(EquityCurve,fast));
	if(EquityLP[0] < LowPass(EquityLP,slow) && falling(EquityLP))
		Lots = -1; // drawdown -> phantom trading
	else
		Lots = 1; // profitable -> normal trading

	plot("EquityLPslow",LowPass(EquityLP,slow),1,BLUE);
	plot("EquityLPfast",LowPass(EquityLP,fast),0,GREEN);
}

function run()
{
	set(PARAMETERS|PLOTNOW|PLOTPRICE);
	StartDate = 20040610;
	EndDate = 20060801;
	BarPeriod = 240;
	asset("EURUSD");

	static int dir, reversed;

	if (is(INITRUN)) {
		dir = -1; // start in mean-reversion mode
		reversed = 0;
	}

	vars Price = series(price());
	vars LRS = series(LinearRegSlope(Price, 9));

	checkEquity(); //set normal or phantom trade mode
	
	// reinvest a portion of profits
	var MarginLong = sqrt(WinLong-LossLong);
	var MarginShort = sqrt(WinShort-LossShort);
	Margin = clamp((MarginLong+MarginShort)/2, .5, 1000);

	if (!reversed && Bar > NumBars / 2) {
		dir = -1 * dir; // switch to trend-following mode
		reversed = 1;
	}

	if (dir == 1 && rising(LRS) || dir == -1 && falling(LRS)) {
		if (!NumOpenLong)
			reverseLong(1);
	} else if (dir == 1 && falling(LRS) || dir == -1 && rising(LRS)) {
		if (!NumOpenShort)
			reverseShort(1);
	}

	PlotWidth = 6000;
	PlotHeight1 = 1000;
	plot("Margin",Margin,1,RED);
	plot("Lots",Lots,1,BLUE);
}


Re: Need some help with equity-curve trading [Re: dusktrader] #427645
08/11/13 12:58
08/11/13 12:58

A
acidburn
Unregistered
acidburn
Unregistered
A



Hm, something like that might actually work. I'm still having trouble to wrap my mind on some details of the phantom trading. When you set Lots to -1, then the strategy goes phantom, but still adds wins and losses to Equity, right? Otherwise I don't see how the equity curve would recover, and get you out of the phantom mode? I might need to (re-)read that parts from the manual, to understand better what's goin' on. reverse* functions are also new to me.

One thing, if you're to apply your logic to my example, you need to remove this block:

Code:
if (!reversed && Bar > NumBars / 2) {
		dir = -1 * dir; // switch to trend-following mode
		reversed = 1;
	}



That's where I manually flipped the strategy to make it profitable, just to show what my final goal with the example is.

What I'm currently investigating is how to tune your checkEquity function to do that for me, automatically. With some very large values for fast & slow (900/1200) I've managed to make it at least stop trading when it detects that strategy started losing, and keep accumulated profits. That's promising! But, thanks to very long periods, it's also rather late in reaction, so some profits are given back and that is the main problem. I'll see if it can be tuned and made to react faster, and also to do the direction change instead of tweaking Margin. Who'd say, so short scripts, and so many details behind. Zorro encourages very condensed code, and that's good, IMHO.

Maybe I should've opened my own topic to discuss this, sorry for hijacking yours. While we both would like to solve some things by peeking into equity curve, we still intend to accomplish quite different things. Your intention is to stop trading for real and switch to phantom mode, and I'd like my strategy to make 180° turns, whenever the equity curve suggests that the strategy has stopped being profitable. So, that it is completely auto-adaptive to market conditions. This is academic in nature atm, but such logic could be very useful for more complex strategies.

But, I'm still not very fond of phantom trading, I admit. tongue

Page 1 of 2 1 2

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1