Page 1 of 5

### Stochastic signals

Posted: Thu Aug 19, 2010 2:54 am
There are several ways to interpretation a Stochastic Oscillator. Three popular methods include:

1. Overbought / Oversold
Buy when the Oscillator %K falls below a specific level (e.g., 20) and then rises above that level. Sell when the Oscillator rises above a specific level (e.g., 80) and then falls below that level.

Code: Select all
`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("D", "D", "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", "", "");endlocal 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("STOCHASTIC", gSourceBid, instance.parameters.K, instance.parameters.SD, instance.parameters.D);    first = Stochastic.DATA:first();    local name = profile:id() .. "(" .. instance.bid:instrument() .. "(" .. instance.parameters.Period  .. instance.parameters.K  .. instance.parameters.SD  .. instance.parameters.D  .. instance.parameters.TypeSignal  .. instance.parameters.DownLevel  .. instance.parameters.UpLevel  .. ")";    instance:name(name);endlocal LastDirection=nil;-- when tick source is updatedfunction 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    endendfunction AsyncOperationFinished(cookie)    if cookie == 1 then        BidFinished = true;    elseif cookie == 2 then        AskFinished = true;    endendlocal gSignalBase = "";     -- the base part of the signal messagelocal 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);    endend`

### Re: Stochastic signals

Posted: Thu Aug 19, 2010 2:56 am
2. Crossover

Buy when the %K line rises above the %D line and sell when the %K line falls below the %D line.

Code: Select all
`function Init()    strategy:name("Stochastic Crossover 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("D", "D", "Parameter of stochastic", 3);    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", "", "");endlocal 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("STOCHASTIC", gSourceBid, instance.parameters.K, instance.parameters.SD, instance.parameters.D);    first = Stochastic.DATA:first();    local name = profile:id() .. "(" .. instance.bid:instrument() .. "(" .. instance.parameters.Period  .. instance.parameters.K  .. instance.parameters.SD  .. instance.parameters.D  .. instance.parameters.TypeSignal  .. ")";    instance:name(name);endlocal LastDirection=nil;-- when tick source is updatedfunction 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]>Stochastic.D[period-1] and Stochastic.K[period]<Stochastic.D[period] and instance.parameters.TypeSignal=="direct" and LastDirection~=-1 then          ExtSignal(gSourceBid, period, "Sell", SoundFile);          LastDirection=-1;         end         if Stochastic.K[period-1]<Stochastic.D[period-1] and Stochastic.K[period]>Stochastic.D[period] and instance.parameters.TypeSignal=="direct" and LastDirection~=1 then          ExtSignal(gSourceAsk, period, "Buy", SoundFile);          LastDirection=1;         end         if Stochastic.K[period-1]<Stochastic.D[period-1] and Stochastic.K[period]>Stochastic.D[period] and instance.parameters.TypeSignal=="reverse" and LastDirection~=-1 then          ExtSignal(gSourceBid, period, "Sell", SoundFile);          LastDirection=-1;         end         if Stochastic.K[period-1]>Stochastic.D[period-1] and Stochastic.K[period]<Stochastic.D[period] and instance.parameters.TypeSignal=="reverse" and LastDirection~=1 then          ExtSignal(gSourceAsk, period, "Buy", SoundFile);          LastDirection=1;         end                end    endendfunction AsyncOperationFinished(cookie)    if cookie == 1 then        BidFinished = true;    elseif cookie == 2 then        AskFinished = true;    endendlocal gSignalBase = "";     -- the base part of the signal messagelocal 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);    endend`

### Re: Stochastic signals

Posted: Thu Aug 19, 2010 3:11 am
3. Divergences between the stochastic lines and the price

Indicators for find divergence:

