Click here to Login





                                                   newbie looking for function help

  0

0
John Faig
2012-01-17 21:14:38


I am trying to recreate an indicator I used in a different trading platform. It takes three moving averages and creates an indicator of the strength of an uptrend (U1, U2, or U3) or a downtrend (D3, D2, D1). The function has lots of errors. I will eventually figure it out, but would appreciate some hlep to move up the learning curve faster.

var score : int;
var sma20_one_day : double;
var sma65_one_day : double;
var sma130_one_day : double;
//VectorD pj = cFunctions.CreateNumericVector();


VectorD sma20 = cFunctions.CompileFormula("a = SMA(20);").GetVectorDouble("a")
VectorD sma65 = cFunctions.CompileFormula("b = SMA(65);").GetVectorDouble("b")
VectorD sma130 = cFunctions.CompileFormula("c = SMA(130);").GetVectorDouble("c")

VectorD close = cFunctions.Close;
for(int i = 130;i<close.Length;i++)
{
sma20_one_day = sma20.GetValue(i);
sma65_one_day = sma65.GetValue(i);
sma130_one_day = sma130.GetValue(i);

//assume D3
score = 3;
//U1
if (sma130_one_day > sma20_one_day) and (sma20_one_day > sma65_one_day) then score = 1;
//U2
if (sma20_one_day > sma130_one_day) and (sma130_one_day > sma65_one_day) then score = 2;
//U3
if (sma20_one_day > sma65_one_day) and (sma65_one_day > sma130_one_day) then score = 3;
//D1
if (sma65_one_day > sma20_one_day) and (sma20_one_day > sma130_one_day) then score = 1;
// D2
if (sma65_one_day > sma130_one_day) and (sma130_one_day > sma20_one_day) then score = 2;

//pj[i] = score;
result.SetValue(i, score);

}



QuantShare
2012-01-18 06:25:30

  0

Best Answer
Hi John,

- The first problem is that you are combining CSharp and JScript codes.

To initialize a variable in JScript:
var score : int;

In CSharp:
int score = 0;

- Some semi-colons are missing (After "CompileFormula")

- Some errors when using the if condition

"and" should be replaced by "&&"
"then" should be removed

If there is only one line of code after "if" then you can use:

if(a == 5) b = 10;

Otherwise you have to use parentheses:

if(a==5)
{
b = 10;
c = 20;
}

- Some other minor errors

Here is the correct code:

int score;
double sma20_one_day;
double sma65_one_day;
double sma130_one_day;


VectorD sma20 = cFunctions.CompileFormula("a = SMA(20);").GetVectorDouble("a");
VectorD sma65 = cFunctions.CompileFormula("b = SMA(65);").GetVectorDouble("b");
VectorD sma130 = cFunctions.CompileFormula("c = SMA(130);").GetVectorDouble("c");

VectorD close = cFunctions.Close;
for(int i = 130;i < close.Length;i++)
{
sma20_one_day = sma20.GetValue(i);
sma65_one_day = sma65.GetValue(i);
sma130_one_day = sma130.GetValue(i);

//assume D3
score = 3;
//U1
if (sma130_one_day > sma20_one_day && sma20_one_day > sma65_one_day) score = 1;
//U2
if (sma20_one_day > sma130_one_day && sma130_one_day > sma65_one_day) score = 2;
//U3
if (sma20_one_day > sma65_one_day && sma65_one_day > sma130_one_day) score = 3;
//D1
if (sma65_one_day > sma20_one_day && sma20_one_day > sma130_one_day) score = 1;
// D2
if (sma65_one_day > sma130_one_day && sma130_one_day > sma20_one_day) score = 2;

//pj[i] = score;
result.SetValue(i, score);

}





John Faig
2012-01-18 23:21:32

  0

Thanks for the feedback. It looks like there may be something wrong with the VECTORD commands. The sma20 function looks OK because the keywords have different colors. Everything from the sma65 statement and below is colored red.
Any further suggestions are welcomed :-)



QuantShare
2012-01-19 05:24:14

  0

No problems here.

Please double check the code and send a screenshot to support@quantshare.com.



No more messages
0




Reply:

No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"










QuantShare

Trading Items
High to low stock pattern for the close price
Advanced Quotes and Custom Databases Function
Random Value for Each Trading Bar
Average Stock Performance for each U.S Exchange
Time-series Sum for the first Minutes/Hours of a Trading Day

How-to Lessons
How to download earnings calendar data for various stocks
How to screen for stocks having a high correlation with the Dow J...
How to get fundamental data for U.S. Stocks
How to download EOD quotes for active and valid stocks only
How to get stocks for a particular index using the global script

Related Forum Threads
Optimization and fitness function for robustness vs for performan...
Looking for a programmer for hire
TimeframeApply function
Custom Multi-Dimensional Fitness Formula (CMDFF) for Optimizer
Comp Function in Trading System Rules

Blog Posts
Looking for trading ideas
Put-Call Ratio for Individual Stocks
Create custom metrics for the statistical data analysis tool
How to Select the Best Market Indicator for your Trading System
How to get buy and sell orders for a portfolio based on a trading...









QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.