How to perform portfolio optimization?

Posted By: antoniorenar

How to perform portfolio optimization? - 03/16/21 20:08

Good afternoon,
I am new in zorro,
I am trying to optimize my system on multiple symbols and that single set of parameters for all symbols but at the moment it throws me a set of parameter for each symbol that I optimize, someone who does the same could give me a hint
Posted By: AndrewAMD

Re: How to perform portfolio optimization? - 03/16/21 20:12

What is your question?
Posted By: antoniorenar

Re: How to perform portfolio optimization? - 03/16/21 21:04

I want to optimize my system in several markets at the same time, and that the optimizer looks for a single set of parameters for all markets
not one for each
can you show me some code?
Thanks for your answer
Posted By: AndrewAMD

Re: How to perform portfolio optimization? - 03/16/21 21:22

Can you give me an example of code that does what you do not expect/want and explain what you expect/want instead?
Posted By: antoniorenar

Re: How to perform portfolio optimization? - 03/17/21 00:20

For instance, the algorithm below optimizes a couple of parameters (TimeFactor and Coef) in the EUR/USD, GBP/USD and AUD/USD symbols. As a result, the best couple of parameters is 3.45 and 2.90 in EUR/USD, 3.64 and 8.01 in GBP/USD, and 3.59 and 8.10 in AUD/USD.
However, if my code would run as I want, we would obtain the same parameters for the 3 symbols (for example, 3.5 and 5 for EUR/USD, GBP/USD and AUD/USD markets). This is an example of multi-symbol algorithm. In this way the optimization look for the same set of parameter for different markets in order to get the best trading results combining the same algorithm with the same parameters in different markets.


function run()
{
set(LOGFILE);
set(PARAMETERS);

StartDate=2018;
NumYears=1;

BarPeriod = 60;
LookBack = 100*5;

while( asset( loop("EUR/USD","GBP/USD","AUD/USD") ) )
{
var TimeFactor = optimize(3,1,5,1);
var Coef = optimize(3,1,10,1);

vars Price = series(price(0));
vars MA1 = series(SMA(Price,30));
vars MA2 = series(SMA(Price,30*TimeFactor));

Stop = Coef*ATR(10);

if(crossOver(MA1,MA2))
enterLong();
else if(crossUnder(MA1,MA2))
enterShort();
}
}
Posted By: AndrewAMD

Re: How to perform portfolio optimization? - 03/17/21 01:12

You move your optimize() calls to outside of the while asset loop. Now instead of six optimized variables, you get two optimized variables, which will then be common to all assets.
Posted By: antoniorenar

Re: How to perform portfolio optimization? - 03/17/21 17:08

I try whit this code, nevertheless it keeps throwing me 6 parameters
Could you help me correcting the fragment and sending it to me?

function run()
{
set(LOGFILE);
set(PARAMETERS);

StartDate=2018;
NumYears=1;

BarPeriod = 60;
LookBack = 100*5;

var TimeFactor = optimize(3,1,5,1);
var Coef = optimize(3,1,10,1);

while( asset( loop("EUR/USD","GBP/USD","AUD/USD") ) )
{
vars Price = series(price(0));
vars MA1 = series(SMA(Price,30));
vars MA2 = series(SMA(Price,30*TimeFactor));

Stop = Coef*ATR(10);

if(crossOver(MA1,MA2))
enterLong();
else if(crossUnder(MA1,MA2))
enterShort();
}
}

Thanks a lot for your time, I hope to reward you
Posted By: AndrewAMD

Re: How to perform portfolio optimization? - 03/17/21 17:33

I forgot to mention - you need to eliminate the loop() call. For that, you can load an asset list and use one of the asset loops instead:
https://manual.zorro-project.com/fortrades.htm
Posted By: antoniorenar

Re: How to perform portfolio optimization? - 03/17/21 23:51

Sorry, I've been in zorro for two days and I can't find the function correctly, would you do me the immense favor of editing the code in the right way, thanks you
Posted By: antoniorenar

Re: How to perform portfolio optimization? - 03/17/21 23:54

function run()
{
set(LOGFILE);
set(PARAMETERS);

StartDate=2018;
NumYears=1;

BarPeriod = 60;
LookBack = 100*5;

var TimeFactor = optimize(3,1,5,1);
var Coef = optimize(3,1,10,1);

while( asset( loop("EUR/USD","GBP/USD","AUD/USD") ) )
{
vars Price = series(price(0));
vars MA1 = series(SMA(Price,30));
vars MA2 = series(SMA(Price,30*TimeFactor));

Stop = Coef*ATR(10);

if(crossOver(MA1,MA2))
enterLong();
else if(crossUnder(MA1,MA2))
enterShort();
}
}
Posted By: AndrewAMD

