Plotting the EMA for the upper and lower wick of either green or red candles in MQL5 - moving-average

Can someone explain to me how I am able to calculate the EMA of only the RED or GREEN candle wicks in MQL5?
So I have the PineScript code because my MQL5 code isn’t as clear.
Thanks in advance for taking the time to look over my question.
// defining what direction a candle has
greenCandle = (close > open)
redCandle = (close < open)
// defining what the upper/lower wick of a candle is
UMA = if close > open
high - close
else
high - open
LMA = if close > open
low - open
else
low - close
// calculating the exponential moving average of the upper/ lower wick
UMAX = ta.ema(UMA, 9)
LMAX = ta.ema(LMA, 9)

Related

How to reward for two parameters in reinforcement learning?

I have a two box that should touch each other in straight line, so I have done two approach to reward:
Approach 1: Reward when distance is decreasing
this approach works well in 50% event after 100 million steps of training. The problem is that two box do not touch each other completely straight and it fails in 50%
Approach 2 Reward when distance is decreasing and radius difference between two box is decreasing
So here is the methods
if(distance < lastDistance)
AddReward(...)
lastDistance = distance;
if(radius < lastRadius )
AddReward(...)
lastRadius = radius;
The problem with approach 2 is that the box 1 which is moving is only rotating after a 10 million steps and even not decreasing distance
So How can I reward for multi parameters (distance, radius) problems
May be you can try to use only the first way and improve it.
You can add more conditions to help your agent to reach the goal.
for example :
reward = 0
if(distance < lastDistance)
reward += 1
lastDistance = distance
if(distance < 5)
reward += 5
if(distance < 3)
reward += 10
PS : the values are chosen randomly. Change them to reach the goal you want.

How can I write code that makes a spiral?

