NexusFi: Find Your Edge


Home Menu

 





Looking for Linear Regression Indicator code that display up and down trend


Discussion in EasyLanguage Programming

Updated
    1. trending_up 5,462 views
    2. thumb_up 6 thanks given
    3. group 2 followers
    1. forum 13 posts
    2. attach_file 1 attachments




 
Search this Thread

Looking for Linear Regression Indicator code that display up and down trend

  #1 (permalink)
Dvdkite
Trieste Italy
 
Posts: 162 since Feb 2018
Thanks Given: 131
Thanks Received: 25

First of all HELLO TO EVERYONE, this is my first post here :-D

I'm Looking for a Linear Regression Indicator code that display up and down trend with two different colors to use in multichart to test a strategy based on entries made where the color is changing (means the trend is supposed to invert) and in different time frame of the day.

I'm very new to easy language programming so any suggestion are appreciated.

Thanks in advance.

REgards,

David

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
Deepmoney LLM
Elite Quantitative GenAI/LLM
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
38 thanks
NexusFi site changelog and issues/problem reporting
27 thanks
GFIs1 1 DAX trade per day journal
18 thanks
The Program
18 thanks
  #3 (permalink)
Dvdkite
Trieste Italy
 
Posts: 162 since Feb 2018
Thanks Given: 131
Thanks Received: 25


Hi Again ...

I tried to modify the standar LINEAR REGRESSION CURVE provided by multichart in order to use 2 different colors in order to show the UP and DOWN trends... here's what I've done:

 
Code
inputs:
	Price( Close ),
	Length( 9 ),
	Displace( 0 ) ;

variables:
	var0( 0 ) ,
	colorOfRegLin(yellow);

condition1 = Displace >= 0 or CurrentBar > AbsValue( Displace ) ;
if condition1 then 
	begin
	var0 = LinearRegValue( Price, Length, 0 ) ;
	Plot1[Displace]( var0, "LinReg",colorOfRegLin,2 ) ;

	                  
	if Displace <= 0 then 
		begin
		condition1 = Price > var0 and var0 > var0[1] and var0[1] <= var0[2] ;
		if condition1 then
			colorOfRegLin = white 
		else 
		begin 
		condition1 = Price < var0 and var0 < var0[1] and var0[1] >= var0[2];
		if condition1 then
			colorOfRegLin = blue ;
		end ;
		end;
	end ;
Actually it seems to work ...
But I'm not sure if it is working ALWAYS CORRECLTY. By setting the Chart with 100 ticks and 131 value for the linear regression I can see the most of the trends... but Sometimes it doesn't change color where it is supposed to do that..



Can you see in the image that almost in the middle of the graphic there's a big UPtrend STILL in BLUE?? it shouls be white!

Anyone can try it? Maybe with the FDAX so we can compare it?

Thanks

Reply With Quote
  #4 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623

Dvdkite,

your conditions for setting the color are very restrictive i.e. if the price doesn't match your Price > var0 or Price < var0 condition on the exact bar where the slope of the regression changes, the color wouldn't be changed.

Apart from that you might want to set the color before you plot the value, so the new color can be used right away.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #5 (permalink)
Dvdkite
Trieste Italy
 
Posts: 162 since Feb 2018
Thanks Given: 131
Thanks Received: 25


ABCTG View Post
Dvdkite,

your conditions for setting the color are very restrictive i.e. if the price doesn't match your Price > var0 or Price < var0 condition on the exact bar where the slope of the regression changes, the color wouldn't be changed.

Apart from that you might want to set the color before you plot the value, so the new color can be used right away.

Regards,

ABCTG

HIT ABCTG thanks for the answer. As I said I'm very new to strategies programming. My code was a simple modifications of the Linear Reg Curve indicator included in the power language editor.. I saw the row with the Alert ("trend is turning Up") and I simply changed it into the color modifications (same thing for the Dw trend alert).
So How would you modify this code in order to make it function correctly?
Thanks again :-D

Reply With Quote
  #6 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623

Dvdkite,

it would depend a bit on your exact condition for the color change. You can for example use the reserved words "crosses over" and "crosses under" to check for a change in the slope of the linear regression.
This check should be performed before (i.e. above in the code) your call "Plot1", so it can use this changed color already. Otherwise you might have a delay of one bar.

If you want to make your programming life easier in the long run, I would suggest to start using meaningful variable names and not generic ones (like var0 or Condition1). This will make your code easier to read and in turn save you time.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #7 (permalink)
Dvdkite
Trieste Italy
 
Posts: 162 since Feb 2018
Thanks Given: 131
Thanks Received: 25


ABCTG View Post
Dvdkite,

it would depend a bit on your exact condition for the color change. You can for example use the reserved words "crosses over" and "crosses under" to check for a change in the slope of the linear regression.
This check should be performed before (i.e. above in the code) your call "Plot1", so it can use this changed color already. Otherwise you might have a delay of one bar.

If you want to make your programming life easier in the long run, I would suggest to start using meaningful variable names and not generic ones (like var0 or Condition1). This will make your code easier to read and in turn save you time.

Regards,

