Need help with db put/get

Section for discussions related to indicators, use of indicators, and building of trading stategies using indicators.

Moderator: admin

Need help with db put/get

Postby Silverthorn » Wed Nov 13, 2019 1:41 am

I have an indicator that I wrote some years ago and am now trying to finish.

I am having trouble saving and retreating values to a db.

My code I am struggling with is

function Init()
indicator:type(core.Indicator);
indicator:name("BAHA");
indicator:description("BAHA");
indicator:setTag("group", "BAHA");
indicator:setTag("replaceSource","t");
indicator:requiredSource(core.Bar);
indicator.parameters:addString ("dbManager", "Database", "", "UseSaved");
indicator.parameters:addStringAlternative ("dbManager", "UseSaved", "", "UseSaved");
indicator.parameters:addStringAlternative ("dbManager", "UseLocal", "", "UseLocal");
indicator.parameters:addStringAlternative ("dbManager", "Save", "", "Save");
end

function Prepare()
instance:ownerDrawn(true);
source = instance.source;

local dbname = "BAHADB" .. source:name() .. instance.source:barSize ();
require("storagedb")
db = storagedb.get_db(dbname);
local dbManager = instance.parameters.dbManager;
if dbManager == "Save" then
db:put(tostring("SR_Offset"),tostring(SR_Offset));
db:put(tostring("SR_Confirmation"),tostring(SR_Confirmation));
db:put(tostring("Speed"),tostring(Speed));
end
if dbManager == "UseSaved" then
SR_Offset = tonumber(db:get (tostring("SR_Offset"), 0));
SR_Confirmation = tonumber(db:get (tostring("SR_Confirmation"), 0));
Speed = tonumber(db:get (tostring("Speed"), 0));
else
SR_Confirmation = instance.parameters.SR_Confirmation;
SR_Offset = instance.parameters.SR_Offset;
Speed = instance.parameters.Speed;
end

When I run UseLocal no problem.
When I save everything is fine.
When I try to UseSaved I get an error saying that I am attempting to perform arithmetic on upvalue "SR_Confirmation" (a nil value)

I'm not really understanding what makes it an upvalue but I think my problem is that I am using strings and numbers incorrectly.
Can anybody give me some help?
User avatar
Silverthorn
 
Posts: 67
Joined: Fri Nov 08, 2013 12:29 am
Location: Brisbane Australia

Re: Need help with db put/get

Postby Silverthorn » Wed Nov 13, 2019 3:15 am

Well I found the problem myself.

I was trying to load the db with values that I only populated afterwards.

SR_Confirmation = instance.parameters.SR_Confirmation;
SR_Offset = instance.parameters.SR_Offset;
Speed = instance.parameters.Speed;

needs to be before

db:put(tostring("SR_Offset"),tostring(SR_Offset));
db:put(tostring("SR_Confirmation"),tostring(SR_Confirmation));
db:put(tostring("Speed"),tostring(Speed));
User avatar
Silverthorn
 
Posts: 67
Joined: Fri Nov 08, 2013 12:29 am
Location: Brisbane Australia


Return to Discussions

Who is online

Users browsing this forum: No registered users and 5 guests