[SOLVED]-Trades closing themselves?! (and opening in odd places)

Posted By: BobbyT

[SOLVED]-Trades closing themselves?! (and opening in odd places) - 07/12/17 02:38

Hi all,

OK, they are most likely not closing themselves but I can't put my finger on what the issue is.
The strategy is simple enough and based on price levels. Once price is inside the upper/lower zone, it places a sell/buy below/above the zone by a certain number of pips. Trades are meant to stay open until TP or the end of the test period.

The code:
Code:
//----globals
if(is(INITRUN))
{
	var sixthLen 	= 1682;
	var splitBy 	= 6;
	//var TP 			= 50*PIP; //takeprofit in pips converted to price value
	//var buffer 		= 10*PIP; //distance from market for pendings to be placed
	var tradeLim	= 2000;	
}

//----sixths functions
var sixthHigh(period, splitBy)
{
	return (HH(period) - ((HH(period)-LL(period))/splitBy));
}

var sixthLow(period, splitBy)
{
	return (LL(period) + ((HH(period)-LL(period))/splitBy));
}

//----order functions
function CheckForTradeOpportunity()
{
	var TP 			= 50*PIP; //takeprofit in pips converted to price value
        var buffer 		= 10*PIP; //distance from market for pendings to be placed
	
	vars Price = series(priceClose()); //create the price series for the selected asset
		
	EntryTime = 10000000;
			
	//check to see if we can enter long
	if(NumOpenLong + NumPendingLong < tradeLim) 
	{
		if(crossUnder(Price, sixthLow(sixthLen, splitBy))) //> need to change this to a comparative function or add another block for non cross events
		{
			enterLong(0, sixthLow(sixthLen, splitBy) + buffer, 0, TP);
			printf("nprice = %.5f, sixthsLow = %.5f, pending price = %.5f", price(), sixthLow(sixthLen, splitBy), sixthLow(sixthLen, splitBy) + buffer);		
		}	
	}
	
	//check to see if we can enter short
	if(NumOpenShort + NumPendingShort < tradeLim) 
	{
		if(crossOver(Price, sixthHigh(sixthLen, splitBy)))
		{
			enterShort(0, sixthHigh(sixthLen, splitBy) - buffer, 0, TP);
			printf("nprice = %.5f, sixthsHigh = %.5f, pending price = %.5f", price(), sixthHigh(sixthLen, splitBy), sixthLow(sixthLen, splitBy) + buffer);	
		}	
	}
}

//----main exectution block, comparable to OnTick()
function run()
{
	set(LOGFILE);
	BarPeriod = 60;
	LookBack = sixthLen;
	//PlotBars = 500;
	StartDate = 2012;
	EndDate = 2017;
	Hedge = 2; //allows full hedging, default = 0 -> no hedgintg	
	
	CheckForTradeOpportunity();
		
//--------------
	
	plot("Lowest", LL(sixthLen), 0, BLACK);
	plot("Highest", HH(sixthLen), 0, BLACK);
	plot("upperTZ", HH(sixthLen) - ((HH(sixthLen)-LL(sixthLen))/splitBy), 0, RED);
	plot("lowerTZ", LL(sixthLen) + ((HH(sixthLen)-LL(sixthLen))/splitBy), 0, RED);
	
	set(PLOTNOW);
}



Inspecting the resulting plot reveals there are trades closing at a loss before the simulation is over. I'm not sure why and it is likely some default global I'm not aware of despite having found and checked it in the manual. Any ideas?

Cheers,
BobbyT

EDIT: changed the start/end period to highlight another problem. There are trades being placed at what appears to be erroneous prices. Check the plot in the first half of July 2010. There is a trade that has opened at the peak of a small rally. This trade is well outside of the trade zone (both upper and lower zones). This is easily seen against the red channel which marks out the inner boundary of the trade zone.
I'm taken aback by the amount of teething problems I'm having switching over to Zorro. Having said that, the platforms I'm familiar with have massive user bases and chances are if you have a problem you can normally find info about it somewhere. Alas, Zorro is still growing. So I have to bother you poor sods if I need help. Sorry (these posts will get more interesting with time but at this stage I'm still learning to crawl before I can walk in Zorro land)
Posted By: jcl

Re: Trades closing themselves?! (and opening in odd places) - 07/12/17 16:41

I learned that prices cannot be erroneous since the market is always right laugh.

I don't know how you wanted your trades to close, but if they behave odd, I can not give another advice than find the reason and fix it. Check the prices and your entry/exit conditions in detail in the log. Follow a trade from start to end. Use the debugger and watch your variables. Once you've got used to examine and debug your scripts systematically, all those issues will be gone.

