Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (degenerate_762, AndrewAMD), 877 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 2 1 2
Re: Trades closing themselves?! (and opening in odd places) [Re: jcl] #467041
07/13/17 09:35
07/13/17 09:35
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
Are you referring to the examples I posted in post #467034 ?

They were correctly formated when they were typed in and posted but the back slashes were removed by the forum system. PCZ brought my attention to it last week. It seems to be only when test is formatted as code.

PS: when I say they print for ~2 months I mean in simulation time. Not real world time. I haven't even had Zorro installed for that long yet tongue


Last edited by BobbyT; 07/13/17 09:42.
Re: Trades closing themselves?! (and opening in odd places) [Re: BobbyT] #467042
07/13/17 09:44
07/13/17 09:44
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
I meant the snippets of code that you posted in the first post of this thread - they all look fine to me, including the printf statements, only that INITRUN section does not look right, but that depends on the rest of your script. Of course I don't know what you intend with that system, so I cannot judge if the trades do what they should.

Re: Trades closing themselves?! (and opening in odd places) [Re: jcl] #467043
07/13/17 10:10
07/13/17 10:10
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
The INITRUN was put in place to initialise some global variables for oter parts of the code to use. They never change and are non-asset dependent so I thought it was a good place to put them.

As for the rest of the code, I thought it looked fine too. But, there are trades closing before the end of the simulation or reaching TP. There are also trades (seemingly) opening at the wrong price.

In order to track down the problem I employed many more printf statements than what is in the first post. In fact, I rewrote the entire srcipt and am now utilising a TMF which prints trade attributes at entry, open and close.
To get around not being able get the entry time of a pending trade once it has opened and the fact trades are not assigned a trade number until they are opened, I utilised the TradeVariable[] slots to store a unique id and the entry time of the trade.

Here's the code. It's a bit longer but I think it's closer to how the platform is intended to be used (code wise):
Code:
//defines
#define myID 	TradeVar[0]	//create a slot in TRADE* to keep a trade varaible
#define myDate	TradeVar[1]
#define M 1000					//length of array to fill with random numers
#define N 9999					//random numbers range from 0-N	 

//globals
if(is(INITRUN))
{
	//indicator variables
	var 			splitBy 	= 6;
	var 			sixthsLen 	= 1682;
	//trade variables
	static var 	        printLim        = 0;	//control switch to stop TMF constantly printing when TradeIsPending is true
	static var 	        tradeNum        = 0;	//place holder to keep track of the number of trades opened/closed
	//random number generator variables
	int 			in ;
	int 			im 		= 0;
	char 			is_used[N] 	= { 0 };
	static var 	        vektor[M];
}

//TMF of sorts
int ManageTrades(var tz)
{
	//static var myID;
	static string type;
	
	if(TradeIsPending && printLim == 0)
	{
		//printf("#nTrade opened %02d.%02d.%02d %02d.%02d", day(TradeTime), month(TradeTime), year(TradeTime)-2000, hour(TradeTime), minute(TradeTime));
		//myID = 0;
		myID = vektor[tradeNum];
		var myYear = (year()-2000)*1000000;
		var myMonth =  month()*10000;
		var myDay = day()*100;
		var myHour = hour();
		myDate = myYear+myMonth+myDay+myHour;
								
		if(TradeIsLong) { type = "Long"; /*printf("Test: %s", type);*/}
			else { type = "Short"; }
				
		printf("n%s trade %.0f pending @ %.5f, TP @ %.5f, TZ @ %.5f", type, (var)myID, (var)TradeEntryLimit, (var)TradeProfitLimit, (var)tz);
		printLim = 1;
		printf("nTradeNum = %.0f, ID should be %.0f", (var)tradeNum, vektor[tradeNum]);
                tradeNum += 1;
	}
	
	if(TradeIsOpen && TradeTime <= 0)
	{	
		printf("n%s trade %.0f opened @ %.5f, TP @ %.5f", type, (var)myID, (var)TradeEntryLimit, (var)TradeProfitLimit);	
		printf("#nTrade opened %.0f", /*myDateTime(TradeBarOpen)*/(var)myDate); //TradeTime no good
	}
	
	if(TradeIsClosed)
	{
		printf("n %s trade %.0f closed @ %.5f, TP @ %.5f", type, (var)myID, (var)TradePriceClose, (var)TradeProfitLimit);		
	}
	return 0;
}


function main()
{
	//generate an arary of random numbers to use for trade IDs
	//from: https://stackoverflow.com/questions/1608181/unique-random-numbers-in-an-integer-array-in-the-c-programming-language
	for (in = N - M; in < N && im < M; in++) 
	{
	  int r = rand() % (in + 1); /* generate a random number 'r' */
	
	  if (is_used[r])
	    /* we already have 'r' */
	    r = in; /* use 'in' instead of the generated number */
	  
	  vektor[im++] = r + 1; /* +1 since your range begins from 1 */
	  is_used[r] = 1;
	}
}
function run()
{
	//flags
	set(PLOTNOW);
	//set(TICKS);
	
	//////Variables/////////////////////////////////////////
	//system variables
	StartDate 	= 20100501;
	EndDate 		= 20110201;
	EntryTime 	= 10000000;
	//Verbose 		= 7;
	
	//trade variables
	var 	TP 			= 50*PIP;
	var 	buffer 		= 10*PIP;	
	////////////////////////////////////////////////////////
		
	//set up price/level series for indicator///////////
	vars sHH			= series(HH(sixthsLen));						//highest high series
	vars sLL			= series(LL(sixthsLen));						//lowest low series
	vars sRange		= series(sHH[1]-sLL[1]);						//market range series
	vars sUpperTZ 	= series(sHH[1] - ((sHH[1]-sLL[1])/6));	//uper trade zone series
	vars sLowerTZ 	= series(sLL[1] + ((sHH[1]-sLL[1])/6));	//lower trade zone series
	vars Price 		= series(priceClose()); 						//close series
	
	//////Trade Criteria////////////////////////////////////
	//short entry
	if(crossOver(Price, sUpperTZ))
	{
		Entry = sUpperTZ[1] - buffer;
		TakeProfit = (sUpperTZ[1] - buffer) - TP;
		printLim = 0;		
		enterShort(ManageTrades, sUpperTZ[1]); 
		//printf("nprice = %.5f, sixthsHigh = %.5f, pending price = %.5f", price(), sUpperTZ[1], sUpperTZ[1] - buffer);	
		//printf("nFresh upperTZ calc to %.5f and pending price is %.5f", HH(sixthsLen) - ((HH(sixthsLen)-LL(sixthsLen))/splitBy), (HH(sixthsLen) - ((HH(sixthsLen)-LL(sixthsLen))/splitBy)) - buffer);
	}
	
	//long entry
	if(crossUnder(Price, sLowerTZ))
	{
		Entry = sLowerTZ[1] + buffer;
		TakeProfit = (sLowerTZ[1] + buffer) + TP;
		printLim = 0;
		enterLong(ManageTrades, sLowerTZ[1]);
		//printf("nprice = %.5f, sixthsLow = %.5f, pending price = %.5f", price(), sLowerTZ[1], sLowerTZ[1] + buffer);		
		//printf("nFresh lowerTZ calc to %.5f and pending price is %.5f", LL(sixthsLen) + ((HH(sixthsLen)-LL(sixthsLen))/splitBy), (LL(sixthsLen) + ((HH(sixthsLen)-LL(sixthsLen))/splitBy)) + buffer);
	}
	////////////////////////////////////////////////////////

	//plot some stuff 
	plot("Lowest", sLL, 0, BLACK);
	plot("Highest", sHH, 0, BLACK);
	plot("upperTZ", sUpperTZ, 0, RED);
	plot("lowerTZ", sLowerTZ, 0, RED);
}




With this new code there are three problems:
1) TradeVariables[] do not seem to always be set. When one fails to be set, so does the other. Which one goes first and why I dno't know
2) Trades are closing before the end of the simulation. Nowhere in the code are there exit orders sent
3) Trades are opening at the wrong price. These trades seem to be linked to the TradeVariables[] dropping their values or not setting them.

Cheers,
BobbyT

Re: Trades closing themselves?! (and opening in odd places) [Re: BobbyT] #467044
07/13/17 10:26
07/13/17 10:26
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Ok. If I understood you right, your initial problem was that your trades close too early. And attempts to fix this by adding more code brought you instead more new problems.

For fixing the closing, I don't see what the problem is. Just look in the log, check why and when a trade closes, and if it's no good, change your TP or whatever else caused the closing. This is not a script problem but an optimization. Only real script problems require debugging.

If you don't understand what your trades are doing, make the script simpler, not more complicated. If the TMF gave you three new problems and is not needed anyway, delete it. Do not add new functions until you have understood what the current functions do. Also delete the if(INITRUN) line and the brackets - they probably do no harm, but are nonsense. if() is a conditional branch in a function, not a global variables section.

Re: Trades closing themselves?! (and opening in odd places) [Re: jcl] #467052
07/13/17 17:24
07/13/17 17:24
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
There were two initial problems: trades closing unexcectedly and trades opening at incorrect prices.

Your initial response was to reduce the code and follow individual trades in the log to see what the issue is.

I deleted everything then rewrote and rechecked every piece of code by either plotting the results of each chunk on the chart or outputting the results of calculated values, entry prices and takeprofit levels to the log.

Even with the most basic of code, and regardless of what takeprofit and buffer limits are set, if an attempt to make a trade in a certain region in history is made, the trades go haywire (they somehow close or their order price is corrupted).