Re: How to perform portfolio optimization? - 03/18/21 03:07

Why did you start a different thread with the same name and a slightly different question?

More importantly, why didn't you attempt to do what I said? Please review:
Originally Posted by AndrewAMD
I forgot to mention - you need to eliminate the loop() call. For that, you can load an asset list and use one of the asset loops instead:
https://manual.zorro-project.com/fortrades.htm

Some questions:
1) Why didn't you replace the loop() call? Fix this.
2) Where is your for(listed_assets){asset(Asset); ... ... } loop? Fix this.
3) Why didn't you load an asset list? To do this, you call assetList(); Fix this.

Required reading:
https://zorro-project.com/manual/en/asset.htm (assetList)
https://manual.zorro-project.com/fortrades.htm (for(listed_assets))
Posted By: antoniorenar

Re: How to perform portfolio optimization? - 03/18/21 19:41

Hello, sorry for new thread I don't understand good the platform
I have something like this
function run()
{
set(LOGFILE);
set(PARAMETERS);

StartDate=2018;
NumYears=1;

BarPeriod = 60;
LookBack = 100*5;

var TimeFactor = optimize(3,1,5,1);
var Coef = optimize(3,1,10,1);

assetList("AssetsDarwinex","EUR/USD","GBP/USD","AUD/USD");

for(listed_assets)
{
asset(Asset);

vars Price = series(price(0));
vars MA1 = series(SMA(Price,30));
vars MA2 = series(SMA(Price,30*TimeFactor));

Stop = Coef*ATR(10);

if(crossOver(MA1,MA2))
enterLong();
else if(crossUnder(MA1,MA2))
enterShort();
}
}
I have an error between assetList(...) and for(listed_assets), where it is no line of code
What can I do to resolve the problem?
May you help me changing my code, please? My knowledge in Zorro does not allow me to advance anymore
Posted By: AndrewAMD

Re: How to perform portfolio optimization? - 03/19/21 00:42

assetList() only takes one or two arguments, yet you have supplied four arguments. That's an error.
Posted By: antoniorenar

Re: How to perform portfolio optimization? - 03/19/21 02:11

I don't really understand anything! You told me to use assetlist() to be able to optimize in 3 or more markets. But if you now tell me that assetlist() only accepts 2 parameters, I can only work with 1 market.

Please, I beg you, if you really know how to do it, modify the code below for me (I attach you too), because I am new in Zorro and its user manual does not solve my doubts because it lacks examples.

Now, if it is not possible to do what I ask you in Zorro, tell me and I leave this trading platform immediately, because effectiveness and efficiency are fundamental for me.

Thank you very much in advance, Andrew.

function run()
{
set(LOGFILE);
set(PARAMETERS);

BarPeriod = 60;
LookBack = 151;

var TimeFactor = optimize(3,1,5,1);

assetList("AssetsDarwinex","EUR/USD","GBP/USD"/*,"AUD/USD"*/);

for(listed_assets)
{
asset(Asset);

vars Price = series(price(0));
vars MA1 = series(SMA(Price,30));
vars MA2 = series(SMA(Price,30*TimeFactor));

Stop = 3*ATR(10);

if(crossOver(MA1,MA2))
enterLong();
else if(crossUnder(MA1,MA2))
enterShort();
}
}

Attached File
FOR ANDREW.txt  (109 downloads)
Posted By: AndrewAMD

Re: How to perform portfolio optimization? - 03/19/21 18:49

assetList("AssetsDarwinex","EUR/USD","GBP/USD"/*,"AUD/USD"*/);

Let's count the parameters:
one: "AssetsDarwinex"
two: "EUR/USD"
three: "GBP/USD"/*
four: "AUD/USD"*/

The last two are garbage entries anyways. And you don't need the second one.

Let's change it to this:

assetList("AssetsDarwinex");

The assetList() call loads a csv file, named AssetsDarwinex.csv in your History folder. Make sure this CSV has only the three assets you need, and you're all set.

Therefore, your for(listed_assets) call will only load the three assets in your CSV file.

Try it.
Posted By: antoniorenar

Re: How to perform portfolio optimization? - 03/19/21 22:17

Now I understand you, Andrew!

I just tried what you told me and.... it works!!! Whichever I choose externally in the market box (to the right of the script name box), I always obtain the same parameters for the 3 markets, just what I wanted.

Thank you VERY MUCH, Andrew!!!
© 2024 lite-C Forums