Posted By: BobbyT

Re: Trades closing themselves?! (and opening in odd places) - 07/12/17 18:34

Hi JCL,

That is always true. until it isn't laugh

That's exactly the thing, the trades are never meant to exit. It's a bad strategy but one that I though would allow me to become familiar with the platform.

After the finding that pending orders are deleted after one bar if they aren't filled, I thought maybe there was something that I had missed in the default global variables that may be causing the issue.

Regardless, I have added print statements which return the entry price and indicator price for each trade and the indicator price always seems to be correct. So I am at a loss as to what the issue is.

I will check out the debug mode to see if I can pick up the issue as it steps through the simulation. But...don't be surprised if you see me back here in the next couple of days whining about the same problem tongue

Cheers,
BobbyT

Posted By: BobbyT

Re: Trades closing themselves?! (and opening in odd places) - 07/12/17 20:52

Well, I'm back much sooner than anticipated.

I'm having problems with print statements not appearing in the log file.

They are entered to the log for ~2 months and then they just stop! Makes debugging pretty difficult if I can't get the information I need from the terminal.

Any suggestions as to what's going on? I'm all for helping myself but this is beyond what information I can get my hands on.

Cheers,
BobbyT
Posted By: BobbyT

Re: Trades closing themselves?! (and opening in odd places) - 07/12/17 22:52

Further to the above, you suggest tracking individual trades through the simulation.

How is that done before a pending trade is opened? Pending orders are not assigned a trade number until they are filled. In some instances (and this is where I think the problem trades on the plot are coming from), trades do not open for several months. It is impossible to trace them without having a trade number/ID

Cheers,
BobbyT
Posted By: Mithrandir77

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 00:47

BobbyT, you can try set(STEPWISE) and use print(TO_LOG) and/or print(TO_HTML) immediately after

Code:
enterLong(0, sixthLow(sixthLen, splitBy) + buffer, 0, TP);



for instance to log a long pending trade. Check

http://zorro-project.com/manual/en/printf.htm
and
http://zorro-project.com/manual/en/mode.htm
Posted By: BobbyT

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 02:22

Hi Mithrandir,

Thanks for the reply. I tried
Code:
printf(TO_LOG,"nStuff", SomeVariables);


but it throws a crash error (#111) when it tries to print those statements.

I tried replacing my desired statements with
Code:
printf(TO_LOG, "ntest message");


which results in the same crash error.

I had also previously tried short-cutting it to the log with
Code:
printf(TO_LOG, "#ntest message");


this runs, but results in messages not being printed to the log after a couple of months.

The above examples were appended immediately after the enterLong/Short calls.

Cheers,
BobbyT

EDIT: forgot about the forum editing out slashes from text. In all the examples there is a backslash before the 'n' (between the # and 'n' in the third example)
Posted By: jcl

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 06:56

Important advice for writing scripts:

When something does not work, find out why _before_ you attempt to fix it - not the other way around. When you wildly modify commands, you`ll end up with nonsense and crashes. There are plenty printf examples in the manual and in C books.

If you don't find your printf in the log, the right reaction is not `ugh, it doesn't work`, but `why is this part of my code not executed?`
Posted By: BobbyT

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 08:06

Hi JCL,

The above print statements were tested based on what is in the manual (with the backslashes missing due to the forum system).

The print statements are contained within a conditional that also sends the trade. For roughly two months both the trade is sent and the print statements make it to the log. After that, only the trade is sent.

This tells me the conditional is being met as the trades are still sending. But printf miraculously stops printing to the log. It does however print to the terminal.

Is the stack full? Do I have to dip my toes into memory allocation?

Cheers,
BobbyT
Posted By: jcl

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 09:19

Hmm. Is it so difficult to at least replicate the syntax of a command from 1000 examples in the manual? Printf is the most often used function in C. This prints to the window and to the log:

printf("\nStuff");

And this only to the log;

printf("#\nStuff");

You got it right in your first code, so why the problems now? But if you definitely can't get familiar with all those commands, syntax, and tiny quotation marks, the last resort would be contacting Support and let them program your script.
Posted By: BobbyT

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 09:35

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

Posted By: jcl

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 09:44

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.
Posted By: BobbyT

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 10:10

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
Posted By: jcl

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 10:26

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.
Posted By: BobbyT

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 17:24

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.
Posted By: jcl

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 17:57

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?
Posted By: BobbyT

Re: Trades closing themselves?! (and opening in odd places) - 07/13/17 18:28

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
© 2024 lite-C Forums