OmniTrader Professional Forum OmniTrader Professional Forum
forums calendars search
today this week
 
register logon control panel Forum Rules
You are currently browsing as a guest.
You should logon to access more features
A Self-Moderated Community - ALL MEMBERS, PLEASE READ!
Vote for Members who contribute the most to your trading, and help us moderate content within the Forums.


  Current location        Thread information  
OmniTrader Professional Forum
OmniLanguage Discussion
Linear Regression Channel - Example Code
Last Activity 7/6/2025 3:47 AM
30 replies, 28672 viewings

Jump to page : 1 2
Now viewing page 1 [25 messages per page]
 
back reply
Printer friendly version

^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : Linear Regression Channel - Example Code
Posted : 6/13/2009 12:55 PM
Post #12185

This indicator is not only a powerful tool for analysis, which fits into the same kind of thinking as N's Fulcrum Tool, but also provides a bucketful of coding-technique examples.

Amongst other things, it shows:
1. how to use PlotPriceTrendLine to show both line-segments and extensions
2. how to calculate a LinReg fit to a set of data (corrected code)
3. how to manually calculate Standard Deviation
4. how to limit calc-time to just the historical bars required
5. how to provide price-basis alternatives to the user
6. how to specify an input date, and how to find the bar it matches
7. how to use the DebugMsg statements to help with program development
8. how to use iif() and line-wrap ( _) features
9. how to assign an initial value in a Dim statement
10. how to name variables for easy reading and short code lines
11. how to use indentation to make code easier to read
12. how to use comments to explain what's going on

The line-colors are set up for use on a black background. If you normally use a light-colored background, you should manually change the colors in the code.

This code corrects an error in LinReg calc's that previously was distributed as a part of various line-drawing indicators (not the standard ones that Matthew has posted from the Nirvana library). The fix is noted in this code with a comment that contains "***fxd***". If you have other code that uses similar LinReg calc's, I strongly suggest that you double-check it against this implementation.

The code also illustrates how to get around a BUG that is currently in OLang - when PlotPrice or PlotPriceTrendLine are used in the indicator, OT automatically opens a new Indicator pane when the indicator is applied to the chart, even though all the plots should have been in the Price pane. By using at least one PlotPrice statement, it provides an object that can be "grabbed" in the extra pane and moved onto the Price pane. This makes the extra pane disappear, and if you save the Template, it will always look the way it's supposed to when you come back to it. When this OLang/OT bug is finally fixed, no change to the code is required - the Price line that is the basis for the LinReg calc will plot correctly in the first place.


Enjoy!



[Edited by Jim Dean on 3/28/2015 7:26 AM]

Attached file : aJDndLinRegChan.rtf (9KB - 1539 downloads)
Attached file : aJDndLinRegChan.png (18KB - 2931 downloads)

^ Top
THOMAS HELGET

Elite
500100
Posts: 610

Joined: 3/22/2006
Location: BALDWINSVILLE, NEW YORK

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 6/13/2009 2:18 PM
Post #12187 - In reply to #12185

Jim:

Double WOW!

Thanks so much for continuting to work on this marvelous piece of coding. A wonderfully professional effort as always!

Tom Helget

[Edited by THOMAS HELGET on 6/13/2009 2:19 PM]

^ Top
mikemc

New User

Posts: 4

Joined: 10/8/2007
Location: denver, co

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 6/22/2009 12:47 PM
Post #12305 - In reply to #12185

Awesome.
^ Top
Super Trader

Regular
2525
Posts: 63

Joined: 12/12/2007

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 6/22/2009 1:04 PM
Post #12307 - In reply to #12185

Thanks Jim

excellent :)
^ Top
rayphua

Member

Posts: 16

Joined: 11/15/2007
Location: Singapore

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 6/25/2009 11:34 PM
Post #12329 - In reply to #12185

Hi Jim or anyone,

Can you help advice how we can code a system that would fire a long the moment the current price or close price hits above the upper line of the regression channel, or a short when the price hits below the lower line? Thanks in advance, and a great piece of coding Jim. : )
^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 6/26/2009 6:06 AM
Post #12330 - In reply to #12329

Sure.

1. Use the regression tool to create the band.
2. Visually overlay N's Fulcrum tool and set its param's so the signals fire when you want.


