Click here to Login





                                                   How to work with vectors?

  0

0
Peter Gum
2010-10-18 14:49:29


This to educate me. I wish to filter from my watchlist any stock that has gapped up in a certain way more than a given value anytime in the past n bars.

My first thought is to build vectors from the recent n bars leading up to the current bar, e.g.: a=vector-of-recent-bars(close,20). How do I do that? Then create a vector, b, of recent highs. Vector c would be the difference; the rule would not select a symbol if the max of c exceeds a too-high value in the specified interval.
I guess I could do this: a=close; but that doesn't limit the elements to only the ones I want. Clarifying this for me would be helpful.

With that said, is there a more idiomatic way to go about this?

Thanks for your guidance.

-Pete



QuantShare
2010-10-19 06:07:08

  0

Best Answer
Hi Pete,

Here is how I would do this. First calculate the difference between today low and yesterday's high. A gap up occurs if this difference is the difference is positive.

For stocks that have gapped up more than a given value:
a = 0.5; // Your value
diff = low - ref(high, 1); // Low minus yesterday's high
gappedup = diff > a;

To get stocks that have gapped up more than a given value anytime in the past n bars, you will need to use the "hhv" function (Highest value over a specified period):
a = 0.5; // Your value
period = 20; // Period
diff = low - ref(high, 1); // Low minus yesterday's high
gappedup = hhv(diff > a, period);



Peter Gum
2010-10-19 07:01:56

  0

Excellent! That is just the lesson in lingo that I need. I was going in the wrong direction with the vector approach. (I may be back for more coaching: my next step is figuring out how to package that into a 'rule' or function....) (My programming inexperience is showing; I know. [grin])
Thanks. That's a simple, clean solution to my question. -Pete



QuantShare
2010-10-19 07:09:46

  0

Thanks Pete.

To create a single indicator, select Tools -> Create Functions then add a new indicator.
In the formula editor type the following:

result = cFunctions.CompileFormula("a = 0.5; period = 20; diff = low - ref(high, 1); gappedup = hhv(diff > a, period);").GetVectorDouble("gappedup");

Click on Save to compile the function.

Please don't hesitate to contact us if you have any questions.



Peter Gum
2010-10-19 15:26:01

  0

Thank you. Neat and efficient. Let me work with it . . .

-Pete



Peter Gum
2010-10-19 17:04:34

  0

I need a little more help. This compiles:
result=cFunctions.CompileFormula("diff=close-ref(high,1);gup=hhv(diff>gapAmnt,period);").GetVectorDouble("gup");
(Note that the escape character (\) is not needed.) I parameterized 'a(gapAmnt)' and 'period' and named the function BigUpGap.
But it always returns NaN, found by putting BigUpGap(0.5,20) in a watchlist display-only column.

What am I doing wrong? I added a "return result;" statement, which compiled, but that did not help. Is there a way I can debug this step-by-step?

On saving, a message advised me to 'call cFunctions.SetForwardAndBackwardBars'. Where should that be called from?

Thank you for your help. -Pete



QuantShare
2010-10-20 05:34:45

  0

- The formula returns NaN because there is an error in this formula. You are accessing unknown variables in the vector-based language (gapAmnt and Period).
You should know that the vector-based language and the .Net scripts do not share any variable. You have defined gapAmnt and Period in the .Net script.

The formula should be:
result=cFunctions.CompileFormula("diff=close-ref(high,1);gup=hhv(diff>" + gapAmnt[0] + "," + period[0] + ");").GetVectorDouble("gup");

- You can debug any .Net script using the function "Global.Trace". The output will be displayed in the trace window (View -> Output).

- The "SetForwardAndBackwardBars" is optional and specify the number of past and future bars the formula reference. For example: 20-Bar Simple Moving Average references 20 past bars and 0 future bars. This function is for optimization purposes.
Example: After the "result=cFunctions.CompileFormula" line you can type:
cFunctions.SetForwardAndBackwardBars(period[0], 0);



