Gamestudio Links
Zorro Links
Newest Posts
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
Release 2.68 replacement of the .par format
by Martin_HH. 09/23/25 20:48
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
1 registered members (dBc), 17,435 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Need help to remove days open gap from subsequent prices for day #488285
06/30/24 14:58
06/30/24 14:58
Joined: Nov 2022
Posts: 20
Tampa Bay
B
bigsmack Offline OP
Newbie
bigsmack  Offline OP
Newbie
B

Joined: Nov 2022
Posts: 20
Tampa Bay
I've been pondering this for a great while and sometimes I just don't get it.

I want to calculate the price gap for the first bar of the day and then remove each day's opening gap from the subsequent prices for day.

I have this inside the asset loop and it looks like it calculates the Degap[0] - Gap correctly for the first time after Gap is set at the first bar but then Gap is reset to 0 for the subsequent bars even though it is set to 0 outside the asset loop.

Maybe this is the wrong approach. Any help would be greatly appreciated - thanks!

Code
	var Gap = 0;

	while(asset(loop("MES","MNQ","MYM","M2K")))	
	{
		if(at(1330 + BarPeriod)){
			Gap = priceO() - priceC(1);
			watch("!StartAvgGap",Gap);
		} 
		var* Degap = series(priceC());
		watch("!AvgGap",Degap[0],Degap[0] - Gap);
        }


Here's a different way I've tried but still don't get it...
Code
// Alice3a: Portfolio trading ///////////////////

function tradeCounterTrend()
{

	TimeFrame = frameSync(4);
	vars Prices = series(price());
	vars Cycles = series(BandPass(Prices,30,2));
	vars Signals = series(FisherN(Cycles,500));
	var Threshold = optimize(1,0.8,1.2,0.1);

	LifeTime = 4*optimize(100,50,150,10);
	Trail = Stop = optimize(10,4,20,2)*ATR(100);
	MaxLong = MaxShort = -1;
	
	var Regime = FractalDimension(Prices,100);
	var RegimeThreshold = optimize(1.5,1.3,1.7,0.1);

	if(okToTrade == true){
		if(Regime > RegimeThreshold)
		{
			if(crossUnder(Signals,-Threshold))
				enterLong(); 
			else if(crossOver(Signals,Threshold))
				enterShort();
		} 
	}
}

function tradeTrend()
{
	TimeFrame = 1;
	vars Prices = series(price());
	vars Trends = series(Laguerre(Prices,optimize(0.05,0.02,0.15,0.01)));
	vars EMA20 = series(EMA(Prices,20));

	Stop = optimize(10,4,20,2)*ATR(100);
	Trail = 0;
	LifeTime = 0;
	MaxLong = MaxShort = -1;
	
	var MMI_Period = optimize(300,100,400,100);
	vars MMI_Raws = series(MMI(Prices,MMI_Period));
	vars MMI_Avgs = series(SMA(MMI_Raws,MMI_Period));
	if(okToTrade == true){
		if(falling(MMI_Avgs)) {
			if(valley(Trends))
				enterLong();
			else if(peak(Trends))
				enterShort();
		}		
	}

}
function run()
{
	if(is(INITRUN)) {
		bool okToTrade = false;
		watch("!okToTradeINITRUN:",okToTrade);
	}
	

	set(PARAMETERS+LOGFILE+TESTNOW+PLOTNOW);
	StartDate = 20240501; // further back due to WFO
	//EndDate = 2018;   // fixed simulation period
	BarPeriod = 5;	// 1 hour bars
	LookBack = 5*400;	// needed for FisherN()
	if(Train) Detrend = TRADES;
	StartMarket = 1330;
	EndMarket = 2001;
	BarMode = BR_MARKET;
	// close all position at 16:15 local time

	if(at(2000)) {
	  printf("\nClosin g all positions");
	  exitLong("**");
	  exitShort("**");
	}
	NumWFOCycles = 10; // activate WFO
	NumCores = -1;		// multicore training (Zorro S only)
	ReTrainDays = 147;
	if(ReTrain) {
		UpdateDays = -1;	// update price data from the server 
		SelectWFO = -1;	// select the last cycle for re-optimization
	}
	if(at(1330 + BarPeriod)){
		//THIS SAYS UNDECLARED IDENTIFIER EVEN THOUGH IT IS DECLARED IN INIT ABOVE
		okToTrade = false;
		watch("!okToTradeFIRSTBAR:",okToTrade);
	}
// portfolio loop
	while(asset(loop("MES","MYM"))){
		vars Prices = series(price());
		
		//ONCE THIS IS EMA TOUCHED FOR THE DAY
		//I WANT TO RETAIN THIS FLAG VALUE AND ONLY TRADE 
		//AFTER IT IS SET TO TRUE
		//THEN RESET IT AT ONLY AT FIRST BAR OF DAY ABOVE
		vars EMA20 = series(EMA(Prices,20));
		if(touch(Prices,EMA20))
			okToTrade = true;	//UNDECLARED ERROR
	
		while(algo(loop("TRND","CNTR")))
		watch("!okToTradeALGO:",okToTrade);
		
		// WANT TO TRADE ONLY AFTER PRICE TOUCHES EMA(20)
		//if(okToTrade == true)
		{
			if(Algo == "TRND") 
				tradeTrend(okToTrade);
			else if(Algo == "CNTR") 
				tradeCounterTrend(okToTrade);			

		}	
	}
}


Last edited by bigsmack; 07/01/24 12:45. Reason: New example added
Re: Need help to remove days open gap from subsequent prices for day [Re: bigsmack] #488286
07/02/24 00:39
07/02/24 00:39
Joined: Nov 2022
Posts: 20
Tampa Bay
B
bigsmack Offline OP
Newbie
bigsmack  Offline OP
Newbie
B

Joined: Nov 2022
Posts: 20
Tampa Bay
It looks like I may have figured out a way to use the EMA approach by setting Lots variable. I haven't tested it completely but it seems to work.

I still need to figure out my initial intention to remove the opening gap from each subsequent bar if I am able.
Code
	if(at(1330)){
		Lots = 0;
		//watch("okToTradeFIRSTBAR:",Lots);
	}
// portfolio loop
	while(asset(loop("MES","MYM","MNQ","M2K"))){
		vars Prices = series(price());
		vars EMA20 = series(EMA(Prices,20));
		if(touch(Prices,EMA20)){
			Lots = 1;
			//watch("okToTradeALGO:",Lots);
		}

Re: Need help to remove days open gap from subsequent prices for day [Re: bigsmack] #488287
07/02/24 10:32
07/02/24 10:32
Joined: Jul 2017
Posts: 787
Z
Zheka Offline
User
Zheka  Offline
User
Z

Joined: Jul 2017
Posts: 787
run() is a just a regular function, that gets' called at each bar (by a Zorro's engine).

So, all variables declared in run are 'local', i.e. they get destroyed/ reinitialized at each new run ().

To retain their values, you need to declare them static or global (i.e. outside run() )..or save them in a series object.


Moderated by  Petra 

Gamestudio download | 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