TradeProfit while TradeIsClosed

Posted By: Sphin

TradeProfit while TradeIsClosed - 09/28/15 23:06

I've seen other codes in here where it should work, but:

Code:
int TradeLong() {
   if (TradeIsClosed) {
      print(TO_LOG,"\nTradeProfit: %.2f",TradeProfit);
   }
}

...
enterLong(TradeLong);
...


produces:

Code:
[668: Mon 06.01.14 15:45]  +1 +0 2/0
TradeProfit: 0.00
[EUR/USD::L6502] Target 1@1.3639: +0.89 at 15:47


Is it normal that TradeProfit is no more available when the trade is closed?

Thanks, Sphin
Posted By: jcl

Re: TradeProfit while TradeIsClosed - 09/29/15 10:35

If the trade were really closed, TradeLong would not be executed anymore and thus nothing would be printed. I wonder why you get a printout at all. For the parameters of closed trades you normally need a for(all_trades) loop.

Can you post that script or send it to support? I'd like to check where this mysterious print comes from.



Posted By: Sphin

Re: TradeProfit while TradeIsClosed - 09/29/15 14:37

It's a bit crazy. If I'm right, the use of the TMF should not change anything concerning trading bevaviour, or does it? I thought to use it for documentation purposes only. But if I take the simple script

Code:
int TradeLong() {
   if (TradeIsClosed) {
      print(TO_LOG,"\nTradeProfit: %.2f",TradeProfit);
   }
}

function run()
{
        set(LOGFILE+TICKS)
	vars Price = series(price());
	vars Trend = series(LowPass(Price,500));
	
	Stop = 4*ATR(100);
	TakeProfit = 10*PIP;

	if(valley(Trend) && NumOpenLong == 0) 
		enterLong(TradeLong);
}



and run it with "enterLong(TradeLong)" and then compare it with "enterLong()" without the TMF, the behaviour of the script changes and it does really strange things when using the TMF. BTW: TakeProfit seems to be necessary to provoke the printing of TradeProfit to the Log.
Posted By: jcl

Re: TradeProfit while TradeIsClosed - 10/01/15 11:58

The trade is not yet closed. If it were closed, the TMF would not run anymore. The profit of trades is only known _after_ they are really closed, so you can retrieve it in a for(all_trades) loop.

The last run of a TMF happens between hitting the stop and closing the trade. This is here the case. So at that time the trade is still open, despite the triggered stop. I suppose its profit is therefore still unknown, that's why you get no value. I can also not see with your script any difference with and without TMF. In both cases the end profit is +92 pips.
Posted By: Sphin

Re: TradeProfit while TradeIsClosed - 10/01/15 12:59

That's strange. For comparism reason I added to my script:

Code:
LookBack=200;
StartDate = 2009;
EndDate = 2015;



From the select box I choose EUR/USD and ran one test with

enterLong(TradeLong);

and Zorro said: Annual +20% +122p

and then I ran one test with

enterLong();

and Zorro said: Annual +5% +31p

In my opinion this is a difference. My Zorro version is 1.34.1.
Posted By: jcl

Re: TradeProfit while TradeIsClosed - 10/01/15 13:15

Are you using the TICKS flag? Try 1.36 and let me know if you then still have the problem. There is a bug listed for 1.34 with the TICKS flag and TMFs - that might be the reason of your difference. If you still have the same problem with 1.36, please let me know - I'll then check that again.

- Update: I just notice a bug in your printf statement - you're printing a float instead of a var. So your printout can not work anyway - see remarks under "Trade Mangement Functions" in the manual.
Posted By: Sphin

Re: TradeProfit while TradeIsClosed - 10/01/15 13:40

Yes, I used ticks and indeed 1.36 shows "Annual +6% +40p" at the end of both versions. Must be the bug described.
Posted By: jcl

Re: TradeProfit while TradeIsClosed - 10/01/15 13:42

It's still strange because I got +92p with your script. Or have you changed something, f.i. downloaded newer prices?
Posted By: Sphin

Re: TradeProfit while TradeIsClosed - 10/01/15 15:45

Okay, if I trade exactly the script from entry #3 in this thread (with a semikolon at the end of 'set(LOGFILE+TICKS)' of course) without the modifications from entry #5 and using the EURUSD_X.bar files from the zip archive of 1.36 I get 'Annual +21% +83p' both with and without the TMF. It's really near to your +92p but anywhere in between there are 9 PIPs lost ...
Posted By: Sphin

