it seems that the time frames code has something wrong it keep generating signal when wrong time frames aligned not the specified ones
I'd appreciate if someone can help fixing this mistake.
Dear Apprentice, can you please help?
- Code: Select all
function Init()
strategy:name("Trend Catcher");
strategy:description("");
strategy:type(core.Both);
strategy:setTag("NonOptimizableParameters", "SendEmail,PlaySound,Email,SoundFile,RecurrentSound,ShowAlert");
strategy.parameters:addGroup("Price Parameters");
strategy.parameters:addString("TF", "Time frame ('m1', 'm5', etc.)", "", "H1");
strategy.parameters:setFlag("TF", core.FLAG_BARPERIODS);
strategy.parameters:addString("TF2", "Time frame ('m1', 'm5', etc.)", "", "H4");
strategy.parameters:setFlag("TF2", core.FLAG_BARPERIODS);
strategy.parameters:addGroup("Notification");
strategy.parameters:addBoolean("ShowAlert", "Show Alert", "", true);
strategy.parameters:addBoolean("PlaySound", "Play Sound", "", true);
strategy.parameters:addBoolean("RecurrentSound", "Recurrent Sound", "", false);
strategy.parameters:addFile("SoundFile", "Sound File", "", "");
strategy.parameters:setFlag("SoundFile", core.FLAG_SOUND);
strategy.parameters:addBoolean("SendEmail", "Send Email", "", true);
strategy.parameters:addString("Email", "Email", "", "");
strategy.parameters:setFlag("Email", core.FLAG_EMAIL);
end
local gSource = nil; -- the source stream
local gSource2 = nil;
local sa1;
local sa2;
local sa12;
local sa22;
local adx2;
local adxD;
local first;
local first2;
local PlaySound;
local RecurrentSound;
local SoundFile;
local Email;
local SendEmail;
function Prepare(nameOnly)
TF = instance.parameters.TF;
TF2 = instance.parameters.TF2;
local name = profile:id() .. "(" .. instance.bid:instrument() .. ")";
instance:name(name);
if nameOnly then
return ;
end
ShowAlert = instance.parameters.ShowAlert;
PlaySound = instance.parameters.PlaySound;
if PlaySound then
RecurrentSound = instance.parameters.RecurrentSound;
SoundFile = instance.parameters.SoundFile;
else
SoundFile = nil;
end
assert(not(PlaySound) or (PlaySound and SoundFile ~= ""), "Sound file must be specified");
SendEmail = instance.parameters.SendEmail;
if SendEmail then
Email = instance.parameters.Email;
else
Email = nil;
end
assert(not(SendEmail) or (SendEmail and Email ~= ""), "E-mail address must be specified");
gSource = ExtSubscribe(1, nil, instance.parameters.TF, instance.parameters.Type == "Bid", "bar");
gSource2 = ExtSubscribe(2, nil, instance.parameters.TF2, instance.parameters.Type == "Bid", "bar");
sa1 = core.indicators:create("EMA", gSource.close, 5);
sa2 = core.indicators:create("EMA", gSource.close, 10);
sa12 = core.indicators:create("EMA", gSource2.close, 5);
sa22 = core.indicators:create("EMA", gSource2.close, 10);
adx2 = core.indicators:create("ADX", gSource2, 14);
first=math.max(sa1.DATA:first(),sa2.DATA:first() );
first2=math.max(sa12.DATA:first(),sa22.DATA:first(),adx2.DATA:first());
end
function ExtUpdate(id, source, period)
period2= core.findDate (gSource2, gSource:date(period), false );
if period < first or period2 < first2
or period < 1 or period2 < 1 then
return ;
end
sa1:update(core.UpdateLast);
sa2:update(core.UpdateLast);
sa12:update(core.UpdateLast);
sa22:update(core.UpdateLast);
adx2:update(core.UpdateLast);
adxD = adx2.DATA[period2] - adx2.DATA[period2-1]
Strnd = ("Bear Trend")
Ltrnd = ("Bull Trend")
ExtSetupSignal("", ShowAlert);
ExtSetupSignalMail("D-ADX="..adxD.."");
------------------------------------------------------------------------------
if sa12.DATA[period2] < sa22.DATA[period2]
and sa1.DATA[period] < sa2.DATA[period]
and adx2.DATA[period2] > 30
and not (sa12.DATA[period2-1] < sa22.DATA[period2-1]
and sa1.DATA[period-1] < sa2.DATA[period-1] ) then
ExtSignal(gSource, period, Strnd, SoundFile, Email, RecurrentSound);
elseif sa12.DATA[period2] > sa22.DATA[period2]
and sa1.DATA[period] > sa2.DATA[period]
and adx2.DATA[period2] > 30
and not ( sa12.DATA[period2-1] > sa22.DATA[period2-1]
and sa1.DATA[period-1] > sa2.DATA[period-1] ) then
ExtSignal(gSource, period, Ltrnd, SoundFile, Email, RecurrentSound);
end
end
dofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");