Help request on 2 time frame strategy devlopment

If you need an Indicator or Signal developed or translated from other language, please post all Indicator development REQUESTS to this section here.

Moderator: admin

Help request on 2 time frame strategy devlopment

Postby Mohamed85 » Sat Aug 26, 2017 6:18 am

I have developed the below signal to generate an alert when the trend aligned on 2 time frames by crossing averages and ADX value for the higher time frame above 30.

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");
Mohamed85
 
Posts: 45
Joined: Sun Aug 21, 2016 1:24 pm

Re: Help request on 2 time frame strategy devlopment

Postby Apprentice » Sun Aug 27, 2017 2:45 am

Try something like that.
test.lua
(4.62 KiB) Downloaded 367 times

Only addition was.

if id~=1 then
return;
end


period2= core.findDate (gSource2, gSource:date(period), false );

period in gSource:date(period) will only work if source id is 1
Above code will ensure this.

Without it, whene source with id 2 is called, provided period will not we applicable for/on gSource:date(period),
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Help request on 2 time frame strategy devlopment

Postby Mohamed85 » Sun Aug 27, 2017 4:18 pm

Thank you so much apprentice for your support as usual, Appreciate your help.
Mohamed85
 
Posts: 45
Joined: Sun Aug 21, 2016 1:24 pm


Return to Indicator and Signal Requests

Who is online

Users browsing this forum: Bing [Bot], EAMONN, Google [Bot] and 11 guests