Re: TradeProfit while TradeIsClosed - 10/08/15 20:47

From the manual:

Quote:
TradeProft: The current profit or loss of the trade in units of the account currency, including costs such as spread, rollover, slippage, and commission.


In this context your statement

Quote:
The profit of trades is only known _after_ they are really closed


confuses me a little because I unterstand the manual's "current" as if TradeProfit shows the actual profit of a trade at any time during the trade. What is right?


And concerning

Quote:
Update: I just notice a bug in your printf statement - you're printing a float instead of a var.


I think I tried to print a var. From the manual:

Quote:
For printing float variables with the %f placeholder, typecast them to (var).


Should I better use

Code:
print(TO_LOG,"\nTradeProfit: %.2f",(var)TradeProfit);



if TradeProfit is a float?
Posted By: DdlV

Re: TradeProfit while TradeIsClosed - 10/09/15 00:47

Hi Sphin. I have played with TradeProfit and yes, TradeProfit is updated with each tick; and yes, (var)TradeProfit works for printing.

Re. the former, however, if you're after the final TradeProfit, you'll have to wait until TradeIsClosed, and per jcl's comments in a for(all_trades) loop at the Bar, not in the TMF.

Regards.
Posted By: Sphin

Re: TradeProfit while TradeIsClosed - 10/09/15 19:04

Thanks DdlV!

But again I have this issue of different results with and without a TMF that should not change anything but only writing TradeProfit to the log. I tried it with Zorro 1.36.4 on 2 different PCs (one real, one VPS) with 2 different OS's (Windows 10 Home and Windows Server 12) and get the same results. I hope anyone, best jcl, can retrace this fact because its very confusing for me.

The code:

Code:
int TradeLong() {
   
   if (TradeIsNewBar) // not printing every tick only once per bar
      print(TO_LOG,"\nTradeProfit: %.2f",(var)TradeProfit);
   
}

function run()
{
   set(PLOTNOW+LOGFILE+TICKS);
	vars Price = series(price());
	vars Trend = series(LowPass(Price,500));
	
	Stop = 4*ATR(100);
	TakeProfit = 10*PIP;

	if(valley(Trend) && NumOpenLong == 0) 
		enterLong();
}




In this version without using the TMF I get always following result:

Annual +23% +93p

If you look at the 1st trade:

[EUR/USD::L5601] Long 1@1.3771 Risk 9$ p at 06:00
[757: Wed 17.02.10 07:00] +0 -2 0/1
[758: Wed 17.02.10 08:00] +0 +0 1/0
[EUR/USD::L5601] Target 1@1.3781: +0.82 at 08:05

It is not exciting, the trade behaves like expected, Zorro exits reaching the TakeProfit at 1.3781 after 10 PIPs.


Now I change:

-- enterLong();
++ enterLong(TradeLong);


a) In most cases, I get the following results:

Annual Loss: -29p

Let's now Look at its 1st trade (the other trades behave analogously):

[EUR/USD::L5601] Long 1@1.3771 Risk 9$ p at 06:00
TradeProfit: 0.05
[EUR/USD::L5601] Exit 1@1.3772: +0.05 at 06:00

I don't know why Zorro exits at 1.3772 while TakeProfit = 10*PIP and in this case this event would be logged as "Target" not as "Exit".


b) Sometimes (but very seldom), if I change:

-- enterLong();
++ enterLong(TradeLong);

in SED, click 'Save' and 'Test' without restart of Zorro:

Annual +23% +93p (same as without TMF), but then take a look at the logged trades:

e.g. 1st trade:

[EUR/USD::L5601] Long 870351@1.3771 Risk 8000543$ p at 06:00
[757: Wed 17.02.10 07:00] +0 -1356395 0/1
[758: Wed 17.02.10 08:00] +0 +326559 1/0
[EUR/USD::L5601] Target 870351@1.3781: +713160$ at 08:05

Funny! laugh The other trades behave the same way and TradeProfit is not written to Log.
Posted By: nanotir

Re: TradeProfit while TradeIsClosed - 10/10/15 21:25

I also understood TradeProfit as the current profit of the trade. I am using it to change the trailing while it is open in a TMF function. Not sure now, if I am using it right tongue
Posted By: Sphin

Re: TradeProfit while TradeIsClosed - 10/11/15 01:36

It seems that TradeProfit indeed does it and if it was logged it was also logged with the right value although during a TMF obviously something happens that should not happen and leads to completely strange results.
© 2024 lite-C Forums