If you're looking for something more comprehensive, that does the entire process automatically (no manual placements req'd), such that it comprises a complete trading strategy that is backtestable -
Well, that is a Much more complex problem. I'm working on it now - it will have a couple thousand lines of code, or more before it's complete.

It will be available for sale, some way or another, if it tests out OK.
Lots of work still to do, so be patient. :-)


^ Top
julesrulesny

Legend
100100100100252525
Posts: 498

Joined: 8/28/2009
Location: NYC

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 10/9/2009 5:51 PM
Post #13450 - In reply to #12330

Very impressed with this as i am just beginning to understand Linear Regression.
Anyone know of any good readings to understand it and this code?? I've read some in random books but never got the full understanding of it..

I am currently going back to school to get a degree in Mathematics.. hopefully that'll help some..
^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 10/9/2009 6:53 PM
Post #13452 - In reply to #13450

Just look up "Least Squares" or "Linear Regression" in wiki or investopedia, etc.

It's a simple way of fitting a straight line to a set of data points, such that the error of the fit is minimized.

There's not much more to it than that.

The code is something I created ... it applies the math needed to do the LinReg calcs.

There is also a set LinReg functions avail in OT's library ... some more useful than others.

^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 10/9/2009 6:55 PM
Post #13453 - In reply to #13452

BTW this code is not "beginners level". But it does have a lot of useful techniques in it.
^ Top
JB

Legend
10010010010025
Posts: 434

Joined: 6/6/2006

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 1/8/2010 5:14 AM
Post #14827 - In reply to #13453

Jim Thank you so much for posting this it is something that I have wanted since I first fired up OT havent tried it yet but it looks to be fantastic.
Well done JB
^ Top
JB

Legend
10010010010025
Posts: 434

Joined: 6/6/2006

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 1/10/2010 7:09 PM
Post #14832 - In reply to #12185

Jim I have now had a good play with the indicator and like it lots. There is an enhancement that I would love to see. I would like to know if the date setting arrangement could be adjusted so that the day could be selected and then the bars moved about by a param slider throughout the session at the back date.

You have made this function avaliable in the dates off mode for the recent session. I think that it hinges on the use of this SymbolData.Numrec code which I do not understand.

Thanks Jim for a great tool which should have been in OT and VT long ago.
^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 1/11/2010 6:05 AM
Post #14835 - In reply to #14832

I'm glad it has been helpful.

I'm sorry but I don't know what you mean by moving bars though a session, or a "back date".
A picture with some markups might help.


^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 1/11/2010 6:11 AM
Post #14836 - In reply to #14835

Btw symboldata.numrec is just an internal variable that holds the total number of bars that are historically loaded for the symbol in question.

The internal variable "bar" holds the bar number of the current bar being processed. The internal loop goes from past to present. The first bar number is zero - the most recent bar number is symboldata.numrec-1.

That's about it.
^ Top
JB

Legend
10010010010025
Posts: 434

Joined: 6/6/2006

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 1/11/2010 7:30 AM
Post #14839 - In reply to #12185

Jim sorry if I have not explained what I mean to convey clearly. I am invariably using the 1 or 5 min intraday timeframe.

Your indicator allows one to show the indicator for the most recent session and adjust the number of bars back from the current bar. When the endbarsago is adjusted form max to min the indicator can be walked forward through the session.

This for me is the primary use of this adjustment It allows me to analyse the state of the Lreg channel realative at any time to other system signals.

The Year month day date adjusment facilitates the indicator to be set to a back date but having set this you cannot then move it forward or backward within that session or the immediately preceeding one.

I hope this clarify my point?
Thanks for your attention JB
^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 1/11/2010 11:40 AM
Post #14842 - In reply to #14839

The indicator was initially designed for EOD use, but it does work OK for RT as well.

It was provided primarily as an "educational" offering, to show how to accomplish a number of different tasks in the code, such as working with dates, calculating Linear regressions, and drawing line-extensions.

The code could be modified to include spec of hours / mins / secs as "begin" times for RT, in addition to the YMD. It could also be modified to eliminate the specific date/time for the beginning of the segment, and just have a "BgnBarsAgo" input.

You can adjust the starting point using the current indicator by specifying a nonzero BarLength and EndBarsAgo INSTEAD OF using the date format ... just set any of the three date inputs to zero to deactivate it. By adjusting those first two inputs, you can put the bar just about anywhere you want.

