Margin Issues / not working?

Posted By: trenki2

Margin Issues / not working? - 01/20/17 09:29

In the following script I calculate the Margin value to use and print it to the console. Even though the computed margin value is always different the strategy seems not to use it as in the result log it always sais:

Code:
Max open margin     1000$



I don't understand how the max open margin can be 1000 when I set the Margin variable in the script to much higher values before entering long.

The code to reproduce this is this one here:

Code:
function run()
{
	set(PARAMETERS);
	
	BarPeriod = 1440;
	StartDate = 1993;
	LookBack = 500;
	Capital = 10000;
	var InitialMargin = 1000;
	
	assetList("AssetsFXCMCFD.csv");
	assetHistory("SPY", FROM_YAHOO);
	asset("SPY");
	
	Margin = InitialMargin * (Balance / Capital);
	
	vars price = series(priceClose());
	var ma = SMA(price, optimize(200, 25, 500));
	
	if (NumOpenLong == 0 && price[0] > ma)
	{
		printf("n%f", Margin);
		enterLong();	
	}
		
	if (NumOpenLong > 0 && price[0] < ma)
		exitLong();
}



AssetsFXCMCFD.csv:
Code:
Name,Price,Spread,RollLong,RollShort,PIP,PIPCost,MarginCost,Leverage,LotAmount,Commission,Symbol
SPY,225.91,0.01,0,0,0.01,0.01,0,10,1,0,

Posted By: jcl

Re: Margin Issues / not working? - 01/20/17 12:32

It would have been bad for us all if the margin were not working, but I do not understand the problem from your code. If you think something is not working, you can just calculate it yourself and compare it with the statistics. The max open margin is not identical with the "Margin" variable. It is the max open lots multiplied with the margin cost per lot. The margin cost is provided by your broker and must be entered in the asset list. The open lots can be summed up in a trade enumeration loop.
Posted By: trenki2

Re: Margin Issues / not working? - 01/20/17 16:43

Ok, so i modified the script to compute the max open margin as you said by computing the max of TradeLots * MarginCost.

Here is the code:

Code:
function run()
{
	BarPeriod = 1440;
	StartDate = 1993;
	LookBack = 200;
	Capital = 10000;
	var InitialMargin = 1000;
	
	assetList("AssetsFXCMCFD.csv");
	assetHistory("SPY", FROM_YAHOO);
	asset("SPY");
	
	Margin = InitialMargin * (Balance / Capital);
	
	// Compute max open margin. 
	// Note: there is only one open trade, so we don't have to sum things up.
	static var MaxOpenMargin = 0;
	for (open_trades)
		MaxOpenMargin = max(MaxOpenMargin, TradeLots * MarginCost);
	
	vars price = series(priceClose());
	var ma = SMA(price, 200);
	
	if (NumOpenLong == 0 && price[0] > ma)
		enterLong();	
		
	if (NumOpenLong > 0 && price[0] < ma)
		exitLong();
		
	if (is(EXITRUN))
		printf("nMax open margin: %f", MaxOpenMargin);
}



In the EXITRUN I print the computed max open margin to the console.
When I run it it prints

Code:
Max open margin: 5134.946152



But When I look into the trade results it says:

Code:
Max open margin     1000$



Am I still doing something wrong here or is this a real issue?
Posted By: jcl

Re: Margin Issues / not working? - 01/27/17 14:14

When I run your script, I get a correct max open margin both ways. However I used the IB assets. Maybe something is wrong in your asset list?
Posted By: trenki2

Re: Margin Issues / not working? - 01/27/17 15:04

I supplied the full contents of my AssetsList.csv file in the original post! You can try with that and reproduce the results.

What could be wrong in the AssetsList? I just copied a line from the IB list, changed the leverage and added the current price for the asset.
Posted By: jcl

Re: Margin Issues / not working? - 01/27/17 16:39

With your asset list I still get similar results. They are not exactly identical since your script sums up the margin one bar later.

I'm using the latest beta version, but I don't think that this affects the margin. Have you changed something else in your installation?
Posted By: trenki2

Re: Margin Issues / not working? - 01/27/17 17:17

No, I have not changed anything. I get this result with a fresh installation.

I have now also tested the latest beta. There I get a different margin value! Its not identical to the one that the script calculates but much closer!
Posted By: Joaquin

Re: Margin Issues / not working? - 01/30/17 11:59

I have also tested your code. In the EXITRUN log I have
Max open margin: 2539.021343
And in the trade log I have
Max open margin 283$
I'm using Zorro 1.50 . Do these results look good to you?
Posted By: trenki2

Re: Margin Issues / not working? - 01/30/17 14:30

Interesting!

In Zorro 1.50.6 i get the following EXITRUN log:
Max open margin: 5188.282166

While in the results I get:
Max open margin 1000$

This is with the above Assets file.

When I use the current Zorro Beta 1.53.2 i get this in the EXITRUN log:
Max open margin: 5280.110168

And in the test results it shows this:
Max open margin 4854$

Now there seems to be a difference in every version. An this is pretty serious since the Max open margin value is a very important value!

Now there is one thing that I forgot in the script and that is to set the EndDate to a fixed date. But still the differences should never be that big.
Posted By: jcl

Re: Margin Issues / not working? - 01/30/17 14:47

This is the same result that I got. There's no margin issue of 1.50.6 mentioned on our bug list. I'll check what the 1.50.6 problem might have been and if someone has maybe fixed it and forgot to put it on the list.
© 2024 lite-C Forums