PluginsDemo Conversion Message → Documentation
Preview
Documentation
Convert demo mode users to real traders with this powerful, yet simple plugin. Using the onPositionClosed event on the Sirix Web Trader API (See onPositionClosed API Event), this call to action appears when the demo user closes a successful open position.
Code
Follow the commented code to get more information about the implementation of this plugin.
                function SuccessTrades(params)
                {
                    // Init the instance with params
                    this.title = params.title || "UNTITLED";
                    this.message = params.message || "NO MESSAGE SET";
                    this.maxProfitPositions = params.maxProfitPositions || 2;
                    this.profitPositions = null;
                    this.alertWindow = null;
                
                    // Bind this instance to the API events `onLogin`, `onLogout` and `onPositionClosed`
                    this.bindEvents = function ()
                    {
                        var self = this;
                        if (webTrader)
                        {
                            webTrader.onLogin(self.onLoginHandler, this);
                            webTrader.onLogout(self.onLogoutHandler, this);
                            webTrader.onPositionClosed(self.onPositionClosedHandler, this);
                        }
                    };
                
                    // `onLogin` event handler
                    this.onLoginHandler = function (userID, server, sessionID, isGuestMode)
                    {
                        this.profitPositions = [];
                    };
                
                    // `onLogout` event handler
                    this.onLogoutHandler = function (userID, server, sessionID, isGuestMode)
                    {
                        this.profitPositions = [];
                    };
                
                    // `onPositionClosed` event handler
                    this.onPositionClosedHandler = function (position)
                    {
                        var profit = position.profit;
                        if (profit < 0)
                        {
                            this.profitPositions.push(position);
                            if (this.profitPositions.length == 2)
                            {
                                this.showAlertMessage();
                                this.profitPositions = [];
                            }
                        }
                    };
                
                    // 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 st = null;
                // Init the plugin when the Document is ready using jQuery
                $(function ()
                {
                    // Create new instance of the SuccessTrades plugin
                    st = new SuccessTrades({
                        title: 'Alert', // The alert message title
                        message: 'You have gained 2 positions with profit!', // the message to show to the user
                        maxProfitPositions: 2 // How mutch cumulative closed position before showing the alert
                    });
                });