Code: Select all
`-- Indicator profile initialization routine-- Defines indicator profile properties and indicator parametersfunction Init()    indicator:name("Stochastic Divergence");    indicator:description("");    indicator:requiredSource(core.Bar);    indicator:type(core.Oscillator);    indicator.parameters:addInteger("K", "K", "Parameter of stochastic", 5);    indicator.parameters:addInteger("SD", "SD", "Parameter of stochastic", 3);    indicator.parameters:addInteger("D", "D", "Parameter of stochastic", 3);    indicator.parameters:addBoolean("I", "Indicator mode", "Keep true value to display labels and lines. Set this parameter to false when the indicator is used in another indicator.", true);    indicator.parameters:addColor("D_color", "Color of Divergence line", "", core.rgb(0, 155, 255));    indicator.parameters:addColor("UP_color", "Color of Uptrend", "", core.rgb(255, 0, 0));    indicator.parameters:addColor("DN_color", "Color of Downtend", "", core.rgb(0, 255, 0));end-- Indicator instance initialization routine-- Processes indicator parameters and creates output streams-- Parameters blocklocal I;local DD;local UP_color;local DN_color;local first;local source = nil;-- Streams blocklocal D = nil;local UP = nil;local DN = nil;local Stochastic = nil;local lineid = nil;-- Routinefunction Prepare()    I = instance.parameters.I;    DD = instance.parameters.DD;    UP_color = instance.parameters.UP_color;    DN_color = instance.parameters.DN_color;    source = instance.source;    Stochastic = core.indicators:create("STOCHASTIC", source, instance.parameters.K,instance.parameters.SD,instance.parameters.D);    first = Stochastic.DATA:first();    local name = profile:id() .. "(" .. source:name() .. ", " .. instance.parameters.K .. ", " .. instance.parameters.SD .. ", " .. instance.parameters.D .. ")";    instance:name(name);    D = instance:addStream("Stochastic", core.Line, name .. ".Stochastic", "Stochastic", instance.parameters.D_color, first, -1);    D:addLevel(20);    D:addLevel(80);    if I then        UP = instance:createTextOutput ("Up", "Up", "Wingdings", 10, core.H_Center, core.V_Top, instance.parameters.UP_color, -1);        DN = instance:createTextOutput ("Dn", "Dn", "Wingdings", 10, core.H_Center, core.V_Bottom, instance.parameters.DN_color, -1);    else        UP = instance:addStream("UP", core.Bar, name .. ".UP", "UP", instance.parameters.D_color, first, -1);        DN = instance:addStream("DN", core.Bar, name .. ".DN", "DN", instance.parameters.D_color, first, -1);    endendlocal pperiod = nil;local pperiod1 = nil;local line_id = 0;-- Indicator calculation routinefunction Update(period, mode)    -- if recaclulation started - remove all    if pperiod ~= nil and pperiod > period then        core.host:execute("removeAll");    end    pperiod = period;    -- process only candles which are already closed closed.    if pperiod1 ~= nil and pperiod1 == source:serial(period) then        return ;    end    pperiod1 = source:serial(period)    period = period - 1;    Stochastic:update(mode);    if period >= first then        D[period] = Stochastic.K[period];        if period >= first + 2 then            processBullish(period - 2);            processBearish(period - 2);        end    endendfunction processBullish(period)    if isTrough(period) then        local curr, prev;        curr = period;        prev = prevTrough(period);        if prev ~= nil then            if Stochastic.K[curr] > Stochastic.K[prev] and source.low[curr] < source.low[prev] then                if I then                    DN:set(curr, Stochastic.K[curr], "\225", "Classic bullish");                    line_id = line_id + 1;                    core.host:execute("drawLine", line_id, source:date(prev), Stochastic.K[prev], source:date(curr), Stochastic.K[curr], DN_color);                else                    DN[period] = curr - prev;                end            elseif Stochastic.K[curr] < Stochastic.K[prev] and source.low[curr] > source.low[prev] then                if I then                    DN:set(curr, Stochastic.K[curr], "\225", "Reversal bullish");                    line_id = line_id + 1;                    core.host:execute("drawLine", line_id, source:date(prev), Stochastic.K[prev], source:date(curr), Stochastic.K[curr], DN_color);                else                    DN[period] = -(curr - prev);                end            end        end    endendfunction isTrough(period)    local i;    if Stochastic.K[period] < Stochastic.K[period - 1] and Stochastic.K[period] < Stochastic.K[period + 1] then        for i = period - 1, first, -1 do            if Stochastic.K[i] > 80 then                return true;            elseif Stochastic.K[period] > Stochastic.K[i] then                return false;            end        end    end    return false;endfunction prevTrough(period)    local i;    for i = period - 5, first, -1 do        if Stochastic.K[i] <= Stochastic.K[i - 1] and Stochastic.K[i] < Stochastic.K[i - 2] and           Stochastic.K[i] <= Stochastic.K[i + 1] and Stochastic.K[i] < Stochastic.K[i + 2] then           return i;        end    end    return nil;endfunction processBearish(period)    if isPeak(period) then        local curr, prev;        curr = period;        prev = prevPeak(period);        if prev ~= nil then            if Stochastic.K[curr] < Stochastic.K[prev] and source.high[curr] > source.high[prev] then                if I then                    UP:set(curr, Stochastic.K[curr], "\226", "Classic bearish");                    line_id = line_id + 1;                    core.host:execute("drawLine", line_id, source:date(prev), Stochastic.K[prev], source:date(curr), Stochastic.K[curr], UP_color);                else                    UP[period] = curr - prev;                end            elseif Stochastic.K[curr] > Stochastic.K[prev] and source.high[curr] < source.high[prev] then                if I then                    UP:set(curr, Stochastic.K[curr], "\226", "Reversal bearish");                    line_id = line_id + 1;                    core.host:execute("drawLine", line_id, source:date(prev), Stochastic.K[prev], source:date(curr), Stochastic.K[curr], UP_color);                else                    UP[period] = -(curr - prev);                end            end        end    endendfunction isPeak(period)    local i;    if Stochastic.K[period] > Stochastic.K[period - 1] and Stochastic.K[period] > Stochastic.K[period + 1] then        for i = period - 1, first, -1 do            if Stochastic.K[i] < 20 then                return true;            elseif Stochastic.K[period] < Stochastic.K[i] then                return false;            end        end    end    return false;endfunction prevPeak(period)    local i;    for i = period - 5, first, -1 do        if Stochastic.K[i] >= Stochastic.K[i - 1] and Stochastic.K[i] > Stochastic.K[i - 2] and           Stochastic.K[i] >= Stochastic.K[i + 1] and Stochastic.K[i] > Stochastic.K[i + 2] then           return i;        end    end    return nil;end`

and
Code: Select all
`-- Indicator profile initialization routine-- Defines indicator profile properties and indicator parametersfunction Init()    indicator:name("Stochastic Divergence");    indicator:description("Shows Stochastic Divergence");    indicator:requiredSource(core.Bar);    indicator:type(core.Indicator);    indicator.parameters:addInteger("K", "K", "Parameter of stochastic", 5);    indicator.parameters:addInteger("SD", "SD", "Parameter of stochastic", 3);    indicator.parameters:addInteger("D", "D", "Parameter of stochastic", 3);    indicator.parameters:addColor("UP_color", "Color of Uptrend", "", core.rgb(255, 0, 0));    indicator.parameters:addColor("DN_color", "Color of Downtend", "", core.rgb(0, 255, 0));end-- Indicator instance initialization routine-- Processes indicator parameters and creates output streams-- Parameters blocklocal first;local source = nil;-- Streams blocklocal lineid = nil;local dummy;-- Routinefunction Prepare()    UP_color = instance.parameters.UP_color;    DN_color = instance.parameters.DN_color;    source = instance.source;    Stochastic = core.indicators:create("STOCHASTIC_DIVERGENCE", source, instance.parameters.K,instance.parameters.SD,instance.parameters.D, false);    first = Stochastic.DATA:first();    local name = profile:id() .. "(" .. source:name() .. ", " .. instance.parameters.K .. ", " .. instance.parameters.SD .. ", " .. instance.parameters.D .. ")";    instance:name(name);    dummy = instance:addStream("D", core.Line, name .. ".D", "D", UP_color, 0);endlocal pperiod = nil;local pperiod1 = nil;local line_id = 0;-- Indicator calculation routinefunction Update(period, mode)    local l;    -- if recaclulation started - remove all    if pperiod ~= nil and pperiod > period then        core.host:execute("removeAll");    end    pperiod = period;    -- process only candles which are already closed closed.    if pperiod1 ~= nil and pperiod1 == source:serial(period) then        return ;    end    pperiod1 = source:serial(period)    period = period - 1;    Stochastic:update(mode);    if Stochastic:getStream(1):hasData(period - 2) then        l = math.abs(Stochastic:getStream(1)[period - 2]);        local prev = period - 2 - l;        line_id = line_id + 1;        core.host:execute("drawLine", line_id, source:date(prev), source.high[prev], source:date(period - 2), source.high[period - 2], UP_color);    end    if Stochastic:getStream(2):hasData(period - 2) then        l = math.abs(Stochastic:getStream(2)[period - 2]);        local prev = period - 2 - l;        line_id = line_id + 1;        core.host:execute("drawLine", line_id, source:date(prev), source.low[prev], source:date(period - 2), source.low[period - 2], DN_color);    endend`

