for(all_trades) inverse order

Posted By: MatPed

for(all_trades) inverse order - 01/24/18 11:01

Hi,
has anybody found a way to browse the trades in the inverse order? Last trades first?
I need to identify the last n trades that respect some conditions.

Thank you
Posted By: Dalla

Re: for(all_trades) inverse order - 01/24/18 13:08

Depends what exectly you are after.

These arrays contain the results for the 20 most recent trades. index 0 = most recent trade.

ResultLong[0] .. ResultLong[19]
ResultShort[0] .. ResultShort[19]

More info
http://zorro-trader.com/manual/en/winloss.htm
Posted By: MatPed

Re: for(all_trades) inverse order - 01/24/18 13:42

Thank You Dalla,
that was exactly my starting point, but it did not work.
I want to calculate the Rolling Expectancy Ratio. Basically I need the Result of the last N trades the mentioned arrays does not provide such information.

the for(all_trade) macro could be used, but unfortunately its sort the trades from the first opened one. Reversing a growing array is not trivial for my coding capabilities, so I was blessing for some help laugh.

Ciao
Posted By: jcl

Re: for(all_trades) inverse order - 01/25/18 07:40

Another macro could be defined for reverse order. Do you need it for all or only for closed trades?
Posted By: MatPed

Re: for(all_trades) inverse order - 01/25/18 09:12

Thank you,
For the specific task only closed would be enough. For general purposes I guess that all trades would be better. I know that this will be an easy task for you, but when you write it can you explain of how it works?

I've tried to read the for(all_trade) macro definition but, I admit, is quite obscure to me...

Ciao
Posted By: jcl

Re: for(all_trades) inverse order - 01/25/18 10:29

Ok, try this: get the latest version 1.74.8 and use for(closed_trades). It is undocumented, but is supposed to enumerate closed trades in backwards order. For aborting the loop, as you probably don't want to go through all trades, use break_trades; instead of break;.
Posted By: MatPed

Re: for(all_trades) inverse order - 01/25/18 11:23

ok, thank you. I guess that The code inside the loop has access to all trade variables. Am I right?
Posted By: Zheka

Re: for(all_trades) inverse order - 01/25/18 13:51

laugh
- Zorro seems to maintain one single list of all trades in a portfolio.
Will it make sense to add a field to Trade struct with a pointer to the previous trade of the same algo/asset component?
Then traversing such a linked list backwards for a component would be quicker, and such backwards looping is mostly needed for a component, not the full portfolio.
- break_trades for some reason does not work; break- does

It would really help if Zorro exposed a variable/pointer to the last closed trade of the current asset/algo combination (without asking a user to go into the for(trades)loop.
Or is there a way?
Posted By: MatPed

Re: for(all_trades) inverse order - 01/26/18 08:42

Hi Zheka,
I am fine with the For() approach I can filter and selecting the trades I need.
Did you already tried for(closed_trades)with version 1.78 and discovered a bug using break_trades ?

I guess that a specific exit from the loop is required in order to proper reset pointers and other stuff or the following for(closed_trades) will not work properly.

Please report your findings.

Thank You
Posted By: Zheka

Re: for(all_trades) inverse order - 01/26/18 09:57

Hi, MatPed,

Suggestion to extend a Trade struct was to improve speed and flexibility of looping - both internally by Zorro and/or for a user; with or without a for()macro.

Quote:
Did you already tried for(closed_trades)with version 1.78 and discovered a bug using break_trades ?
Yes, I did. A simple for(closed_trades)loop with a counter to print details of last 20 trades only printed for 1 trade with break_trades and all 20 with a normal break.
If more complex scenaria would work properly with a normal "break" - I did not test.
Posted By: MatPed

Re: for(all_trades) inverse order - 01/26/18 09:59

Thank you.
Can you post the coded used for the test?

Ciao
Posted By: Zheka

Re: for(all_trades) inverse order - 01/26/18 11:23

Here it is.
Quote:
if (is(EXITRUN))
{
int ii=0;
for(closed_trades)
{
ii++;
printf("# Asset: %s, Algo=%s, Trade Profit=%f, ii=%i",TradeAsset,TradeAlgo, TradeProfit,ii);

if (ii>20) break_trades;
}
}
Posted By: jcl

Re: for(all_trades) inverse order - 01/26/18 14:46

For the moment, better use

if(ii>20) { break_trades; }
Posted By: MatPed

Re: for(all_trades) inverse order - 01/29/18 23:39

Hi,
trying to build up an array with trade results for further calculation. My understanding is that I have to filter by Algo/Asset the trades in the loop. This is the code:

Code:
#define MaxRolling 50
var rer(int ii){
// rolling expectancy ratio: ((AvgWin / AvgLoss) * WinRate) – (1 – WinRate)

	bool debug = TRUE;
	var resultAA[MaxRolling];
	int i=0; for (i=0; i<MaxRolling; i++) resultAA[i] = 0; // Init the Array

	string myAsset = Asset;
	string myAlgo = Algo;
	i=0;
	for(closed_trades) {
		if( TradeIsPhantom &&  myAsset==TradeAsset && myAlgo==TradeAlgo) {
			resultAA[i] = TradeProfit/TradeUnits/PIP;
			if(debug) {
				print(TO_ANY, "n Asset: %s - ALGO: %s - #Trades: %d - PIP: %.2f", Asset, Algo, i, resultAA[i]);	
			}
			i++;	
			if (i >= ii) {break_trades;}
		}
}
	return .0;
}



nothing happens. Trade results ar printed if the last condition in the IF statement is removed. I do not understand what I am missing.

Thank you
Posted By: Dalla

Re: for(all_trades) inverse order - 01/30/18 03:50

Hmm, I'm thinking that bool is case sensitive perhaps?
Try
bool debug = true;
Posted By: MatPed

Re: for(all_trades) inverse order - 01/30/18 07:47

Nice try Dalla laugh but it does not change the behavior o the script.
the weird things is that the the error seems to be
&& myAlgo==TradeAlgo
if removed the array is printed.

by the way, I am calling the rer function just before of the enter command in workshop 6, for test purposes
Ciao
Posted By: Dalla

Re: for(all_trades) inverse order - 01/30/18 10:11

OK, instead of doing
Code:
myAlgo==TradeAlgo


try
Code:
strstr(myAlgo,TradeAlgo)



You should probably do this for TradeAsset as well.
Comparing strings with == and != is usually not a good idea.
Posted By: Zheka

Re: for(all_trades) inverse order - 01/30/18 10:15

This is because "strings" are elusive animals (constants?):-)

This works:

char* myAsset[16]; myAsset =strcpy(myAsset,Asset);
char* myAlgo[16]; myAlgo = strcpy(myAlgo,Algo);
....
if( !strcmp(myAsset,TradeAsset) && !strcmp(myAlgo,TradeAlgo))


Dalla's suggestion also works.
Posted By: MatPed

Re: for(all_trades) inverse order - 01/30/18 11:08

Dalla, Zheka. You are my heroes! Thank you
© 2024 lite-C Forums