Using a TMF to enter and exit at seconds and getting seconds==0

Posted By: Mithrandir77

Using a TMF to enter and exit at seconds and getting seconds==0 - 12/04/14 04:22

I made the following code by modifying workshop 4 so that it enters a trade after it receives confirmation that the trend is continuing, the problem is that inside the TMF tradeAtSeconds() I do printf("%d",seconds()) and it always prints 0 , if the barperiod is 1 shouldn't it be printing different seconds corresponding to the ocurrences of ticks inside 1 minute bars? Thanks for your help!

Code:
// Workshop 4: Trend Trading ///////////////////
#include <profile.c>

#define initialPrice TradeVar[0]
#define seconds TradeVar[1]

int tradeAtSeconds()
{
	seconds = second();
	printf("\nseg: %d\n",seconds);
	if (seconds > 50){	
	   printf("\nTrade %d exited cuz out of time at: %d/%d/%d %d:%d:%d\n",TradeID,day(),month(),year(),hour(),minute(),second());
		return 1;
	}
	if (TradeIsLong){		
		if (TradeIsNewBar)//first tick of a new 60 seconds-bar, I set the Entry Limit to 12 pips above initialPrice		
			TradeEntryLimit = initialPrice + 12 * PIP;			
		else
			if (!TradeIsOpen)
				if (TradePriceOpen >= TradeEntryLimit){
					printf("\nLong %d entered at: %d/%d/%d %d:%d:%d\n",TradeID,day(),month(),year(),hour(),minute(),second());			
					return 2+4;}
			else
				if (TradeProfit/TradeUnits/PIP >= 27){//close the trade at this very second
					printf("\nLong %d exited at: %d/%d/%d %d:%d:%d\n",TradeID,day(),month(),year(),hour(),minute(),second());
					return 1;
				}								
	}
	else{
		if (TradeIsNewBar)//first tick of a new 60 seconds-bar, I set the Entry Limit to 12 pips below initialPrice
			TradeEntryLimit = initialPrice - 12 * PIP;			
		else
			if (!TradeIsOpen)
				if (TradePriceOpen <= TradeEntryLimit){
					printf("\nShort %d entered at: %d/%d/%d %d:%d:%d\n",TradeID,day(),month(),year(),hour(),minute(),second());		
					return 2+4;}
			else
				if (TradeProfit/TradeUnits/PIP >= 27){//close the trade at this very second
					printf("\nShort %d exited at: %d/%d/%d %d:%d:%d\n",TradeID,day(),month(),year(),hour(),minute(),second());
					return 1;
				}	
	}
	return 4;
}

function run()
{
	BarPeriod = 1;
	set(TICKS);
	StartDate = 20140606;
	EndDate = 20140624;
	vars Price = series(price());
	vars Trend = series(LowPass(Price,500));
	
	Stop = 4*ATR(100);
	initialPrice = priceClose();
	seconds = 0;
	if(valley(Trend))
		enterLong(tradeAtSeconds());
	else if(peak(Trend))
		enterShort(tradeAtSeconds());

 //zoom in a certain date
//	PlotDate = 20140606;
//	PlotBars = 300;
//	ColorEquity = ColorDD = 0;
//	ColorUp = ColorDn = 0x00AAAAAA;
//	plot("Trend",Trend[0],0,BLACK);
//	set(PLOTNOW);
	

	PlotWidth = 800;
	PlotHeight1 = 300;
//	ColorWin = ColorLoss = 0;
//	plotTradeProfile(-50); // plot the trade profit distribution
	set(LOGFILE); // log all trades
}

Posted By: jcl

Re: Using a TMF to enter and exit at seconds and getting seconds==0 - 12/04/14 08:48

I don't know if this is the reason, but you're not printing "seconds" correctly. TradeVar is a var, not an int, so you must print it with %f, not %d.
Posted By: Mithrandir77

Re: Using a TMF to enter and exit at seconds and getting seconds==0 - 12/04/14 15:53

Originally Posted By: jcl
I don't know if this is the reason, but you're not printing "seconds" correctly. TradeVar is a var, not an int, so you must print it with %f, not %d.


Thanks for your response jcl. Indeed I changed the printf, now it is

seconds = second();
printf("\nseg: %f\n",seconds);

but it prints "seg: 0.000000" moreover look at what it prints when i invoke

printf("\nLong %d entered at: %d/%d/%d %d:%d:%d\n",TradeID,day(),month(),year(),hour(),minute(),second());

second() returns an int but it is always returning 0 so I think the problem is that second is bugged or the tmf is not being called every tick. Is the code of second() available to check?

Posted By: jcl

Re: Using a TMF to enter and exit at seconds and getting seconds==0 - 12/04/14 16:12

Did you trade your script at all? Or was it only a test?

The included files contain M1 data, that's minutes, no seconds. There are no tick data included.
Posted By: Mithrandir77

Re: Using a TMF to enter and exit at seconds and getting seconds==0 - 12/04/14 19:35

Originally Posted By: jcl
Did you trade your script at all? Or was it only a test?

The included files contain M1 data, that's minutes, no seconds. There are no tick data included.


Oh, my bad you are right. I was testing. So I would have to try with histdata tick data. But the file from histadata comes in the format yyyyMMdd hhmmmiliseconds bid ask (volume?):

20141102 170007143 1.25058 1.25098 0
20141102 170008393 1.25057 1.25097 0
20141102 170018143 1.2506 1.251 0

so should I use the average of bid and ask for the open,high,low and close of the tick struct?
Posted By: jcl

Re: Using a TMF to enter and exit at seconds and getting seconds==0 - 12/05/14 09:00

No, use the ask price only. Zorro generally uses the ask price for all its calculations. - Using tick data for backtests is tricky due to the high data amount that will quickly exceed the memory on your PC. That's why a special data structure, with a cache, is normally required for this and is also on our list for future Zorro features.
Posted By: Mithrandir77

Re: Using a TMF to enter and exit at seconds and getting seconds==0 - 12/05/14 13:51

Originally Posted By: jcl
No, use the ask price only. Zorro generally uses the ask price for all its calculations. - Using tick data for backtests is tricky due to the high data amount that will quickly exceed the memory on your PC. That's why a special data structure, with a cache, is normally required for this and is also on our list for future Zorro features.


Ok, thanks for the clarification. One more question, according to http://zorro-trader.com/manual/en/bar.htm

Quote:
Zorro's trading is normally based on bar periods. The strategy script is run at the end of every bar, and buy or sell commands are executed at the price of the begin of the next bar. By extending the candle time frame so that it covers several bars, trades can be placed in the middle of a candle. The bar period plays no role in trade mangement functions that evaluate entry or exit conditions and run on any tick. A tick is the arrival of a new asset price quote, which can happen several times per second. A bar period contains many, many ticks.


and

http://zorro-trader.com/manual/en/trade.htm

Quote:
TMF return values:
0 - check the trade's Entry, Stop, TakeProfit, and Trail parameters, and exit or enter accordingly.
1 - if the trade is still open or pending, exit it now.
2 - if the trade is still pending, enter it now.


So according to this, If a tmf returns 2, Zorro enters immediately (at a precision of a second at least). Is this right? Or does Zorro always enter trades at the begin of the next bar? Thanks!
Posted By: jcl

Re: Using a TMF to enter and exit at seconds and getting seconds==0 - 12/09/14 09:19

Yes, it enters immediately. TMFs run at any tick, not only at the end of a bar.
© 2024 lite-C Forums