Hi, i have one problem i can't solve. I'm making options for user to write script in my app. I would like to implement function cross like the one on https://www.tradingview.com/study-sc...nce/#fun_cross
this is my implementation,
bool MathScript::cross(int a, int b)
{
bool ret = false;
if(a > b && aPrev < bPrev)
ret = true;
if(a < b && aPrev > bPrev)
ret = true;
return ret;
}
bool MathScript::cross(int a, int b)
{
bool ret = false;
if(a > b && aPrev < bPrev)
ret = true;
if(a < b && aPrev > bPrev)
ret = true;
return ret;
}
To copy to clipboard, switch view to plain text mode
m_Engine = new QScriptEngine(this);
MathScript *scriptMath = new MathScript;
QScriptValue objectValueMath = m_Engine->newQObject(scriptMath);
m_Engine->globalObject().setProperty("math", objectValueMath);
QScriptValue result = m_Engine->evaluate("math.cross(10, 10");
qDebug() << "evaluate " << result.toBool();
m_Engine = new QScriptEngine(this);
MathScript *scriptMath = new MathScript;
QScriptValue objectValueMath = m_Engine->newQObject(scriptMath);
m_Engine->globalObject().setProperty("math", objectValueMath);
QScriptValue result = m_Engine->evaluate("math.cross(10, 10");
qDebug() << "evaluate " << result.toBool();
To copy to clipboard, switch view to plain text mode
Now this, is working, but it is wrong. The problem is i need to store previous values, inside cross(), now sure how to do that. The code is evaluated, then new data comes in, so it has to store previous values.
I guess one solution would be to use
context->callee().setProperty("Aprev", context->argument(0));
context->callee().setProperty("Aprev", context->argument(0));
To copy to clipboard, switch view to plain text mode
but this would be working only for one math.cross();
but the user can enter multiple of them, example
var r1 = math.cross(1, 2);
var r2 = math.cross(3, 4);
Bookmarks