Zorro version 2.35 - GET_POSITION not working (MT4 bridge)

Posted By: simonkrebs

Zorro version 2.35 - GET_POSITION not working (MT4 bridge) - 04/06/21 20:47

Hello,

I have tested the Alice6 script from the Blackbook. Unfortunately the determination of OldLots does not work, so the positions from the last cycle (2 months before) are not closed.

The problem seems to be that the brokercommand GET_POSITION does not give a result. This should work since Zorro version 2.35, but unfortunately does not for me.

Then wrote a short test script, it doesn't give any results either. Tested with Darwinex and Global Prime (Zorro 2.35 with MT4 bridge).

Here's the test script:

Code
function run() {
	set(PLOTNOW+BALANCE+LOGFILE);
	set(PRELOAD); //reduce load for fetching data from MT4 server
	set(NOLOCK); //speed up API access
	LookBack = 0;

	assetList("AssetsGP");	
	asset("EUR/USD");
	
	printf("\n%d", (int)brokerCommand(GET_POSITION,Asset)); //doesnt work

	exitLong("*"); //workaround for simply closing all open positions in the trading account -> doesn't work, too

	if(Live && !is(LOOKBACK)) 
		quit("Ok!");
}



As a workaround, I tried just using exitLong to close all positions, but that doesn't work either. It just doesn't do anything at all. Here my guess is that the trades from the previous sessions are not recognized as open positions.

Can anyone help me with this?

Thx,

Simon
Posted By: jyd

Re: Zorro version 2.35 - GET_POSITION not working (MT4 bridge) - 04/07/21 06:14

GET_POSITION works for me. Have you updated your Zorro MT4 EA, which contains the implementation of GET_POSITION? My understanding is that exitLong() only works with the Zorro tracked orders. If it is not tracked, it can't be closed.
Posted By: simonkrebs

Re: Zorro version 2.35 - GET_POSITION not working (MT4 bridge) - 04/07/21 14:57

Hi jyd, I checked again today and it did not work. Here's the code for my script:

Code
// Simple portfolio rotation /////////////
//////////////////////////////////////////

#define RISK	0.7	// no risk, no fun

var Momentums[100],Weights[100];

void rotate(int GoLong)
{
	var Invest = RISK*(Capital+ProfitTotal);
	for(listed_assets) {
		int NewLots = Invest*Weights[Itor]/MarginCost;
		if(Test) {
			if(NewLots > LotsPool && GoLong)
				enterLong(NewLots-LotsPool);
			else if(NewLots < LotsPool)
				exitLong(0,0,LotsPool-NewLots);
		} else if(Live) {
			int OldLots = brokerCommand(GET_POSITION,Asset);
			if(NewLots > OldLots && GoLong)
				enterLong(NewLots-OldLots);
			else if(NewLots < OldLots)
				enterShort(OldLots-NewLots);
		}
	}
}

void run() 
{
	set(PLOTNOW|LOGFILE);
	StartDate = ifelse(Live,NOW,20120101); 
	EndDate = 20181231;
	BarPeriod = 1440;
	LookBack = 100;
	SaveMode = 0;
	
	assetList("AssetsSP30");
	Capital = slider(1,10000,0,20000,"Capital","");
	
	for(listed_assets) { 
		asset(Asset);
		vars Prices = series(priceClose());
		Momentums[Itor] = ROC(Prices,LookBack-1);
	}

	if((Live && !is(LOOKBACK))
		|| (Test && tdm(0) == 1 && month(0)%2)) // first trading day of every second month
	{
		distribute(Weights,Momentums,NumAssetsListed,5,0.5); 
		rotate(0);	// exit old positions
		rotate(1);	// enter new positions
	}

	if(Live && !is(LOOKBACK)) quit("Ok!");
}


I did two attempts, on the first he opened 5 positions. Directly after I started the script a second time. This time Zorro tried to open another 5 positions, but could not close the trades from the previous run. So there was not enough margin left to place all orders.

Can anyone help me fix this?

Thanks in advance,

Simon

Attached picture Bildschirmfoto 2021-04-07 um 16.48.33.jpg
Attached picture Bildschirmfoto 2021-04-07 um 16.49.08.jpg
Posted By: AndrewAMD

Re: Zorro version 2.35 - GET_POSITION not working (MT4 bridge) - 04/07/21 15:01

Quote
Have you updated your Zorro MT4 EA
You did not confirm that you updated your EA on the MT4 side. Did you? When in doubt, delete your EA completely and reinstall it.
Posted By: AndrewAMD

Re: Zorro version 2.35 - GET_POSITION not working (MT4 bridge) - 04/07/21 15:10

Also, the code you are showing was designed for an NFA plugin. The MT4 bridge is not an NFA plugin. This needs a rewrite.
Posted By: simonkrebs

Re: Zorro version 2.35 - GET_POSITION not working (MT4 bridge) - 04/07/21 16:51

Thanks AndrewAMD, yes I updated the EA (removed all files and replaced with the DLL and EQ4 files from the zip).

So you mean the code is not working with MT4? What's the reason for that?
Posted By: AndrewAMD

Re: Zorro version 2.35 - GET_POSITION not working (MT4 bridge) - 04/07/21 17:50

The non-NFA plugins (Oanda, MT4, etc) have an "enter trade exit trade" interface to the broker. So individual trades must be managed.

The NFA plugins (IB, etc) have an "order placement" interface to the broker. You place orders to change position. Position tracking is especially useful here. There is no need to manage individual "trades".

These have implications on how you should program your script for live trading.

Further reading:
https://zorro-project.com/manual/en/trading.htm
https://zorro-project.com/manual/en/hedge.htm
https://zorro-project.com/manual/en/buylong.htm
https://zorro-project.com/manual/en/trademode.htm
Posted By: simonkrebs

Re: Zorro version 2.35 - GET_POSITION not working (MT4 bridge) - 04/07/21 18:12

Ok, I see. Thanks for your answer, that was very helpful.

Is there a possibility to just close all trades before rotate(1) is been executed?
Posted By: jyd

Re: Zorro version 2.35 - GET_POSITION not working (MT4 bridge) - 04/08/21 00:16

AndrewAMD is right. The code you posted seems to be JCL's stock investment code from black book, which is designed for IB. MT4 is a FX and CFDs trading platform. So make sure you know what you are trying to do first.
Posted By: oldflatop

Re: Zorro version 2.35 - GET_POSITION not working (MT4 bridge) - 04/21/21 12:50

isn't the closing condition only in test mode?

"if((Live && !is(LOOKBACK)) || (Test && tdm(0) == 1 && month(0)%2)) // first trading day of every second month"
Posted By: simonkrebs

Re: Zorro version 2.35 - GET_POSITION not working (MT4 bridge) - 04/22/21 11:36

No, the if clause conditions are both for Live (whenever executed) or Test mode (every two months).

I'm now closing all positions instead of changing them. For me it's working now..

if((Live && !is(LOOKBACK))
|| (Test && tdm(0) == 3 && month(0)%2)) //2 months vs 1 month cycle has better performance due to lower costs
{
//The 10 largest positive elements of the Values array are taken
distribute(Weights,Momentum,NumAssetsListed,20,0.1);

//rotate(0); // exit old positions -> doesn't work, because MT4 doesnt support NFA

//Instead just close all open positions
for(used_assets) {
asset(Asset); // select the current component
exitLong(0,0);
}

rotate(1); // enter new positions
}
© 2024 lite-C Forums