TradeIsEntry appears to always be False

Posted By: Trading4pips

TradeIsEntry appears to always be False - 03/27/18 18:51

I'm trying to use TradeIsEntry (http://zorro-project.com/manual/en/trade.htm) in my script. However, I'm finding it's always false, even after trades have been entered into. I've tested this in 1.74 and the beta (1.80.7)

I found anther script here: http://www.opserver.de/ubb7/ubbthreads.p...true#Post452443 that uses TradeIsEntry where it appears that TradeIsEntry is also not working.

After I've inserted printf statements in the above code (see below), it is showing that TradeIsEntry is always false.

How can I properly use TradeIsEntry such that it returns True?


Code:
#define H1 (60/BarPeriod)
#define H24 (1440/BarPeriod)
#define W1 (7200/BarPeriod)

int myTMF1() {
	if (TradeIsEntry) { 
		AssetVar[2] += 1; 
			if (TradeIsLong) AssetVar[3] += 1;
			if (TradeIsShort) AssetVar[4] += 1;
		printf("A. \nTradeIsEntry: %.5f, AssetVar[2]: %.5f, AssetVar[3]: %.5f, AssetVar[4]: %.5f\n",TradeIsEntry,AssetVar[2],AssetVar[3],AssetVar[4]);

	}
	return 0;
}

int myTMF() { 

	if (TradeIsEntry) {AssetVar[2] = AssetVar[2] + 1; 
		if (TradeIsLong) AssetVar[3] += 1;
		if (TradeIsShort) AssetVar[4] += 1;
	}
	if (TradeIsLong and TradeIsStop and AssetVar[4] < 1){
		enterShort(myTMF);
		printf("\nB. entershort\n");
		
	printf("\nC. TradeIsEntry: %.5f, AssetVar[2]: %.5f, AssetVar[3]: %.5f, AssetVar[4]: %.5f\n",TradeIsEntry,AssetVar[2],AssetVar[3],AssetVar[4]);
	}
	return 0;
	if (TradeIsShort and TradeIsStop and AssetVar[3] < 1) {
		enterLong(myTMF);	
		printf("\nD. entershort\n");
	}	
	return 0;

	return 0;
}

function frameAlign(BOOL Event){ 
	// Let time frame start when Event == true
	// f.i. frameAlign(hour() == 0); aligns to midnight 
	TimeFrame = 1;
	vars Num = series(0); // use a series for storing Num between calls
	Num[0] = Num[1]+1;    // count Num up once per bar  
	if(!Event) 
	TimeFrame = 0;      // continue current time frame  
	else {
		TimeFrame = -Num[0]; // start a new time frame
		Num[0] = 0;          // reset the counter  
	}
}

function run() {
	set(TICKS|PARAMETERS|FACTORS|LOGFILE); 
	Weekend = 3; // don't run TMF at weekend
	StartDate = 20140901;
	EndDate = 20141231; //preserve 2015 data for OOS testing
	BarPeriod = 60;
	LookBack = 209;
	if (is(INITRUN)) { AssetVar[0] = 0; AssetVar[1] = 0; AssetVar[2] = 0; AssetVar[3] = 0; AssetVar[4] = 0; }
	
	TimeFrame = W1;
	frameAlign(dow() == 7 and hour() == 23); //align weekly chart to a UTC open time of Sunday 2200 (note it is 2100 in summer)
	vars closeW1 = series(priceClose());
	AssetVar[1] = closeW1[0];
	
	TimeFrame = H1;
	if (dow() == 7 and hour() == 23) { AssetVar[2] = 0; AssetVar[3] = 0; AssetVar[4] = 0; } //reset to zero at start of each week
	int exitDay = 5;
	int exitHour = 16;
	
	var entryMultiple = 4;//
	Stop = 30 *PIP;
	Trail =2 * entryMultiple * ATR(168); // optimize(0.5, 0.25, 3.0, 0.25) * entryMultiple * ATR(168); //
		
	if (AssetVar[2] == 0) {
		if (!(dow() == exitDay and hour() >= exitHour)) {
			Entry = closeW1[0] + entryMultiple*ATR(168);
			if (NumOpenLong == 0) enterLong(myTMF1);
			Entry = closeW1[0] - entryMultiple*ATR(168);
			if (NumOpenShort == 0) enterShort(myTMF1); 
			}
		}
	if (AssetVar[2] > 0) {
		if (!(dow() == exitDay and hour() >= exitHour)) {
			Entry = closeW1[0];
			if (NumOpenLong == 0 and AssetVar[3] < 1) {	enterLong(myTMF);} 
			if (NumOpenShort == 0 and AssetVar[4] < 1) { enterShort(myTMF);}
			}
		}
		
		if (dow() == exitDay and hour() == exitHour) {exitLong("*"); exitShort("*");}
		plot("closeW1", closeW1, MAIN, RED);
	
}

