How to load historical data for Supertrend and Trend Signal

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

How to load historical data for Supertrend and Trend Signal

Postby Daveatt » Thu Dec 13, 2018 11:20 am

Hi Apprentice and team

I'm trying to develop a strategy based on Trend signal + Supertrend. It works well but I'd like to to pre-fetch X amount of bars before doing all the calculations etc
As it's a scalping strategy based on signals convergence of Trend Signal + Supertrend, the strategy will wait by default to collect futur bars respecting the conditions for Trend signal + Supertrend
But when I launch the strategy, I'd like to look back at X previous candles and calculate the existing trend. And if there is a trend, the strategy will have to take the trade

I browsed extensively this forum and found out that I have to use the ExtSubscribe1 function as per this thread (viewtopic.php?f=18&t=63076&p=104482&hilit=extsubscribe1#p104482)
It's very clear how to use it for a MVA, but do you think it's compatible with a Supertrend and Trend Signal ?

Code: Select all

local BARS_HISTORY = 500;
TF = "m1";

-- in prepare function
function Prepare(name_only)
        Source = ExtSubscribe(1, nil, TF, instance.parameters.Type == "Bid", "bar");
    T1_SUPERTREND = core.indicators:create("ST", Source, T1_SUPERTREND_Period + BARS_HISTORY, T1_SUPERTREND_Multiplier, core.rgb(0, 255, 0), core.rgb(255, 0, 0));
end

-- in ExtUpdate function
function ExtUpdate(id, source, period)
  if (T1_SUPERTREND.DATA:hasData(period - BARS_HISTORY)) then
      core.host:trace("T1 ST period : " .. period " " .. T1_SUPERTREND[T1_SUPERTREND:size()  - BARS_HISTORY]);
  end
end


For me T1_SUPERTREND.DATA:hasData(period - BARS_HISTORY) never contains any data.

Thanks a lot in advance for your insight
Daveatt
Daveatt
 
Posts: 62
Joined: Mon Jan 30, 2017 10:00 am

Re: How to load historical data for Supertrend and Trend Sig

Postby Apprentice » Thu Dec 13, 2018 2:20 pm

Can you provide web reference for ST indicator?
We have a few version on the forum.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36435
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: How to load historical data for Supertrend and Trend Sig

Postby Daveatt » Fri Dec 14, 2018 3:33 am

Thanks for coming back to me so promptly
I've been using the ST.lua (single stream) from this link : viewtopic.php?f=17&t=605
Daveatt
 
Posts: 62
Joined: Mon Jan 30, 2017 10:00 am

Re: How to load historical data for Supertrend and Trend Sig

Postby Apprentice » Fri Dec 14, 2018 2:09 pm

Your request is added to the development list under Id Number 4370
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36435
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: How to load historical data for Supertrend and Trend Sig

Postby Apprentice » Mon Dec 17, 2018 5:33 am

local BARS_HISTORY = 500;
Code: Select all
T1_SUPERTREND = core.indicators:create("ST", Source, T1_SUPERTREND_Period , T1_SUPERTREND_Multiplier, core.rgb(0, 255, 0), core.rgb(255, 0, 0));


and

Code: Select all

function ExtUpdate(id, source, period)

if period  (Source:size()-1-period) < BARS_HISTORY then
return;
end

 
if (T1_SUPERTREND.DATA:hasData(period )) then
      core.host:trace("T1 ST period : " .. period " " .. T1_SUPERTREND[T1_SUPERTREND:size() - 1]);
end
end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36435
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: How to load historical data for Supertrend and Trend Sig

Postby Apprentice » Mon Dec 17, 2018 9:21 am

If your goal to have BARS_HISTORY bars loaded.

Try to replace
ExtSubscribe(id, instrument, tf, is_bid, "bar");
with
ExtSubscribe1(id, instrument, tf, BARS_HISTORY, is_bid, "bar");
will do what you need.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36435
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: How to load historical data for Supertrend and Trend Sig

Postby Daveatt » Mon Dec 17, 2018 11:49 am

Thanks Apprentice

When I try your code (including the ExtSubscribe1), I have the following issue

"attempt to call method 'size' (a nil value)"
Seems it doesn't like T1_SUPERTREND:size()

This piece "period (Source:size()-1-period)" doesn't work either for me.

I attached a sample Supertrend strategy where I tried to make your advices working.

Could you please have a quick look and let me know how I can fetch the previous (BARS_HISTORY) previous periods and display in the log the previous SUPERTREND values ?

Thanks a mil
Attachments
ST history strategy.lua
(21.05 KiB) Downloaded 426 times
Daveatt
 
Posts: 62
Joined: Mon Jan 30, 2017 10:00 am

Re: How to load historical data for Supertrend and Trend Sig

Postby Daveatt » Mon Dec 17, 2018 12:41 pm

Uploading a version that is compiling at least :)
Just struggling now to iterate and fetch the historical values for the ST
Attachments
ST history strategy.lua
Second version
(21.2 KiB) Downloaded 423 times
Daveatt
 
Posts: 62
Joined: Mon Jan 30, 2017 10:00 am

Re: How to load historical data for Supertrend and Trend Sig

Postby Daveatt » Tue Dec 18, 2018 4:19 am

Found out a solution :)

Code: Select all
-- browse all the previous BARS_HISTORY candles
        for i = 0, BARS_HISTORY, 1 do
         if Indicator.DATA:hasData(Indicator.DATA:size() - BARS_HISTORY + i) then
            core.host:trace("previous value i : " .. i .. " " .. Indicator.DATA[Indicator.DATA:size() - BARS_HISTORY + i]);
         end
        end


You helped me figuring it out.
Thanks a lot
Daveatt
Daveatt
 
Posts: 62
Joined: Mon Jan 30, 2017 10:00 am


Return to Indicator and Signal Requests

Who is online

Users browsing this forum: Bing [Bot] and 23 guests