If you'd like to have it work some other way, then why not try fiddling with the code yourself? That's why I posted it ... so that others could learn how to fish :~))

^ Top
JB

Legend
10010010010025
Posts: 434

Joined: 6/6/2006

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 1/11/2010 12:19 PM
Post #14844 - In reply to #12185

Jim you are kind thank you once again I have already had a play to see what I could come up with . It is very instructional for me and I will hope to pass the reasonable student level soon Thanks for the free hand should we post any mods for comment?
^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 1/11/2010 12:28 PM
Post #14845 - In reply to #14844

In order to keep this thread fairly clean, why not post any offshoot mod's in a sep new thread ... I'll see the posts and will be happy to help you on your journey.
^ Top
JB

Legend
10010010010025
Posts: 434

Joined: 6/6/2006

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 1/12/2010 6:03 AM
Post #14859 - In reply to #12185

Jim Thanks for the suggestion and what a good idea. When I have something I will post it seperately and give notice here to say the new thread is up.
Thnaks for the kind offer of guidance and support it will be most gratefully received.

[Edited by JB on 1/12/2010 6:06 AM]

^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 1/14/2010 11:21 PM
Post #14896 - In reply to #14859

I've recently received some requests for further explanation about how to use this indicator. I originally posted it as a good code example ... but it IS most definitely a useful little tool for its own sake.

So, I'm attaching a chart template and a picture to illustrate some things about it. The full screenshot is really big, so I broke it into two pieces for posting here ... but the full version is attached as well. The top picture is the left part of the chart and the next one down is the right part of the chart.





[Edited by Jim Dean on 3/28/2015 7:26 AM]

Attached file : LinRegChan Demo Screen - Right.png (59KB - 2278 downloads)
Attached file : LinRegChan Demo Screen - Left.png (61KB - 2273 downloads)
Attached file : LinRegChan Demo Screen - FULL.png (120KB - 961 downloads)

^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 1/14/2010 11:44 PM
Post #14897 - In reply to #14896

Comments about the charts I just posted:

FIRST, PLEASE READ THE STEPS 1-5 and a-c and the *'d notes ... that's the nutshell. The parameters are shown in the open properties-box on the left.

NOTE that the sequence of the header-line indicator-ID's is NOT the same as the order that the LinReg's are painted. A little "puzzle" for you to figure out :~))

The yellow underlines at the top show the settings for the line length (first), and the number of barsago (ie back from right edge) that the line ENDS at. The FINAL indicator-ID on the far right uses the line length, ignores the barsago, and uses the DATE inputs to define the VERY FIRST "A" LinReg zone.

The cyan underlines at the top show the Std Dev bandwidth multiplier settings for each case. These were adjusted by eyeball to "contain the action" (at least, as I see it).

OK ... maybe a few random comments are in order re each instance ...
A
This is a nice example of an untradeable consolidation zone. Price breaks out of the zone to the downside then shoots up ... a "head fake" ... but note that it did not violate the support levels from the bars at the far left of the chart. Note that the StdDev input is wider here than for any other segment ... typical for consolidations.
B
Here's a nice run up. Very tight StdDev multiplier allows the bands to give you an excellent exit signal ... presumably intraday, when price crosses the band (maybe a partial exit), with full exit once price closes across the band. You could choose to exit, or if you've a mind to, just reverse the position.
C
A shorter run, but still gives nice tight bands with clear and good exit, if you chose to reverse out of the prior run. Or, if you use the C-retrace as a "breather" ... draw the lines anyway to find a good entry for the next move ...
D
Nice entry out of C, and again a fast, tight band with a great intraday exit point (top of the big red bar).
E
Well ... I drew the lines after the gap ... but NOTE that you got a very early indication of the beginning of this drop, from the clean exit out of D ... you could have easily captured the gap. I find that it's sometimes best to ignore big gaps for band-drawing and build the band that searches for an exit after the gap. But ... try it both ways. Either way, it gives you a decent exit.
F
I could have drawn a consolidation zone in between the end of E and the beginning of F, but the chart would have been too messy. That zone probably would have given a so-so entry for F ... but the clean exit from F would have prevented any loss even if you got in late.
G
The final channel on the far right is STILL IN ACTION ... note that the darker-color extension lines (blue and orange) do not appear, since the most recent bar fits nicely in the band. We'll see how long it lasts.

