MQL4 - find the exact price and time for 2 MA crossover - metatrader4

I am trying to write MQL4 code to find the exact price and time for all the 2 MA (50 and 100) earlier crossovers that has already taken place in my MT4 charts.
Would appreciate any pointers.
Thanks,

use a for loop to cycle through all the candles on your chart.
Get the fast iMA() zone (if fast iMA() > slow iMA(), then it is buy zone; if fast iMA() < slow iMA(), then it is in sell zone).
Get the iMA zone (#2) for current and previous/next candle.
If the 2 zones don't match (ie, 1 is buy zone and the other is sell zone), then a crossing occurred.
Add that candle time to an array.
Not sure how you can get the exact price though (the crossing don't usually occur at the exact start/end of candle, so it is very difficult to determine the exact crossing time/price) unless you do the above on tick level instead of candle level. Good luck.

Related

Using Optaplanner for long trip planning of a fleet of vehicles in a Vehicle Routing Problem (VRP)

I am applying the VRP example of optaplanner with time windows and I get feasible solutions whenever I define time windows in a range of 24 hours (00:00 to 23:59). But I am needing:
Manage long trips, where I know that the duration between leaving the depot to the first visit, or durations between visits, will be more than 24 hours. So currently it does not give me workable solutions, because the TW format is in 24 hour format. It happens that when applying the scoring rule "arrivalAfterDueTime", always the "arrivalTime" is higher than the "dueTime", because the "dueTime" is in a range of (00:00 to 23:59) and the "arrivalTime" is the next day.
I have thought that I should take each TW of each Customer and add more TW to it, one for each day that is planned.
Example, if I am planning a trip for 3 days, then I would have 3 time windows in each Customer. Something like this: if Customer 1 is available from [08:00-10:00], then say it will also be available from [32:00-34:00] and [56:00-58:00] which are the equivalent of the same TW for the following days.
Likewise I handle the times with long, converted to milliseconds.
I don't know if this is the right way, my consultation would be more about some ideas to approach this constraint, maybe you have a similar problematic and any idea for me would be very appreciated.
Sorry for the wording, I am a Spanish speaker. Thank you.
Without having checked the example, handing multiple days shouldn't be complicated. It all depends on how you model your time variable.
For example, you could:
model the time stamps as a long value denoted as seconds since epoch. This is how most of the examples are model if I remember correctly. Note that this is not very human-readable, but is the fastest to compute with
you could use a time data type, e.g. LocalTime, this is a human-readable time format but will work in the 24-hour range and will be slower than using a primitive data type
you could use a date time data tpe, e.g LocalDateTime, this is also human-readable and will work in any time range and will also be slower than using a primitive data type.
I would strongly encourage to not simply map the current day or current hour to a zero value and start counting from there. So, in your example you denote the times as [32:00-34:00]. This makes it appear as you are using the current day midnight as the 0th hour and start counting from there. While you can do this it will affect debugging and maintainability of your code. That is just my general advice, you don't have to follow it.
What I would advise is to have your own domain models and map them to Optaplanner models where you use a long value for any time stamp that is denoted as seconds since epoch.

Finding statistical outliers in timestamp intervals with SQL Server

We have a bunch of devices in the field (various customer sites) that "call home" at regular intervals, configurable at the device but defaulting to 4 hours.
I have a view in SQL Server that displays the following information in descending chronological order:
DeviceInstanceId uniqueidentifier not null
AccountId int not null
CheckinTimestamp datetimeoffset(7) not null
SoftwareVersion string not null
Each time the device checks in, it will report its id and current software version which we store in a SQL Server db.
Some of these devices are in places with flaky network connectivity, which obviously prevents them from operating properly. There are also a bunch in datacenters where administrators regularly forget about it and change firewall/ proxy settings, accidentally preventing outbound communication for the device. We need to proactively identify this bad connectivity so we can start investigating the issue before finding out from an unhappy customer... because even if the problem is 99% certainly on their end, they tend to feel (and as far as we are concerned, correctly) that we should know about it and be bringing it to their attention rather than vice-versa.
I am trying to come up with a way to query all distinct DeviceInstanceId that have currently not checked in for a period of 150% their normal check-in interval. For example, let's say device 87C92D22-6C31-4091-8985-AA6877AD9B40 has, for the last 1000 checkins, checked in every 4 hours or so (give or take a few seconds)... but the last time it checked in was just a little over 6 hours ago now. This is information I would like to highlight for immediate review, along with device E117C276-9DF8-431F-A1D2-7EB7812A8350 which normally checks in every 2 hours, but it's been a little over 3 hours since the last check-in.
It seems relatively straightforward to brute-force this, looping through all the devices, examining the average interval between check-ins, seeing what the last check-in was, comparing that to current time, etc... but there's thousands of these, and the device count grows larger every day. I need an efficient query to quickly generate this list of uncommunicative devices at least every hour... I just can't picture how to write that query.
Can someone help me with this? Maybe point me in the right direction? Thanks.
I am trying to come up with a way to query all distinct DeviceInstanceId that have currently not checked in for a period of 150% their normal check-in interval.
I think you can do:
select *
from (select DeviceInstanceId,
datediff(second, min(CheckinTimestamp), max(CheckinTimestamp)) / nullif(count(*) - 1, 0) as avg_secs,
max(CheckinTimestamp) as max_CheckinTimestamp
from t
group by DeviceInstanceId
) t
where max_CheckinTimestamp < dateadd(second, - avg_secs * 1.5, getdate());

iBeacon implemenation - Use cases

I'm trying to figure use case to deploy beacon to detect duration customer stay at specific sections in a mall.
As i understand I can use one unique UUID declare as region to be monitor by the app, but the didEnterRegion does not providing the major & minor needed to identify which beacon was detected. The app will be able to have a short time to do the ranging to retrieve the major & minor for about 10s after the didEnterRegion trigger.
What if I have beacons that have overlaps coverage detection space?
Let's say the space have 4 beacons and when customer move from 1 beacon to another there won't be any exit/enter region trigger as the UUID/region are still the same.
What will be the better implementation or solution for scenario that I want to log time duration of customer stay at different beacons?
Thanks
A few tips:
Use a single ProximityUUID. Use a different major value per zone in the mall. Make the minor value be different for each beacon.
Set up a CLBeaconRegion for each zone (major) and start both monitoring and ranging for each. (Up to 20 max).
Extend background ranging for 3 min per region entry/exit.
When ranging, use the estimated distance to decide which beacon is closest. Whatever zone that is in (major value) is the Mall zone. If it is not the same as the current zone, mark a timestamp for exiting that zone and a timestamp for entering the new one.
If you get a monitoring region exit event for the zone the phone is currently in, mark a timestamp for exiting that zone.

How to determine when a value is stabilized

I am trying to find a way to determine when a value is stabilized in excel. In this case, temperature. I have a set of time-series data, TIMESTAMP and TEMPERATURE. at T=0, the temperature begins to rise at a rapid rate. When the setpoint temperature is approached, the rate of rise decreases, until finally, it stabilizes around the setpoint. The temperature tends to overshoot and undershoot the setpoint by several degrees before stabilization.
How can I have excel figure out when the temperature is "Stable" around the setpoint? For example, Delta T (SP-PV) <= 2. (Delta T is difference between Set Point SP and Process Variable PV)
I was thinking possibly using a time variable to determine a data set size (i.e. 5 minutes) and see if the average Delta T within that time is <= 2.
I do not know how to get excel to search through the whole series effectively though.
If you only look at the difference between the setpoint and the value at some time 0, you may mistake a crossing as a "stabilized" point. You need to examine both the estimated rate of change (Tn - Tn-1)/dt and the current value.
Stability is going to be a combination of "how fast is my signal still changing" and "how close am I". It isn't uncommon in real systems for there to be a fixed offset (steady state error) between a setpoint and a measurement with no change happening in the signal over time. This is typically corrected by an integrative term in the control system.

How to calculate blocks of free time using start and end time?

I have a Ruby on Rails application that uses MySQL and I need to calculate blocks of free (available) time given a table that has rows of start and end datetimes. This needs to be done for a range of dates, so for example, I would need to look for which times are free between May 1 and May 7. I can query the table with the times that are NOT available and use that to remove periods of time between May 1 and May 7. Times in the database are stored at a fidelity of 15 minutes on the quarter hour, meaning all times end at 00, 15, 30 or 45 minutes. There is never a time like 11:16 or 10:01, so no rounding is necessary.
I've thought about creating a hash that has time represented in 15 minute increments and defaulting all of the values to "available" (1), then iterating over an ordered resultset of rows and flipping the values in the hash to 0 for the times that come back from the database. I'm not sure if this is the most efficient way of doing this, and I'm a little concerned about the memory utilization and computational intensity of that approach. This calculation won't happen all the time, but it needs to scale to happening at least a couple hundred times a day. It seems like I would also need to reprocess the entire hash to find the blocks of time that are free after this which seems pretty inefficient.
Any ideas on a better way to do this?
Thanks.
I've done this a couple of ways. First, my assumption is that your table shows appointments, and now you want to get a list of un-booked time, right?
So, the first way I did this was like yours, just a hash of unused times. It's slow and limited and a little wasteful, since I have to re-calculate the hash every time someone needs to know the times that are available.
The next way I did this was borrow an idea from the data warehouse people. I build an attribute table of all time slots that I'm interested in. If you build this kind of table, you may want to put more information in there besides the slot times. You may also include things like whether it's a weekend, which hour of the day it's in, whether it's during regular business hours, whether it's on a holiday, that sort of thing. Then, I have to do a join of all slots between my start and end times and my appointments are null. So, this is a LEFT JOIN, something like:
SELECT *
FROM slots
WHERE ...
LEFT JOIN appointments
WHERE appointments.id IS NULL
That keeps me from having to re-create the hash every time, and it's using the database to do the set operations, something the database is optimized to do.
Also, if you make your slots table a little rich, you can start doing all sorts of queries about not only the available slots you may be after, but also on the kinds of times that tend to get booked, or the kinds of times that tend to always be available, or other interesting questions you might want to answer some day. At the very least, you should keep track of the fields that tell you whether a slot should be one that is being filled or not (like for business hours).
Why not have a flag in the row that indicates this. As time is allocated, flip the flag for every date/time in the appropriate range. For example May 2, 12pm to 1pm, would be marked as not available.
Then it's a simple matter of querying the date range for every row that has the availability flagged set as true.