Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 06/30/24 02:01
Lapsa's very own thread
by Lapsa. 06/26/24 12:45
Executing Trades on Next Bar Open
by Zheka. 06/20/24 14:26
A simple game ...
by VoroneTZ. 06/18/24 10:50
Face player all the time ...
by bbn1982. 06/18/24 10:25
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (bigsmack, TipmyPip, 1 invisible), 1,205 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mino, squik, AemStones, LucasJoshua, Baklazhan
19061 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: 15
Tampa Bay
B
bigsmack Online OP
Newbie
bigsmack  Online OP
Newbie
B

Joined: Nov 2022
Posts: 15
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; Yesterday at 12:45. Reason: New example added
Re: Need help to remove days open gap from subsequent prices for day [Re: bigsmack] #488286
12 hours ago
12 hours ago
Joined: Nov 2022
Posts: 15
Tampa Bay
B
bigsmack Online OP
Newbie
bigsmack  Online OP
Newbie
B

Joined: Nov 2022
Posts: 15
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
2 hours ago
2 hours ago
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 | 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