Margin (and leverage) calculation

There is some code floating around calculating margin requirements of a system, based on the SIGNALS table. The basic idea of the incorrect ones (and I didn’t see correct ones yet) to go through the SIGNALS table and calculate a trade value by multiplying the quantity by the pointvalue, and price.

If the signal is an opening trade, we add this to a running total. If it is a closing trade, we subtract. This (the running total) the chart we display.

This, IMHO is not correct for at least two reasons:

(1) If the system trades multiple symbols, this literally mixes apples and oranges. I.e. if it trades both AAPL and ORAN, it will not distinguish between them. For systems that trade only one symbol (e.g. ES) this is not an issue.

(2) In the second part of the calculation (when we close the trade) we subtract from the running total a different amount than we added. This is because the execution price of the closing trade is different (most of the time different, if one wants to be pedantic) than the entry price. This is a distortion that bothers me (YMMV).

I don’t have a bullet proof solution for this. Right now what I am doing (mostly because #1 is a real problem for multi-symbol systems) that I run the query over the SIGNALS table adding a “GROUP BY tradeId” clause. When processing the result I calculate an openingSignalTotal quantity, i.e. I do the above mentioned calculations but only for opening trades (and either for apple or for orange, but not mixing them together.) To the running total then I add this at the open date and subtract this at the close date. I.e. I do not calculate the signal total for the closing signal, rather I subtract the opening margin.

This is not a perfect solution for #2 (as it doesn’t take into account the possible extra margin during DD), but it is one (imperfect) solution and more importantly it does address #1 in its entirety.

HTH, GL to all.

Joseph

2 Likes

I developed the C# code below for my first attempt of trading a group of stocks. I am using it on the system called RebelRebel trading Dow30 stocks. It worked like crap the first couple of weeks and then I tweeked it and it is working well now - (stops trading when your margin is reached). The code lets you decide which stocks to trade (in this case Dow30 stocks, how much you want to spend on each stock (zMargin variable) and how much you have in total (AccountMax), which is 2x the C2 amount I stated when I developed the code. Love to here your thoughts on it and good luck.

protected override void OnBarUpdate()
{

	if (Time[0].DayOfWeek == DayOfWeek.Friday

// && ToTime(Time[0]) >= 140000
)
{
ExitLong(“ExitLongClose”, “”);
ExitShort(“ExitShortClose”, “”);
return;
}

	if (BarsInProgress == 0
		)
	{
		if (
			Time[0].DayOfWeek == DayOfWeek.Monday
			&& Bars.FirstBarOfSession && FirstTickOfBar
		)
		{
					
			xMargin = 0;//reset margin counter
			yMargin = 0;//reset margin counter
			
		}
		
	}

		if (xMargin >= zMargin
			)
		{
		return;//stops trading when your margin reaches your set amount(zMargin)
		}


for (int idx = 0; idx <= 30 && xMargin <= zMargin; idx++) // loop through all of the DOW30 stocks or whatever you decide to list
{

	NumUnits = (int)Math.Floor(AccountMax/Closes[idx][0]);// calculate how many shares you can buy	

	if (BarsInProgress == idx// only perform code on stock brought into the loop
			&& ToTime(Times[idx][0]) >= 093000
			&& ToTime(Times[idx][0]) <= 160000
		)
	{

		if (
			// put code here for Trade	
			)
		{
			EnterShort(idx,(NumUnits), "TradeName");
		if (
			Position.MarketPosition == MarketPosition.Long//if short trade is a reversal then do this
				)
			{
			yMargin = Closes[idx][0]*NumUnits;
			xMargin = xMargin;
			}
			else//if not reversal then do this
			{
			yMargin = Closes[idx][0]*NumUnits;
			xMargin = xMargin + yMargin;	
			}
			SetTrailStop("TradeName", CalculationMode.Ticks, (stopsize), false);
			}
////	repeat for long trades or other types of trades.	
	
	
	}

}

Margin is available as a column in TimeSheet.

See https://collective2.com/c2explorer_help/html/ff5c67cf-1ee5-43b0-bc45-b1bf8f71a9be.htm

The margin used for a forex system is not populating correctly and only shows zero. I am using system ID 101623769. This is an issue as different currencies have different margins, and the leverage calculations based on the SIGNALS table do not provide a good approximation of how much margin is needed to trade the system.

Is there a way to apply IB’s margins to forex to the calculations in that column?

Edit: The code in Measuring Leverage works for Forex systems

I know this is an old thread, but I have been trying to used the MarginUsed column in the TimeSheet but it appears as Seph mentioned it is always zero even for systems who are currently using margin according to their statistics on their page. Was something changed?

Charles,
can you give me a specific system id?

Thank you.

I was just looking at Smart Volatility Margin, 102427283. The description page shows that margin is being used. But when I run the Timesheet it shows zero margin every day. I have had this problem with every system. I can’t seem to find a system that uses margin according to the public timesheet code. Can you? Do you see what is wrong, or what I am doing wrong?
image

Hello!

Bad news.

We do not support margin analysis in the Collective API currently.
C2Explorer uses it, so it does not work…
Sorry.
I’ll remove or disable it in C2Explorer until C2 API will work again.

Okay I figured something like that may have been the case. I know you are probably busy, but if you don’t mind and can remember will you make an update here if this changes? Also, thank you for looking into it!