FXCM Forex Trading

Crossover Alert indicator and signal

Here you can post and download custom indicators. PLEASE: Do not start topics unless you are posting your own indicator, they will be moved to appropriate section even if you do.

Moderator: admin

Crossover Alert indicator and signal

Postby Alexander.Gettinger » Thu May 27, 2010 1:49 am

viewtopic.php?f=27&t=1074&p=2047#p2047

EMA_58_Crossover.png


Indicator:
Code: Select all
function Init()
    indicator:name("EMA_58_Crossover");
    indicator:description("");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Indicator);

    indicator.parameters:addColor("EMA5_color", "Color of EMA5", "Color of EMA5", core.rgb(0, 255, 0));
    indicator.parameters:addColor("EMA8_color", "Color of EMA8", "Color of EMA8", core.rgb(0, 255, 255));
    indicator.parameters:addColor("EMA20_color", "Color of EMA20", "Color of EMA20", core.rgb(255, 255, 0));
    indicator.parameters:addColor("UP_color", "Color of UP", "Color of UP", core.rgb(0, 255, 0));
    indicator.parameters:addColor("DN_color", "Color of DN", "Color of DN", core.rgb(255, 0, 0));
end

local first;
local source = nil;
local buffEMA5=nil;
local buffEMA8=nil;
local buffEMA20=nil;
local buffUp=nil;
local buffDn=nil;

local EMA5;
local EMA8;
local EMA20;

function Prepare()
    source = instance.source;
    EMA5=core.indicators:create("EMA", source.close, 5);
    EMA8=core.indicators:create("EMA", source.open, 8);
    EMA20=core.indicators:create("EMA", source.close, 20);
    first = EMA20.DATA:first();
    local name = profile:id() .. "(" .. source:name() .. ")";
    instance:name(name);
    buffEMA5 = instance:addStream("EMA5", core.Line, name .. ".EMA5", "EMA5", instance.parameters.EMA5_color, first);
    buffEMA8 = instance:addStream("EMA8", core.Line, name .. ".EMA8", "EMA8", instance.parameters.EMA8_color, first);
    buffEMA20 = instance:addStream("EMA20", core.Line, name .. ".EMA20", "EMA20", instance.parameters.EMA20_color, first);
    buffUp = instance:createTextOutput ("Up", "Up", "Wingdings", 10, core.H_Center, core.V_Top, instance.parameters.UP_color, first);
    buffDn = instance:createTextOutput ("Dn", "Dn", "Wingdings", 10, core.H_Center, core.V_Bottom, instance.parameters.DN_color, first);
end

function Update(period, mode)
    if (period>first+9) then
     EMA5:update(mode);
     EMA8:update(mode);
     EMA20:update(mode);
     
     local Range=0.;
     local AvgRange=0.;
     for i=period-9,period,1 do
      AvgRange=AvgRange+math.abs(source.high[i]-source.low[i]);
     end
     Range=AvgRange/10.;
     local fastMAnow=EMA5.DATA[period];
     local fastMAprevious=EMA5.DATA[period-1];
     local slowMAnow=EMA8.DATA[period];
     local slowMAprevious=EMA8.DATA[period-1];
     
     buffEMA5[period]=fastMAnow;
     buffEMA8[period]=slowMAnow;
     buffEMA20[period]=EMA20.DATA[period];
     
     if fastMAnow>slowMAnow and fastMAprevious<slowMAprevious then
      buffUp:set(period, source.low[period]-Range*0.75, "\225", "");
     end
     if fastMAnow<slowMAnow and fastMAprevious>slowMAprevious then
      buffDn:set(period, source.high[period]+Range*0.75, "\226", "");
     end

    end
end


Signal:
Code: Select all
function Init()
    strategy:name("EMA_58_Crossover signal");
    strategy:description("");

    strategy.parameters:addGroup("Parameters");
   
    strategy.parameters:addString("Period", "Timeframe", "", "m5");
    strategy.parameters:setFlag("Period", core.FLAG_PERIODS);

    strategy.parameters:addGroup("Signals");
    strategy.parameters:addBoolean("ShowAlert", "Show Alert", "", true);
    strategy.parameters:addBoolean("PlaySound", "Play Sound", "", false);
    strategy.parameters:addFile("SoundFile", "Sound File", "", "");
