PluginsMargin Level → Documentation
Preview
Documentation
This plugin alerts traders that their margin level has dropped to 30% or below. This encourages traders to deposit more funds and prevent uncomfortable situations where a trader’s positions are liquidated. This plugin checks the current balance of the logged-in user at every X second interval.
Code
Follow the commented code to get more information about the implementation of this plugin.
                function MarginLevel(params) {
                    // Init the instance with params
                    this.title = params.title || "UNTITLED";
                    this.message = params.message || "NO MESSAGE SET";
                    this.intervalTime = (params.interval || 5) * 1000;
                    this.triggerMargin = params.triggerMargin || 30;
                    this.notify = true;
                    this.interval = null;
                    this.alertWindow = null;
                
                    // Bind this instance to the API events `onLogin` and `onLogout`
                    this.bindEvents = function ()
                    {
                        var self = this;
                        if (webTrader)
                        {
                            webTrader.onLogin(self.onLoginHandler, this);
                            webTrader.onLogout(self.onLogoutHandler, this);
                        }
                    };
                
                    // `onLogin` event handler
                    this.onLoginHandler = function (userID, server, sessionID, isGuestMode)
                    {
                        var self = this;
                        if (self.interval)
                        {
                            window.clearInterval(self.interval);
                            self.interval = null;
                        }
                        self.notify = true;
                        self.interval = window.setInterval(window.fetchBalanceData, self.intervalTime);
                    };
                
                    // `onLogout` event handler
                    this.onLogoutHandler = function (userID, server, sessionID, isGuestMode)
                    {
                        var self = this;
                        if (self.interval)
                        {
                            window.clearInterval(self.interval);
                            self.interval = null;
                        }
                        self.notify = true;
                    };
                
                    // Pops up the alert message, we used here the `PopBox` plugin
                    // but any other popup plugin will do the trick
                    this.showAlertMessage = function ()
                    {
                        if (this.alertWindow)
                        {
                            this.alertWindow.close();
                        }

                        this.alertWindow = new PopBox({
                            width: 250,
                            height: 100,
                            html: '
' + this.title + '
' + this.message + '
' }); this.alertWindow.open(); }; // Bind to the API events as soon as this instance is ready this.bindEvents(); }
Initializing plugin
                var ml = null;
                // Init the plugin when the Document is ready using jQuery
                $(function ()
                {
                    // Create new instance of the MarginLevel plugin
                    ml = new MarginLevel({
                        title: 'Alert', // The alert message title
                        message: 'Your margin level is at 30%', // the message to show to the user
                        intervalTime: 5, // The time interval in seconds to check the current user margin
                        triggerMargin: 30 // What is the triggering margin
                    });
                });
            
                // The interval function
                window.fetchBalanceData = function ()
                {
                    // This is the getBalance function of the API,
                    // received callback function to call with the balance object as a parameter
                    webTrader.getBalance(function (balance)
                    {
                        if (balance.marginLevel <= ml.triggerMargin)
                        {
                            if (ml.notify)
                            {
                                ml.notify = false;
                                ml.showAlertMessage();
                            }
                        } else
                        {
                            ml.notify = true;
                        }
                    });
                }