I just started learning to programming in Excel VBA, and I have a problem.
I want to draw a spiral with a program, but a can not solve it well.
I would like to use for and do until and do while cycles only.
Here is my code:
Sub spiral()
For i = 1 To 16
xmm = xmm - 1
ymm = ymm - 1
Do Until xp = i
xp = xp + 1
Cells(5 + xp, 5) = i
Loop
Do Until yp = i
yp = yp + 1
Cells(5 + xp, 5 + yp) = i + 1
Loop
Do Until xm > xp
xm = xm + 1
Cells(5 + xp + i * xmm, 5 + yp) = i + 2
Loop
Do Until ym > yp
ym = ym + 1
Cells(5 + xp + xmm, 5 + yp + i * ymm) = i + 3
Loop
Next i
End Sub
Thanks!
I can't think of any practical reason you'd need this, but that said, I'm often wasting time messing around with odd tasks "just to see if I can." 😊
Access VBA Circles
Recently a spiral was a self-assigned challenge I played with in SQL, although it originated with a circle in Access VBA from another useless project of mine:
Access World : Making an Analog Clock on an MS Access Form
This taught me a number of concepts including how to draw a circle on a grid.
Drawing a circle is almost the identical process as drawing the hands on that clock. If, rather than drawing the whole line of the clock hand, I instead draw only the tip of it (aka, the radius), all the way around, the 360°, then I have a circle.
SQL Circle
Say that ten times fast!
Then one day I wondered how hard it would be to convert the circle code to SQL Server, exploiting the graphing feature of the Stack Exchange Data Explorer (SEDE). A graph is drawn on a grid, so it's the same concept.
Here is one of several results (again, all useless):
SEDE : Draw Random Circles on chart
Click Run Query and then when it's finished calculating click the Graph tab. (You'll also need to either login with your Stack Exchange ID, or do the captcha.) There are various constants you can play with in the user prompts (below the SQL).
SQL Spiral
Then at some point I wondered about spirals.
(...but probably should have been time for me to get a life? Naaaa, lol)
A spiral is simply a modified circle except the radius decreases (or increases) as you progress around the circumference -- and you don't stop at 360° -- instead just keep going and going, and continually changing the radius at a steady rate.
SEDE : Draw a spiral
Again, click Run Query and then when it's finished calculating click the Graph tab. (You'll also need to either login with your Stack Exchange ID, or do the captcha.) There are various constants you can play with in the user prompts (below the SQL).
Spiral in Excel
I'm not going to waste any more time on spirals today, but if I was going to draw one in Excel worksheet cells, I would resize all cells on the worksheet to a tiny square grid (maybe width=1, height=10), and then I would borrow the code from the SQL example, adapting it to Excel (which woudn't take much work).
The Access VBA example may be more confusing to adapt since there a lot of programmatic manipulation of controls going on (which can't be done at runtime).
Or, the Access example could be duplicated in Excel using controls (ie., lines) instead of the grid method you were trying so far.
More Information:
Wikipedia : Midpoint Circle Algorithm
rosettacode : Midpoint circle algorithm examples (code in 39 languages)
tutsPlus.com : Working with circles/spirals
Math Overflow : How to spiral coordinates in an outward pattern on 2D grid?
Base on some answers from this thread (Looping in a spiral), I've managed to come with this solution
Sub spiral()
x = 0
y = 0
d = 1
m = 1
i = 1
j = 1
Do While i < 6
Do While 2 * x * d < m
Cells(x + 5, y + 5).Value = j
x = x + d
j = j + 1
Loop
Do While 2 * y * d < m
Cells(x + 5, y + 5).Value = j
y = y + d
j = j + 1
Loop
d = -1 * d
m = m + 1
i = i + 1
Loop
End Sub
Not sure if it is something that you are looking for thou, as you didn't specify the exact output that you want to achieve.

How to determine the boundaries in binary search?

I know how binary search works but I always make small mistakes when I need to implement one.
The following code is the solution for leetcode 287 find the duplicate number
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int low = 1, high = nums.size() - 1;
while (low < high) {
int mid = low + (high - low) * 0.5;
int cnt = 0;
for (auto a : nums) {
if (a <= mid) ++cnt;
}
if (cnt <= mid) low = mid + 1;
else high = mid;
}
return low;
}
};
There are several places I am confused about:
1.the condition for the while loop low<high or low<=high
2.a<=mid or a<mid (specific for this example)
3.cnt<= mid or cnt<mid
4.low=mid+1 or low=mid
5.high=mid or high=mid-1
6.which value do I return?
Is there a good way to remember or understand which the correct combinations to use?
When writing a binary search there are a couple of things to consider. The first is what interval range you are searching over and specifically, how you are defining it.
For example, it could be inclusive of both low and high, meaning [low, high], but it could also be exclusive of high, [low, high). Which of these you choose will change the rest of your algorithm.
The obvious implication is the initial values. Generally, high should be the length of the array if it is exclusive and it should be one less if it's inclusive, but it could be something entirely different depending on the problem you're solving.
For the while loop you want it to terminate when the search interval is empty, meaning there are no more candidates to check. If you are using the interval [low, high], then this will be empty when low is strictly greater than high (for example, [5,5] contains 5, but [6,5] contains nothing), so the while loop will check for the opposite, low <= high. However, if you use the interval [low, high), then this interval is empty when low is equal to high, so the while loop needs to check for low < high.
Within the while loop, after checking mid, you want to remove it from the interval so you don't check it again. If high is inclusive, then you have to use one less than mid as the new high in order to exclude it from the interval. But if high is exclusive, then setting high equal to mid is enough to exclude it.
As for when to update low vs high, this depends on what you're searching for. Besides the basic binary search where you just want to know if something exists exactly in the collection, you will have to consider what to do when you are as close as you can get.
In C++ for example, the more useful versions of binary_search are called lower_bound and upper_bound. If the value being searched for doesn't exist in the container, then these both return the same position, namely the first position which is larger than the search value. This is convenient since this is the position you should insert that value if you want to keep the container sorted. However, if the value is in the container, possibly multiple times, then lower_bound will return the first occurrence of the value, whereas upper_bound will still return the first position larger than the value (or in other words, a right bound to the location of the values).
To get these different behaviors you update either the low or high bound when mid is equal to the search value. If you want the lower bound, then you want to continue searching the lower half of your search range, so you bring high down. If you want the high bound, then you bring low up. In your example, it brings low up when cnt == mid, so it will find an upper bound.
As for what to return, it depends on both your search interval and what you're looking for. In your example, the while loop is checking (low < high), so low and high will be equal when it breaks and it doesn't matter which you use, but even then you may want to return left - 1 or left + 1 depending on the problem. If the while loop is (low <= high) then when it breaks low == high + 1, so it will depend on what you're looking for. When in doubt you can always think through an example.
So to put this all to use, here is a version of the solution you mentioned, but using an interval of [low, high] rather than [low, high):
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int low = 1, high = nums.size() - 2;
while (low <= high) {
int mid = low + (high - low) * 0.5;
int cnt = 0;
for (auto a : nums) {
if (a <= mid) ++cnt;
}
if (cnt <= mid) low = mid + 1;
else high = mid - 1;
}
return low;
}
};
PS: The reason I didn't mention the interval (low, high] or (low, high) is because it messes with the math around calculating the mid index. Because int math will round down, you can end up in a situation where mid is searched again. For example, if low is 7 and high is 9, then low + (high - low) * 0.5 will be 8. After updating low to 8 (since it's exclusive you wouldn't add one), low + (high - low) * 0.5 will still be 8 and your loop will never terminate. You can get around this by adding 1 to the part being divided by 2, but generally it's cleaner to go with an interval where low is inclusive.
You could just use leetcode's guide to binary search for reference. https://leetcode.com/explore/learn/card/binary-search

