Gamestudio Links
Zorro Links
Newest Posts
Binance Futures Plugin
by OptimusPrime. 11/27/20 17:03
DirectX : 3d Hardware Reset Failure
by Emre. 11/27/20 11:06
Sortino(vars Data, int TimePeriod): var
by danatrader. 11/26/20 20:45
Need code for Williams Sentiment Index
by danatrader. 11/26/20 20:42
Optimizing global & local parameters
by dBc. 11/25/20 15:16
Enter FOK orders simultaneously
by jcl. 11/25/20 10:55
looking how basic functions are made
by jcl. 11/25/20 10:47
AUM Magazine
Latest Screens
Paradox Vector
The Space Between
Pogostuck: Rage With Your Friends
Worst Case Z
Who's Online Now
2 registered members (OptimusPrime, 1 invisible), 494 guests, and 10 spiders.
Key: Admin, Global Mod, Mod
Newest Members
MMK, raed, liquidbread, Dmitry_Shesterin, ddczp
18543 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
New TradeMode: TR_CORRECTIVE (for NFA accounts) #477777
07/27/19 15:42
07/27/19 15:42
Joined: Feb 2017
Posts: 1,129
Chicago
AndrewAMD Online OP
Serious User
AndrewAMD  Online OP
Serious User

Joined: Feb 2017
Posts: 1,129
Chicago
jcl,

It would be helpful if there was a new TradeMode made available: TR_CORRECTIVE. This would be specifically designed for NFA accounts. The design intent is to synchronize the Pool position with the broker position without requiring any manual intervention.

It's similar to TR_PHANTOM, but with a twist:
* Upon enterLong/enterShort, the Pool does *not* send an entry order to the broker.
* Upon exit, the Pool does *indeed* send an exit order to the broker.

The reasons for this are twofold:
1) The bookkeeping would be better.
2) Corrective actions can and should be fully automatic. It is better if a human does not need to manually intervene with a trading system.

An example use: LotsPool is +80 but my actual position is -40 for reasons unknown. So I enable TR_PHANTOM and then enterShort by 120 lots. Therefore both LotsPool and the actual position are now correctly showing -40. The correction is then documented in my logs.

Moreover, if I stop trading, and I click yes to close all trades, the system will actually close out the -40 lots position correctly instead of shorting by another 80 lots, closing the script at -100 lots.

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) [Re: AndrewAMD] #477791
07/29/19 08:19
07/29/19 08:19
Joined: Jul 2000
Posts: 27,401
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,401
Frankfurt
Although LotsPool is documented as read-only, I think you can modify it and thus easily synchronize the account with no need of a new trade mode. Try it. Let me know if something does not work.

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) [Re: AndrewAMD] #477792
07/29/19 10:04
07/29/19 10:04
Joined: Feb 2017
Posts: 1,129
Chicago
AndrewAMD Online OP
Serious User
AndrewAMD  Online OP
Serious User

Joined: Feb 2017
Posts: 1,129
Chicago
Let me look into this. Thanks.

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) [Re: AndrewAMD] #477875
08/09/19 01:55
08/09/19 01:55
Joined: Feb 2017
Posts: 1,129
Chicago
AndrewAMD Online OP
Serious User
AndrewAMD  Online OP
Serious User

Joined: Feb 2017
Posts: 1,129
Chicago
Well, this did work a little bit, but I have found there are problems if the sum of pool trade positions are not equal to LotsPool. For example, if I close Zorro and click "Yes" to close all trades, the pool trades get closed - this is problematic if I modify LotsPool, thus putting the pool trades and LotsPool out of sync.

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) [Re: AndrewAMD] #477878
08/09/19 18:01
08/09/19 18:01
Joined: Feb 2017
Posts: 1,129
Chicago
AndrewAMD Online OP
Serious User
AndrewAMD  Online OP
Serious User

Joined: Feb 2017
Posts: 1,129
Chicago
Here is an example implementation of flattening a position. It works, but I suspect I might be messing with Zorro's bookkeeping.

This is intrabar only. You cannot run this code from run(). Also, it assumes one Asset and one Algo.

As you can see, not only do i need to override LotsPool, but I also need to override LotsVirtual and LotsPhantom to keep the system sane. Finally, the net position of open virtual trades, phantom trades, and pool trades need to match LotsVirtual, LotsPhantom, and LotsPool, respectively.

Code
LotsPool = (int)brokerCommand(GET_POSITION,SymbolTrade);
//exit all virtual trades without sending to broker
setf(TradeMode,TR_PHANTOM);
for(open_trades){
	if(TradeAlgo != "foo") continue;
	if(!TradeIsVirtual) continue;
	ThisTrade->flags &= ~TR_NET;
	printf("\nExiting virtual trade, no send to broker, id: %d",TradeID);
	exitTrade(ThisTrade);
	ThisTrade->flags |= TR_NET;
}
//inject phantom trade and convert it to regular virtual.
setf(TradeMode,TR_PHANTOM);
Lots=1;
printf("\ncheckpoint enterlong");
TRADE* pTr = enterLong();
resf(TradeMode,TR_PHANTOM);
LotsVirtual = 1;
LotsPhantom = 1;
pTr->flags |= TR_PHANTOM|TR_NET;
Lots=1;
printf("\ncheckpoint exittrade");
exitTrade(pTr); // <--about to try this
LotsPool = (int)brokerCommand(GET_POSITION,SymbolTrade);

