trainroutine return & store / reuse

Posted By: danatrader

trainroutine return & store / reuse - 02/02/20 14:27

Hi,

I try to establish a subroutine / function that measures market cycle length in train mode.
Already somwhat working.


So I call the following:

Code
// double portfolio loop
  while(asset(loop("EUR/USD")))
	  if(Train){
		traincycles();
	  }
  while(algo(loop("TRND")))
  {
    Margin = 0.5 * OptimalF * Capital;
    if(Algo == "TRND") 
      tradeTrend();



The function itself contains the following:

Code
function traincycles()
{
	
	vars Prices = series(priceClose());
	
	int Cycle = optimize(10, 1, 55, 1);
	int CycleFast = optimize(10, 1, Cycle, 1);
	int TrendCycle = (Cycle+CycleFast)/2;

	vars Signals = series(ReFlex(Prices,Cycle));
	vars SignalsFast = series(ReFlex(Prices,CycleFast));

//Good Cycle Train Algo II
if(SignalsFast[0] > 0 && Signals[0] > 0) enterShort();
if(SignalsFast[0] < 0 && Signals[0] < 0) enterLong();

return(TrendCycle);

}



Now the question is, how to store the return value global and make it available for


Code
while(algo(loop("TRND")))
  {
    Margin = 0.5 * OptimalF * Capital;
    if(Algo == "TRND") 
      tradeTrend();




???
Posted By: danatrader

Re: trainroutine return & store / reuse - 02/02/20 15:14

Assume using AlgoVar is the choice?

Now I save them with

Code

if(Train && TrainCycle == 1){
SaveMode = SV_ALGOVARS+SV_ALGOVARS2;
			AlgoVar[0] = traincycles();
}



Is that correct?

How do I make use of the AlgoVar specified by the Asset / Algo it was generated for?
Is that handled internally by Zorro or is that something I have to do?
Posted By: AndrewAMD

Re: trainroutine return & store / reuse - 02/02/20 16:33

First of all, never put optimize() and series() calls inside of if(Train). This will screw up your script.

Next, your traincycles() call should be inside of if(Algo == "TRND"). So either tradeTrend() would have the traincycle as an argument, or you could even call traincycles() inside of tradeTrend().
Posted By: danatrader

Re: trainroutine return & store / reuse - 02/02/20 16:40

Thank you Andrew, I will change it.

But how do I make sure the traincycle result is saved for later use on the specific asset / algo?

I will fight my way through, I am thankful for your advice.
Posted By: AndrewAMD

Re: trainroutine return & store / reuse - 02/02/20 16:45

Train mode will optimize the optimize calls. Zorro saves the respective values for the optimize() calls. After you [Train], those optimal values will be retrieved in [Test] and [Trade] modes.
Posted By: danatrader

Re: trainroutine return & store / reuse - 02/02/20 18:29

Great, thank you a lot, I call it from inside the trend.
Passing everything as argument.

Step by step, getting there.
Looks actually a lot nicer.


Okay, now that brings another problem.

I have a nice function to train the curves, I can also look at them in TEST.
But things change, once I try to add the real trading rules.

Either they are included in the train run, or in test, they are not shown.
Below full script so far.


Code

#include <default.c>
#include <Reflex.c>

function traincycles(vars Prices, int Cycle, int CycleFast, vars Signals, vars SignalsFast)
		{

			if(SignalsFast[0] > 0 && Signals[0] > 0) enterShort();
			if(SignalsFast[0] < 0 && Signals[0] < 0) enterLong();

		}

function tradeTrend()
{
	
	vars Prices = series(priceClose());
	
	int Cycle = optimize(10, 1, 55, 1);
	int CycleFast = optimize(1, 1, Cycle, 1);
	
	
	vars Signals = series(ReFlex(Prices,Cycle));
	vars SignalsFast = series(ReFlex(Prices,CycleFast));
	
	
	if(Train && !Test){
		traincycles(Prices, Cycle, CycleFast, Signals, SignalsFast);
	}

	int TrendCycle = Cycle + CycleFast;
	vars SignalsTrend = series(TrendFlex(Prices,TrendCycle));
	
	plot("ReFlex",Signals,NEW|LINE,RED);
	plot("ReFlexFast",SignalsFast,LINE,GREEN);
	plot("TrendFlex",SignalsTrend,LINE,YELLOW);

}

function run()
{
  if (is(INITRUN)) NumCores = 16;
  set(PARAMETERS+FACTORS+LOGFILE);
  BarPeriod = 1440; 
  LookBack = 2000;
  StartDate = 2005;
  NumTrainCycles = 3;
  //NumWFOCycles = 10;  
  Capital = 10000;
  MaxLong = MaxShort = 1;
  Hedge=5;

  if(ReTrain) {
    UpdateDays = -1;  
    SelectWFO = -1;	
    reset(FACTORS); 
  }
 
// double portfolio loop
  while(asset(loop("EUR/USD")))
  while(algo(loop("TRND")))
  {
    Margin = 0.5 * OptimalF * Capital;
    if(Algo == "TRND") 
      tradeTrend();
  }
}





Now I want to start adding simple "real rules" in the TREND Algo.
They should not be part of Train, but be part of Test, while the call off the traincycles function should be called always in test, but only in train on demand (e.g. edit script).

Code
	if(valley(SignalsTrend))
		enterLong();
	if(peak(SignalsTrend))
		enterShort();

Posted By: danatrader

Re: trainroutine return & store / reuse - 02/02/20 19:15

It works great with commenting out... as intended, now I got some nicely matching curves and a trigger.
Posted By: danatrader

Re: trainroutine return & store / reuse - 02/03/20 19:57

Well, the curves are nice, the idea behind seems to work, BUT,

I am still lacking some dev skills.

How would I transfer the

optimize(10, 1, 55, 1)
and
optimize(1, 1, Cycle, 1)

to the function traincycles???

Second question, how would I pass back two values to the calling function.

Or, is it possible to directly modify them in the function traincycles?
I know that is not (if possible in C) good practice, to modify global variables inside a function instead of handing them back out.
Posted By: AndrewAMD

Re: trainroutine return & store / reuse - 02/03/20 22:01

You're already passing the optimized values into the function. Simply use them.

EDIT: If you want to edit the values and retain the edits in the original variables, pass pointers instead of values. If you don't know what that means, either learn C or eliminate the function entirely and move the functionality into the body so you don't have to worry about pointers.
Posted By: danatrader

Re: trainroutine return & store / reuse - 02/04/20 20:15

Thank you again.

Yes learn C, Zorro is my vehicle for doing so.

I got at least now the function running with pointers.
Thank you for the hint.

I assume I still have plenty of mistakes, but sometimes those hints are the really valuable things.
© 2024 lite-C Forums