Why this script is not trading today?
Any Idea?

//////////////////////////////////////////////////////
// Mean Variance Optimization ////////////////////////
//////////////////////////////////////////////////////

//#define DAYS 252 // 1 year 252
//#define DAYS 231 // 11 Months
//#define DAYS 210 // 10 Months
#define DAYS 189 // 9 Months
//#define DAYS 168 // 8 Months
//#define DAYS 147 // 7 Months
//#define DAYS 126 // 6 Months

#define NN 300 // max number of assets
#define WEIGHTCAP 0.25 // Cap 0.15 - 0.5 Range
#define LEVERAGE 4 // 1-20 leverage
//#define MAXETF 7
//////////////////////////////////////////////////////

string Names[NN];
string Symbols[NN]; // Store the ISIN Code
vars Returns[NN];
var Means[NN];
var Covariances[NN][NN];
var Weights[NN];
int OldLots[NN];
var ThePrice[NN];
int N = 0;


function run()
{
BarPeriod = 1440;
LookBack = DAYS;
NumYears = 6;
AssetZone = WET;

set(PRELOAD+BALANCE); // allow extremely long lookback period
set(LOGFILE);
Verbose = 0;

// AssetList = "Lyxsor.csv";
// AssetList = "AzionarioSettoriali.csv";
// AssetList = "ETF2016-OK.csv";
// AssetList = "ETF2016-7.csv";
AssetList = "AssetsZ8-originale.csv";

vars Returns[NN];
var TotalCapital = slider(1,20000,1000,50000,"Capital","Total capital to distribute");
var VFactor = slider(2, 0 ,0, 100,"Risk","Variance factor");

if(is(INITRUN)) {
while(Names[N]=loop(Assets)) {
if(strstr(Names[N], "#")== NULL)
{
assetHistory(Names[N], FROM_YAHOO);
asset(Names[N]);
Symbols[N] = Symbol; // Store the isin code for quick referenze
// printf("\n Names, Asset, Symbol %d: %s - %s - %s", N, Names[N], Asset, Symbol);
if(N++ >= NN) break;
}
}
}

int i;
// for(i=0; i<N; i++) printf("\n%d - %s",i, Names[i]);
for(i=0; i<N; i++){
// if(!is(INITRUN)){
asset(Names[i]);
Returns[i] = series((priceClose(0)-priceClose(1))/priceClose(1));
// }
}

if(tdm() == 1 && !is(LOOKBACK)){
int j;
for(i=0; i<N; i++) {
Means[i] = Moment(Returns[i],LookBack,1);
// printf("\n%d - %s: %f",i, Names[i], Means[i]);
for(j=0; j<N; j++) Covariances[N*i+j] = Covariance(Returns[i],Returns[j],LookBack);
}
var BestVariance = markowitz(Covariances, Means, N, WEIGHTCAP);
var MinVariance = markowitzReturn(0,0);
markowitzReturn(Weights,MinVariance+VFactor/100.*(BestVariance-MinVariance));

#ifdef MAXETF
int* idx = sortIdx(Weights,N);
var TotalWeight = 0;
Spread = RollLong = RollShort = Commission = Slippage = 0;

for(i=N-MAXETF; i<N; i++) // sum up the 4 highest weights
TotalWeight += Weights[idx[i]];
for(i=0; i<N; i++) {
if(idx[i] < N-MAXETF)
Weights[i] = 0;
else // adjust weights so that their sum is still 1
Weights[i] /= TotalWeight;
}
#endif

// change the portfolio composition according to new weights
// var TheCapital = TotalCapital + WinTotal;
printf ( "\n\n --- %s ---",datetime());
for(i=0; i<N; i++) {
asset(Names[i]);
MarginCost = priceClose()/LEVERAGE;

// RollShort = RollLong =0;
// Spread = FIXEDCOM;

int NewLots = TotalCapital * Weights[i] / MarginCost;

if(NewLots > OldLots[i]) {
enterLong(NewLots-OldLots[i]);
}
else if(NewLots < OldLots[i]){
exitLong(0, 0, OldLots[i]-NewLots );
}
// printf("\nBestV: %f - MinV: %f", BestVariance, MinVariance);
printf("\n%-15.10s- %s: OldLots: %d NewLots: %d Price: %.2f",Names[i],Symbols[i], OldLots[i], NewLots, priceClose() );
OldLots[i] = NewLots;
}
}
}


Ciao

Last edited by MatPed; 09/01/16 14:10.