Anyone here good at math?

Questions about MultiCharts and user contributed studies.
User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Anyone here good at math?

Postby furytrader » 15 Mar 2012

I am working on a project that involves creating a complicated multi-line chart overlay for analyzing trends, picking turning points, etc. I am experimenting with first creating one trendline in EasyLanguage, and trying to nail down the math involved with rotating an existing trendline via Easylanguage, once an angle of rotation is defined.

The code I have written allows the user to identify two points on a chart, using date, time and price to identify the location, and then drawing a trendline between those two points.

Here is the code:

Code: Select all

Inputs: PointADate("03/14/2012"), PointATime(2045), PointAPrice(1391);
Inputs: PointBDAte("03/14/2012"), PointBTime(2345), PointBPrice(1393.50);
Inputs: LineColor(DarkBlue), LineWidth(2);
Inputs: RotateAngle(0);

Variables: PointCDate(0), PointCTime(0), PointCPrice(0), PointCBarNum(0);
Variables: PointDDate(0), PointDTime(0), PointDPrice(0), PointDBarNum(0);

Variables: TempADate(0), TempBDate(0),TestDate(0);
Variables: Line1_ID(0);

If BarNumber = 1 Then Begin

TestDate = StringToDate(PointADate);
TempADate = JulianToDate(TestDate);

TestDate = StringToDate(PointBDate);
TempBDate = JulianToDate(TestDate);

End;

If Date = TempADate And Time = PointATime Then Begin

If PointCDate <> 0 Then Begin
PointDDate = Date;
PointDTime = Time;
PointDPrice = PointAPrice;
PointDBarNum = BarNumber;
End;

If PointCDate = 0 Then Begin
PointCDate = Date;
PointCTime = Time;
PointCPrice = PointAPrice;
PointCBarNum = BarNumber;
End;

End;

If Date = TempBDate And Time = PointBTime Then Begin

If PointCDate <> 0 Then Begin
PointDDate = Date;
PointDTime = Time;
PointDPrice = PointBPrice;
PointDBarNum = BarNumber;
End;

If PointCDate = 0 Then Begin
PointCDate = Date;
PointCTime = Time;
PointCPrice = PointBPrice;
PointCBarNum = BarNumber;
End;

End;

If LastBarOnChart = TRUE Then begin

Line1_ID = TL_new(PointCDate,PointCTime,PointCPrice,PointDDate,PointDTime,PointDPrice);
value1 = TL_SetColor(Line1_ID,LineColor);
value1 = TL_SetSize(Line1_ID,LineWidth);

End;
As you can see, the code identifies the bar number and the price for each endpoint of the trendline.

I have seen that the formula for rotating a line about an axis is as follows:

x' = x Cos(θ) - y Sin(θ)
y' = x Sin(θ) + y Cos(θ)

The problem I have is that I don't know how to rotate this line using the coordinate system of price vs. bar number.

