Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (TipmyPip, AndrewAMD), 1,151 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
OptimalF(Long/Short) and Algo ids #479605
04/10/20 04:35
04/10/20 04:35
Joined: Mar 2020
Posts: 41
J
jbhunter Offline OP
Newbie
jbhunter  Offline OP
Newbie
J

Joined: Mar 2020
Posts: 41
I am trying to get a handle on how the OptF factors are used. I am running into issues getting them to work inside of a function called within asset/algo loops. I am beginning to believe that the OptF is out of scope when you are in a called function. However the asset/algo is still known.

What am I missing here? Should the OptF factors always be called within the scope of run()? It appears the printf statement in run() is picking them up when Bar==1.

I have looked the documentation here but this is just not making sense to me.
https://manual.zorro-project.com/optimalf.htm


Code
 EUR/USD:TRND:L   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
 EUR/USD:TRND:S   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
V 2.257 on Thu 20-04-09 23:17:26
Test: testOptF EUR/USD 2010
Assets AssetsFix
Read testOptF.fac

 EUR/USD:TRND OptF=1.000 OptFLong=2.000 OptFShort=3.000
 EUR/USD:TRND:L   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
 EUR/USD:TRND:S   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
 EUR/USD:TRND:L   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
 EUR/USD:TRND:S   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
 EUR/USD:TRND:L   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
 EUR/USD:TRND:S   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000


Code
testOptF.fac
EUR/USD:TRND        1.000  0.52    3/7     100.0
EUR/USD:TRND:L      2.000  0.00    0/5       2.6
EUR/USD:TRND:S      3.000  0.52    3/2      97.4




Code
/// testOptF.c

function tradeTrend(int sl)
{
	if(sl==1) //goLong
	{
		Margin = 0.5 * OptimalFLong * Capital * sqrt(1 + ProfitClosed/Capital);
		printf("\n %s:%s:L   Lots=%i   OptimalF=%.3f OptimalFShort=%.3f OptimalFLong=%.3f",Asset,Algo,Lots, OptimalF,OptimalFShort,OptimalFLong);
		enterLong();
	}
	else if(sl==0) //goShort
	{
		Margin = 0.5 * OptimalFShort * Capital * sqrt(1 + ProfitClosed/Capital);
		printf("\n %s:%s:S   Lots=%i   OptimalF=%.3f OptimalFShort=%.3f OptimalFLong=%.3f",Asset,Algo,Lots,OptimalF,OptimalFShort,OptimalFLong);
		enterShort();
	}
}

function run()
{
	set(PARAMETERS+FACTORS+LOGFILE+PLOTNOW);
	StartDate = 20100101;
	EndDate = 20100105;
	BarPeriod = 1440;
	LookBack = 0;
	Capital = 5000;
	Verbose = 1;
	
	if (Bar == 1)
	{	
		while(asset(loop("EUR/USD")))
		{
			while(algo(loop("TRND")))
			{
				if(Test)
					printf("\n %s:%s OptF=%.3f OptFLong=%.3f OptFShort=%.3f\n\n", Asset,Algo, OptimalF, OptimalFLong, OptimalFShort);
			}
		}
	}

	while(asset(loop("EUR/USD")))
	{
		while(algo(loop("TRND")))
		{
			if(Algo == "TRND") 
				tradeTrend(1); //goLong
			if(Algo == "TRND") 
				tradeTrend(0); //goShort
		}	
	}
}

Re: OptimalF(Long/Short) and Algo ids [Re: jbhunter] #479614
04/12/20 03:33
04/12/20 03:33
Joined: Mar 2020
Posts: 41
J
jbhunter Offline OP
Newbie
jbhunter  Offline OP
Newbie
J

Joined: Mar 2020
Posts: 41
Unless I hear any feedback I am assuming that I am not able to access the OpF parameters within functions such as tradeTrend in above code. Not a big deal justs make the run() code a bit longer. I was hoping to capture the Margin cost within the alg function.

Re: OptimalF(Long/Short) and Algo ids [Re: jbhunter] #479617
04/12/20 10:27
04/12/20 10:27
Joined: Apr 2008
Posts: 586
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 586
Austria
OptimalFLong is a global variable, it is always used within functions. How else would you use it?

Re: OptimalF(Long/Short) and Algo ids [Re: jbhunter] #479629
04/12/20 22:37
04/12/20 22:37
Joined: Mar 2020
Posts: 41
J
jbhunter Offline OP
Newbie
jbhunter  Offline OP
Newbie
J

Joined: Mar 2020
Posts: 41
Any explanation to the behavior above?

Log when called from run() when Bar==1

Code
 EUR/USD:TRND OptF=1.000 OptFLong=2.000 OptFShort=3.000


Below called from functions referenced in run()

Code
 EUR/USD:TRND:L   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
 EUR/USD:TRND:S   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
 EUR/USD:TRND:L   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
 EUR/USD:TRND:S   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
 EUR/USD:TRND:L   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000
 EUR/USD:TRND:S   Lots=0   OptimalF=0.000 OptimalFShort=0.000 OptimalFLong=0.000

Re: OptimalF(Long/Short) and Algo ids [Re: jbhunter] #479632
04/13/20 08:17
04/13/20 08:17
Joined: Apr 2008
Posts: 586
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 586
Austria
The manual has two good advices for avoiding such mistakes:

"If you print a predefined variable, double check its type (var, float, int, or string)."

"For debugging purposes, the watch function is often more convenient than the printf function. It allows breakpoints, needs no format string, and prevents errors by printing wrong variable types."

https://manual.zorro-project.com/printf.htm

https://manual.zorro-project.com/lots.htm

Lots is var not int.

Re: OptimalF(Long/Short) and Algo ids [Re: jbhunter] #479636
04/13/20 13:57
04/13/20 13:57
Joined: Feb 2017
Posts: 1,729
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,729
Chicago
By the way, you are setting the Margin input value, then you are checking the unset Lots input value. Setting Margin does not set the Lots variable.

Re: OptimalF(Long/Short) and Algo ids [Re: jbhunter] #479638
04/13/20 16:09
04/13/20 16:09
Joined: Mar 2020
Posts: 41
J
jbhunter Offline OP
Newbie
jbhunter  Offline OP
Newbie
J

Joined: Mar 2020
Posts: 41
Thank you Petra! The Lots=%i was indeed causing the following OptF values in the printf statement to be incorrect. Changing to Lots=@.2f fixed the display problem and the OptF values are printing correctly.

I will be trying to use the watch function to avoid these mistakes in the future. Thanks again!


Andrew, thanks for the note. I still need to do some experimenting to fully understand how they are connected.


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1