Setting TradeStopLimit and TradeTrailLimit

Posted By: Cstyle

Setting TradeStopLimit and TradeTrailLimit - 06/29/16 00:33

I'm trying to write a simple strategy which starts to trail only after the trade is profitable. I'm using a TMF with TradeStopLimit and TradeTrailLimit, but it is not behaving as I would expect:

Code:
#include <profile.c>

int TrailWhenProfitable()
{
	static bool TrailInitiated;
	
 	if(NumOpenLong > 0 )
	{
	
			if (priceClose() > TradePriceOpen + (2 * ATR(100)) && TrailInitiated == false)
				{
					TradeStopLimit = TradePriceOpen;
					TradeTrailLimit =  TradePriceOpen + ATR(100);
					TrailInitiated = true;
					printf("  TradeStopLimit = %.3f,  TradeTrailLimit = %.3f", TradeStopLimit, TradeTrailLimit);
					printf("  ATR = %.3f,  priceClose = %.3f", ATR(100), priceClose());
					printf("  TradePriceOpen = %.3f,  TradePriceClose = %.3f", TradePriceOpen, TradePriceClose);
					
				}
	}
	else 
		TrailInitiated = false;
   	
   	
   return 0;
   		
}

function run()
{
	StartDate = 2009;
	EndDate = 2016; 	
	LookBack = 500;
	set(LOGFILE); // log all trades
	set(TICKS);  // normally needed for TMF

	vars Price = series(price());
	vars Trend = series(LowPass(Price,500));
	

  	
	if(valley(Trend) && NumOpenLong == 0) 
	{
		enterLong(TrailWhenProfitable);
	}
	
  
}



The values being written to the log for TradeStopLimit and TradeTrailLimit via printf also don't seem right. What am I doing wrong?

Thanks!
Posted By: Cstyle

Re: Setting TradeStopLimit and TradeTrailLimit - 06/29/16 00:37

Also, the forum is removing all of the tabs from my code, making it more difficult to read. All of the code is left justified, no indentation. What is the correct way to post code so that this doesn't happen?

Thanks!
Posted By: jcl

Re: Setting TradeStopLimit and TradeTrailLimit - 06/30/16 12:09

It's [code], not [quote].
Posted By: DdlV

Re: Setting TradeStopLimit and TradeTrailLimit - 06/30/16 15:45

Hi Cstyle. A couple of thoughts:

When looking for help (in any forum), things like "The values being written" are too vague. Much better to also post the log or a snippet showing the values, bar where they occurred, etc., as well as why the values "don't seem right".

Re. the code, take a look at TradePriceClose vs. priceClose().

HTH.
Posted By: Cstyle

Re: Setting TradeStopLimit and TradeTrailLimit - 06/30/16 16:40

Originally Posted By: jcl
It's [code], not [quote].


Perfect. I fixed my original post. Thanks!
Posted By: Cstyle

Re: Setting TradeStopLimit and TradeTrailLimit - 07/01/16 22:43

Originally Posted By: DdlV
Hi Cstyle. A couple of thoughts:

When looking for help (in any forum), things like "The values being written" are too vague. Much better to also post the log or a snippet showing the values, bar where they occurred, etc., as well as why the values "don't seem right".

Re. the code, take a look at TradePriceClose vs. priceClose().

HTH.


DdlV, I swapped out TradePriceClose with priceClose, and updated the code in my post above. The strategy behaves the same, ie. not the way I would expect.

Here are the values in the log file that don't seem right:

Quote:
[NAS100::L6901] Long 1@1393 at 17:00

[1270: Thu 07.05.09 18:00] +0 +0 1/0
[1271: Thu 07.05.09 19:00] +0 +0 1/0
[1272: Thu 07.05.09 20:00] +0 +0 1/0
[1273: Thu 07.05.09 21:00] +0 +0 1/0
[1274: Thu 07.05.09 22:00] +0 +0 1/0
[1275: Thu 07.05.09 23:00] +0 +0 1/0
[1276: Fri 08.05.09 00:00] +0 +1 1/0
[1277: Fri 08.05.09 01:00] +0 +1 1/0
[1278: Fri 08.05.09 02:00] +0 +1 1/0
[1279: Fri 08.05.09 03:00] +0 +1 1/0 TradeStopLimit = 73151834298078158000000.000, TradeTrailLimit = 0.000 ATR = 6.676, priceClose = 1407.000 TradePriceOpen = 75262725486622238000000.000, TradePriceClose = 0.000
[NAS100::L6901] Trail 1@1393 Stop 1394 at 03:450


priceClose and ATR look fine in the log file. (Note that the asset is NAS100 using the data available from the zorro website.) All other values do not look right.

Why is TradePriceOpen so high? Shouldn't it be set to 1393?

Why is TradeTrailLimit set to 0? If TradePriceOpen is a huge number and I'm setting TradeTrailLimit equal to TradePriceOpen plus ATR, shouldn't TradeTrailLimit equal the huge number plus 6.676?

Again, thanks so much for your help!
Posted By: Cstyle

Re: Setting TradeStopLimit and TradeTrailLimit - 07/01/16 23:10

Looks like I just needed to typecast them to (var) so that they would print correctly. Problem solved!
© 2024 lite-C Forums