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
Can you help answer these questions from other members on futures io?
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:
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?
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
The following user says Thank You to ABCTG for this post:
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
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
The following user says Thank You to ABCTG for this post:
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.
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:
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
The following user says Thank You to Dvdkite for this post:
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.
The following 2 users say Thank You to ABCTG for this post:
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.
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
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?
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.
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.
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.