Looking for Linear Regression Indicator code that display up and down trend - futures io
futures io futures trading



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


Discussion in EasyLanguage Programming

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




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

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

(login for full post details)
  #1 (permalink)
Trieste Italy
 
 
Posts: 159 since Feb 2018
Thanks: 129 given, 20 received

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 futures io?
NT8 indicator to simulate a Funding Evaluation where to …
NinjaTrader
Where can I find Cost to Trade and Book Depth info?
Emini and Emicro Index
Automatic pattern research with NT
Elite Algorithmic NinjaTrader Trading
ZigZag with Depth and Backstep
EasyLanguage Programming
Which kind of Blockvolume is visible? - order processing
Emini and Emicro Index
 
 
(login for full post details)
  #3 (permalink)
Trieste Italy
 
 
Posts: 159 since Feb 2018
Thanks: 129 given, 20 received


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
 
(login for full post details)
  #4 (permalink)
 
 
Posts: 2,230 since Apr 2013
Thanks: 409 given, 1,471 received

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
The following user says Thank You to ABCTG for this post:
 
(login for full post details)
  #5 (permalink)
Trieste Italy
 
 
Posts: 159 since Feb 2018
Thanks: 129 given, 20 received


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
 
(login for full post details)
  #6 (permalink)
 
 
Posts: 2,230 since Apr 2013
Thanks: 409 given, 1,471 received

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
The following user says Thank You to ABCTG for this post:
 
(login for full post details)
  #7 (permalink)
Trieste Italy
 
 
Posts: 159 since Feb 2018
Thanks: 129 given, 20 received


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
The following user says Thank You to Dvdkite for this post:
 
(login for full post details)
  #8 (permalink)
 
 
Posts: 2,230 since Apr 2013
Thanks: 409 given, 1,471 received

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
The following 2 users say Thank You to ABCTG for this post:
 
(login for full post details)
  #9 (permalink)
Trieste Italy
 
 
Posts: 159 since Feb 2018
Thanks: 129 given, 20 received


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
The following user says Thank You to Dvdkite for this post:
 
(login for full post details)
  #10 (permalink)
 
 
Posts: 2,230 since Apr 2013
Thanks: 409 given, 1,471 received


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
 
(login for full post details)
  #11 (permalink)
Trieste Italy
 
 
Posts: 159 since Feb 2018
Thanks: 129 given, 20 received


ABCTG View Post
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

That's interesting to understand. So, I'm using 100 tick bars setup for the graph in my strategy, and it could be a problem beacause if in those 100 tick the price drop down more that the Stop Loss it can lost more since the stop order goes live.

Shall I simply set
 
Code
[IntrabarOrderGeneration = true]
to avoid this problem? Will, by this way, the system send the stop on takeprofit orders immediately after execution of the entry?

The problem is that I have an entry conditions(for example for LONG ENTRY) that has to open a position " at the close of the bar after the bar of the linear regression color change " ( basically at the close price of the bar after the bar with LINEAR_REG > LINEAR_REG[1]). So here I don't want it to be executed during intrabar time...
but of course I want se send IMMEDIATELY the SL e TP order even in intrabars....

Could it be a solution to set [IntrabarOrderGeneration = true] before the stop loss and take profit orders and then se it FALSE after that?

Reply With Quote
 
(login for full post details)
  #12 (permalink)
 
 
Posts: 2,230 since Apr 2013
Thanks: 409 given, 1,471 received

Dvdkite,

attributes like IntrabarOrderGeneration are global for your code, you can't switch them on and off or have it only work for a section within the same code. They would allow to issue the stop and target directly after an entry, but you can accomplish the same using SetStopLoss and SetProfitTarget.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
 
(login for full post details)
  #13 (permalink)
Trieste Italy
 
 
Posts: 159 since Feb 2018
Thanks: 129 given, 20 received


ABCTG View Post
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

Hello again,


So If I understood correctly the "trick" is simply to use SetStopLoss and SetProfitTarget. Both will be executed "REALLY" next tick (so intrabar) Even if for this strategy intrabar order generation is false.

On the other side, if in another stategy I'm going to use intrabar order generation TRUE, the orders below will be executed at the same time as they would be in the form of SetStopLoss and SetProfitTarget.

Am I correct?
Thanks

David

Reply With Quote
 
(login for full post details)
  #14 (permalink)
 
 
Posts: 2,230 since Apr 2013
Thanks: 409 given, 1,471 received

Dvdkite,

I would suggest testing it, so you can see for yourself. The reserved words SetStopLoss and SetProfitTarget can trigger the exits during the bar.

When using intrabar order generation the stop and target orders in your code would be placed as soon as the code detects the MarketPosition being +1 or -1.

Regards,

ABCTG

Follow me on Twitter Reply With Quote


futures io Trading Community Platforms and Indicators EasyLanguage Programming > Looking for Linear Regression Indicator code that display up and down trend


Last Updated on March 6, 2018


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
 

Battlestations! Show us your trading desk - $1,500 in prizes!

March
 

Importance of Finding Your Own Way w/Adam Grimes

Elite only
 

Journal Challenge w/Jigsaw

April
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts