Merging indicators intro strategy

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

Merging indicators intro strategy

Postby ellydrinh » Sun May 05, 2019 5:59 pm

Hello FXCM community,

I am trying to merge two indicators into a strategy to automatically place orders.
One of my indicator is using an internal stream, and I cannot find a way to get the same result on a custom strategy.

- INDICATOR
Code: Select all
local CCIPeriod = 10
local StochSmooth = 1
local StochPeriod = 10

function Prepare()
    cci = core.indicators:create("CCI", source, CCIPeriod);
    rawstoch = instance:addInternalStream(0, 0);
    ma = core.indicators:create("MVA", rawstoch, StochSmooth);
end

function Update(period, mode)
    local TodayCCI = cci.DATA[period];
    local TodayLowCCI = mathex.min(cci.DATA, period - StochPeriod + 1, period);
    local TodayHighCCI = mathex.max(cci.DATA, period - StochPeriod + 1, period);

    rawstoch[period] = 100 * (TodayCCI - TodayLowCCI) / (TodayHighCCI - TodayLowCCI);

    ma:update(mode);
end


I need my rawstoch internal stream to update my ma indicator.
Any help ?

Thanks for your precious help
ellydrinh
 
Posts: 4
Joined: Sun May 05, 2019 5:49 pm

Re: Merging indicators intro strategy

Postby Apprentice » Mon May 06, 2019 7:33 am

Unfortunately, the internal stream/buffer was removed from the strategy a few years ago.
You will have to read indicator output from the strategy.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Merging indicators intro strategy

Postby ellydrinh » Mon May 06, 2019 10:26 am

Thanks for your quick reply.
So the idea is to use core.indicators:findIndicator (http://www.fxcodebase.com/bin/products/IndicoreSDK/3.4.0/help/Lua/web-content.html#indicator_manager.findIndicator.html)
In order to get my live indicator, then get the output?

Or is there any other way to get the output of an indicator inside my strategy?

Thanks
ellydrinh
 
Posts: 4
Joined: Sun May 05, 2019 5:49 pm

Re: Merging indicators intro strategy

Postby Apprentice » Tue May 07, 2019 9:15 am

Can you provide an indicator used, will write an example.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Merging indicators intro strategy

Postby ellydrinh » Tue May 07, 2019 9:32 am

I need values from those 2 indicators, that I would like to use in my strategy (ExtUpdate()):

File attachment __.lua:
Code: Select all
function Init()
    indicator:name("Averages");
    indicator:description("");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Oscillator);

    -- ...
end

function Prepare()

    source = instance.source;

    -- ...

    assert(core.indicators:findIndicator("AVERAGES") ~= nil, "Please, download and install AVERAGES indicator");

    indicator1 = core.indicators:create("AVERAGES", source, First_Method, First_Period, false);
    indicator2 = core.indicators:create("AVERAGES", source, Second_Method, Second_Period, false);

    first = source:first() + math.max(Second_Bars, First_Bars, indicator1.DATA:first(), indicator2.DATA:first());

    OUT = instance:addInternalStream(0, 0);
end


function Update(period, mode)

    indicator1:update(mode);
    indicator2:update(mode);

    if not indicator1.DATA:hasData(period) or not indicator2.DATA:hasData(period) or not indicator1.DATA:hasData(period - First_Bars) or not indicator2.DATA:hasData(period - Second_Bars) then
        return;
    end

    local d, t, d1, t1;
    d, t = source:date(period);
    d1, t1 = source:date(period - 1);
    local S1 = (indicator1.DATA[period] - indicator1.DATA[period - First_Bars]) / source:pipSize();
    local S2 = (indicator2.DATA[period] - indicator2.DATA[period - Second_Bars]) / source:pipSize();

    -- Values I have interest in (0, -1, 1)
    if S1 > First_Slope and S2 > Second_Slope then
        OUT[period] = 1;
    elseif S1 < -First_Slope and S2 < -Second_Slope then
        OUT[period] = -1;
    else
        OUT[period] = 0;
    end
end


File attachment __2.lua:
Code: Select all
function Init()
    indicator:name("CCI Stochastic");
    indicator:description("");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Oscillator);

    -- ...
end

function Prepare(nameOnly)
    source = instance.source;

    -- ...

    cci = core.indicators:create("CCI", source, CCIPeriod);
    rawstoch = instance:addInternalStream(0, 0);
    ma = core.indicators:create("MVA", rawstoch, StochSmooth);
end