SUMMARY:
This chart was picked AT RANDOM, btw. You can use this tool on virtually any chart, to ID ups, downs, and flats. This is a key concept that I work with regularly.

^ Top
Duxx

Elite
500100100100252525
Posts: 881

Joined: 2/18/2009

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 2/18/2010 7:45 AM
Post #15443 - In reply to #12185

Hi Jim

Thanks for sharing this

Quick questions:

1) You mentioned that slope is reward and risk (wigle) - so basically as long as the stock is within the channel the slope is intact so there is your reward. Dont understand the Wigle thing - what do you mean with wigle and how this is the risk?

2) I have seen in many post that you like to use the Guts price for many indicators - can you elaborate why do you think this is a better price point than H/L or Close numbers.?


3) What is the difference between having an extension and not having an extension (i.e. Half, Full, etc)
Thank you

[Edited by Duxx on 2/18/2010 7:54 AM]

^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 2/18/2010 8:33 AM
Post #15444 - In reply to #15443

Hi, Duxx ... great questions!

"Slope" is "Reward" in that your reward is how much money you make, and how FAST you make it. Sometimes traders forget the concept of compounding and ROI ... even though a slope of Price vs Time is upwards, it may be so gentle that you might as well put the money in long-term CD's, since they have NO risk. So, being aware of the Slope, in terms of DOLLARS PER DAY, is an IMPORTANT "Filtering" criteria for evaluating potential trades.

