Hi programmers
Is it possible to have a signal for this indicator??
Whenever it cross the over bot-sold line, to have a sound signal.
I try to use the standard stochastic signal, but it does not work well.
Thanks and regards
Jernej
Moderator: admin
function Init()
strategy:name("Stochastic Overbought/Oversold signal");
strategy:description("");
strategy.parameters:addGroup("Parameters");
strategy.parameters:addString("Period", "Timeframe", "", "m5");
strategy.parameters:setFlag("Period", core.FLAG_PERIODS);
strategy.parameters:addInteger("K", "K", "Parameter of stochastic", 5);
strategy.parameters:addInteger("SD", "SD", "Parameter of stochastic", 3);
strategy.parameters:addInteger("DownLevel", "DownLevel", "", 20);
strategy.parameters:addInteger("UpLevel", "UpLevel", "", 80);
strategy.parameters:addString("TypeSignal", "Type of signal", "", "direct");
strategy.parameters:addStringAlternative("TypeSignal", "direct", "", "direct");
strategy.parameters:addStringAlternative("TypeSignal", "reverse", "", "reverse");
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 Stochastic;
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);
Stochastic = core.indicators:create("SSD1", gSourceBid, instance.parameters.K, instance.parameters.SD);
first = Stochastic.DATA:first();
local name = profile:id() .. "(" .. instance.bid:instrument() .. "(" .. instance.parameters.Period .. instance.parameters.K .. instance.parameters.SD .. instance.parameters.TypeSignal .. instance.parameters.DownLevel .. instance.parameters.UpLevel .. ")";
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
Stochastic: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 Stochastic.K[period-1]>instance.parameters.UpLevel and Stochastic.K[period]<instance.parameters.UpLevel and instance.parameters.TypeSignal=="direct" and LastDirection~=-1 then
ExtSignal(gSourceBid, period, "Sell", SoundFile);
LastDirection=-1;
end
if Stochastic.K[period-1]<instance.parameters.DownLevel and Stochastic.K[period]>instance.parameters.DownLevel and instance.parameters.TypeSignal=="direct" and LastDirection~=1 then
ExtSignal(gSourceAsk, period, "Buy", SoundFile);
LastDirection=1;
end
if Stochastic.K[period-1]<instance.parameters.DownLevel and Stochastic.K[period]>instance.parameters.DownLevel and instance.parameters.TypeSignal=="reverse" and LastDirection~=-1 then
ExtSignal(gSourceBid, period, "Sell", SoundFile);
LastDirection=-1;
end
if Stochastic.K[period-1]>instance.parameters.UpLevel and Stochastic.K[period]<instance.parameters.UpLevel and instance.parameters.TypeSignal=="reverse" 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
Return to Indicator and Signal Requests
Users browsing this forum: Bing [Bot] and 20 guests