Peter Gum
2010-10-20 07:40:28

  0

Thanks for yourpatience; things are getting clearer.
Your change compiles and works as expected, although it still generates a few NaN's (it generates NaN's for RSI on the same few symbols). The charted data looks fine to me.
When to use vector notation and when not to gets confusing, as you saw in this case, and there are no warning messages that a 'mix' of symbol types has been used. That's probably not easy to detect?
Adding the last line of your post does not compile: period[0] "cannot convert double to int". I'm not sure what that is about?

-Pete



QuantShare
2010-10-20 09:27:35

  0

- You should get NaN only on the first 20 (period) bars.

- We will add a warning message if there are errors in formula.

- For the "SetForwardAndBackwardBars", I forgot the integer conversion:
cFunctions.SetForwardAndBackwardBars((int)period[0], 0);



Peter Gum
2010-10-20 10:24:16

  0

I don't know what I did, and my formula has not changed, but the NaN's have disappeared from my display as have the associated symbols (they got filtered out on some basis.)

I'm not sure how I should have known that a simple cast might solve my problem? How could I have found out the 'type' of 'period'? In the varialbles list it is typed simply as a 'number'.

In any event this is 'not available in this context': cFunctionsSetForwardAndBackwardBars((int)period[0],0);

-Pete



QuantShare
2010-10-20 13:54:24

  0

Custom function parameters are either double or string arrays.

You missed the point between "cFunctions" and "SetForwardAndBackwardBars".

To see the list of available functions, click on CONTROL + SPACE. If you type "." after "cFunctions" you will see the list of functions available in "cFunctions" class.



Peter Gum
2010-10-20 16:51:39

  0

Oops. Thank you; I missed that one. You have been very helpful.
Its a powerful language for expressing trading ideas concisely and clearly. You have taken care of a lot of boilerplate which should translate into efficient coding for us traders. But it takes some getting used to.
Nothing is as educational as well commented code, so I encourage you to identify examples of instructive coding when you see it. Pointing to things we should download if only for the purpose of examining the code would be a helpful step.
I'm on my learning curve . . .

-Pete



QuantShare
2010-10-21 05:46:04

  0

It is a very good idea to download trading objects and then examining the code behind them.


Peter Gum
2010-10-21 07:00:31

  0

For my function the "Return Numeric Array' is checked, but I do not know whether it should be or not? What choice does that make for my function?


QuantShare
2010-10-21 07:35:43

  0

If it is checked then the function returns a numeric vector/array.
If it is not then the function returns a string vector/array. (Example: Industry() function)



John Lyons
2012-10-07 03:50:59

  0

"It is a very good idea to download trading objects and then examining the code behind them."

Please forgive my nubeness...

Can you please explain how I do this? I've tried opening a few objects that I have already downloaded and used, but I cannot see their code.

Thank you.

John.



GS
2012-10-07 05:37:50

  0

Go to Tools -> Create Functions and select the function you have downloaded, you will be able to see the code.


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
How far are the close prices from the support line
Minimum Variance Optimization with Solver V1.1- AMM
Intraday Trading System with Daily Ranking
Double / Triple Smoothened EMA with Flexible Alpha
Enhanced Candlestick Chart with Volume Variation

How-to Lessons
How to use QuantShare with Metastock data
How to associate an index with a list of stocks
How to screen for stocks having a high correlation with the Dow J...
How to create a chart with a black background
How to speed up watchlist and screener plug-ins when working with...

Related Forum Threads
How can I see charts with logarithmic scale on y-axis?
How are corporate action adjustments treated with Yahoo Finance
How QuantShare interacts with market circuit breakers?
How to asci import file with date in this format YYYYMMDD
How do I use version 3.42 with custom data?

Blog Posts
Industry Analysis - How to Compare Stocks with their Industries
How to Backtest an Intraday Stock Trading System with EOD Ranking
How scripts communicate with each other
How to Optimize a Trading System with Thousands of Billions of Co...
How to measure market strength with the composite tool









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.