ABCTG

Hi ABCTG and thanks again for your suggestion.
As suggested from a friend I was able to obtain the correct linear reg curve with the up and down trend simply removing some restrictions from the conditions... It seems to work correctly.

 
Code
inputs:
	Price( Close ),
	Length( 131 ),
	Displace( 0 ) ;

variables:
	var0( 0 ) ,
	colorOfRegLin(yellow);

condition1 = Displace >= 0 or CurrentBar > AbsValue( Displace ) ; 
if condition1 then 
	begin
	var0 = LinearRegValue( Price, Length, 0 ) ;
	Plot1[Displace]( var0, "LinReg",colorOfRegLin,2 ) ;
		                  
	if Displace <= 0 then 
	     begin
   	 	if condition1= var0 > var0[1] then begin

          	 //Alert( "Indicator turning up" ) 
    		  SetPlotColor[1](1, cyan);  // con questo settaggio cambia colore una candela prima(credo corretto)
    		 //colorOfRegLin = cyan 
     		   		 
        	end
   	else begin 
       if condition1=var0<var0[1] then
               //Alert( "Indicator turning down" ) ;
                SetPlotColor[1](1, yellow);
               //colorOfRegLin = yellow; 
      		end;
		end;
	end;
Now I'm doing some test to better understand how the entries work in MC. I'm trying to buy when the indicator is supposed to turning up and I was tryng to create an OCO order that, If I'm not wrong, here at MC it should be coded by this way:

 
Code
if (marketposition = 0) then begin 
	buy 1 contract next bar at market;
	setstoploss(EntryPrice - 20);
	setprofittarget(Entryprice + 20); 
end
actually I'm totally confused because when I applied this signal to the FDAX 03 ( I only have something like 4 or 5 tick days of quotation (webank with t3 open give you only 1 day tick data.... ) at the first run it said that the MaxBarBack was to low(50) to calculate strategy.. then I went to the property strategy and changed it to 100.. nothing.. then 10000..nothing... then with 1000 I was able to see a lot (a hundred in 2 days) of "wrong operations"... with 2000 I can only see 1 Operation...

so what is the correct value for MaxBarsBack? what does it really mean? I (of course) checked documentation but it cannot explain to me the difference in the result....

Let me explane better: If I wanto to back test a strategy over 1 YEAR of Future tick by tick, what kind of value shall I set? I don't think that the system is requiring the total number of hystoric bars to analyze....

Regards,

David

Reply With Quote
Thanked by:
  #8 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623

David,

the Maximum Number of bars a study will reference are the number of bars that your studies require to be computed.
Assuming you have an average with the length of 100, this would require 100 bars to be on the chart. Consequently a strategy with an average with the length of 100 will raise the error message when you set Max Bars back lower than 100. In your case the regression requires at least "Length" number of bars before it will be computed.

It's best to use SetStopLoss and SetProfitTarget unconditionally. In your code they actually wouldn't be placed when you are in a position, because at that moment MarketPosition is different than zero.

Regards,

ABCTG

P.S.: By the way, you can use the "Thanks" button next to a post on futures.io to show your appreciation for a post.

Follow me on Twitter Reply With Quote
Thanked by:
  #9 (permalink)
Dvdkite
Trieste Italy
 
Posts: 162 since Feb 2018
Thanks Given: 131
Thanks Received: 25


ABCTG View Post
David,

the Maximum Number of bars a study will reference are the number of bars that your studies require to be computed.
Assuming you have an average with the length of 100, this would require 100 bars to be on the chart. Consequently a strategy with an average with the length of 100 will raise the error message when you set Max Bars back lower than 100. In your case the regression requires at least "Length" number of bars before it will be computed.

It's best to use SetStopLoss and SetProfitTarget unconditionally. In your code they actually wouldn't be placed when you are in a position, because at that moment MarketPosition is different than zero.

Regards,

ABCTG

P.S.: By the way, you can use the "Thanks" button next to a post on futures.io to show your appreciation for a post.


Thanks againg (also with all buttons :-D), I understand how MaxBarsBack is working.

About the stop loss I changed the set up using this form (and the entry position is on another place with its proper condititions):

 
Code
if marketposition = 1 then begin

	Sell("TargetLong") 1 contract Next  Bar at (entryprice + TakeProfit) limit;
	Sell("StopLong") 1 contract Next Bar at (entryprice - StopLoss) stop;

end;
	
if marketposition = -1 then begin

	buytocover("TargetShort") 1 contract Next  Bar at (entryprice - TakeProfit) limit;
	buytocover("StopShort") 1 contract Next Bar at (entryprice + StopLoss) stop;

end;
By building a code like this it should work as an "emulated" OCO...

Regards,

David

Reply With Quote
Thanked by:
  #10 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,431 since Apr 2013
Thanks Given: 481
Thanks Received: 1,623


Dvdkite,

the only thing to keep in mind that unless you are working with intra bar order generation, your stop and target won't be placed during the entry bar with the code you posted.

Regards,

ABCTG

Follow me on Twitter Reply With Quote




Last Updated on March 6, 2018


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts