Gamestudio Links
Zorro Links
Newest Posts
Placing Order during Market Close
by jcl. 07/12/24 13:57
New Zorro version 2.62
by jcl. 07/12/24 13:56
Delaying Exit Order
by vicknick. 07/12/24 07:14
Zorro Trader GPT
by TipmyPip. 07/11/24 05:43
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 07/10/24 15:06
SGT_FW
by Aku_Aku. 07/10/24 10:34
Gravity
by rayp. 07/09/24 23:07
Z strategies with MT5 wit own EA’s
by Grant. 07/07/24 15:37
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, NoBrainer, 1 invisible), 689 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rramsey, Erasand, Cristoig2001, Mino, squik
19064 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Higher timeframe determining direction for lower timeframe #487549
06/04/23 00:45
06/04/23 00:45
Joined: Nov 2022
Posts: 15
Tampa Bay
B
bigsmack Offline OP
Newbie
bigsmack  Offline OP
Newbie
B

Joined: Nov 2022
Posts: 15
Tampa Bay
Hi,
Does anybody have basic code pattern they are willing to share for a higher timeframe determining direction for lower timeframe?

I have the Black Book script examples for trading with different assets, algos, and timeframes but I'm trying to figure out the easiest way to keep track of higher timeframe dummy trades to only take trades on lower timeframe in that direction.

I've spent many days on this and just think I'm out of my league.

I thought about having multiple Zorro instances running with a master script where the higher timeframe systems share positions with the lower timeframe systems.

That may be way overengineering and stretch my abilities a bit but it seems like it would be the most flexible way to pull this off.

Maybe my whole approach is just not based in reality. Maybe I need a for(opentrades) loop inside the higher time frame function.

I tried to call the lower time frame function from inside the higher timeframe function but that caused issues with optimizing the htf parameters.

It seems like using the Phantom trades might be a good way to do this. Am I way off base here?

It seems like the LotsPhantom is always 0 with this code and I'm not sure if you can just turn the Phantom flag on and off like this.

I'd appreciate any insights into this.

Code
 
function tradeCorrelationHTF()
{
        // PHANTOM set on always before calling this function and turned off after
	TimeFrame = frameSync(4);   // for 60 minute timeframe
        // Trade logic
        enterLong();
}

function tradeCorrelation()
{
	TimeFrame = 1;
        // Trade logic
	if(LotsPhantom > 0)
		enterLong();
}

function run()
{
	set(PARAMETERS+FACTORS+LOGFILE+PLOTNOW);
	BarPeriod = 15;
	while(asset(loop("EUR/USD","USD/JPY")))
	{
		while(algo(loop("CO","TC")))
		{
			if(Algo = "TC")
				tradeCorrelation();
			else if(Algo = "CO"){
                                // TrainMode PHANTOM Exclude phantom trades. Otherwise phantom trades are treated as normal trades in the training process. 
                                // Guessing that I should not set this - I should not exclude phantom trades from training because they 
				// setf(TrainMode,PHANTOM);

                                // TradeMode TR_PHANTOM Enter trades in phantom mode. The trades are simulated and not sent to the broker API. 
				setf(TradeMode,TR_PHANTOM);

				tradeCorrelationHTF();

                                // Turn Phantom off for lower timeframes to actually trade
				resf(TrainMode,PHANTOM);
				resf(TradeMode,TR_PHANTOM);
		}
	}
}


Thanks!

Re: Higher timeframe determining direction for lower timeframe [Re: bigsmack] #487568
06/07/23 00:12
06/07/23 00:12
Joined: Nov 2022
Posts: 15
Tampa Bay
B
bigsmack Offline OP
Newbie
bigsmack  Offline OP
Newbie
B

Joined: Nov 2022
Posts: 15
Tampa Bay
It seems like I figured out a good solution to this so I'm going to put it up to see how it rolls.

I think I was on the right track with my initial code where the HighTimeFrame(HTF) calls the LowTimeFrame(LTF) function

They are essentially the same function with different TimeFrame and code to interact with run loop and each other.

// Both timeframes should be trained and have a par file written during trainmode
// They are trained in the same run loop and then in TRADEMODE
// The HTF calls the LTF trading function with the current HTF regime 1:L or -1:S
// 0 If no HTF position

Remarks:
Making the HTF trade function trade phantom is optional and can be commented out or use #ifdef or slider
I only did one test run but the total stats only showed the LTF stats just like the manual says when HTF is phantom mode
I hope this actually works and that I am not seeing things after going after it for a few days

Code

function tradeCorrelationLTF(int posvar)
{
	// posvar is position run or HTF 
	// run loop calls with 0 for TRAINMODE
	// HTF calls with 0, 1, -1 for TRADEMODE
	
	// Always trade LowerTimeFrame(LTF) so reset Phantom to off
	resf(TradeMode,TR_PHANTOM);
	
	// Algo is set/called by loop in TRAINMODE but for TRADEMODE
	// this LTF function is called by HTF so algo is not set so
	// setting it here for not TRAINMODE
	if(!(is(TRAINMODE)))
		algo("COLTF");
		
	TimeFrame = 1;
	
	// Same Trade logic as HTF
	
	// Entry Condition posvar >= 0 long or posvar <=0 for short
	if(posvar >= 0){
		if(longcondition){
				enterLong();
		}
	}
	
	if(posvar <= 0){
		if(shortcondition){
				enterShort();
		}
	}
}

function tradeCorrelationHTF()
{
	TimeFrame = frameSync(4);   // for 60 minute timeframe
	
	// Same Trade logic as LTF
	
	// Turn on Phantom for HTF TRADEMODE
	// This is optional - may help for day-trading
	// in the HTF direction -- comment out if 
	// you want to trade both HTF and LTF timeframes
	
	setf(TradeMode,TR_PHANTOM);

	// Entry Logic
	// enterLong(); 
	// or
	// enterShort();
	
	// Both timeframes should be trained and have a par file written during trainmode
	// They are trained in the same run loop but when not being trained
	// Then the HTF calls the LTF trading function with the current HTF regime 1:L or -1:S
	// 0 If no HTF position
	if(!is(TRAINMODE)){
		if(TradeIsLong)
			tradeCorrelationLTF(1);
		else if(TradeIsShort)
			tradeCorrelationLTF(-1);	
		else tradeCorrelationLTF(0);	
	}	
}

function run()
{
	set(PARAMETERS+FACTORS+LOGFILE+PLOTNOW);
	BarPeriod = 15;
	while(asset(loop("EUR/USD","USD/JPY")))
	{
		while(algo(loop("COHTF","COLTF")))
		{
			if(Algo == "COHTF"){
				tradeCorrelationHTF();
			}
			else if(Algo == "COLTF"){
				if(is(TRAINMODE))
					tradeCorrelationLTF(0);	// 0 = no trade position for TRAINMODE
			}
		}
	}
}



Last edited by bigsmack; 06/07/23 15:44.

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