help required to fix a signal's code

Moderator: admin

help required to fix a signal's code

Postby Mohamed85 » Fri Oct 21, 2016 2:07 pm

Hi all, Have a nice day,

I'm new in coding and have been developing a signal to send alert when moving averages crosses within a box range.
after getting done with it, however it doesn't send any error, it doesn't generate any kind of alert, I hope that someone can give it a second eye to fix the mistake that I can't find.

Hi Apprentice, can you help, please?

Code: Select all
-- Strategy profile initialization routine
-- Defines Strategy profile properties and Strategy parameters
-- TODO: Add minimal and maximal value of numeric parameters and default color of the streams
function Init()
    strategy:name("Range cross");
    strategy:description("");
    strategy:type(core.Signal);
    strategy:setTag("group", "Oscillators");
    strategy:setTag("NonOptimizableParameters", "SendEmail,PlaySound,Email,SoundFile,RecurrentSound,ShowAlert");

    strategy.parameters:addGroup("Price Parameters");
    strategy.parameters:addString("TF", "Time frame ('m1', 'm5', etc.)", "", "m1");
    strategy.parameters:setFlag("TF", core.FLAG_PERIODS);
    strategy.parameters:addGroup("Parameters");
   
    strategy.parameters:addInteger("SF", "First Averege Period", "First Averege Period", 9);
    strategy.parameters:addInteger("LF", "Second Averege Period", "Second Averege Period", 20);

    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

-- strategy instance initialization routine
-- Processes strategy parameters and creates output streams
-- TODO: Calculate all constants, create instances all necessary indicators and load all required libraries
-- Parameters block
local gSource = nil; -- the source stream
local ema1;
local mva2;
local SF, LF;
local PlaySound;
local RecurrentSound;
local SoundFile;
local Email;
local SendEmail;
local max, maxpos, max2, max2pos;
local min, minpos, min2, min2pos;
local range;
local atr;
local up;
local down;


--TODO: Add variable(s) for your indicator(s) if needed


-- Routine
function Prepare(nameOnly)

    SF = instance.parameters.SF;
    LF = instance.parameters.LF;
    TF = instance.parameters.TF;
   


   local name = profile:id().."("..instance.bid:instrument()..",".. tostring(SF)..",".. tostring(LF)..")";
    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");
    --TODO: Find indicator's profile, intialize parameters, and create indicator's instance (if needed)
   
 
    ema1 = core.indicators:create("EMA", gSource.close, SF);
    mva2 = core.indicators:create("MVA", gSource.close, LF);
    atr = core.indicators:create("ATR", gSource, 14);
   
    first1 = atr.DATA:first()+2;
   
    BUY = ("BUY")
    SELL = ("SELL")
    ExtSetupSignal("cross" ..","..TF.. ":", ShowAlert);
    ExtSetupSignalMail("MVA cross");
   
end

-- strategy calculation routine
-- TODO: Add your code for decision making
-- TODO: Update the instance of your indicator(s) if needed
function ExtUpdate(id, source, period)

   
    ema1:update(core.UpdateLast);
    mva2:update(core.UpdateLast);

    if period < 1 or not(ema1.DATA:hasData(period - 1)) or not(mva2.DATA:hasData(period - 1)) or not(atr.DATA:hasData(period - 1)) then
        return ;
    end
   
    range = core.rangeTo(period, 20);
    max, maxpos, max2, max2pos = mathex.max2(gSource.high, range);
    min, minpos, min2, min2pos = mathex.max2(gSource.low, range);
   
    if math.abs(max - max2) < 0.5 * atr.DATA[period]
       and math.abs(min - min2) < 0.5 * atr.DATA[period] then
       up = math.avg(max, max2)
       down = math.avg(min, min2)
    end


 if  gSource.close[period] < up  and  gSource.close[period] > down
    and core.crossesOver(ema1.DATA, mva2.DATA, period)  then
        ExtSignal(gSource, period, BUY, SoundFile, Email, RecurrentSound);
       
 elseif gSource.close[period] < up  and  gSource.close[period] > down
    and core.crossesUnder(ema1.DATA, mva2.DATA, period) then
        ExtSignal(gSource, period, SELL, 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 required to fix a signal's code

Postby Apprentice » Fri Oct 28, 2016 6:02 am

Range cross Signal.lua
(5.34 KiB) Downloaded 830 times

Can you describe your idea.
See my comments.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: help required to fix a signal's code

Postby Mohamed85 » Sat Oct 29, 2016 4:09 am

Thank you so much Apprentice for your continuous support, your comments enlightened me and fixed that code for sure,
My idea simply is to pick the ranging boxes that meets certain conditions out of the market, I am still developing the proper conditions that should hint break out direction
Mohamed85
 
Posts: 45
Joined: Sun Aug 21, 2016 1:24 pm


Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 8 guests