In other words, the way to calculate the new bar number (x') incorporates both a price level and a bar number - these are two different units, but the traditional rotation formula presented above is based on having the x-axis and y-axis being of the same units.

Does anyone have any thoughts or suggestions on how to reconcile calculations using two different unit types?

Thanks!

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Anyone here good at math?

Postby TJ » 15 Mar 2012

....
Does anyone have any thoughts or suggestions on how to reconcile calculations using two different unit types?

Thanks!
can you draw a diagram to illustrate your thoughts?

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Anyone here good at math?

Postby furytrader » 15 Mar 2012

Sure, I've attached a simple screen shot. This shows the trend line (the dark blue, heavy line) drawn using the code above (in the e-Mini S&P 500).

What I am looking to do is allow the user to input an angle, between 0 and 360 degrees, and have the code automatically rotate the trend line from its starting point by the number of degrees in the angle. This rotated trend line appears with the dotted line.

Although the screenshot shows both the original trendline and the new, rotated trendline, the idea would be that only the rotated trendline appears.
Attachments
SampleTrendLine.png
(93.58 KiB) Downloaded 752 times

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Anyone here good at math?

Postby TJ » 15 Mar 2012

Sure, I've attached a simple screen shot. This shows the trend line (the dark blue, heavy line) drawn using the code above (in the e-Mini S&P 500).

What I am looking to do is allow the user to input an angle, between 0 and 360 degrees, and have the code automatically rotate the trend line from its starting point by the number of degrees in the angle. This rotated trend line appears with the dotted line.

Although the screenshot shows both the original trendline and the new, rotated trendline, the idea would be that only the rotated trendline appears.
Image

You are looking to find the price value at "e".

You have to find out the value of a, b, c, and d to get e.
Attachments
angle.jpg
(6.46 KiB) Downloaded 921 times

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Anyone here good at math?

Postby TJ » 15 Mar 2012

if a = 30 degrees
and a + d = 60 degrees,
then
e = 2 times of c.

You can just fudge the ratio to come up with "e". No advanced math is needed.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Anyone here good at math?

Postby bowlesj3 » 15 Mar 2012

Does this kind of thinking help?

Since stretching a chart scale or widening the bar spacing would effect angle but also effect all lines equally then why not do this.

Imagine a perfect rectangular box on your chart. It has 10 bars left to right and 10 units of price vertically.

Okay in this case a line running up from the lower left corner of the box to the top right corner of the box would be represented as 1 unit of change per bar. In other words 10units divided by 10 bars = value 1 unit of change per bar. Now that is a 45 degree angle (if you stretch your scale and units properly that is such that it looks like a box).

Now if you change it to 5 units divided by 10 bars this would be .5 change per bar. That would be an angle exactly 1/2 of the 45 degree angle I suspect.

Now if you change it to 15 units divided by 10 bars this would be 1.5 units change per bar. That would be an angle 1/2 way between.

Perfectly flat lines would be 0/10 which is zero.
Downward lines would be -10/10 for -1 or downward 45 degrees.

Perfectly vertical would be infinity/10 (not possible).

So the basic idea is simply get the unit change per bar (this number is behind all trend lines). At that point you can stretch the chart or change the bar spacing to your hearts content.

Here is why I think angle is of no value. Price movement is over time. It has nothing to do with distance (especially the distance you have chosen on your chart screen). Price units up or down also has nothing to do with distance on your screen which you can change at will. Case in point: If real bad news comes out on a bulletin prices will drop very fast. Fast means in a short period of time. So a trend line with the number -50 behind it is a bearish trend line. At tend line with +50 suggests a heck of a lot of buying in a very short period of time.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Anyone here good at math?

Postby furytrader » 16 Mar 2012

Thanks to both of you, TJ and BowlesJ3, for taking the time and energy to respond to my post.

TJ - I think the formula you provided doesn't represent a true circular rotation, because the length of the black line and the red line is different. For this to be a true rotation, they would need to stay the same length;

BowlesJ3 - I agree that the angle is a weird concept when you can change/resize the time axis.

What I am really trying to do is to create similar to a Gann Fann, but you can't create drawing objects directly in either MultiCharts or TS, so I have to try to do it with an indicator.

What I am discovering, though, it is that it is very hard.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Anyone here good at math?

Postby bowlesj3 » 16 Mar 2012

Asking Google with carefully worded search strings may be a way to figure it out.

gregorio123456
Posts: 117
Joined: 08 Nov 2005
Been thanked: 3 times

Re: Anyone here good at math?

Postby gregorio123456 » 16 Mar 2012

good morn.........

try w this Tangent(45º)=1 and arctangent(1)=45º .....maybe help

bar1time=8h 30min bar11time=8h 40min

bar1price=10€ bar11price=20€

20€-10=10€
bar11time-bar1time=10min

so pending or derived= 10€/10min=1€ for min = 1

arctangent(1)= 45º this is degrees of line in scale of chart X and Y of 1x1

if you want line 2 x 1 so value of pending is 2 in scale of chart X and Y of 1x1

arctangent(2) = 63.3333333º

jo

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Anyone here good at math?

Postby furytrader » 20 Mar 2012

Thanks Gregorio - the solution I came up with uses the ArcTangent value and I am getting results that look very close to being right, if not 100% right. I am still working on it but here is the code I have:

Code: Select all

Inputs: PointADate("03/14/2012"), PointATime(2045), PointAPrice(1391);
Inputs: PointBDAte("03/14/2012"), PointBTime(2345), PointBPrice(1393.50);
Inputs: LineColor(DarkBlue), LineWidth(2);

Variables: PointCDate(0), PointCTime(0), PointCPrice(0), PointCBarNum(0);
Variables: PointDDate(0), PointDTime(0), PointDPrice(0), PointDBarNum(0);
Variables: TempADate(0), TempBDate(0),TestDate(0);
Variables: Line1_ID(0);
Variables: PriceModifier(0);

If BarNumber = 1 Then Begin

TestDate = StringToDate(PointADate);
TempADate = JulianToDate(TestDate);

TestDate = StringToDate(PointBDate);
TempBDate = JulianToDate(TestDate);

End;

// Determine which point appears first chronologically, and capture its values
// in the variable called "PointC"

If Date = TempADate And Time = PointATime Then Begin

If PointCDate <> 0 Then Begin
PointDDate = Date;
PointDTime = Time;
PointDPrice = PointAPrice;
PointDBarNum = BarNumber;
End;

If PointCDate = 0 Then Begin
PointCDate = Date;
PointCTime = Time;
PointCPrice = PointAPrice;
PointCBarNum = BarNumber;
End;

End;

// Next, capture the values for the latter point under "PointD"

If Date = TempBDate And Time = PointBTime Then Begin

If PointCDate <> 0 Then Begin
PointDDate = Date;
PointDTime = Time;
PointDPrice = PointBPrice;
PointDBarNum = BarNumber;
End;

If PointCDate = 0 Then Begin
PointCDate = Date;
PointCTime = Time;
PointCPrice = PointBPrice;
PointCBarNum = BarNumber;
End;

End;

// Now that we've reached the bar on the chart, let's perform our calculations
// and draw our trendline

If LastBarOnChart = TRUE Then begin

// The price modifier value is used to help in "setting equal" the
// values of the price when compared to the price number values. It is an estimate.

Pricemodifier = PointDBarNum/PointDprice;

value1 = (PointDPrice-PointCPrice) * PriceModifier; // The difference in the two prices - the "rise"
value2 = PointDBarNum - PointCBarNum; // The difference in the two bar numbers - the "run"
value3 = value1/value2; // The "rise over run"

value4 = ArcTangent(value3); // This tells us the angle of the rise over run
Print(value4, Sine(value4), Cosine(Value4));

value5 = value4 + 90; // We add some degrees to the original angle - this could be anything
Print(value5, Sine(value5), Cosine(Value5));

value6 = value1 * (Sine(value5)/Sine(Value4))/PriceModifier; // We adjust the value of the rise to incorporate the new angle
value7 = value2 * (CoSine(Value5)/CoSine(value4)); // We adjust the value of the run to incorporate the new angle

// We recalculate the value of Point D to reflect the new angle

PointDPrice = PointCPrice + value6;
PointDBarNum = Round(PointCBarNum + value7,0);
PointDDate = Date[BarNumber-PointDBarNum];
PointDTime = Time[BarNumber - PointDBarNum];

Line1_ID = TL_new(PointCDate,PointCTime,PointCPrice,PointDDate,PointDTime,PointDPrice);
value1 = TL_SetColor(Line1_ID,LineColor);
value1 = TL_SetSize(Line1_ID,LineWidth);

End;


Return to “MultiCharts”