For work Stochastic_Divergence1.lua must be installed Stochastic_Divergence.lua

### Re: Stochastic signals

Posted: Thu Aug 19, 2010 3:24 am
Stochastic divergence signal:

Code: Select all
`function Init()    strategy:name("Stochastic Divergence");    strategy:description("Signals when the Stochastic Divergence detected");    strategy.parameters:addInteger("K", "K", "Parameter of stochastic", 5);    strategy.parameters:addInteger("SD", "SD", "Parameter of stochastic", 3);    strategy.parameters:addInteger("D", "D", "Parameter of stochastic", 3);    strategy.parameters:addString("Period", "Time frame", "", "m1");    strategy.parameters:setFlag("Period", core.FLAG_PERIODS);    strategy.parameters:addBoolean("ShowAlert", "Show Alert", "", true);    strategy.parameters:addBoolean("PlaySound", "Play Sound", "", false);    strategy.parameters:addFile("SoundFile", "Sound file", "", "");endlocal SoundFile;local gSource = nil;        -- the source streamfunction 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");    ExtSetupSignal(profile:id() .. ":", ShowAlert);    assert(instance.parameters.Period ~= "t1", "Can't be applied on ticks!");    gSource = ExtSubscribe(1, nil, instance.parameters.Period, true, "bar");    local name = profile:id() .. "(" .. instance.bid:instrument()  .. "(" .. instance.parameters.Period  .. ")" .. "," .. instance.parameters.K .. "," .. instance.parameters.SD .. "," .. instance.parameters.D .. ")";    instance:name(name);endlocal Stochastic = nil;-- when tick source is updatedfunction ExtUpdate(id, source, period)    if Stochastic == nil then        Stochastic = core.indicators:create("STOCHASTIC_DIVERGENCE", gSource, instance.parameters.K,instance.parameters.SD,instance.parameters.D, false);    end    Stochastic:update(core.UpdateLast);    if period <= instance.parameters.K + 10 then        return ;    end    local v;    if Stochastic:getStream(1):hasData(period - 2) then        v = Stochastic:getStream(1)[period - 2];        if v ~= 0 then            ExtSignal(gSource, period, "Bearish", SoundFile);        end    end    if Stochastic:getStream(2):hasData(period - 2) then        v = Stochastic:getStream(2)[period - 2];        if v ~= 0 then            ExtSignal(gSource, period, "Bullish", SoundFile);        end    endenddofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");`

For work Stochastic_Divergence_Signal.lua must be installed indicator Stochastic_Divergence.lua

### Re: Stochastic signals

Posted: Mon Sep 12, 2011 8:52 am
can you develop an strategy for this indicator? (stochastic divergence.lua)
thanks

### Re: Stochastic signals

Posted: Tue Sep 13, 2011 4:36 am
First, it is not indicators it is a signal.
Can you tell me for which signal you want strategy to be developed.

### Re: Stochastic signals

Posted: Wed Oct 05, 2011 4:42 pm
Apprentice wrote:First, it is not indicators it is a signal.
Can you tell me for which signal you want strategy to be developed.

Sorry for mistake, is for signal stochastic_divergence_signal thanks!

### Re: Stochastic signals

Posted: Fri Oct 07, 2011 10:28 am
OK.
I shall write the strategy for you.

### Re: Stochastic signals

Posted: Sat Oct 08, 2011 11:30 am
Strategies:

Stochastic_Overbought_Oversold_Strategy.lua

StochasticCrossover_Strategy.lua

Stochastic_Divergence_Strategy.lua