function Update(period, mode)

    cci:update(mode);

    if period < cci.DATA:first() + StochPeriod then
        return;
    end

    local TodayCCI = cci.DATA[period];
    local TodayLowCCI = mathex.min(cci.DATA, period - StochPeriod + 1, period);
    local TodayHighCCI = mathex.max(cci.DATA, period - StochPeriod + 1, period);

    rawstoch[period] = 100 * (TodayCCI - TodayLowCCI) / (TodayHighCCI - TodayLowCCI);

    ma:update(mode);

    if period < ma.DATA:first() then
        return;
    end

    -- values I have interest in (1, -1)
    if ma.DATA[period] > OB and ma.DATA[period - 1] <= OB then
        return 1
    end
    if ma.DATA[period] < OS and ma.DATA[period - 1] >= OS then
        return -1
    end
   
    -- ...
end


Thanks a lot for your time
Attachments
__.lua
(1.52 KiB) Downloaded 328 times
__2.lua
(1.13 KiB) Downloaded 328 times
ellydrinh
 
Posts: 4
Joined: Sun May 05, 2019 5:49 pm

Re: Merging indicators intro strategy

Postby Apprentice » Wed May 08, 2019 4:36 pm

Indicator_A.lua
(921 Bytes) Downloaded 354 times

1 for up candle
-1 for down candle

Indicator_B.lua
(1.16 KiB) Downloaded 329 times

1 Price > SMA od Typical Price
-1 Price < SMA od Typical Price

Strategy Example 1.lua
(30.7 KiB) Downloaded 333 times

Open Long
Indicator_A==1
Indicator_B==1
Open Short
Indicator_A==-1
Indicator_B==-1

Strategy Example 2.lua
(30.27 KiB) Downloaded 335 times

Same as Strategy Example 1.lua calculated internaly.

If you are interested in private SDK instructions contact me via email.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia


Re: Merging indicators intro strategy

Postby ellydrinh » Sun May 19, 2019 5:41 pm

Thanks for the precious help.
I have an unknown error (file attached) on the Bar-based indicator creation, in my strategy, when I try to apply your example.
I am using NAS100, I see the name correctly output.

Code: Select all
function Prepare(nameOnly)
    local source = instance.bid;
    local name = profile:id() .. "(" .. source:name() .. ", " .. instance.bid:instrument() .. ")";
    instance:name(name);

    if nameOnly then
        return;
    end

    assert(core.indicators:findIndicator("REM_AVERAGES_MASO") ~= nil, "Install REM_AVERAGES_MASO.LUA indicator");
    assert(core.indicators:findIndicator("REM_CCI_STOCHASTIC") ~= nil, "Install REM_CCI_STOCHASTIC.LUA indicator");

    Source = ExtSubscribe(1, nil, "m5", instance.parameters.Type == "Bid", "bar");

    square_indicator = core.indicators:create("REM_AVERAGES_MASO", Source.close);

-- ERROR on this line:
    arrow_indicator = core.indicators:create("REM_CCI_STOCHASTIC", Source);
end


My indicator is OK on standalone though. Here is the Init() sample:
Code: Select all
function Init()
    indicator:name("REM_CCI_STOCHASTIC");
    indicator:description("");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Oscillator);

    -- Additional parameters here...
end

function Prepare(nameOnly)
    local name = profile:id() .. "(" .. source:name() .. ", " .. tostring(CCIPeriod) .. ", " .. tostring(StochPeriod) .. ", " .. tostring(StochSmooth) .. ")";
    instance:name(name);

    source = instance.source;
    cci = core.indicators:create("CCI", source, CCIPeriod);
end


If i refer to your example, it should be ok:
Strategy Example 1.lua:
Code: Select all
function Prepare(nameOnly)
    Source = ExtSubscribe(2, nil, TF, instance.parameters.Type == "Bid", "bar");
    Indicator1 = core.indicators:create("INDICATOR_A", Source.close)

    -- My code breaks here with the unreadable error message
    Indicator2 = core.indicators:create("INDICATOR_B", Source);
end


Any idea what causes this error?
I can share you the whole files in PM if needed

Thank you for your time
Attachments
strategy_error.png
ellydrinh
 
Posts: 4
Joined: Sun May 05, 2019 5:49 pm

Re: Merging indicators intro strategy

Postby Apprentice » Tue May 21, 2019 5:22 am

Will need code of your strategy, all indicator used.
You can send them to my email mario(.)jemic(@)gmail(.)com
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia


Return to Indicator and Signal Requests

Who is online

Users browsing this forum: Google [Bot] and 13 guests