enterTrade() with TMF

Posted By: SBGuy

enterTrade() with TMF - 04/21/22 16:21

Hi, trying to use enterTrade() with a TMF for the first time.
https://zorro-project.com/manual/en/buylong.htm

If the TMF function name is assignedTMF(), do I simply make T.manage = (void *) assignedTMF() like below?

It compiles without any errors.

Thanks!

Code
				TRADE T; memset(&T,0,sizeof(T)); 
				T.tEntryDate = wdate(0);	
				T.flags = TR_OPEN|TR_LONG;		
				strcpy(T.Skill,Asset); // Asset
				T.nID = 1000000+TradeID;
				T.nLots = TradeLots*Multiplier;
				T.fStopLimit = Stop;
				T.fTrailLimit = Trail;
				T.fEntryPrice = TradeUnderlying;	
				T.manage = (void *) assignedTMF();
				enterTrade(&T);

Posted By: AndrewAMD

Re: enterTrade() with TMF - 04/21/22 16:33

Why are you invoking assignedTMF? Don't invoke it (omit the parentheses) so that you return the pointer to the function itself rather than the return of the function invocation (erroneously cast to a pointer).

You can always test your TMFs to make sure they work, too.
Posted By: SBGuy

Re: enterTrade() with TMF - 04/21/22 16:47

I'm always bad with pointers :-)

Thanks for the help Andrew!
Posted By: SBGuy

Re: enterTrade() with TMF - 04/21/22 17:13

Ok, I tried these variations

T.manage = (void *) assignedTMF;
T.manage = (void *)assignedTMF;
T.manage = assignedTMF;

The trade was entered, but assignedTMF was not called.

My test TMF is just a print statement.

int assignedTMF()
{
printf("\n[%s] assignedTMF called!",Asset);
return 0;
}

The STRUCT member definition in trading.h is:

void *manage; // trade management function pointer
Posted By: AndrewAMD

Re: enterTrade() with TMF - 04/21/22 19:26

So it turns out you have to configure the TMF after you call enterTrade.
Code
int do_tmf(){
	printf("#\ndo_tmf() invoked");
	return 0;
}
function run() {
	BarPeriod = 1440;
	set(LOGFILE);
	Verbose = 7|DIAG;
	LookBack = 0;
	assetList("AssetsIB");
	asset("DIS"); // DIS.t6,  Nov 1999 thru Feb 2022
	if(Init || is(LOOKBACK)) return;
	if(Bar==20){
		printf("#\nEntering trade...");
		TRADE T; memset(&T,0,sizeof(T)); 
		T.tEntryDate = wdate(0);	
		T.flags = TR_OPEN|TR_LONG;		
		// strcpy(T.Skill,Asset); // Asset
		// T.nID = 1000000+TradeID;
		// T.nLots = TradeLots*Multiplier;
		T.nLots = 1;
		// T.fStopLimit = Stop;
		// T.fTrailLimit = Trail;
		// T.fEntryPrice = TradeUnderlying;	
		T.fEntryPrice = priceC();	
		// T.manage = (void *) do_tmf;  // this does not work.
		TRADE* p = enterTrade(&T);
		if(p){
			printf("#\nTrade entry confirmed.");
			p->manage = (void *) do_tmf; // But this does.
		}
		else{
			printf("#\nTrade entry failed.");
		}
	}
	if(Bar==30){
		printf("#\nExiting long...");
		exitLong();
	}
	if(Bar==40){
		quit("All done.");
	}
}

