Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (7th_zorro, Quad, VoroneTZ, 1 invisible), 623 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, 11honza11, ccorrea, sakolin, rajesh7827
19046 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
First Attempt - Chaining Indicators together #480633
06/22/20 04:17
06/22/20 04:17
Joined: Jun 2020
Posts: 5
P
pchen90 Offline OP
Newbie
pchen90  Offline OP
Newbie
P

Joined: Jun 2020
Posts: 5
I'm attempting to piece together the concept in the following blog post together in Zorro as my first attempt at a script. The idea is to use percent rank of historical volatility as an oscillator describing regimes that are either favourable to mean-reverting or trend-following strategies. Similar to the MMI example on the financial hacker blog, or strategies based on the Hurst exponent.

https://quantumfinancier.wordpress.com/2010/08/27/regime-switching-system-using-volatility-forecast/

The indicator is this:

percentrank(MA(percentrank(Stdev(close,21),252),21),250)

I'm basically just trying to get the indicator value to spit out the right value at this point. The choice of trendfollowing/MR strategies are just copied from existing strategies in the manual for now.

Code
// Regime switching ///////////////////
#include <profile.c>
#define USE_PREDICT

function run()
{
	//StartDate = 20150101;
	EndDate = 20171231; // fixed simulation period 
	Verbose = 2;
	BarPeriod = 1440;

	LookBack = 300;	// needed for MMI
	asset("SPY");
	set(LOGFILE,PLOTNOW); // log all trades

	vars Prices = series(priceClose());
	
	// get the trend series  	
	vars Osc = series(StochEhlers(series(price()),10,20,10));
	
	// get the RSI series  
   vars RSI12 = series(RSI(Prices,12));
		
	// Regime decision ////////////////////
	// Calc indicator	- percentrank(MA(percentrank(Stdev(close,21),252),21),250)
	vars std = series(StdDev(Prices,20));	
	vars pcr_std = series(PercentRank(std, 252,std[0])); // returns a value
	vars sma_pcr = series(SMA(pcr_std,21));
	vars pcr_sma = series(PercentRank(sma_pcr, 252,sma_pcr[0]));		

	
	// Trend following - Ehler's 
	if(pcr_sma[0] < 50) 
	{
		#ifndef USE_PREDICT	
		if(crossOver(Osc,0.8)) 
			enterShort();
		if(crossUnder(Osc,0.2))
			enterLong();
		#else		
		if(predict(CROSSOVER,series(Osc[0]-0.8),10,0.01) > -5) 
			enterShort();
		if(predict(CROSSOVER,series(0.2-Osc[0]),10,0.01) > -5) 
			enterLong();
	}	



	// MR - RSI 2
	else
	{				 
		// set up stop / profit levels
		  Stop = 200*PIP;
		  TakeProfit = 200*PIP;
		 
		// if rsi crosses over buy level, exit short and enter long
		  if(crossOver(RSI12,75))
			 enterLong();
		// if rsi crosses below sell level, exit long and enter short
		  if(crossUnder(RSI12,25))
			 enterShort();
	
	}
	// Plotting

	//plotTradeProfile(-50); 
	
	plot("StochEhlers",Osc,NEW,RED);
	plot("Threshold1",.2,0,BLACK);
	plot("Threshold2",.8,0,BLACK);
	
	
	
	
	
	
}




It's been a while since I've written anything in C so apologies in advance if there's an obvious solution I'm missing! Thanks in advance...

Re: First Attempt - Chaining Indicators together [Re: pchen90] #480634
06/22/20 05:29
06/22/20 05:29
Joined: May 2015
Posts: 390
Czech Republic
G
Grat Offline
Senior Member
Grat  Offline
Senior Member
G

Joined: May 2015
Posts: 390
Czech Republic
missing #endif

// Trend following - Ehler's
if(pcr_sma[0] < 50)
{
#ifndef USE_PREDICT
if(crossOver(Osc,0.8))
enterShort();
if(crossUnder(Osc,0.2))
enterLong();
#else
if(predict(CROSSOVER,series(Osc[0]-0.8),10,0.01) > -5)
enterShort();
if(predict(CROSSOVER,series(0.2-Osc[0]),10,0.01) > -5)
enterLong();
#endif

Re: First Attempt - Chaining Indicators together [Re: Grat] #480637
06/22/20 11:39
06/22/20 11:39
Joined: Jun 2020
Posts: 5
P
pchen90 Offline OP
Newbie
pchen90  Offline OP
Newbie
P

Joined: Jun 2020
Posts: 5
Thank you for chiming in Grat...unfortunately still getting error 041 (wrong series usage) and error 047 (no bars tot plot) as a result.

I read the manual which pointed me to look at the bars in question (console outputs between the errors "Bar 15: 14 - bar 16: 16") but I have no idea what this means...is there an option I missed for more expansive log details? Or is this the kind of thing developing and debugging in VS is suited for?

Re: First Attempt - Chaining Indicators together [Re: pchen90] #480639
06/22/20 12:54
06/22/20 12:54
Joined: May 2015
Posts: 390
Czech Republic
G
Grat Offline
Senior Member
Grat  Offline
Senior Member
G

Joined: May 2015
Posts: 390
Czech Republic
try this:
Code
    vars c1 = series(Osc[0]-0.8);
	vars c2 = series(0.2-Osc[0]);
	// Trend following - Ehler's 
	if(pcr_sma[0] < 50) 
	{
		#ifndef USE_PREDICT	
		if(crossOver(Osc,0.8)) 
			enterShort();
		if(crossUnder(Osc,0.2))
			enterLong();
		#else		
		if(predict(CROSSOVER,c1,10,0.01) > -5) 
			enterShort();
		if(predict(CROSSOVER,c2,10,0.01) > -5) 
			enterLong();
        #endif
	}	


Re: First Attempt - Chaining Indicators together [Re: Grat] #480640
06/22/20 13:00
06/22/20 13:00
Joined: Jun 2020
Posts: 5
P
pchen90 Offline OP
Newbie
pchen90  Offline OP
Newbie
P

Joined: Jun 2020
Posts: 5
Thanks! Can see now that I was using a series inside the if statement which was throwing it off....

Re: First Attempt - Chaining Indicators together [Re: pchen90] #485829
04/27/22 07:19
04/27/22 07:19
Joined: Apr 2022
Posts: 21
H
HamzaAhmed Offline
Newbie
HamzaAhmed  Offline
Newbie
H

Joined: Apr 2022
Posts: 21
Set Verbose = 7; to see greater log details.


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1