// Penny Picking strategy ///////////////////
#include <profile.c>
int TrailWhenProfitable()
{
bool TrailInitiated;
static bool TrailInitiatedA;
static bool TrailInitiatedB;
static bool TrailInitiatedC;
while(asset(loop("UK100","GER30", "NAS100")))
{
if(NumOpenLong > 0 )
{
var AverageOpen = 0;
int Count = 0;
for(open_trades)
if(TradeIsOpen)
{
AverageOpen += TradePriceOpen;
Count++;
}
if(Count)
AverageOpen /= Count;
switch (Asset)
{
case "UK100":
TrailInitiated = TrailInitiatedA;
break;
case "GER30":
TrailInitiated = TrailInitiatedB;
break;
case "NAS100":
TrailInitiated = TrailInitiatedC;
break;
}
if (priceClose() > AverageOpen + (2 * ATR(100)) && TrailInitiated == false)
{
TradeStopLimit = priceClose() - 2 * ATR(100);
TradeTrailLimit = priceClose() - ATR(100);
TrailInitiated = true;
printf(" TradeStopLimit = %.3f, TradeTrailLimit = %.3f", (var) TradeStopLimit, (var) TradeTrailLimit);
printf(" ATR = %.3f, priceClose = %.3f", ATR(100), priceClose());
printf(" AverageOpen = %.3f, ltod = %f", AverageOpen, (var) ltod(ET));
switch (Asset)
{
case "UK100":
printf("Asset UK100");
break;
case "GER30":
printf("Asset GER30");
break;
case "NAS100":
printf("Asset NAS100");
break;
default:
printf("None of them! ");
} // switch
}
if (TrailInitiated == true && ltod(ET) > 1230)
{
exitLong();
printf(" ltod = %f", (var) ltod(ET));
}
} // if NumOpenLong > 0
else
TrailInitiated = false;
switch (Asset)
{
case "UK100":
TrailInitiatedA = TrailInitiated;
break;
case "GER30":
TrailInitiatedB = TrailInitiated;
break;
case "NAS100":
TrailInitiatedC = TrailInitiated;
break;
} // switch
} // while asset loop
return 0;
}
function run()
{
StartDate = 2009;
EndDate = 2016;
LookBack = 500;
set(LOGFILE); // log all trades
set(TICKS); // normally needed for TMF
while(asset(loop("UK100","GER30","NAS100")))
{
vars Price = series(price());
vars Trend = series(LowPass(Price,500));
if(valley(Trend) && NumOpenLong < 4)
{
var AverageOpen = 0;
var AverageProfit = 0;
int Count = 0;
for(open_trades)
if(TradeIsOpen)
{
AverageOpen += TradePriceOpen;
Count++;
}
if(Count)
AverageOpen /= Count;
AverageProfit = (priceClose() - AverageOpen)/AverageOpen;
switch (Count)
{
case 0:
enterLong(TrailWhenProfitable);
break;
case 1:
if (AverageProfit < -0.10)
{
enterLong(TrailWhenProfitable);
printf(" enter 2nd trade ");
}
break;
case 2:
if (AverageProfit < -0.15)
{
enterLong(TrailWhenProfitable);
printf(" enter 3rd trade ");
}
break;
case 3:
if (AverageProfit < -0.20)
{
enterLong(TrailWhenProfitable);
printf(" enter 4th trade ");
}
break;
default:
printf("None of them! ");
} // switch count
} // if trending
} // while asset loop
} // run