Posted By: Trading4pips

Re: TradeIsEntry appears to always be False - 03/28/18 16:00

I just tried the above code in 1.80.8 and TradeIsEntry is still not working (it's always False).

Does anyone know if there is there a bug with TradeIsEntry? If TradeIsEntry is working properly, would someone please provide me with a working example of how to use it.
Posted By: Petra

Re: TradeIsEntry appears to always be False - 03/28/18 16:27

Code:
int tmf() {
	if(TradeIsEntry) printf("\nTradeIsEntry!");
	return 0;
}

function run() {
	Entry = 10*PIP;
	if(random() > 0.9)
		enterLong(tmf);
	if(random() < -0.9)
		exitLong();
}



Posted By: Sphin

Re: TradeIsEntry appears to always be False - 03/28/18 16:28

It's more a guess but TradeIsEntry assumes an Entry limit has been hit. Can it be that you search for TradeIsOpen?
Posted By: Trading4pips

Re: TradeIsEntry appears to always be False - 03/28/18 19:23

ty @Sphin, TradeIsOpen also didn't work.

ty @Petra, the code helped a lot and showed me that the issue in the code I posted above were the printf statements. For those that are interested, here is the above code with the correct printf statements:

Code:
#define H1 (60/BarPeriod)
#define H24 (1440/BarPeriod)
#define W1 (7200/BarPeriod)

int myTMF1() {
	if (TradeIsEntry) { 
		AssetVar[2] += 1; 
			if (TradeIsLong){
				AssetVar[3] += 1;
				printf("\nA. TradeIsLong & AssetVar[3] is %.2f\n",(var)AssetVar[3]);
			}	
			if (TradeIsShort) {
				AssetVar[4] += 1;
				printf("\nB. TradeIsShort & AssetVar[4] is %.2f\n",(var)AssetVar[4]);
			}
		printf("\nC. TradeIsEntry: %.2d, AssetVar[2]: %.2f, AssetVar[3]: %.2f, AssetVar[4]: %.2f\n",TradeIsEntry,(var)AssetVar[2],(var)AssetVar[3],(var)AssetVar[4]);

	}
	return 0;
}

int myTMF() { 

	if (TradeIsEntry) {AssetVar[2] = AssetVar[2] + 1; 
		if (TradeIsLong) AssetVar[3] += 1;
		if (TradeIsShort) AssetVar[4] += 1;
	}
	if (TradeIsLong and TradeIsStop and AssetVar[4] < 1){
		enterShort(myTMF);
		printf("\nD. entershortn");
		
	printf("\nE. TradeIsEntry: %.2d, AssetVar[2]: %.2f, AssetVar[3]: %.2f, AssetVar[4]: %.2f\n",TradeIsEntry,(var)AssetVar[2],(var)AssetVar[3],(var)AssetVar[4]);
	}
	return 0;
	if (TradeIsShort and TradeIsStop and AssetVar[3] < 1) {
		enterLong(myTMF);	
		printf("\nE. entershort\n");
	}	
	return 0;

	return 0;
}

function frameAlign(BOOL Event){ 
	// Let time frame start when Event == true
	// f.i. frameAlign(hour() == 0); aligns to midnight 
	TimeFrame = 1;
	vars Num = series(0); // use a series for storing Num between calls
	Num[0] = Num[1]+1;    // count Num up once per bar  
	if(!Event) 
	TimeFrame = 0;      // continue current time frame  
	else {
		TimeFrame = -Num[0]; // start a new time frame
		Num[0] = 0;          // reset the counter  
	}
}

function run() {
	set(TICKS|PARAMETERS|FACTORS|LOGFILE); 
	Weekend = 3; // don't run TMF at weekend
	StartDate = 20140901;
	EndDate = 20141231; //preserve 2015 data for OOS testing
	BarPeriod = 60;
	LookBack = 209;
	if (is(INITRUN)) { AssetVar[0] = 0; AssetVar[1] = 0; AssetVar[2] = 0; AssetVar[3] = 0; AssetVar[4] = 0; }
	
	TimeFrame = W1;
	frameAlign(dow() == 7 and hour() == 23); //align weekly chart to a UTC open time of Sunday 2200 (note it is 2100 in summer)
	vars closeW1 = series(priceClose());
	AssetVar[1] = closeW1[0];
	
	TimeFrame = H1;
	if (dow() == 7 and hour() == 23) { AssetVar[2] = 0; AssetVar[3] = 0; AssetVar[4] = 0; } //reset to zero at start of each week
	int exitDay = 5;
	int exitHour = 16;
	
	var entryMultiple = 4;//
	Stop = 30 *PIP;
	Trail =2 * entryMultiple * ATR(168); // optimize(0.5, 0.25, 3.0, 0.25) * entryMultiple * ATR(168); //
		
	if (AssetVar[2] == 0) {
		if (!(dow() == exitDay and hour() >= exitHour)) {
			Entry = closeW1[0] + entryMultiple*ATR(168);
			if (NumOpenLong == 0) enterLong(myTMF1);
			Entry = closeW1[0] - entryMultiple*ATR(168);
			if (NumOpenShort == 0) enterShort(myTMF1); 
			}
		}
	if (AssetVar[2] > 0) {
		if (!(dow() == exitDay and hour() >= exitHour)) {
			Entry = closeW1[0];
			if (NumOpenLong == 0 and AssetVar[3] < 1) {	enterLong(myTMF);} 
			if (NumOpenShort == 0 and AssetVar[4] < 1) { enterShort(myTMF);}
			}
		}
		
		if (dow() == exitDay and hour() == exitHour) {exitLong("*"); exitShort("*");}
		plot("closeW1", closeW1, MAIN, RED);
	
}

Posted By: Trading4pips

Re: TradeIsEntry appears to always be False - 03/28/18 19:32

Unfortunately, I still can't resolve the issue with TradeIsEntry in my code.
It appears to always be false since the printf statement in the TMF never prints.

Here is a snippet of my code. Can anyone review and let me know what I need to change to resolve the issue? Thanks.

Code:
/*                        DISCLAIMERS: 
   The following code and ideas are for educational purposes only
    and is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR,
         CONDITIONS OF ANY KIND, either express or implied.

  Trading involves substantial risk of loss and is not suitable for all 
   investors.  Past performance is not indicative of future results.

   DESCRIPTION: Identify Hi and Low of Open Range and enter into 3 trades 
      on break of either Hi or Low. Set 1 Stop & 3 Targets for scale outs.
*/

// Define AssetVar to be used in run function & TMF
#define EntryHI AssetVar[0]
#define EntryLO AssetVar[1]
#define Range AssetVar[2]
#define TradesToday AssetVar[3]
#define TradesLong AssetVar[4]
#define TradesShort AssetVar[5]

int tmf() {
	// 3 longs / 3 shorts - with 1 Stop & 3 Targets for scale outs
	enterLong(0,EntryHI,Range,Range); // (Lots, Entry, Stop, Target)
	enterLong(0,EntryHI,Range,Range*2);
	enterLong(0,EntryHI,Range,Range*3);
	enterShort(0,EntryLO,Range,Range);
	enterShort(0,EntryLO,Range,Range*2);
	enterShort(0,EntryLO,Range,Range*3);

	if (TradeIsEntry) { // ?? Always appears to be false as printf is never printed. ??
		printf("\nTMF-A. TradeIsEntry! %.5d\n",TradeIsEntry);
		TradesToday += 1;
		if (TradeIsLong) {
			TradesLong += 1;
			printf("\nTMF-B. Trade for %s is Long.\n   TradesToday: (%.0f) & TradesLong: (%.0f) s/b 1, while TradesShort: (%.0f) s/b 0\n",Asset,TradesToday,TradesLong,TradesShort);
		}
		
		if (TradeIsShort) {
			TradesShort += 1;
			printf("\nTMF-C. Trade for %s is Short.\n   TradesToday: (%.0f) & TradesShort: (%.0f) s/b 1, while TradesLong: (%.0f) s/b 0\n",Asset,TradesToday,TradesShort,TradesLong);
		}
	}
	return 0;
}

function run() {
	set(TICKS|LOGFILE); //|RISKLIMIT|PLOTNOW);	
	BarPeriod = 30;		// 1 Hour
	var WiggleRoom = 1;	// Room in *PIPs to add/subtract from entries 
	
	int limit = 3; 		// max number of trades
	Risk = 200; 		// Max Risk per trade
	
	// DATE & TIME VARIABLES (TIMES ARE IN CHICAGO TIME (Central))
	StartDate = 20100102;	// Start date
	EndDate = 20100105;	// End date
	
	AssetZone = CST; 	// Set timezone to Central (i.e. Chicago)
	
	int marketstarthour	= 8;
	int marketstartminute	= 30;
	int marketendhour	= 15;
	int marketendminute	= 0;
	int StartTradingHour	= 9;
	int StartTradingMinute	= 0;
	int StopTradingHour	= 14;
	int StopTradingMinute	= 30;
	int OpenRangeHourEnds 	= 9;	// Open Range Hour ends
	int OpenRangeMinuteEnds = 0;	// Open Range Minute ends

	// COSTS
	Spread = Commission = Slippage = RollLong = RollShort = 0;  // Ignore costs
	
	// INITRUN
	if (is(INITRUN)) {
		EntryHI = 0; EntryLO = 0; Range = 0; TradesToday = 0; TradesLong = 0;
			TradesShort = 0; } // Clear Variables prior to initial run

	while(asset(loop("SPX500"))) {			// Define Assets

		vars hi	= series(priceHigh());  	// Create Series of Highs
		vars lo	= series(priceLow());		// Create Series of Lows
	
		// Print details for debugging
		printf("\nA. %s - hi %.5f, lo %.5f\n",Asset,hi[0],lo[0]);
	
		vars BarHI = series(MaxVal(hi,BarPeriod)); 	// Series of Max Highs
		vars BarLO = series(MinVal(lo,BarPeriod));	// Series of Min Lows
		vars BarRange = series(BarHI[0]-BarLO[0]); 	// Series of Hi-Lo Ranges
		
		// Print details for debugging
		printf("\nB. %s - BarHI %.5f, BarLO %.5f, BarRange %.5f\n",Asset,BarHI[0],BarLO[0],BarRange[0]);
		
		if(lhour() == OpenRangeHourEnds && minute() == OpenRangeMinuteEnds) {
			EntryHI = BarHI[0]+WiggleRoom*PIP;
			EntryLO = BarLO[0]-WiggleRoom*PIP;
			Range = BarRange[0]+(2*WiggleRoom*PIP);
					
			// Print details for debugging
			printf("\nC. %s - EntryHI: %.5f, EntryLO: %.5f, Range: %.5f\n",Asset,EntryHI,EntryLO,Range);
		}
		
		if (lhour() >= StartTradingHour && minute() >= StartTradingMinute && lhour() <= StopTradingHour && minute() <= StopTradingMinute ) {
			
			tmf();	// Call TMF for trades
			//enterLong(tmf);
			//enterShort(tmf);	
		}
	}
}

Posted By: Trading4pips

Re: TradeIsEntry appears to always be False - 04/03/18 13:55

Can anyone review my code in the last post and let me know what I need to change so that TradeIsEntry is not always false?
Posted By: Zheka

Re: TradeIsEntry appears to always be False - 04/03/18 14:45


Frist, As is, your int tmf() is not a "trade-management function". It is not called as an argument in an entry/exit statement, so it is just a normal function.
As a normal function, it has no access to Trade variables , incl. TradeIsEntry.

Second, TradeIsEntry has a value when a trade is entered with "Entry"; Entry has to be set to a value.
Review the manual on Entry.

I did not run nor experimented with the code. Just first observations..
Posted By: Trading4pips

Re: TradeIsEntry appears to always be False - 04/03/18 17:43

ty @Zheka for the comments,

I had tried calling tmf with entry statements but commented them out because they did not work:
//enterLong(tmf);
//enterShort(tmf);

The main issue I'm having is that the manual and all of the examples I've found call the tmf using just enterLong(tmf) or enterShort(tmf). However, I want to enter into either 3 long or short trades (depending on which way the price breaks out) with specific stops and different targets. This is why I have set up these:

enterLong(0,EntryHI,Range,Range); // (Lots, Entry, Stop, Target)
enterLong(0,EntryHI,Range,Range*2);
enterLong(0,EntryHI,Range,Range*3);
enterShort(0,EntryLO,Range,Range);
enterShort(0,EntryLO,Range,Range*2);
enterShort(0,EntryLO,Range,Range*3);

Obviously I'm doing something wrong, but reading the manual does not help as I can not find examples of what I'm trying to do. Any assistance would be greatly appreciated.
Posted By: Zheka

Re: TradeIsEntry appears to always be False - 04/03/18 18:15

Explore also the second point of my reply.
Posted By: Trading4pips

Re: TradeIsEntry appears to always be False - 04/03/18 18:30

I've explored your second point six ways to Sunday. In fact I've spent over a week reading the manual and forum trying to figure this issue out. Part of my issue is that I'm not a programmer and I'm trying to learn as I build my first Zorro algo. So it appears that I need more help than a suggestion to read the manual.
Posted By: AndrewAMD

Re: TradeIsEntry appears to always be False - 04/03/18 19:54

You're in over your head. Start over.

Learn how function pointers work and why it's relevant for TMF's:
http://zorro-project.com/manual/en/function.htm
http://zorro-project.com/manual/en/trade.htm

Point being, a TMF is not meant to be called as a function by your script. If you configured your script correctly, Zorro will call your TMF from the backend.

Also, don't name a TMF from a TMF.

For more context, take a peek at the baskets example under Tips. Note that the helper functions are distinct from the TMFs.
http://www.zorro-trader.com/manual/en/tips.htm
© 2024 lite-C Forums