Measuring Leverage

I tried this code and changed some syntaxe quirks to be able to compile it and run.
(Apparently we changed something in the server and the old code did not compile.)

It runs fine now.

Thanks Jozsika!

// https://forums.collective2.com/t/measuring-leverage/8394/6
// Select FOREX Systems we want in charts.
// NOT generalized yet to trade any system.
Int64[] systemsIds = { 94987184, // Just forex
 101623769 // Athena Triumph
 // 83803243 // Zip4x
 };

// Set starting date (YYYY,MM,DD)
var startingDate = new DateTime(2015,01,01);

// Dictionary for currency -> USD multiplier
var MultiplierByName = new Dictionary<string,double>() 
{ 
 {"USD",1.0},
 {"AUD", 0.7493},
 {"CAD", 0.7569},
 {"CHF", 1.02},
 {"EUR", 1.1158},
 {"GBP", 1.3},
 {"JPY", 0.009777},
 {"NZD", 0.7265}
};

// Create chart objects
ITimeSeriesChart notionalValueChart = new TimeSeriesChart();
notionalValueChart.Name = "Total margin";
ITimeSeriesChart leverageMultipleChart = new TimeSeriesChart();
leverageMultipleChart.Name = "Leverage Multiple";

// Variables
Random random = new Random();
decimal netOpenPos = 0;
var nvData = new List<ITimeSeriesPoint>();
var lmData = new List<ITimeSeriesPoint>();
var npData = new List<ITimeSeriesPoint>();

// Add Systems to charts
foreach (var id in systemsIds)
{
 // Initialize Charts data for System
 IChartTimeSeries nvChSeries = new ChartTimeSeries();
 nvChSeries.Type = ChartTypes.Line;
 nvChSeries.Name = (from sys in C2SYSTEMS where sys.SystemId == id select sys.SystemName).First();
 nvChSeries.Color = Color.FromArgb(random.Next(255), random.Next(255), random.Next(255));
IChartTimeSeries lmChSeries = new ChartTimeSeries();
lmChSeries.Type = ChartTypes.Line;
lmChSeries.Name = nvChSeries.Name;
lmChSeries.Color = nvChSeries.Color;

// Reset Variables for System
// netOpenPos = 0;
// netValue is the total we need in margin. So when we open a position, increases.
double netValue = 0.0;
nvData = new List<ITimeSeriesPoint>();
lmData = new List<ITimeSeriesPoint>();

// Identify Positions and NotionalValue
foreach (var sigQ in (from sig in C2SIGNALS 
                    where (sig.SystemId == id && sig.TradedWhen > startingDate)
                    orderby sig.TradedWhen ascending
                    select new 
                    {mplier=sig.Currency,d=sig.TradedWhen,q=sig.Quant,a=sig.Action,p=sig.TradePrice,ptV=sig.PtValue}
                   ))
{
  // The price is NOT part of the tradeValue. For FOREX the trade is measured
  // by the quantity * pointValue of the second member of the pair.
  double tradeValue = (double)(sigQ.q * sigQ.p * sigQ.ptV);
  tradeValue *= MultiplierByName[sigQ.mplier];
	// tradevalus is $25K of quantity

// double tradeValue = (double)sigQ.q * 25000.0;
 // For opening trade (regardless of direction) our margin increases,
 // for closing trade it is decreases.
 if(sigQ.a == "BTO" || sigQ.a == "STO") 
 { netValue += (double)tradeValue; }
 else if(sigQ.a == "BTC" || sigQ.a == "STC")
 { netValue -= (double)tradeValue; }
 nvData.Add( new TimeSeriesPoint() { DateTime = sigQ.d, Value = netValue });
 }
// Identify Leverage
foreach (var nv in nvData)
{
  var eqPtVal = (from eqdata in C2EQUITY where (eqdata.SystemId == id && eqdata.DateTime > nv.DateTime) orderby eqdata.DateTime ascending select eqdata.Value).First();
  lmData.Add(new TimeSeriesPoint() { DateTime = nv.DateTime, Value = Math.Abs(nv.Value/(double)eqPtVal) });
}

// Configure Charts
nvChSeries.Data = nvData;
notionalValueChart.Add(nvChSeries);
lmChSeries.Data = lmData;
leverageMultipleChart.Add(lmChSeries);

}

// Plot the Charts
CHART = notionalValueChart;
HR();
CHART = leverageMultipleChart;

1 Like