end

local SoundFile;
local gSourceBid = nil;
local gSourceAsk = nil;
local first;

local BidFinished = false;
local AskFinished = false;
local LastBidCandle = nil;

local EMA5;
local EMA8;

function Prepare()
    ShowAlert = instance.parameters.ShowAlert;
    if instance.parameters.PlaySound then
        SoundFile = instance.parameters.SoundFile;
    else
        SoundFile = nil;
    end
   
    assert(not(PlaySound) or (PlaySound and SoundFile ~= ""), "Sound file must be specified");
    assert(instance.parameters.Period ~= "t1", "Signal cannot be applied on ticks");

    ExtSetupSignal("Altr Trend signal:", ShowAlert);

    gSourceBid = core.host:execute("getHistory", 1, instance.bid:instrument(), instance.parameters.Period, 0, 0, true);
    gSourceAsk = core.host:execute("getHistory", 2, instance.bid:instrument(), instance.parameters.Period, 0, 0, false);

    EMA5 = core.indicators:create("EMA", gSourceBid.close, 5);
    EMA8 = core.indicators:create("EMA", gSourceBid.open, 8);

    first = EMA8.DATA:first();

    local name = profile:id() .. "(" .. instance.bid:instrument() .. "(" .. instance.parameters.Period  .. ")";
    instance:name(name);
end

local LastDirection=nil;

-- when tick source is updated
function Update()
    if not(BidFinished) or not(AskFinished) then
        return ;
    end

    local period;

    -- update moving average
    EMA5:update(core.UpdateLast);
    EMA8:update(core.UpdateLast);

    -- calculate enter logic
    if LastBidCandle == nil or LastBidCandle ~= gSourceBid:serial(gSourceBid:size() - 1) then
        LastBidCandle = gSourceBid:serial(gSourceBid:size() - 1);

        period = gSourceBid:size() - 1;
        if period > first then
         if EMA5.DATA[period]<EMA8.DATA[period] and EMA5.DATA[period-1]>EMA8.DATA[period-1] and LastDirection~=-1 then
          ExtSignal(gSourceBid, period, "Sell", SoundFile);
          LastDirection=-1;
         end
         if EMA5.DATA[period]>EMA8.DATA[period] and EMA5.DATA[period-1]<EMA8.DATA[period-1] and LastDirection~=1 then
          ExtSignal(gSourceAsk, period, "Buy", SoundFile);
          LastDirection=1;
         end
       
        end
    end
end

function AsyncOperationFinished(cookie)
    if cookie == 1 then
        BidFinished = true;
    elseif cookie == 2 then
        AskFinished = true;
    end
end

local gSignalBase = "";     -- the base part of the signal message
local gShowAlert = false;   -- the flag indicating whether the text alert must be shown

-- ---------------------------------------------------------
-- Sets the base message for the signal
-- @param base      The base message of the signals
-- ---------------------------------------------------------
function ExtSetupSignal(base, showAlert)
    gSignalBase = base;
    gShowAlert = showAlert;
    return ;
end

-- ---------------------------------------------------------
-- Signals the message
-- @param message   The rest of the message to be added to the signal
-- @param period    The number of the period
-- @param sound     The sound or nil to silent signal
-- ---------------------------------------------------------
function ExtSignal(source, period, message, soundFile)
    if source:isBar() then
        source = source.close;
    end
    if gShowAlert then
        terminal:alertMessage(source:instrument(), source[period], gSignalBase .. message, source:date(period));
    end
    if soundFile ~= nil then
        terminal:alertSound(soundFile, false);
    end
end
Attachments
EMA_58_Crossover.lua
(2.75 KiB) Downloaded 908 times
EMA_58_Crossover_Signal.lua
(4.05 KiB) Downloaded 992 times
Alexander.Gettinger
FXCodeBase: Confirmed User
 
Posts: 2460
Joined: Wed Mar 31, 2010 9:40 pm
Location: Russia, Omsk

Re: Crossover Alert indicator and signal

Postby Apprentice » Wed Jan 11, 2017 6:06 am

Indicator was revised and updated.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 14248
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia


Return to Custom Indicators

Who is online

Users browsing this forum: chipsoft, Google [Bot] and 7 guests