For fun: why did Zorro not trade in some places?

Posted By: dusktrader

For fun: why did Zorro not trade in some places? - 08/01/13 13:54

I'm playing with some new features I got from the updated Workshop 5 tutorial (which will presumably appear in an upcoming Zorro version)...

Listed below are the small modifications I made to Workshop5_3 to play with this. But what I'm interested in at the moment is the result chart... look at these 2 charts. One implements the new function which limits max trades to 1. The other has no limits.

Look specifically at the timeframe of 2009. I would like to know why Zorro takes trades when there is no limit, but when trades are limited via the function it does not. Seems like it should at least take 1 trade during this period.

I'm just trying to understand the "why" about this. Thanks in advance if you can shed any light...

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)
    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)
    enterLong();
// otherwise, close all opposite trades
  else if(!mode(HEDGING))
    exitShort();
  return 0;
}

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

	if(ReTrain) {
		UpdateDays = -1;	// update price data from the server 
		SelectWFO = -1;	// select the last cycle for re-optimization
	}
	
// 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);

// buy and sell
	Stop = optimize(4,2,8) * ATR(100);
	Trail = 4*ATR(100);
	if(crossUnder(Signal,-Threshold))
		reverseLong(1); //max 1 trade, update stops
		//enterLong();
	else if(crossOver(Signal,Threshold))
		reverseShort(1); //max 1 trade, update stops
		//enterShort();

	PlotWidth = 800;
	PlotHeight1 = 300;
}



Attached picture EURUSD_max1trade.png
Attached picture EURUSD_nolimit.png
Posted By: jcl

Re: For fun: why did Zorro not trade in some places? - 08/01/13 14:51

It does not omit trades with my workshop 5 script. But I believe your reverseShort function has a mistake, here is mine:

Code:
TRADE* reverseShort(int MaxTrades)
{
	if(Stop > 0) 
		exitShort(0,priceClose()+Stop); 
	if(TakeProfit > 0) 
		exitShort(0,-(priceClose()-TakeProfit));
	if(NumOpenShort < MaxTrades)
		return enterShort();
	else if(!mode(HEDGING))
		exitLong();
	return 0;
}

Posted By: swingtraderkk

Re: For fun: why did Zorro not trade in some places? - 08/01/13 15:07

Jcl,

Apolgies,

I'm not a C programmer, can you explain what TRADE* does please?
Posted By: jcl

Re: For fun: why did Zorro not trade in some places? - 08/02/13 06:27

It is a pointer to the trade struct, or 0 when the trade could not be opened.

Pointers and structs are not explained in the tutorial because you won't need them in simple strategies. They are used in more complex scripts or strategies. You can find at the end of workshop 3 some links to C courses where pointers and structs are explained.
Posted By: swingtraderkk

Re: For fun: why did Zorro not trade in some places? - 08/02/13 08:15

OK,

So I think I now understand pointers and what a struct is.

The syntax is still confusing me:

Are you telling the function reverseShort() that when passed an integer called max trades it is to execute whats inside the {} and then return a pointer to a trade struct?

Why is this necessary or superior to simply declaring the function with function?
Posted By: jcl

Re: For fun: why did Zorro not trade in some places? - 08/02/13 08:26

In the example above it's not necessary.

In the general case, it's useful for finding if the trade was opened or not, or for storing the trade pointer for other purposes such as maintaining a trade statistics list or something similar.
Posted By: dusktrader

Re: For fun: why did Zorro not trade in some places? - 08/02/13 10:17

Hi jcl, my reverseShort function did have an error, thanks for finding that!

But I'm still getting the same result, even when I copy/paste your exact code from above. (ie, the missing trades section mentioned above)

However please note I have changed:
Code:
TRADE* reverseShort(int MaxTrades)


to simply
Code:
function reverseShort(int MaxTrades)



I am running v1.12.1. The TRADE* notation doesn't seem to work as written in this version?
Posted By: dusktrader

Re: For fun: why did Zorro not trade in some places? - 08/02/13 15:26

Aha! I think I figured this out. The reason that the chart trades look different when changing the strategy from using enterLong() to reverseLong() is because I've been re-training it between those changes.

When re-training, apparently it gets different parameters that cause it to actually trade differently.

This light-bulb went off in my head when I accidentally forgot to re-train after flipping the strategy. I just clicked Test and then Result... and doing this (ie, using same parameter set) shows trading throughout (or not) consistently during that 2009 period.

Now I feel accomplished LOL. I can say the week hasn't been a waste for my ongoing Zorro knowledge...
© 2024 lite-C Forums