In order to track the trades, print statements reporting order time, order price, takeprofit levels and the relevant indicator level were added pretty much everywhere prices were calcualted or orders were sent. This is when I found print trades stopped being printed to the log after a repeatedly observable period (though this period differs instrument to instrument). Also, this is where I tried printf(TO_LOG,"nStuff") which results in runtime errors and printf("#n") which also results in dropped print statements.

Enter the TMF. To get around the print statement issue, and to ensure the absolute most correct trade specific data was reported, I employed a TMF that pulls information from the TRADE* of each trade.
Upon sending the pending order, the TradeEntryLimit, TradeProfitLimit and the indicator value at the time of sending the trade are reported by the TMF. As TradeBarOPen and TradeTime for pending orders are reset once the trade is opened, a integer representation of the order send time is calculated and added to #define myDate TradeVar[1]. Also seeing as pending trades do not have a TradeID, a non-repeating random number generator was added (code in is main() ). Trades are assigned a number form the random number array based on total orders sent (manually iterated with each order entry call) and stored in #define myID TradeVar[0].
At order send, the TMF sends the previously generated trade ID (myID), the open price (TradeEntryLimit) and take profit prices (TradeProfitLimit) and the stored previously calculated order send date (myDate).
At order close, the TMF sends again myID, TradeEntryLimit and TradeProfitLimit.

In this way, the TMF replaces allows tracking of trades at stages of their life when they are basically untrackable and allows observation of the time elapsed between sending the pending order and when the order is opened.

The TMF was validated on a small historical period, limiting trading to one open/pending position at a time and then multiple positions, with everything behaving as it should.

However, testing the new code over the original period which was causing problems reveals the three things I previously mentioned. Trades close themselves out unexpectedly, trades open at incorrect prices and printf statements (from the TMF) stop being pushed to the log.
What is interesting, and this is where the TMF comes into it's own, is that by inspecting the log and following the erroneous trades, you can see that at some point between sending the trade and opening the trade, the TRADE* TradeVar[0] and TradeVar[1] lose their values. This does not happen on every trade. There also is no fixed period of time that must elapse between trade send and trade open for these values to be 'lost'.

Regardless of whether using printf on its own or using a TMF to report trade information, the trade distribution and trade 'errors' are the same. I have employed every trick I can think of from my experience with R and MQL4 in order to capture the correct data and ensure the correct data is reported. I have rebuild and validated the code piece-by-piece and still cannot find the problem. I have done extensive coding in R (have run a number of bioinformatics projects along with various financial hacking projects) and have coded countless indicators and strategies in MQL4. A few years back I even fixed the R-MT4 bridge after it was broken by an update to MQL4. But alas, I cannot find what the issue is with this code.

I'm sorry for the long post, but I felt that you should have an up-to-date report on what it is that I've done and how I arrived at the code as it is now. I hope I have demonstrated I am not looking for handouts here but have spent considerable time and energy to find the cause of the issue, only to be no closer than I was at the time of the original post. Having said that, I have become a lot more familiar with various aspects of lite-C and Zorro which I was totally unfamiliar with before so it hasn't been a complete waste of time tongue

Cheers,
BobbyT

Edit: This is all observable on AUDCAD using the period set in the script. Other instruments (not all) display the same behavior at some point in history.

Last edited by BobbyT; 07/13/17 17:27.
Re: Trades closing themselves?! (and opening in odd places) [Re: BobbyT] #467053
07/13/17 17:57
07/13/17 17:57
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Sorry, I still do not really understand the problem. Yes, I understand that your script does not work and is trading haywire, but I do not see why it is so difficult to fix it. It's only a few lines.

Can you show, as a first step, in the log what the problem is?

Re: Trades closing themselves?! (and opening in odd places) [Re: BobbyT] #467054
07/13/17 18:28
07/13/17 18:28
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
Addendum: The control statements in the TMF were not sufficient. They have been imporved and myID and myDate are no longer dropped (or accidentally rewritten) and I am able to fully track individual trades now. Errors are bound to happen at 3 in the morning smirk

This allowed me to find the issue causing the trade closures (at least in the most recent script). I had forgotten to set hedge = 2. All trades now close as expected. This was revealed by setting verbose = 7 which reports trades being 'reverse' as the reason for closure.

This also allowed me to see that trades I thought were opening at incorrect prices were merely opening some time after they are placed. I did not realise that plot() only marks the start point of trades from when they are opened not when they are placed.

So all problems fixed. Now onto bigger things such as making the strategy run on multiple timeframes. Time to get acquainted with 'algo' functions laugh

Thanks for all your help on this JCL. This little exercise has been more than informative and I think I'm now familiar enough with lite-C/Zorro that you should be hearing from me (as OP) a bit less often tongue

Cheers,
BobbyT

Last edited by BobbyT; 07/13/17 18:33.
Page 2 of 2 1 2

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1