I'm interested in this topic and jad some communication with jcl on Financial Hacker, I use ZorroS and my broker is IB, I want to test MVO and get all the metrics just like Z8. I'm new to programming, barely managed some codes like follwoing:
function run()
{
set(PARAMETERS);
BarPeriod = 1440;
LookBack = 252 ;
NumYears = 7;
Verbose = 0;
set(PRELOAD); // allow extremely long lookback period
NumWFOCycles = 5;
MonteCarlo = 1000;
Capital = 100000;//intent to calculate CARG
assetList("AssetsETF.csv");
string Names[NN];
vars Returns[NN];
var Means[NN];
var Covariances[NN][NN];
var Weights[NN];
int OldLots[NN] ;//positions
var Allmystake = 100000 ;//equals to Capital
var VFactor = 10;
int i,j;
static var BestVariance = 0;
static var MinVariance = 0;
static int Month = 0;
//Get Assets History and calculate returns
int N = 0;
while(Names[N] = loop((Assets)))
{
if(is(INITRUN))
assetHistory(Names[N],FROM_YAHOO);
asset(Names[N]);
Returns[N] = series((priceClose(0)-priceClose(1))/priceClose(1));
if(N++ >= NN) break;
}
//Initialize Allmystake and positions
if(is(FIRSTRUN))
{
Allmystake = 100000;
printf("n%d Initial Allmystake: %d$", date(), Allmystake);
for(i=0; i<N; i++)
{
OldLots[i] = 0;
}
}
if(tdm() == 1 && !is(LOOKBACK)) {
{
for(i=0; i<N; i++) {
Allmystake += OldLots[i]*priceClose(); //recalculate the Allmystake upon the positions
Means[i] = Moment(Returns[i],LookBack,1);
for(j=0; j<N; j++)
Covariances[N*i+j] = Covariance(Returns[i],Returns[j],LookBack);
}
BestVariance = markowitz(Covariances,Means,N,0.5);
MinVariance = markowitzReturn(0,0);
markowitzReturn(Weights,MinVariance+VFactor/100.*(BestVariance-MinVariance));//change the positions as to new weights
Month++;
for(i=0; i<N; i++)
{
asset(Names[i]);
MarginCost = priceClose()/Leverage;
int NewLots = Allmystake*Weights[i]/MarginCost;
printf("n%s : Date%d OldLots: %d NewLots: %d %.0f$", Names[i], date(), OldLots[i], NewLots,priceClose());
if (NewLots > OldLots[i])
enterLong(NewLots-OldLots[i]);
else if(NewLots < OldLots[i])
exitLong(0,0,OldLots[i]-NewLots);
OldLots[i] = NewLots;
}
}
Allmystake = 0;//reset Allmystake
printf("nDate %d ",date());
}
}
Unfortunately something went so wrong, Allmystake has always been 0 and no positions have been reported.I guess I didn't calculate Allmystake properly, which is the total equity I can invest.
Anyone can help? Thank you in advance.
Jeff
Last edited by Jeff1228; 02/10/17 01:40.