"Wiggle" is "Risk" in that the very short-term ups and downs ... the bar-to-bar ATR "wobble" (like a drunkard that's been to TOO MANY "bars"), illustrates the RISK involved in taking a trade. The "wiggly-er" the price action is, the more likely you will get stopped out by a deep enough pullback that you decide the trade was a mistake. So, being aware of VOLATILITY-wiggle, in terms of DOLLARS PER DAY, is also an IMPORTANT "filtering" criteria.

To do a balanced job of evaluating those two metrics, you need to COMBINE THEM - a "hard number threshold" for volatility is not a good filter, since obviously the daily-ATR volatility of GOOG will be different than that of CIEN ... and a visually GENTLE-sloped GOOG will have higher DAILY price change than a fairly STEEP-sloped CIEN. However, if you DIVIDE the Slope by the Volatility for each of those stocks, they can be compared on a Risk/Reward basis.

Here's how: first, define what you consider to be your "typical hold time" for a run that ends up with some profit. Let's say it's ten days. If you multiply ten days times the dollars per day slope of the line you are looking at ... that tells you your potential Reward.

Then define what you consider to be a minimum-safe initial entry-stop cushion(delta) - that defines your Risk. Finally, divide Reward by Risk ... if you don't get a value of about 2:1 or higher, I'd question whether the trade is worth it ... and if it's less than 1.5:1, I'd say fugeddabouddit. If it's more than 4:1, then be aware that slopes like that rarely last very long, and if you try to catch that wave, you'll probably enter just before it does a retrace.

OK ... about Guts.

I'm sorta proud of this one. I "invented" it a long time ago, and have found it to be VERY useful. It's one of a handful of ideas that I've come up with (along with its siblings "Wick" and "Shadow") that I think would benefit the entire industry.

"Guts" is simply a weighted average of the prices on the bar. The "body" portion of the bar (fat part of the candle) typically has more volume-action during the day than the "tails" portion of the bar (above and below the body). So, if you want a REPRESENTATIVE PRICE for that bar, which shows what most traders during THAT BAR felt the stock was "worth", a WEIGHTED combination of OHLC should be used. For "Guts", I choose to weight O & C (the body) twice as much as H & L (the tails). That is:
Guts = (H+L + O+O + C+C) / 6

Historically, people use Close as the "most representative price". Well, if you have to choose between OHLC, I fully agree. But, when using programmable platforms like OT, we DON'T have to choose ... thus, Guts is borne.

There is an easy way to demonstrate the value of Guts (hoo-rah!). Create this simple Indicator and plot it on a candle-chart with a black background:
#Indicator
dim Guts as single
Guts = (H+L + O+O + C+C) / 6
plotprice("Guts",Guts,white,2)
plotprice("Close",Close,yellow,2)
plotprice("GutsMA",SMA(Guts,3),cyan,2)
plotprice("CloseMA",SMA(Close,3),orange,2)
Return Guts

First, notice which lines look "smoother" ... the ones related to Guts typically do. YET THERE IS NO "LAG", which usually is the penalty you pay to get smoothness.

Next, SBAS (Step Back And Squint) ... look at WHERE the white and yellow lines are in relation to the general movement of the candles ... the white lines tend to be more in the "center".

I like to think of Guts as an "extremely fast MA of Close, without lag". I find that using Guts for MA's and other indicators offers noticeably better results for SHORT period situations. The SMA(Guts,20) is virtually the same as SMA(Close,20) ... but SMA(Guts,2) is meaningfully different than SMA(Close,2). The breakpoint is somewhere between 5 & 10, depending on what indicator you are using.

Note ... when you are writing code for things like candle patterns or stoploss exits, etc, you SHOULD use CLOSE in most cases. REASON ... Close has a "TIME" associated with it .. and those kinds of decisions are time-based ones. So, Guts is not the solution to all things, but it does help with a lot of them.

FINALLY ... about the "Guts Family" ...
I created two MORE price points, which I call Wick and Shadow (Shad for short) ... these are the mid-way points of the tails, above and below:
Wick = ( H + Math.Max(O,C) ) / 2
Shad = ( L + Math.Min(O,C) ) / 2
I consider Wick and Shad to be useful substitutions for H & L, in situations where I am worried that bar's "spike" up or down might be formed from just one or a few "fluke" trades ... I don't like to have my trading decisions based on things like that. So, when I write Pivot code, or draw LinReg channels, I will often base it on Wick and Shad instead of H & L. Again, without going through a bunch of examples, it just "looks righter" to me on the chart ... it's what my eyeball gravitates to, rather than the pointy sharp ends.

There are, of course, more pricepoints like this ... it's a big family, the others are occasionally useful but not quite as important:
Neck = Math.Max(O,C)
Foot = Math.Min(O,C)
Body = (O + C) / 2
Range = (H + L) / 2
Morning = (O+O + H+L) / 4
Afternoon = (H+L + C+C) / 4

The industry long ago defined a price that is also a cousin of the family:
Typical = (H + L + C) / 3
(I think Guts is MUCH more useful than this, but I include it for completeness).

One cute little factiod that you may not have noticed ... ALL of these Guts-family price-point names have UNIQUE first-letters: G, W, S, N, F, B, R, M, A (and T). And none of them get confused with our friends O, H, L, C (or V).

So, who knows? Maybe one of these years, some software might come out that supports ALL the pricepoints, natively:
OHLCVGWSNFBRMAT
Sounds like someone who is trying to talk with a mouthful of food.

I hope this was useful to you, and makes you think over some of the "basic assumptions" that everyone seems to take for granted.

[Edited by Jim Dean on 1/17/2011 7:25 AM]

^ Top
JB

Legend
10010010010025
Posts: 434

Joined: 6/6/2006

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 2/18/2010 4:11 PM
Post #15457 - In reply to #12185

Jim thanks for this. It is an area that I have been wondering about and you seem to have it buttoned up. I like guts and have been using them in the regression as per your default.

I have just parked the code into an indicator and I like its natural smoothing effect or that at least is the visual impression when used to plot linreg(guts,period) as opposed to linreg(period)
^ Top
Duxx

Elite
500100100100252525
Posts: 881

Joined: 2/18/2009

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 9/15/2010 1:35 PM
Post #17117 - In reply to #14897

Jim

Have you tried or developed a stop that exits on when the price cross over the lower line or upper line of the channel?

Thanks
^ Top
Jim Dean

Sage
2000100010010010010025
Posts: 3433

Joined: 3/13/2006
Location: L'ville, GA

User Profile
 
Subject : RE: Linear Regression Channel - Example Code
Posted : 9/15/2010 1:51 PM
Post #17121 - In reply to #12185

Hi, Duxx ...

I've done a lot more with this, yes ... but I'm not giving it away for free. Sorry ;~)
Jump to page : 1 2
Now viewing page 1 [25 messages per page]
back reply

Legend    Action      Notification  
Administrator
Forum Moderator
Registered User
Unregistered User
E-Mail this thread to a friend
Toggle e-mail notification


Nirvana Systems
For any problems or issues please contact our Webmaster at webmaster@nirvsys.com.