/*  OUTPUT: 


Load AssetsIB
History DIS.t6
DIS 5609 ticks read
1289(1872) bars 2017-01-03..2022-02-14 generated
DIS: 0..1288, 2017-01-03..2022-02-14
V 2.475 on Thu 22-04-21 14:22:33 (Zorro S Subscription)
Test: test_entertrade1 DIS 2017..2022
Assets AssetsIB

[1: Wed 17-01-04 15:40] 102.10/102.92\101.73/102.87 -0.03
[2: Thu 17-01-05 15:40] 102.61/103.04\102.24/102.81 -0.03
[3: Fri 17-01-06 15:40] 103.42/104.69\103.16/104.34 -0.03
[4: Mon 17-01-09 15:40] 103.96/104.20\103.51/103.75 -0.03
[5: Tue 17-01-10 15:40] 103.31/104.19\102.78/103.77 -0.03
[6: Wed 17-01-11 15:40] 103.92/104.83\103.77/104.78 -0.03
[7: Thu 17-01-12 15:40] 103.52/104.36\102.86/102.95 -0.03
[8: Fri 17-01-13 15:40] 102.60/103.74\102.35/103.46 -0.03
[9: Tue 17-01-17 15:40] 103.32/104.29\103.07/103.37 -0.03
[10: Wed 17-01-18 15:40] 102.38/103.78\102.26/103.55 -0.03
[11: Thu 17-01-19 15:40] 103.33/103.41\102.52/102.71 -0.03
[12: Fri 17-01-20 15:40] 102.78/103.23\102.62/103.08 -0.03
[13: Mon 17-01-23 15:40] 102.77/103.10\102.03/102.56 -0.03
[14: Tue 17-01-24 15:40] 102.64/103.46\102.52/103.31 -0.03
[15: Wed 17-01-25 15:40] 103.62/103.80\103.40/103.44 -0.03
[16: Thu 17-01-26 15:40] 103.60/104.00\103.38/103.46 -0.03
[17: Fri 17-01-27 15:40] 103.87/104.74\103.55/104.65 -0.03
[18: Mon 17-01-30 15:40] 105.18/106.32\104.32/106.22 -0.03
[19: Tue 17-01-31 15:40] 105.88/106.08\105.30/105.94 -0.03
[20: Wed 17-02-01 15:40] 106.00/107.22\105.96/106.56 -0.03
Entering trade...
[DIS::L02002] Enter Long  1@106.56
Trade entry confirmed.
do_tmf() invoked

[21: Thu 17-02-02 15:40] -0.70 -0.70 0/1 106.47/106.79\105.84/105.90 -0.03

Thursday 17-02-02  Profit +0.41 ----
[DIS::L02002] -0.70 s0.0 c105.9 e106.6

do_tmf() invoked

[22: Fri 17-02-03 15:40] -1.00 -1.00 0/1 106.18/106.42\105.52/105.60 -0.03

Friday 17-02-03  Loss -0.30 ----
[DIS::L02002] -1.00 s0.0 c105.6 e106.6

do_tmf() invoked

[23: Mon 17-02-06 15:40] -1.70 -1.70 0/1 105.24/105.46\104.65/104.90 -0.03

Monday 17-02-06  Loss -0.70 ----
[DIS::L02002] -1.70 s0.0 c104.9 e106.6

do_tmf() invoked

[24: Tue 17-02-07 15:40] -2.24 -2.24 0/1 104.98/105.06\104.14/104.36 -0.03

Tuesday 17-02-07  Loss -0.54 ----
[DIS::L02002] -2.24 s0.0 c104.4 e106.6

do_tmf() invoked

[25: Wed 17-02-08 15:40f] -2.24 -2.24 0/1 104.90/106.68\104.34/104.36 -0.03

Wednesday 17-02-08  Profit 0 ----
[DIS::L02002] -2.24 s0.0 c104.4 e106.6

do_tmf() invoked

[26: Thu 17-02-09 15:40] -1.77 -1.77 0/1 104.43/105.17\104.36/104.83 -0.03

Thursday 17-02-09  Profit +0.47 ----
[DIS::L02002] -1.77 s0.0 c104.8 e106.6

do_tmf() invoked

[27: Fri 17-02-10 15:40] -1.99 -1.99 0/1 104.69/105.12\103.96/104.61 -0.03

Friday 17-02-10  Loss -0.22 ----
[DIS::L02002] -1.99 s0.0 c104.6 e106.6

do_tmf() invoked

[28: Mon 17-02-13 15:40] -1.62 -1.62 0/1 104.31/105.22\104.26/104.98 -0.03

Monday 17-02-13  Profit +0.37 ----
[DIS::L02002] -1.62 s0.0 c105.0 e106.6

do_tmf() invoked

[29: Tue 17-02-14 15:40] -0.97 -0.97 0/1 105.12/105.63\104.87/105.63 -0.03

Tuesday 17-02-14  Profit +0.65 ----
[DIS::L02002] -0.97 s0.0 c105.6 e106.6

do_tmf() invoked

[30: Wed 17-02-15 15:40] -1.11 -1.11 0/1 105.23/105.97\105.13/105.49 -0.03

Wednesday 17-02-15  Loss -0.14 ----
[DIS::L02002] -1.11 s0.0 c105.5 e106.6

Exiting long...
Exit  DIS  (0)
[DIS::L02002] exit
[DIS::L02002] Sell 1@105.50: -1.10 Bid 105.47 at 15:40:00 Wed
Opn 106.56 Cls 105.50 Spr 0.03 Slp 0.01 Rol 0.00 Com 0.01 Lif 10 Net 1
Max Loss 0.00 -> 1.10
Max Bars 0 -> 30-20
do_tmf() invoked

[31: Thu 17-02-16 15:40] 105.64/106.15\105.40/106.00 -0.03
[32: Fri 17-02-17 15:40] 105.55/105.80\104.86/105.37 -0.03
[33: Tue 17-02-21 15:40] 105.26/105.52\105.12/105.33 -0.03
[34: Wed 17-02-22 15:40] 105.02/105.58\104.87/105.43 -0.03
[35: Thu 17-02-23 15:40] 105.60/105.79\104.92/105.06 -0.03
[36: Fri 17-02-24 15:40] 104.95/105.62\104.45/105.62 -0.03
[37: Mon 17-02-27 15:40] 105.25/105.77\105.15/105.54 -0.03
[38: Tue 17-02-28 15:40] 105.32/105.70\105.15/105.40 -0.03
[39: Wed 17-03-01 15:40] 105.95/107.04\105.95/106.31 -0.03
[40: Thu 17-03-02 15:40] 106.27/106.47\105.75/105.88 -0.03
Loss -1.10$  MI -0.0180$  DD 2.24$  Capital 55.00$
Trades 1  Win 0.0%  Avg -110.1p  Bars 10
AR -0%  PF 0.00  SR 0.00  UI 0%  R2 0.41

All done.

*/
Posted By: SBGuy

Re: enterTrade() with TMF - 04/21/22 20:35

You're a genius! Worked like a charm.

Thanks Andrew!
Posted By: SBGuy

Re: enterTrade() with TMF - 04/30/22 18:02

Any idea, how I might add a trailing stop when using enterTrade?

Setting Stop and Trail before enterTrade() appears to have no effect.

Manually settting T.fStopLimit = TradeUnderlying-Stop works for a single stop.

But I can't seem to get a trailing stop to work.

Code
				Stop = 0.10*priceClose(0);
				Trail = 0.20*Stop;
			
				TRADE T; memset(&T,0,sizeof(T)); 
				T.tEntryDate = wdate(0);	
				T.flags = TR_OPEN|TR_LONG;		
				strcpy(T.Skill,Asset); // Asset
				T.nID = 1000000+TradeID;
				T.nLots = Lots;
				T.fStopLimit = priceClose(0)-Stop;
				// T.fStopDiff = -1*Stop;
				//T.fTrailDiff = -1*Trail;				
				T.fEntryPrice = priceClose(0);	
		
				TRADE* p = enterTrade(&T);
				if (p) {
					p->manage = (void *) assignedTMF;
				}

© 2024 lite-C Forums