How to calculate this simple animation effect (physics engine)?

I am implementing a very simple animation effect for a game. The scenario is like this:
there is a elastic rubber line, length is 1 meter, when it is extended over 1 meter, it is elastic.
the line connects two dots A and B like this, the distance is S, S > 1 meter
A <------------- B
then fix dot A, and releases B, the line takes B to the direction of A
I want to know how to calculate time T, which B costs to move X meters towards A (X <= S).
Any ideas?
Thanks!
I have been meaning to learn how to animate these kinds of images in sage (a python based platform for math) for a while, so i used this as an excuse. I hope this code snippet and image is helpful.
A = 3
w = 0.5
# x = f(t) = A cos(wt) inside elastic region
# with x = displacement from 1 meter mark
# in the below code, x is the displacement from origin (x = A cos(wt) + 1)
# find speed when we cross the one meter mark
# f'(t) = -Aw sin(wt), but this is also max speed
# ie f'(t at one meter mark) = -Aw
speed_max = -A * w
# time to reach max speed + time to cross last meter
eta = float(pi/2 * 1/w + 1/abs(speed_max))
# the function you were looking for
def time_left(x):
if x < 1:
return x/abs(speed_max)
else:
return 1/w * arccos((x-1)/A)
It may not be clear in the image but within one meter of the origin there is no acceleration.

angle for particular co-ordinate in projectile path?

I have xy co-ordinate like (200,200). I know the angle calculation from the origin the ball throws. How can I find the initial velocity to reach that particular xy co-ordinate when ball is thrown in 2d Environment?
Iam using
x = v0cosq0t;
y = v0sinq0t - (1/2)gt2.
but time is needed. Without time can I do it? any help please?
I'm assuming that you want the ball to hit that specific point (200,200) at the apex of its path. Well, my physics is a bit rusty, but this is what I've thrown together:
v_y = square_root(2*g*y),
where g is a positive number reflecting the acceleration due to gravity, and y being how high you want to go (200 in this case).
v_x = (x*g) / v_y,
where x is how far in the x direction you want to go (200 in this case), g is as before, and Vy is the answer we got in the previous equation.
These equations remove the need for an angle. However, if you'd rather have the velocity + angle, that's simple:
v0 = square_root(v_x^2 + v_y^2)
and
angle = arctan(v_y / v_x).
Here is the derivation, if you're interested:
(1/2)at^2 + v_yt + 0 = y
(1/2)at^2 + v_yt - y = 0
by quadratic formula,
t = (-v_y +/- square_root(v_y^2 - 2ay)) / a
we also have another equation, because at the apex the vertical velocity is 0:
0 = v_y + at
substitute:
0 = v_y + (-v_y +/- square_root(v_y^2 - 2ay))
0 = square_root(v_y^2 - 2ay)
0 = v_y^2 - 2ay
v_y = square_root(-2ay), or
v_y = square_root(2gy)
For v_x:
v_x*t = x
from before, t = v_y / a, so
v_x = (x*g)/v_y
I hope that made enough sense.
Im sure you can assume the velocity change is instantaneous. Games physics always has some 'dodgy' parts in it because it is too computationally expensive or not important enough to get right down the low granularity information.
You can start the velocity ass instantaneous, and then using a timer class to measure then time between each frame (very rough way of doing it), or you can have a timer class set up in an update loop that will update the physics every x seconds.