//cancel all virtual trades
for(open_trades){
	if(TradeAlgo != "foo") continue;
	if(!TradeIsVirtual) continue;
	printf("\nCanceling virtual trade id: %d",TradeID);
	cancelTrade(ThisTrade);
}

// cancel remaining pool trades
for(open_trades){
	if(TradeAlgo != "foo") continue;
	if(!TradeIsPool) continue;
	printf("\nCanceling pool trade id: %d",TradeID);
	cancelTrade(ThisTrade);
}
LotsPool = (int)brokerCommand(GET_POSITION,SymbolTrade);


Erm, it's probably too verbose. I think I can rewrite this into a simpler void set_position(int pos) function.

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) [Re: AndrewAMD] #477879
08/10/19 02:39
08/10/19 02:39
Joined: Feb 2017
Posts: 1,129
Chicago
AndrewAMD Online OP
Serious User
AndrewAMD  Online OP
Serious User

Joined: Feb 2017
Posts: 1,129
Chicago
I will test this when the market opens, this looks like a much cleaner implementation:

Code
void sync_pool_to_broker_position(){
	int tm = TradeMode, lp = LotsPool, lv = LotsVirtual, lph = LotsPhantom;
	LotsPool = brokerCommand(GET_POSITION,SymbolTrade);
	int lp_diff = LotsPool - lp;
	if(lp_diff){
		setf(TradeMode,TR_PHANTOM);
		Lots = abs(lp_diff);
		TRADE* pTr;
		if(lp_diff>0)
			pTr = enterLong();
		else if(lp_diff<0)
			pTr = enterShort();
		setf(pTr->flags,TR_NET);
		resf(pTr->flags,TR_PHANTOM);
	}
	TradeMode = tm; LotsVirtual = lv; LotsPhantom = lph;
}

void force_pool_order(){
	if(LotsPool==LotsVirtual) return;
	char algoname[16];
	strcpy(algoname,Algo);
	algo("HACK");
	int tm = TradeMode, lv = LotsVirtual, lph = LotsPhantom;
	setf(TradeMode,TR_PHANTOM);
	TRADE* pTr = enterLong(1);
	setf(pTr->flags, TR_PHANTOM|TR_NET);
	resf(TradeMode,TR_PHANTOM);
	exitTrade(pTr);
	TradeMode = tm; LotsVirtual = lv; LotsPhantom = lph;
	algo(algoname);
}

void set_broker_position(int pos){
	sync_pool_to_broker_position();
	if(LotsVirtual == pos){
		force_pool_order();
		return;
	}
	
	int tm = TradeMode, lv = LotsVirtual;
	int ph_diff = LotsPhantom - LotsVirtual;
	
	// close all virtual trades, this asset. No orders sent to broker.
	setf(TradeMode,TR_PHANTOM);
	for(open_trades){
		if(strcmp(TradeAsset,Asset)) continue;
		if(!TradeIsVirtual) continue;
		resf(ThisTrade->flags,TR_NET);
		exitTrade(ThisTrade);
		setf(ThisTrade->flags,TR_NET);
	}
	LotsVirtual = 0; LotsPhantom = ph_diff;
	if(!pos){
		force_pool_order();
	}
	else{
		resf(TradeMode,TR_PHANTOM);
		Lots = abs(pos);
		if(pos>0)
			enterLong();
		else if(pos<0)
			enterShort();
	}
	TradeMode = tm;
}


EDIT: Don't use this code, it's garbage. I need to give it one more try.

Last edited by AndrewAMD; 08/12/19 19:11. Reason: no good
Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) [Re: AndrewAMD] #481922
11/20/20 12:40
11/20/20 12:40
Joined: Aug 2018
Posts: 95
O
OptimusPrime Online
Junior Member
OptimusPrime  Online
Junior Member
O

Joined: Aug 2018
Posts: 95
Hi Andrew: Do you have a new version of this ? Thanks so much!
Optimus


Thanks so much,

OptimusPrime

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) [Re: AndrewAMD] #481924
11/20/20 18:54
11/20/20 18:54
Joined: Feb 2017
Posts: 1,129
Chicago
AndrewAMD Online OP
Serious User
AndrewAMD  Online OP
Serious User

Joined: Feb 2017
Posts: 1,129
Chicago
Use enterTrade(). This feature was added to Zorro after I made this request.

Another comparable new feature is the brokerCommand GET_TRADES. It deletes all of your Zorro-side trades and replaces them all with detected trades on the broker plugin. Obviously, your broker plugin must support this feature.


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1