Hi,
I am a data scientist/python developer by background and trying to create a scheduled Atlas Trigger function to run at the end of the day to aggregate the floating equity data (from a MetaTrader5 Windows client application) inserted into a MongoDB Collection asynchronously at random time points. I followed some Atlas trigger samples as an example, but I dont have any JS experience so I would be very happy if I could receive any guidance on writing the function correctly. The typical sample document that will be inserted into this FloatingEquity
collection is below:
time:2022-03-22T13:55:14.000+00:00
_id:62501c4e1128da601d62a83e
ClosedPositions:1
equity:25001
I just need the aggregation to return the min() and max() value of the equity
value along with the time
of occurence for the entire day, so each day’s aggregation will just have one record or document, with four fields (or four columns if you are imagining a dataframe), viz, Equity_day_low
, Equity_low_timestamp
,Equity_day_high
, Equity_high_timestamp
. This aggregated data needs to be inserted into a new collection called FloatingEquityDailyReport
.
The other important point is the timezones issues, I am in India so I scheduled the Atlas trigger to execute every day +3 hrs from GMT using cron schedule 59 2 * * *
but the timestamps are inserted as per Europe/Bucharest
timezone of the broker, so the trigger function needs to pay attention to the timezones of the MT5 broker server time as well. I would like to know if I am doing this correctly?
The code for the Atlast Trigger is below, along with the screenshots:
exports = function() {
/*A Scheduled Trigger will always call a function without arguments.Documentation on Triggers: https://docs.mongodb.com/realm/triggers/overview */
const mongodb = context.services.get('MT-Cluster');
const FloatingEquityRaw = mongodb.db("MT5_AccountMetrix").collection("FloatingEquity");
const FloatingEquityAggReport = mongodb.db("MT5_AccountMetrix").collection("FloatingEquityDailyReport");
const generatedObjectId = new BSON.ObjectId();
//Generate Daily Report
return FloatingEquityRaw.aggregate([
{
$match: {
createdAt:{
$gte: new Date(new Date().setHours(0,0,0,0)), //This DateTime needs to be in 'Europe/Bucharest' TimeZone of Mt5 broker
$lt : new Date(new Date().setHours(23,59,59,999)), //This DateTime needs to be in 'Europe/Bucharest' TimeZone of Mt5 broker
},
},
},
{
$group:{
_id: generatedObjectId,
min: {$min : 1}, //Need to capture the minimum equity value for the day along with the timestamp of the event occurence
max: {$max : 1} //Need to capture the maximum equity value for the day along with the timestamp of the event occurence
},
},
]).next()
.then(dailyReport => {
FloatingEquityAggReport.insertOne(dailyReport);
})
.catch(err => console.error("Failed to Generate FloatingEquityDataReport:", err));
}
This is probably a minor fix for a JavaScript / MongoDB Atlas expert and should not take much time. Looking forward to receiving any gracious help with regard to this issue.
Best Regards,
Dilip