#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);
}
}