Ext.namespace('GAIN'); var ui = '/uk/UIService.asmx'; GAIN.messageDealProcessing = "Deal request is processing."; GAIN.messageOrderProcessing = "Order request is processing."; GAIN.messageAnotherDealProcessing = "Another Deal request is processing. Please wait."; GAIN.messageAnotherOrderProcessing = "Another Order request is processing. Please wait."; GAIN.TrailingStopPipErrorMessage = "Pip specified in your trailling stop order is out of acceptable range"; GAIN.LABELS_ORDERRATE_OrderDefaultText = 'Order Rate'; GAIN.LABELS_ORDERRATE_TrailingStopText = 'Live Rate'; //-------TradingObject------------------------------------------------------------------------------------ GAIN.TradingObject = function (config){ this.url = ""; this.refreshInterval = 1000;//milli second this.data = null; this.jsonPara = {}; this.status = {requesting:false, dataReady:false, init:false, repeatable:true, dataInit:false, enabled:true}; GAIN.TradingObject.superclass.constructor.call(this); Ext.apply(this, config); this.aConnection = new Ext.data.Connection(); this.addEvents({ "dataInit": true, "Updating": true, "Loaded" : true, "LoadFailed": true }); function TransformText(input, url){ var temp; switch (url){ case ui + "/GetUserRates3": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"Bid":').replace(/\"C\":/g,'"Offer":').replace(/\"D\":/g,'"Status":'); case ui + "/GetUserRates4": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"Bid":').replace(/\"C\":/g,'"Offer":').replace(/\"D\":/g,'"ChangeInPoints":').replace(/\"E\":/g,'"Status":'); break; case ui + "/GetUserRates3_3": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"Bid":').replace(/\"C\":/g,'"Offer":').replace(/\"D\":/g,'"ClosingBid":').replace(/\"E\":/g,'"High":').replace(/\"F\":/g,'"Low":').replace(/\"G\":/g,'"Status":'); break; case ui + "/GetUserRates5": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"Bid":').replace(/\"C\":/g,'"Offer":').replace(/\"D\":/g,'"ChangeInPoints":').replace(/\"E\":/g,'"High":').replace(/\"F\":/g,'"Low":').replace(/\"G\":/g,'"Status":'); break; case ui + "/GetUserRates6": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"Bid":').replace(/\"C\":/g,'"Offer":').replace(/\"D\":/g,'"ChangeInPoints":').replace(/\"E\":/g,'"High":').replace(/\"F\":/g,'"Low":').replace(/\"G\":/g,'"cPercent":').replace(/\"H\":/g,'"Status":'); break; case ui + "/GetUserRates7": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"Bid":').replace(/\"C\":/g,'"Offer":').replace(/\"D\":/g,'"ChangeInPoints":').replace(/\"E\":/g,'"High":').replace(/\"F\":/g,'"Low":').replace(/\"H\":/g,'"Decimals":'). replace(/\"G\":/g,'"Status":'); break; case ui + "/GetProductSettings2": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"IDecimals":').replace(/\"C\":/g,'"AliasName":'); break; case ui + "/GetProductSettings3": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"IDecimals":').replace(/\"F\":/g,'"Notation":'); break; case ui + "/GetProductSettings6": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"IDecimals":').replace(/\"C\":/g,'"MaxOrderLots":').replace(/\"D\":/g,'"OrderSize":').replace(/\"E\":/g,'"DefaultOrderLots":').replace(/\"F\":/g,'"Notation":').replace(/\"G\":/g,'"CounterProduct":').replace(/\"H\":/g,'"MarginFactor":').replace(/\"I\":/g,'"MaxOrderPips":').replace(/\"J\":/g,'"PipDifference":').replace(/\"K\":/g,'"DefaultTradeLots":').replace(/\"L\":/g,'"TradeSize":').replace(/\"M\":/g,'"AliasName":').replace(/\"N\":/g,'"Description":').replace(/\"O\":/g,'"BoldIndex":').replace(/\"P\":/g,'"SuperScriptIndex":').replace(/\"Q\":/g,'"ContractExpiryDescription":').replace(/\"R\":/g,'"DailyClosureExpiryTimeDescription":').replace(/\"S\":/g,'"UnitsPerLotDescription":').replace(/\"T\":/g,'"MinimumTickDescription":').replace(/\"U\":/g,'"Type":').replace(/\"V\":/g,'"OrderPipsAway":').replace(/\"W\":/g,'"TickValue":').replace(/\"X\":/g,'"UnderlyingExchangeMonth":'); break; case ui + "/GetMargins3": temp = input.replace(/\"A\":/g,'"PostedMargin":').replace(/\"B\":/g,'"UnrealizedProfit":').replace(/\"C\":/g,'"RealizedProfit":').replace(/\"D\":/g,'"USDPostedMargin":').replace(/\"E\":/g,'"USDRealizedProfit":').replace(/\"F\":/g,'"MarginBalance":'); break; case ui + "/GetPositions4": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"Contract":').replace(/\"C\":/g,'"Unrealized":').replace(/\"D\":/g,'"AverageRate":').replace(/\"E\":/g,'"USDValue":'); break; case ui + "/GetPositions8": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"Contract":').replace(/\"C\":/g,'"Unrealized":').replace(/\"D\":/g,'"AverageRate":').replace(/\"E\":/g,'"USDValue":').replace(/\"F\":/g,'"LimitRate":').replace(/\"G\":/g,'"StopRate":').replace(/\"H\":/g,'"ProfitLoss":'); break; case ui + "/GetProductDeals6": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"Dealsequence":').replace(/\"C\":/g,'"Contract":').replace(/\"D\":/g,'"Rate":').replace(/\"E\":/g,'"BuySell":').replace(/\"F\":/g,'"Status":'); break; case ui + "/GetOrders4": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"Legs":').replace(/\"W\":/g,'"Amount":').replace(/\"X\":/g,'"Rate":').replace(/\"Y\":/g,'"Basis":').replace(/\"Z\":/g,'"BuySell":'); break; case ui + "/GetOrdersC": temp = input.replace(/\"A\":/g,'"Product":').replace(/\"B\":/g,'"Legs":').replace(/\"C\":/g,'"Type":').replace(/\"D\":/g,'"ReferenceNumber":').replace(/\"E\":/g,'"Expiry":').replace(/\"F\":/g,'"Assp":').replace(/\"G\":/g,'"TrailingPoints":').replace(/\"W\":/g,'"Amount":').replace(/\"X\":/g,'"Rate":').replace(/\"Y\":/g,'"Basis":').replace(/\"Z\":/g,'"BuySell":').replace(/\"T\":/g,'"Seq":').replace(/\"U\":/g,'"Status":').replace(/\"V\":/g,'"TSP":'); break; default: return input; break; }; return temp; }; var ajob = function(index, isForceRun){ if ( isForceRun || (this.status.enabled == true && (this.status.repeatable == true || this.status.dataInit != true) && !this.status.requesting ) ){ this.status.requesting = true; this.fireEvent('Updating'); this.aConnection.request( { url: this.url, method: "POST", headers:{"Content-Type":"application/json"}, scope: this, jsonData: this.jsonPara, callback: function(options, success, response){ this.status.requesting = false; if (success){ var aResponseText = TransformText(response.responseText, this.url); var temp = Ext.util.JSON.decode(aResponseText); if (temp.d == null){ this.fireEvent('LoadFailed'); }else{ if (this.data != null && this.data.clear) { this.status.dataReady = false; this.data.clear(); for (var i =0; i < temp.d.length; i++) { this.data.replace(temp.d[i]); }; this.status.dataReady = true; }else{ this.status.dataReady = false; this.data = temp.d; this.status.dataReady = true; } if (this.status.dataInit == false){ this.status.dataInit = true; this.fireEvent('dataInit'); }; this.fireEvent('Loaded'); }; }else{ this.fireEvent('LoadFailed'); } } } ); }; }; var task; this.init = function() { task = { run: ajob.createDelegate(this), interval: this.refreshInterval }; ajob.call(this); var runner = new Ext.util.TaskRunner(); runner.start(task); this.status.init = true; }; this.requestData = ajob.createDelegate(this); }; Ext.extend(GAIN.TradingObject, Ext.util.Observable, {}); //-------TradingObjects----------------------------------------------------------------------------------- GAIN.CheckStatus = function (config){ GAIN.CheckStatus.superclass.constructor.call(this); Ext.apply(this, config); this.data = null; this.init(); }; Ext.extend(GAIN.CheckStatus, GAIN.TradingObject, {}); GAIN.Rates = function (config){ GAIN.Rates.superclass.constructor.call(this); Ext.apply(this, config); this.data = new Ext.util.MixedCollection(false, function(item){ return item.Product; }); this.init(); }; Ext.extend(GAIN.Rates, GAIN.TradingObject, {}); GAIN.Positions = function (config){ GAIN.Positions.superclass.constructor.call(this); this.hasPLPip = false; this.hasUnRealizedPL =false; Ext.apply(this, config); this.data = new Ext.util.MixedCollection(false, function(item){ return item.Product; }); this.init(); }; Ext.extend(GAIN.Positions, GAIN.TradingObject, {}); GAIN.Deals = function (config){ GAIN.Deals.superclass.constructor.call(this); this.status.enabled = false; Ext.apply(this, config); this.data = new Ext.util.MixedCollection(false, function(item){ return item.Dealsequence; }); this.init(); }; Ext.extend(GAIN.Deals, GAIN.TradingObject, {}); GAIN.Margin = function (config){ GAIN.Margin.superclass.constructor.call(this); Ext.apply(this, config); this.data = null; this.init(); }; Ext.extend(GAIN.Margin, GAIN.TradingObject, {}); GAIN.Orders = function (config){ GAIN.Orders.superclass.constructor.call(this); Ext.apply(this, config); this.data = new Ext.util.MixedCollection(false, function(item){ return item.ReferenceNumber; }); this.init(); }; Ext.extend(GAIN.Orders, GAIN.TradingObject, {}); GAIN.ProductSettings = function (config){ GAIN.ProductSettings.superclass.constructor.call(this); this.status.repeatable = false; Ext.apply(this, config); this.data = new Ext.util.MixedCollection(false, function(item){ return item.Product; }); this.init(); }; Ext.extend(GAIN.ProductSettings, GAIN.TradingObject, {}); GAIN.AccountSettings = function (config){ GAIN.AccountSettings.superclass.constructor.call(this); this.status.repeatable = false; Ext.apply(this, config); this.data = null; this.init(); }; Ext.extend(GAIN.AccountSettings, GAIN.TradingObject, {}); GAIN.EconodayResearch = function (config){ GAIN.EconodayResearch.superclass.constructor.call(this); this.status.repeatable = false; Ext.apply(this, config); this.data = null; this.init(); }; Ext.extend(GAIN.EconodayResearch, GAIN.TradingObject, {}); GAIN.DealData = function(){ this.Dealsequence = ""; this.Product = ""; this.BuySell = ""; this.BankConfirmation = ""; this.DealDate = ""; this.Rate = ""; this.Contract = ""; this.Counter = ""; this.Reference = ""; this.Sequence = ""; this.Status = ""; }; GAIN.OrderLegData = function(){ this.BuySell = ""; this.Amount = ""; this.Basis = ""; this.Rate = ""; this.TPS = 0; this.Status = ""; this.Seq = 0; this.ConfirmationNumber = ""; this.Type = ""; }; GAIN.OrderData = function(){ this.Assp = ""; this.DealReference = ""; this.Expiry = ""; this.OrderDate = ""; this.Product = ""; this.ReferenceNumber = ""; this.TrailingPoints = ""; this.Type = ""; this.Legs = []; }; GAIN.Expires = {EOD:{text:'End of Day',value:'EOD'}, GTC:{text:'Good Till Cancelled',value:'GTC'}}; GAIN.BuySell = {B:{text:'Buy',value:'B'}, S:{text:'Sell',value:'S'}}; GAIN.BuySell = {T:{text:'Limit',value:'T'}, S:{text:'Stop',value:'S'}}; GAIN.OrderType = { MARKET_ORDER:{text:'Market',value:'MARKET_ORDER'}, SINGLE_ORDER: { text: 'Single', value: 'SINGLE_ORDER' }, SINGLE_ORDER_LIMIT: { text: 'Limit', value: 'SINGLE_ORDER_LIMIT' }, SINGLE_ORDER_STOP: { text: 'Stop', value: 'SINGLE_ORDER_STOP' }, OCO_ORDER:{text:'One Cancels Other',value:'OCO_ORDER'}, IF_THEN:{text:'If Then',value:'IF_THEN'}, IF_THEN_OCO:{text:'If Then OCO',value:'IF_THEN_OCO'}, TRAILINGSTOP:{text:'Trailing Stop',value:'TRAILINGSTOP'}, SINGLE_ASSP: { text: 'Single', value: 'SINGLE_ASSP' }, SINGLE_ASSP_LIMIT: { text: 'Limit', value: 'SINGLE_ASSP_LIMIT' }, SINGLE_ASSP_STOP: { text: 'Stop', value: 'SINGLE_ASSP_STOP' }, OCO_ASSP:{text:'One Cancels Other',value:'OCO_ASSP'}, TRAILINGSTOP_ASSP:{text:'Trailing Stop',value:'TRAILINGSTOP_ASSP'} }; GAIN.ActionType = {PLACE:'PLACE',MODIFY:'MODIFY', CANCEL:'CANCEL'}; GAIN.OrderDropDown = { PLACE_true: ['SINGLE_ASSP_LIMIT', 'SINGLE_ASSP_STOP', 'OCO_ASSP', 'TRAILINGSTOP_ASSP'], PLACE_false: ['MARKET_ORDER', 'SINGLE_ORDER_LIMIT', 'SINGLE_ORDER_STOP', 'OCO_ORDER', 'IF_THEN', 'IF_THEN_OCO', 'TRAILINGSTOP'], MODIFY_true: ['SINGLE_ASSP_LIMIT', 'SINGLE_ASSP_STOP', 'OCO_ASSP', 'TRAILINGSTOP_ASSP'], MODIFY_false: ['SINGLE_ORDER_LIMIT', 'SINGLE_ORDER_STOP' , 'OCO_ORDER', 'IF_THEN', 'IF_THEN_OCO', 'TRAILINGSTOP'], Cancel_true: ['SINGLE_ASSP_LIMIT', 'SINGLE_ASSP_STOP', 'OCO_ASSP', 'TRAILINGSTOP_ASSP'], Cancel_false: ['SINGLE_ORDER_LIMIT', 'SINGLE_ORDER_STOP', 'OCO_ORDER', 'IF_THEN', 'IF_THEN_OCO', 'TRAILINGSTOP'] }; GAIN.ORDERUI = { PLACE_false_SINGLE_ORDER:{orderlabel:{visible:true},product:{visible:true,enable:true},orderType:{visible:true,enable:true},ASSP:{visible:false,enable:true},Expiry:{visible:true,enable:true},Legs:1,BuySell:{visible:true,enable:true},Contract:{visible:true,enable:true},Basis:{visible:true,enable:true},Rate:{visible:true,enable:true},Pip:{visible:true,enable:true}}, PLACE_false_SINGLE_ORDER_LIMIT: { orderlabel: { visible: true }, product: { visible: true, enable: true }, orderType: { visible: true, enable: true }, ASSP: { visible: false, enable: true }, Expiry: { visible: true, enable: true }, Legs: 1, BuySell: { visible: true, enable: true }, Contract: { visible: true, enable: true }, Basis: { visible: true, enable: false, value:'T' }, Rate: { visible: true, enable: true }, Pip: { visible: true, enable: true} }, PLACE_false_SINGLE_ORDER_STOP: { orderlabel: { visible: true }, product: { visible: true, enable: true }, orderType: { visible: true, enable: true }, ASSP: { visible: false, enable: true }, Expiry: { visible: true, enable: true }, Legs: 1, BuySell: { visible: true, enable: true }, Contract: { visible: true, enable: true }, Basis: { visible: true, enable: false, value: 'S' }, Rate: { visible: true, enable: true }, Pip: { visible: true, enable: true} }, PLACE_false_MARKET_ORDER: { orderlabel: { visible: false }, product: { visible: true, enable: true }, orderType: { visible: true, enable: true }, ASSP: { visible: false, enable: false }, Expiry: { visible: false }, Legs: 1, BuySell: { visible: true, enable: true }, Contract: { visible: true, enable: true }, Basis: { visible: false }, Rate: { visible: true, enable: false }, Pip: { visible: false, enable: false} }, PLACE_false_OCO_ORDER:{orderlabel:{visible:true},product:{visible:true,enable:true},orderType:{visible:true,enable:true},ASSP:{visible:false,enable:true},Expiry:{visible:true,enable:true},Legs:2,BuySell:{visible:true,enable:true},Contract:{visible:true,enable:true},Basis:{visible:true,enable:true},Rate:{visible:true,enable:true},Pip:{visible:true,enable:true}}, PLACE_false_IF_THEN:{orderlabel:{visible:true},product:{visible:true,enable:true},orderType:{visible:true,enable:true},ASSP:{visible:false,enable:true},Expiry:{visible:true,enable:true},Legs:2,BuySell:{visible:true,enable:true},Contract:{visible:true,enable:true},Basis:{visible:true,enable:true},Rate:{visible:true,enable:true},Pip:{visible:true,enable:true}}, PLACE_false_IF_THEN_OCO:{orderlabel:{visible:true},product:{visible:true,enable:true},orderType:{visible:true,enable:true},ASSP:{visible:false,enable:true},Expiry:{visible:true,enable:true},Legs:3,BuySell:{visible:true,enable:true},Contract:{visible:true,enable:true},Basis:{visible:true,enable:true},Rate:{visible:true,enable:true},Pip:{visible:true,enable:true}}, PLACE_false_TRAILINGSTOP:{orderlabel:{visible:true},product:{visible:true,enable:true},orderType:{visible:true,enable:true},ASSP:{visible:false,enable:true},Expiry:{visible:true,enable:true},Legs:1,BuySell:{visible:true,enable:true},Contract:{visible:true,enable:true},Basis:{visible:true,enable:false,value:'S'},Rate:{visible:true,enable:false},Pip:{visible:true,enable:true}}, PLACE_true_SINGLE_ASSP:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:true},ASSP:{visible:true,enable:false},Expiry:{visible:true,enable:false,value:'GTC'},Legs:1,BuySell:{visible:true,enable:false},Contract:{visible:true,enable:false},Basis:{visible:true,enable:true},Rate:{visible:true,enable:true},Pip:{visible:true,enable:true}}, PLACE_true_SINGLE_ASSP_LIMIT: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: true }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false, value: 'GTC' }, Legs: 1, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false, value: 'T' }, Rate: { visible: true, enable: true }, Pip: { visible: true, enable: true} }, PLACE_true_SINGLE_ASSP_STOP: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: true }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false, value: 'GTC' }, Legs: 1, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false, value: 'S' }, Rate: { visible: true, enable: true }, Pip: { visible: true, enable: true} }, PLACE_true_OCO_ASSP: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: true }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false, value: 'GTC' }, Legs: 2, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false }, Rate: { visible: true, enable: true }, Pip: { visible: true, enable: true} }, PLACE_true_TRAILINGSTOP_ASSP:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:true},ASSP:{visible:true,enable:false},Expiry:{visible:true,enable:false,value:'GTC'},Legs:1,BuySell:{visible:true,enable:false},Contract:{visible:true,enable:false},Basis:{visible:true,enable:false,value:'S'},Rate:{visible:true,enable:false},Pip:{visible:true,enable:true}}, MODIFY_false_SINGLE_ORDER:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:false},ASSP:{visible:false,enable:false},Expiry:{visible:true,enable:true},Legs:1,BuySell:{visible:true,enable:true},Contract:{visible:true,enable:true},Basis:{visible:true,enable:true},Rate:{visible:true,enable:true},Pip:{visible:true,enable:true}}, MODIFY_false_SINGLE_ORDER_LIMIT: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: false }, ASSP: { visible: false, enable: false }, Expiry: { visible: true, enable: true }, Legs: 1, BuySell: { visible: true, enable: true }, Contract: { visible: true, enable: true }, Basis: { visible: true, enable: false, value: 'T' }, Rate: { visible: true, enable: true }, Pip: { visible: true, enable: true} }, MODIFY_false_SINGLE_ORDER_STOP: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: false }, ASSP: { visible: false, enable: false }, Expiry: { visible: true, enable: true }, Legs: 1, BuySell: { visible: true, enable: true }, Contract: { visible: true, enable: true }, Basis: { visible: true, enable: false, value: 'S' }, Rate: { visible: true, enable: true }, Pip: { visible: true, enable: true} }, MODIFY_false_OCO_ORDER: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: false }, ASSP: { visible: false, enable: false }, Expiry: { visible: true, enable: true }, Legs: 2, BuySell: { visible: true, enable: true }, Contract: { visible: true, enable: true }, Basis: { visible: true, enable: true }, Rate: { visible: true, enable: true }, Pip: { visible: true, enable: true} }, MODIFY_false_IF_THEN:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:false},ASSP:{visible:false,enable:false},Expiry:{visible:true,enable:true},Legs:2,BuySell:{visible:true,enable:true},Contract:{visible:true,enable:true},Basis:{visible:true,enable:true},Rate:{visible:true,enable:true},Pip:{visible:true,enable:true}}, MODIFY_false_IF_THEN_OCO:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:false},ASSP:{visible:false,enable:false},Expiry:{visible:true,enable:true},Legs:3,BuySell:{visible:true,enable:true},Contract:{visible:true,enable:true},Basis:{visible:true,enable:true},Rate:{visible:true,enable:true},Pip:{visible:true,enable:true}}, MODIFY_true_SINGLE_ASSP:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:true},ASSP:{visible:true,enable:false},Expiry:{visible:true,enable:false},Legs:1,BuySell:{visible:true,enable:false},Contract:{visible:true,enable:false},Basis:{visible:true,enable:true},Rate:{visible:true,enable:true},Pip:{visible:true,enable:true}}, MODIFY_true_SINGLE_ASSP_LIMIT: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: true }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false }, Legs: 1, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false, value: 'T' }, Rate: { visible: true, enable: true }, Pip: { visible: true, enable: true} }, MODIFY_true_SINGLE_ASSP_STOP: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: true }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false }, Legs: 1, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false, value: 'S' }, Rate: { visible: true, enable: true }, Pip: { visible: true, enable: true} }, MODIFY_true_OCO_ASSP: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: true }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false }, Legs: 2, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false }, Rate: { visible: true, enable: true }, Pip: { visible: true, enable: true} }, MODIFY_true_TRAILINGSTOP_ASSP:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:true},ASSP:{visible:true,enable:false},Expiry:{visible:true,enable:false,value:'GTC'},Legs:1,BuySell:{visible:true,enable:false},Contract:{visible:true,enable:false},Basis:{visible:true,enable:false,value:'S'},Rate:{visible:true,enable:false},Pip:{visible:true,enable:true}}, CANCEL_false_SINGLE_ORDER:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:false},ASSP:{visible:true,enable:false},Expiry:{visible:true,enable:false},Legs:1,BuySell:{visible:true,enable:false},Contract:{visible:true,enable:false},Basis:{visible:true,enable:false},Rate:{visible:true,enable:false},Pip:{visible:true,enable:false}}, CANCEL_false_SINGLE_ORDER_LIMIT: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: false }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false }, Legs: 1, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false }, Rate: { visible: true, enable: false }, Pip: { visible: true, enable: false} }, CANCEL_false_SINGLE_ORDER_STOP: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: false }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false }, Legs: 1, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false }, Rate: { visible: true, enable: false }, Pip: { visible: true, enable: false} }, CANCEL_false_OCO_ORDER: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: false }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false }, Legs: 2, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false }, Rate: { visible: true, enable: false }, Pip: { visible: true, enable: false} }, CANCEL_false_IF_THEN:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:false},ASSP:{visible:true,enable:false},Expiry:{visible:true,enable:false},Legs:2,BuySell:{visible:true,enable:false},Contract:{visible:true,enable:false},Basis:{visible:true,enable:false},Rate:{visible:true,enable:false},Pip:{visible:true,enable:false}}, CANCEL_false_IF_THEN_OCO:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:false},ASSP:{visible:true,enable:false},Expiry:{visible:true,enable:false},Legs:3,BuySell:{visible:true,enable:false},Contract:{visible:true,enable:false},Basis:{visible:true,enable:false},Rate:{visible:true,enable:false},Pip:{visible:true,enable:false}}, CANCEL_false_TRAILINGSTOP:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:false},ASSP:{visible:true,enable:false},Expiry:{visible:true,enable:false},Legs:1,BuySell:{visible:true,enable:false},Contract:{visible:true,enable:false},Basis:{visible:true,enable:false},Rate:{visible:true,enable:false},Pip:{visible:true,enable:false}}, CANCEL_true_SINGLE_ASSP:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:false},ASSP:{visible:true,enable:false},Expiry:{visible:true,enable:false},Legs:1,BuySell:{visible:true,enable:false},Contract:{visible:true,enable:false},Basis:{visible:true,enable:false},Rate:{visible:true,enable:false},Pip:{visible:true,enable:false}}, CANCEL_true_SINGLE_ASSP_LIMIT: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: false }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false }, Legs: 1, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false }, Rate: { visible: true, enable: false }, Pip: { visible: true, enable: false} }, CANCEL_true_SINGLE_ASSP_STOP: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: false }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false }, Legs: 1, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false }, Rate: { visible: true, enable: false }, Pip: { visible: true, enable: false} }, CANCEL_true_OCO_ASSP: { orderlabel: { visible: true }, product: { visible: true, enable: false }, orderType: { visible: true, enable: false }, ASSP: { visible: true, enable: false }, Expiry: { visible: true, enable: false }, Legs: 2, BuySell: { visible: true, enable: false }, Contract: { visible: true, enable: false }, Basis: { visible: true, enable: false }, Rate: { visible: true, enable: false }, Pip: { visible: true, enable: false} }, CANCEL_true_TRAILINGSTOP_ASSP:{orderlabel:{visible:true},product:{visible:true,enable:false},orderType:{visible:true,enable:false},ASSP:{visible:true,enable:false},Expiry:{visible:true,enable:false},Legs:1,BuySell:{visible:true,enable:false},Contract:{visible:true,enable:false},Basis:{visible:true,enable:false},Rate:{visible:true,enable:false},Pip:{visible:true,enable:false}} }; GAIN.Utility = {}; GAIN.Utility.populateDropDown = function(dropdown, associateArray, keyArray) { dropdown.options.length = 0; var i = 0; if (keyArray != null) { if (associateArray != null) { for (i = 0; i < keyArray.length; i++) { try { dropdown.options[i] = new Option(associateArray[keyArray[i]].text, keyArray[i]); } catch (e) { }; }; } else { for (i = 0; i < keyArray.length; i++) { try { dropdown.options[i] = new Option(keyArray[i], keyArray[i]); } catch (e) { }; }; }; } else { i = 0; for (key in associateArray) { dropdown.options[i] = new Option(associateArray[key].text, associateArray[key].value); i++; }; }; }; GAIN.Utility.setDropDownValue = function (dropdown, value){ if (isNaN(value)){ if (value >= -1 && value < dropdown.options.length){ dropdown.selectedIndex = value; }; }else{ for (var i=0;i < dropdown.options.length ; i++) { if (dropdown.options[i].value == value){ dropdown.selectedIndex = i; break; } }; }; }; GAIN.Utility.cutRateIn3Parts = function(rate, iDecimal){ var index = rate.indexOf("."); if (index == 0) {rate = "0" + rate; index++;} else if (index == -1) {index = rate.length; rate = rate + ".";}; var NoOfDecimal = iDecimal - (rate.length - (index + 1)); for (var i=0; i < NoOfDecimal; i++){ rate = rate + "0"; } if (iDecimal == 5 || iDecimal == 3){ return {first:rate.substr(0,rate.length -3),second:rate.substr(rate.length -3,2),third:rate.substr(rate.length -1,1)}; }else{ if (rate.substr(rate.length - 1, 1) == '.') { rate = rate.substr(0, rate.length - 1); } return {first:rate.substr(0,rate.length -2),second:rate.substr(rate.length -2,2),third:""}; }; }; GAIN.Utility.cutProductRateIn3Parts = function(product, rate){ var index = rate.indexOf("."); var aProductInfo = aTradingManager.ProductSettingObject.data.get(product); if (index == 0) {rate = "0" + rate; index++;} else if (index == -1) {index = rate.length; rate = rate + ".";}; var NoOfDecimal = aProductInfo.IDecimals - (rate.length - (index + 1)); for (var i=0; i < NoOfDecimal; i++){ rate = rate + "0"; } var BoldIndex = index + parseInt(aProductInfo.BoldIndex); if (index + parseInt(aProductInfo.SuperScriptIndex) > rate.length - 1){ return {first:rate.substr(0,BoldIndex),second:rate.substr(BoldIndex,2),third:""}; }else{ return {first:rate.substr(0,BoldIndex),second:rate.substr(BoldIndex,2),third:rate.substr(index + parseInt(aProductInfo.SuperScriptIndex),1)}; } /*if (iDecimal == 5 || iDecimal == 3){ return {first:rate.substr(0,rate.length -3),second:rate.substr(rate.length -3,2),third:rate.substr(parseInt(aProductInfo.SuperScriptIndex),1)}; }else{ if (rate.substr(rate.length - 1, 1) == '.') { rate = rate.substr(0, rate.length - 1); } return {first:rate.substr(0,rate.length -2),second:rate.substr(rate.length -2,2),third:""}; };*/ }; //-------TradingManager----------------------------------------------------------------------------------- GAIN.TradingManager = function(config, initPara) { GAIN.TradingManager.superclass.constructor.call(this); this.addEvents({ "RatesUpdating": true, "PositionUpdating": true, "OrderUpdating": true, "MarginUpdating": true, "AccountSettingUpdating": true, "ProductSettingUpdating": true, "RatesUpdated": true, "PositionUpdated": true, "OrderUpdated": true, "MarginUpdated": true, "AccountSettingUpdated": true, "ProductSettingUpdated": true, "RatesUpdateFailed": true, "PositionUpdateFailed": true, "OrderUpdateFailed": true, "MarginUpdateFailed": true, "AccountSettingUpdateFailed": true, "ProductSettingUpdateFailed": true, "DealUpdateFailed": true, "EditOrder": true, "Logout": true, "NetworkFailure": true, "Initialized": true }); /*Reading from ProductSettingObj//this.defaultOrderPip = 30;*/ this.defaultTrailingPip = 10; this.isPlacingDeal = false; this.isPlacingOrder = false; this.LoginStatus = false; this.NetworkFailureThrehold = 15; this.EditingOrder = null; //action, OrderID, isASSP //var DealListingPair = null; // = null; var preRates = null; this.Rates = null; this.Positions = null; this.Margin = null; this.Orders = null; this.Deals = null; Ext.apply(this, config); var noOfconsecutiveFailure = 0; this.LoginStatus = (initPara.LoginStatus) ? initPara.LoginStatus : this.LoginStatus; this.NetworkFailureThrehold = (initPara.NetworkFailureThrehold) ? initPara.NetworkFailureThrehold : this.NetworkFailureThrehold; function reset_noOfconsecutiveFailure() { noOfconsecutiveFailure = 0; }; this.isInitialized = function() { //should be replaced by actual object status try { // alert(this.AccountSettingObject.status.enabled + " " + this.AccountSettingObject.status.dataInit + " " + this.ProductSettingObject.status.enabled + " " // + this.ProductSettingObject.status.dataInit + " " + this.RateObject.status.enabled + " " + this.RateObject.status.dataInit + " " // + this.DealsObject.status.enabled + " " + this.DealsObject.status.dataInit + " " + this.PositionsObject.status.enabled + " " // + this.PositionsObject.status.dataInit + " " + this.MarginObject.status.enabled + " " + this.MarginObject.status.dataInit + " " // + this.OrdersObject.status.enabled + " " + this.OrdersObject.status.dataInit // ); if (this.AccountSettingObject.status.enabled && !this.AccountSettingObject.status.dataInit) return false; if (this.ProductSettingObject.status.enabled && !this.ProductSettingObject.status.dataInit) return false; if (this.RateObject.status.enabled && (!this.RateObject.status.dataInit || this.Rates == null)) return false; if (this.DealsObject.status.enabled && !this.DealsObject.status.dataInit) return false; if (this.PositionsObject.status.enabled && (!this.PositionsObject.status.dataInit || this.Positions == null)) return false; if (this.MarginObject.status.enabled && (!this.MarginObject.status.dataInit || this.Margin == null)) return false; if (this.OrdersObject.status.enabled && (!this.OrdersObject.status.dataInit || this.Orders == null)) return false; return true; } catch (e) { return false; }; }; function checkInitialization() { if (this.isInitialized()) { this.fireEvent('Initialized'); }; } var ratesUpdated = function() { // if (preRates == null) // { // preRates = this.Rates.clone(); // }else{ // this.Rates.eachKey(function(){ // if (preRates.get(key)){ // preRates.get(key).Bid = item.Bid; // }; // }, this); // } //Ext.destroy(preRates); if (this.RateObject.data.getcount == 0) return; preRates = this.Rates; var isInit = (this.Rates == null); this.Rates = this.RateObject.data.clone(); if (isInit) { checkInitialization.call(this) }; // update rate change up/down if (preRates == null) { for (var i = 0; i < this.Rates.getCount; i++) { this.Rates.get(i).upDown = 0; }; } else { for (var i1 = 0; i1 < this.Rates.getCount(); i1++) { var key = this.Rates.getKey(this.Rates.get(i1)); if (!preRates.get(key)) { this.Rates.get(i1).upDown = 0; } else { var comparsion = 0; try { var temp = parseFloat(this.Rates.get(key).Bid) - parseFloat(preRates.get(key).Bid); if (isNaN(temp)) { comparsion = 0; } else { if (temp > 0) comparsion = 1; if (temp == 0) comparsion = 0; if (temp < 0) comparsion = -1; }; } catch (e) { } this.Rates.get(i1).upDown = comparsion; comparsion = 0; try { var temp2 = parseFloat(this.Rates.get(key).Offer) - parseFloat(preRates.get(key).Offer); if (isNaN(temp2)) { comparsion = 0; } else { if (temp2 > 0) comparsion = 1; if (temp2 == 0) comparsion = 0; if (temp2 < 0) comparsion = -1; }; } catch (e) { } this.Rates.get(i1).OfferupDown = comparsion; try { if (this.Rates.get(key).ChangeInPoints) { this.Rates.get(key).ChangeInPip = this.getPipDifference(key, 0, this.Rates.get(key).ChangeInPoints); } } catch (e) { } }; }; }; this.fireEvent('RatesUpdated'); //update position positionsUpdated.call(this, false); //positionUpdate will call Margin Update at the end and it also include deals update //update order ordersUpdated.call(this, false); return true; }; var positionsUpdated = function(isPositionUpdate) { if (this.PositionsObject.status.enabled == false && !isPositionUpdate) { return false; }; var tempPositions; if (isPositionUpdate) { tempPositions = this.PositionsObject.data.clone(); for (var i = 0; i < tempPositions.getCount(); i++) { tempPositions.get(i).Deals = []; } } else { if (this.Positions == null) return false; else tempPositions = this.Positions; }; var tempRates = this.Rates; for (var i2 = 0; i2 < tempPositions.getCount(); i2++) { var aposition = tempPositions.get(i2); try { var key = tempPositions.getKey(tempPositions.get(i2)); if (tempRates.get(key)) { if (parseInt(aposition.Contract) > 0) { aposition.LRate = tempRates.get(key).Bid; } else { aposition.LRate = tempRates.get(key).Offer; } aposition.upDown = tempRates.get(key).upDown; if (this.PositionsObject.hasPLPip) { if (parseInt(aposition.Contract) > 0) { aposition.PLPip = this.getPipDifference(key, aposition.AverageRate, tempRates.get(key).Bid); } else { aposition.PLPip = this.getPipDifference(key, tempRates.get(key).Offer, aposition.AverageRate); }; }; } } catch (e) { }; try { if (this.PositionsObject.hasUnRealizedPL) { this.getUnRealizedPnl(aposition); }; } catch (e) { }; }; var isInit = (this.Positions == null); this.Positions = tempPositions; if (isInit) { checkInitialization.call(this) }; //do P/L calculation //update Deals if (updateDeals.call(this, isPositionUpdate) == false) this.fireEvent('PositionUpdated'); dealsUpdated.call(this,false); marginUpdated.call(this, false); return true; }; var dealsUpdated = function() { if (!this.DealsObject.jsonPara.product || this.DealsObject.jsonPara.product == "") { return false; }; var tempDeals; tempDeals = this.DealsObject.data.clone(); var tempRates = this.Rates; for (var i = 0; i < tempDeals.getCount(); i++) { try { var key = tempDeals.get(i).Product; if (tempRates.get(key)) { if (parseInt(tempDeals.get(i).Contract) > 0) { tempDeals.get(i).LRate = tempRates.get(key).Bid; } else { tempDeals.get(i).LRate = tempRates.get(key).Offer; } tempDeals.get(i).upDown = tempRates.get(key).upDown; if (this.PositionsObject.hasPLPip) { if (parseInt(tempDeals.get(i).Contract) > 0) { tempDeals.get(i).PLPip = this.getPipDifference(key, tempDeals.get(i).Rate, tempRates.get(key).Bid); } else { tempDeals.get(i).PLPip = this.getPipDifference(key, tempRates.get(key).Offer, tempDeals.get(i).Rate); }; }; if (this.PositionsObject.hasUnRealizedPL) { tempDeals.get(i).AverageRate = tempDeals.get(i).Rate; this.getUnRealizedPnl(tempDeals.get(i)); }; }; } catch (e) { }; }; //do P/L calculation this.Deals = tempDeals; //update Deals try { for (var i2 = 0; i2 < this.Positions.getCount(); i2++) { this.Positions.get(i2).Deals = []; }; for (var i3 = 0; i3 < tempDeals.getCount(); i3++) { var aPair = tempDeals.get(i3).Product; this.Positions.get(aPair).Deals.push(tempDeals.get(i3)); }; } catch (e) { }; this.fireEvent('PositionUpdated'); return true; }; var updateDeals = function(isPositionUpdate) { if (!this.DealsObject.jsonPara.product || this.DealsObject.jsonPara.product == null || this.DealsObject.jsonPara.product == '') { return false; }; if (!isPositionUpdate) { //update rates on deals collection var tempRates = this.Rates; for (var i = 0; i < this.Positions.getCount(); i++) { if (this.Positions.get(i).Deals && this.Positions.get(i).Deals.length > 0) { var key = this.Positions.getKey(this.Positions.get(i)); if (tempRates.get(key)) { try { for (var j; j < this.Positions.get(i).Deals.length; j++) { if (parseInt(this.Positions.get(key).Deals[j].Contract) > 0) { this.Positions.get(key).Deals[j].LRate = tempRates.get(key).Bid; } else { this.Positions.get(key).Deals[j].LRate = tempRates.get(key).Offer; } this.Positions.get(key).Deals[j].upDown = tempRates.get(key).upDown; if (this.PositionsObject.hasPLPip) { if (parseInt(this.Positions.get(key).Deals[j].Contract) > 0) { this.Positions.get(key).Deals[j].PLPip = this.getPipDifference(key, this.Positions.get(key).Deals[j].Rate, tempRates.get(key).Bid); } else { this.Positions.get(key).Deals[j].PLPip = this.getPipDifference(key, tempRates.get(key).Offer, this.Positions.get(key).Deals[j].Rate); }; }; if (this.PositionsObject.hasUnRealizedPL) { this.Positions.get(key).Deals[j].AverageRate = this.Positions.get(key).Deals[j].Rate; this.getUnRealizedPnl(tempDeals.get(i)); }; }; } catch (e) { }; }; }; }; // P/L update for deals this.fireEvent('PositionUpdated'); return true; } else { //make ajax call to get latest deal list this.DealsObject.requestData(1, true); return true; }; }; var marginUpdated = function(isMarginUpdate) { if (this.MarginObject.status.enabled == false) { return false; }; if (isMarginUpdate) { var isInit = (this.Margin == null); this.Margin = this.MarginObject.data; if (isInit) { checkInitialization.call(this) }; }; //Margin Calculation try { var tempPositions = this.Positions; var tempUnrealised = 0; var tempUnrealisedInUSD = 0; var RequiredMargin = 0; //console.log('start margin'); var BlowoutLevel = parseFloat(this.AccountSettingObject.data.BlowOutLevel); //console.log('BlowoutLevel ' + BlowoutLevel); for (var i = 0; i < tempPositions.getCount(); i++) { tempUnrealised = tempUnrealised + parseFloat(tempPositions.get(i).Unrealized); //console.log('tempUnrealised ' + tempUnrealised); tempUnrealisedInUSD = tempUnrealisedInUSD + tempPositions.get(i).UnrealizedinUSD; //console.log('tempUnrealisedInUSD ' + tempUnrealisedInUSD); var MarginFactor = this.ProductSettingObject.data.get(tempPositions.get(i).Product).MarginFactor; //console.log('MarginFactor ' + MarginFactor); RequiredMargin = RequiredMargin + Math.abs(parseFloat(tempPositions.get(i).USDValue)) * MarginFactor / 100; //console.log('RequiredMargin ' + RequiredMargin); } this.Margin[0].UnrealizedProfit = tempUnrealised.toFixed(2); var MarginBalanceUSD = parseFloat(this.Margin[0].USDPostedMargin) + parseFloat(this.Margin[0].USDRealizedProfit) + tempUnrealisedInUSD; //console.log('MarginBalanceUSD ' + MarginBalanceUSD); if (MarginBalanceUSD > 0) { this.Margin[0].Meter = 100 * (1 - RequiredMargin * BlowoutLevel / 100 / MarginBalanceUSD); } else { this.Margin[0].Meter = 0; }; //console.log('Meter ' + this.Margin[0].Meter); } catch (e) { }; this.fireEvent('MarginUpdated'); return true; }; var ordersUpdated = function(isOrderUpdate) { if (this.OrdersObject.status.enabled == false) { return false; }; var tempOrder; if (isOrderUpdate) { tempOrder = this.OrdersObject.data.clone(); } else { if (this.Orders == null) return false; else tempOrder = this.Orders; }; var tempRates = this.Rates; if (tempRates != null) { for (var i = 0; i < tempOrder.getCount(); i++) { var key = tempOrder.get(i).Product; if (tempRates.get(key)) { tempOrder.get(i).LRate = tempRates.get(key).Bid; tempOrder.get(i).upDown = tempRates.get(key).upDown; for (var j = 0; j < tempOrder.get(i).Legs.length; j++) { if(tempOrder.get(i).Legs[j].BuySell=='S') tempOrder.get(i).Legs[j].LRate = tempRates.get(key).Bid; else tempOrder.get(i).Legs[j].LRate = tempRates.get(key).Offer; tempOrder.get(i).Legs[j].RDiff = this.getRateDifference(key, tempOrder.get(i).Legs[j].Rate, tempOrder.get(i).Legs[j].LRate); } } }; }; var isInit = (this.Orders == null); this.Orders = tempOrder; if (isInit) { checkInitialization.call(this) }; this.fireEvent('OrderUpdated'); return true; }; function AccountSettingUpdated() { this.fireEvent('AccountSettingUpdated'); }; function ProductSettingUpdated() { this.fireEvent('ProductSettingUpdated'); }; function disableTradingObjects() { this.CheckStatusObject.status.enabled = false; this.AccountSettingObject.status.enabled = false; this.ProductSettingObject.status.enabled = false; this.RateObject.status.enabled = false; this.DealsObject.status.enabled = false; this.PositionsObject.status.enabled = false; this.MarginObject.status.enabled = false; this.OrdersObject.status.enabled = false; }; function fireInternalEvent(eventName) { if (eventName.indexOf('Failed' > -1)) { noOfconsecutiveFailure++; if (noOfconsecutiveFailure > this.NetworkFailureThrehold) { //disableTradingObjects.call(this); reset_noOfconsecutiveFailure(); this.fireEvent("NetworkFailure"); }; }; this.fireEvent(eventName); }; function CheckStatusUpdated() { reset_noOfconsecutiveFailure(); if (this.LoginStatus == true && this.CheckStatusObject.data != true) { disableTradingObjects.call(this); this.fireEvent("Logout"); } }; this.CheckStatusObject = new GAIN.CheckStatus({ url: ui + "/GetStatus", refreshInterval: 30000 }); this.CheckStatusObject.addListener('Loaded', CheckStatusUpdated.createDelegate(this)); this.CheckStatusObject.addListener('LoadFailed', fireInternalEvent.createDelegate(this, ["CheckStatusUpdateFailed"])); var AccountSettingsPara = initPara.AccountSettings; if (!AccountSettingsPara) AccountSettingsPara = { url: ui + "/GetAccountSettings", refreshInterval: 2000, status: { requesting: false, dataReady: false, init: false, repeatable: false, dataInit: false, enabled: true} }; this.AccountSettingObject = new GAIN.AccountSettings(AccountSettingsPara); this.AccountSettingObject.addListener('dataInit', checkInitialization.createDelegate(this)); this.AccountSettingObject.addListener('Loaded', AccountSettingUpdated.createDelegate(this)); this.AccountSettingObject.addListener('Loaded', reset_noOfconsecutiveFailure); this.AccountSettingObject.addListener('Updating', fireInternalEvent.createDelegate(this, ["AccountSettingUpdating"])); this.AccountSettingObject.addListener('LoadFailed', fireInternalEvent.createDelegate(this, ["AccountSettingUpdateFailed"])); var ProductSettingsPara = initPara.ProductSettings; if (!ProductSettingsPara) ProductSettingsPara = { url: ui + "/GetProductSettings", refreshInterval: 2000, status: { requesting: false, dataReady: false, init: false, repeatable: false, dataInit: false, enabled: true} }; this.ProductSettingObject = new GAIN.ProductSettings(ProductSettingsPara); this.ProductSettingObject.addListener('dataInit', checkInitialization.createDelegate(this)); this.ProductSettingObject.addListener('Loaded', ProductSettingUpdated.createDelegate(this)); this.ProductSettingObject.addListener('Loaded', reset_noOfconsecutiveFailure); this.ProductSettingObject.addListener('Updating', fireInternalEvent.createDelegate(this, ["ProductSettingUpdating"])); this.ProductSettingObject.addListener('LoadFailed', fireInternalEvent.createDelegate(this, ["ProductSettingUpdateFailed"])); var RatesPara = initPara.Rates; if (!RatesPara) RatesPara = { url: ui + "/GetUserRates", refreshInterval: 2000 }; this.RateObject = new GAIN.Rates(RatesPara); this.RateObject.addListener('dataInit', checkInitialization.createDelegate(this)); this.RateObject.addListener('Loaded', ratesUpdated.createDelegate(this)); this.RateObject.addListener('Loaded', reset_noOfconsecutiveFailure); this.RateObject.addListener('Updating', fireInternalEvent.createDelegate(this, ["RatesUpdating"])); this.RateObject.addListener('LoadFailed', fireInternalEvent.createDelegate(this, ["RatesUpdateFailed"])); var DealsPara = initPara.Deals; if (!RatesPara) DealsPara = { url: ui + "/GetProductDeals", refreshInterval: 30000, status: { requesting: false, dataReady: false, init: false, repeatable: false, dataInit: false, enabled: false }, jsonPara: { product: ''} }; this.DealsObject = new GAIN.Deals(DealsPara); this.DealsObject.addListener('dataInit', checkInitialization.createDelegate(this)); this.DealsObject.addListener('Loaded', dealsUpdated.createDelegate(this)); this.DealsObject.addListener('Loaded', reset_noOfconsecutiveFailure); this.DealsObject.addListener('LoadFailed', fireInternalEvent.createDelegate(this, ["DealUpdateFailed"])); var PositionsPara = initPara.Positions; if (!PositionsPara) PositionsPara = { url: ui + "/GetPositions", refreshInterval: 30000 }; this.PositionsObject = new GAIN.Positions(PositionsPara); this.PositionsObject.addListener('dataInit', checkInitialization.createDelegate(this)); this.PositionsObject.addListener('Loaded', positionsUpdated.createDelegate(this, [true])); this.PositionsObject.addListener('Loaded', reset_noOfconsecutiveFailure); this.PositionsObject.addListener('Updating', fireInternalEvent.createDelegate(this, ["PositionUpdating"])); this.PositionsObject.addListener('LoadFailed', fireInternalEvent.createDelegate(this, ["PositionUpdateFailed"])); var MarginPara = initPara.Margin; if (!MarginPara) MarginPara = { url: ui + "/GetMargins", refreshInterval: 30000 }; this.MarginObject = new GAIN.Margin(MarginPara); this.MarginObject.addListener('dataInit', checkInitialization.createDelegate(this)); this.MarginObject.addListener('Loaded', marginUpdated.createDelegate(this, [true])); this.MarginObject.addListener('Loaded', reset_noOfconsecutiveFailure); this.MarginObject.addListener('Updating', fireInternalEvent.createDelegate(this, ["MarginUpdating"])); this.MarginObject.addListener('LoadFailed', fireInternalEvent.createDelegate(this, ["MarginUpdateFailed"])); var OrdersPara = initPara.Orders; if (!OrdersPara) OrdersPara = { url: ui + "/GetOrders", refreshInterval: 30000 }; this.OrdersObject = new GAIN.Orders(OrdersPara); this.OrdersObject.addListener('dataInit', checkInitialization.createDelegate(this)); this.OrdersObject.addListener('Loaded', ordersUpdated.createDelegate(this, [true])); this.OrdersObject.addListener('Loaded', reset_noOfconsecutiveFailure); this.OrdersObject.addListener('Updating', fireInternalEvent.createDelegate(this, ["OrderUpdating"])); this.OrdersObject.addListener('LoadFailed', fireInternalEvent.createDelegate(this, ["OrderUpdateFailed"])); //Place Deal method //Close Deal this.PlaceDeal = function(dealType, aDealData, isClose, aCallback) { if (!this.isPlacingDeal) { this.isPlacingDeal = true; var aConnection = new Ext.data.Connection(); aConnection.request( { url: ui + "/PlaceDeal", method: "POST", headers: { "Content-Type": "application/json" }, scope: this, jsonData: { aType: dealType, aDeal: aDealData, isClose: isClose }, callback: function(options, success, response) { var temp = null; if (success) { temp = Ext.util.JSON.decode(response.responseText); this.PositionsObject.requestData(1, true); this.OrdersObject.requestData(1, true); } try{ aCallback(success, temp);} catch(e){;} this.isPlacingDeal = false; } }); return { success: true, message: GAIN.messageDealProcessing }; } else { return { success: false, message: GAIN.messageAnotherDealProcessing }; }; }; //place Order method //Modify Order method this.PlaceOrder = function PlaceOrder(action, aOrderData, aCallback) { if (!this.isPlacingOrder) { if (action != 'CANCEL' && (aOrderData.Type == 'TRAILINGSTOP' || aOrderData.Type == 'TRAILINGSTOP_ASSP')) { var tempPS = this.ProductSettingObject.data.get(aOrderData.Product); if (parseFloat(aOrderData.TrailingPoints) < tempPS.PipDifference || parseFloat(aOrderData.TrailingPoints) > tempPS.MaxOrderPips) { return { success: false, message: GAIN.TrailingStopPipErrorMessage + '(' + tempPS.PipDifference + ' - ' + tempPS.MaxOrderPips + ' )' }; }; }; this.isPlacingOrder = true; var aConnection = new Ext.data.Connection(); aConnection.request( { url: ui + "/OrderRequest", method: "POST", headers: { "Content-Type": "application/json" }, scope: this, jsonData: { aAction: action, aOrder: aOrderData }, callback: function(options, success, response) { this.isPlacingOrder = false; var temp = null; if (success) { temp = Ext.util.JSON.decode(response.responseText); this.PositionsObject.requestData(1, true); this.OrdersObject.requestData(1, true); } aCallback(success, temp); } }) return { success: true, message: GAIN.messageOrderProcessing }; } else { return { success: false, message: GAIN.messageAnotherOrderProcessing }; }; }; //Edit Order this.EditOrderInDock = function(ActionType, OrderID, isASSP, product, LimitStop, fromDeal) { if (this.isPlacingDeal || this.isPlacingOrder) return; var OrderType = null; if (OrderID == null && isASSP == true) { var aposition; try { aposition = this.Positions.get(product); } catch (e) { }; if (fromDeal) { OrderType = 'OCO_ASSP'; } else { if (aposition) { if (aposition.LimitRate != 0 || aposition.StopRate != 0) { ActionType = 'MODIFY'; var tempOrders = this.Orders; try { for (var i = 0; i < tempOrders.getCount(); i++) { if (tempOrders.get(i).Product == product && tempOrders.get(i).Assp == 'True') { var aOrder = tempOrders.get(i); OrderID = aOrder.ReferenceNumber; if (aOrder.Type == 'TRAILINGSTOP_ASSP') { //alert('Trailing stop cannot be modified!!'); //return; ActionType = 'CANCEL'; OrderType = 'TRAILINGSTOP_ASSP'; break; } if ((aOrder.Type == 'SINGLE_ASSP' || aOrder.Type == 'SINGLE_ASSP_LIMIT' || aOrder.Type == 'SINGLE_ASSP_STOP') && aOrder.Legs[0].Basis != LimitStop) { OrderType = 'OCO_ASSP'; break; }; }; }; } catch (e) { }; } else { OrderType = 'OCO_ASSP'; }; } else { //not decide yet OrderType = 'OCO_ASSP'; }; }; } this.EditingOrder = { action: ActionType, orderID: OrderID, isASSP: isASSP, product: product, LimitStop: LimitStop, OrderType: OrderType }; this.fireEvent('EditOrder'); }; this.updateDealListingPair = function(Pair) { this.DealsObject.jsonPara.product = Pair; if (Pair == '') { for (var i = 0; i < this.Positions.getCount(); i++) { this.Positions.get(i).Deals = []; }; this.fireEvent('PositionUpdated'); } else { this.DealsObject.requestData(1, true); }; }; this.getOrderSpinnerInfoModify = function(Order, OrderSeq) { var aOrder = this.Orders.get(Order); var aOrderType = aOrder.Type; // if (newOrderType != null) { // aOrderType = newOrderType; // }; if (!aOrder) return null; var aLeg = aOrder.Legs[OrderSeq]; if (!aLeg) { aLeg = {}; if (aOrder.Type == 'SINGLE_ASSP' || aOrder.Type == 'SINGLE_ASSP_LIMIT' || aOrder.Type == 'SINGLE_ASSP_STOP') { aLeg.BuySell = aOrder.Legs[0].BuySell; if (aOrder.Legs[0].Basis == 'T') { aLeg.Basis = 'S'; } else { }; aLeg.Rate = null; aLeg.TSP = 0; aLeg.Amount = aOrder.Legs[0].Amount; aOrderType = "OCO_ASSP"; } else { return null; }; }; var tempPip = null; if (aLeg.TSP != 0) tempPip = aLeg.TSP; return this.getOrderSpinnerInfoNew(aOrder.Product, aLeg.BuySell, aLeg.Basis, aLeg.Rate, tempPip, aOrderType, aLeg.Amount, aOrder.Legs[0].Rate, OrderSeq); }; this.getOrderSpinnerInfoNew = function(product, BuySell, LimitStop, inputRate, inputPip, OrderType, orderAmount, firstLegRate, seq) { var increment; var outputRate; var outputPip; var outputLimitStop; var outputBuySell; var outputSize; var noOfDecimal; var aposition = null; try { aposition = this.Positions.get(product); } catch (e) { }; var aLRate = this.Rates.get(product); var aProductInfo = this.ProductSettingObject.data.get(product); if (!aLRate) return null; noOfDecimal = this.getNoOfDemicalPlace(product); if (noOfDecimal==null) return null; increment = 1 / Math.pow(10, noOfDecimal); //logic for getting increment, rate and pip //alert(OrderType); switch (OrderType) { case "MARKET_ORDER": if (BuySell == 'B') return { BuySell: BuySell, LimitStop: null, Rate: aLRate.Offer, Pip: 0, increment: increment, size: outputSize, decimalPlaces: noOfDecimal }; else return { BuySell: BuySell, LimitStop: null, Rate: aLRate.Bid, Pip: 0, increment: increment, size: outputSize, decimalPlaces: noOfDecimal }; break; case "SINGLE_ORDER": case "SINGLE_ORDER_STOP": case "SINGLE_ORDER_LIMIT": case "OCO_ORDER": outputRate = inputRate; outputLimitStop = LimitStop; outputBuySell = BuySell; if (outputBuySell == 'B') { if (inputRate) { outputPip = this.getPipDifference(product, aLRate.Offer, inputRate); } else { if (inputPip) { outputPip = inputPip; } else { if (LimitStop == 'T') { outputPip = aProductInfo.OrderPipsAway * -1; } else { outputPip = aProductInfo.OrderPipsAway; }; }; outputRate = this.getRatePlusPip(product, aLRate.Offer, outputPip); }; } else { if (inputRate) { outputPip = this.getPipDifference(product, aLRate.Bid, inputRate); } else { if (inputPip) { outputPip = inputPip; } else { if (LimitStop == 'T') { outputPip = aProductInfo.OrderPipsAway; } else { outputPip = aProductInfo.OrderPipsAway * -1; }; }; outputRate = this.getRatePlusPip(product, aLRate.Bid, outputPip); }; }; if (orderAmount != null) { outputSize = orderAmount.replace("-", ""); }; break; case "IF_THEN": case "IF_THEN_OCO": outputRate = inputRate; outputLimitStop = LimitStop; outputBuySell = BuySell; if (outputBuySell == 'B') { if (inputRate) { if (seq != 0) { outputPip = this.getPipDifference(product, firstLegRate, inputRate); } else { outputPip = this.getPipDifference(product, aLRate.Offer, inputRate); }; } else { if (inputPip) { outputPip = inputPip; } else { if (LimitStop == 'T') { outputPip = aProductInfo.OrderPipsAway * -1; } else { outputPip = aProductInfo.OrderPipsAway; }; }; if (seq != 0) { outputRate = this.getRatePlusPip(product, firstLegRate, outputPip); } else { outputRate = this.getRatePlusPip(product, aLRate.Offer, outputPip); }; }; } else { if (inputRate) { if (seq != 0) { outputPip = this.getPipDifference(product, firstLegRate, inputRate); } else { outputPip = this.getPipDifference(product, aLRate.Bid, inputRate); }; } else { if (inputPip) { outputPip = inputPip; } else { if (LimitStop == 'T') { outputPip = aProductInfo.OrderPipsAway; } else { outputPip = aProductInfo.OrderPipsAway * -1; }; }; if (seq != 0) { outputRate = this.getRatePlusPip(product, firstLegRate, outputPip); } else { outputRate = this.getRatePlusPip(product, aLRate.Bid, outputPip); }; }; }; if (orderAmount != null) { outputSize = orderAmount.replace("-", ""); }; break; case "SINGLE_ASSP": case "SINGLE_ASSP_STOP": case "SINGLE_ASSP_LIMIT": case "OCO_ASSP": if (!aposition) return null; if (parseInt(aposition.Contract) >= 0) outputBuySell = "S"; else outputBuySell = "B"; outputRate = inputRate; outputLimitStop = LimitStop; //alert(aposition.Contract); outputSize = Math.abs(aposition.Contract); //alert(outputSize); if (outputBuySell == 'B') { if (inputRate) { outputPip = this.getPipDifference(product, aposition.AverageRate, inputRate); } else { if (inputPip) { outputPip = inputPip; } else { if (LimitStop == 'T') { outputPip = aProductInfo.OrderPipsAway * -1; } else { outputPip = aProductInfo.OrderPipsAway; }; }; outputRate = this.getRatePlusPip(product, aposition.AverageRate, outputPip); }; } else { if (inputRate) { outputPip = this.getPipDifference(product, aposition.AverageRate, inputRate); } else { if (inputPip) { outputPip = inputPip; } else { if (LimitStop == 'T') { outputPip = aProductInfo.OrderPipsAway; } else { outputPip = aProductInfo.OrderPipsAway * -1; }; }; outputRate = this.getRatePlusPip(product, aposition.AverageRate, outputPip); }; }; break; case "TRAILINGSTOP_ASSP": if (!aposition) return null; if (parseInt(aposition.Contract) >= 0) outputBuySell = "S"; else outputBuySell = "B"; outputLimitStop = "S"; outputSize = Math.abs(aposition.Contract); if (inputPip) outputPip = inputPip; else outputPip = this.defaultTrailingPip; if (outputBuySell == 'B') { outputRate = aLRate.Offer; } else { outputRate = aLRate.Bid; }; break; case "TRAILINGSTOP": // if (parseInt(aposition.Contract) >= 0) outputBuySell = "S"; // else outputBuySell = "B"; outputBuySell = BuySell; outputLimitStop = LimitStop; outputSize = orderAmount; if (outputBuySell == 'B') { outputRate = aLRate.Offer; } else { outputRate = aLRate.Bid; }; if (inputPip) outputPip = inputPip; else outputPip = this.defaultTrailingPip; if (outputBuySell == 'B') { outputRate = aLRate.Offer; } else { outputRate = aLRate.Bid; }; if (orderAmount != null) { outputSize = orderAmount.replace("-", ""); }; break; default: return null; break; } return { BuySell: outputBuySell, LimitStop: outputLimitStop, Rate: outputRate, Pip: outputPip, increment: increment, size: outputSize, decimalPlaces: noOfDecimal }; }; this.getRateDifference = function(product, rate1, rate2) { var decmialPlace = this.getNoOfDemicalPlace(product); var temp = decmialPlace-(decmialPlace % 2); if (decmialPlace!=null) { if(isNaN(rate2)){rate2=0;} return ((parseFloat(rate2) - parseFloat(rate1)) * Math.pow(10, temp)).toFixed((decmialPlace % 2)); } else { return null; }; }; this.getPipDifference = function(product, rate1, rate2) { var decimalPlace = this.getNoOfDemicalPlace(product); /* [iDecimal-(iDecimal MOD 2)]*-1 10 */ if (decimalPlace!=null) { var temp; //if (decimalPlace == 3 || decimalPlace == 5) temp = decimalPlace - 1; else temp = decimalPlace; //return ((parseFloat(rate2) - parseFloat(rate1)) * Math.pow(10, temp)).toFixed(1); temp = decimalPlace-(decimalPlace % 2); if(isNaN(rate2)){rate2=0;} return ((parseFloat(rate2) - parseFloat(rate1)) * Math.pow(10, temp)).toFixed(1); } else { return null; }; }; this.getRatePlusPip = function(product, rate, pip) { var decimalPlace = this.getNoOfDemicalPlace(product); if (decimalPlace!=null) { var temp; //if (decimalPlace == 3 || decimalPlace == 5) temp = decimalPlace - 1; else temp = decimalPlace; temp = decimalPlace-(decimalPlace % 2); if(isNaN(pip)){pip=0;} return ((parseFloat(rate) + pip / Math.pow(10, temp))).toFixed(decimalPlace); } else { return null; }; }; this.getNoOfDemicalPlace = function(product) { var temp = this.ProductSettingObject.data.get(product); if (temp != null) return temp.IDecimals; else return null; }; this.getRateIncrement = function(product) { var decimalPlace = this.getNoOfDemicalPlace(product); if (decimalPlace!=null) { return 1 / Math.pow(10, decimalPlace); } else { return null; }; }; this.getUnRealizedPnl = function(aposition) { var BasePair = this.AccountSettingObject.data.BasePair; var BaseCurrency = (BasePair == "USD/USD") ? "USD" : BasePair.replace("USD", "").replace("/", ""); var RateDifference; var tempRate = this.Rates; if (parseInt(aposition.Contract) > 0) { RateDifference = parseFloat(tempRate.get(aposition.Product).Bid) - parseFloat(aposition.AverageRate); } else { RateDifference = parseFloat(tempRate.get(aposition.Product).Offer) - parseFloat(aposition.AverageRate); }; //console.log("aposition.Product " + aposition.Product); //console.log("RateDifference " + RateDifference); var CurrencyPnL = parseInt(aposition.Contract) * RateDifference //console.log("CurrencyPnL " + CurrencyPnL); var Unrealized = null; if (aposition.Product.substr(4, 3) == BaseCurrency) { Unrealized = CurrencyPnL; //console.log("right side = basecurrency " + Unrealized); } else { //console.log("Unrealized F " + Unrealized); }; var RevalRate; //console.log(aposition.Product.substr(4,3)); if (aposition.Product.substr(0, 3) == "USD") { if (CurrencyPnL > 0) { RevalRate = parseFloat(tempRate.get(aposition.Product).Offer); } else { RevalRate = parseFloat(tempRate.get(aposition.Product).Bid); }; }; //console.log("RevalRate " + RevalRate); var UnRealizedDollarPnL; if (aposition.Product.substr(4, 3) == "USD") { UnRealizedDollarPnL = CurrencyPnL; //console.log("usd right " + UnRealizedDollarPnL); } else if (aposition.Product.substr(0, 3) == "USD") { UnRealizedDollarPnL = CurrencyPnL / RevalRate; //console.log("usd left " + UnRealizedDollarPnL); } else { var CounterProduct = this.ProductSettingObject.data.get(aposition.Product).CounterProduct; var CounterProductNotation = this.ProductSettingObject.data.get(CounterProduct).Notation; //console.log("CounterProduct " + CounterProduct + " " + CounterProductNotation); if (CounterProductNotation == "E") { if (CurrencyPnL > 0) { UnRealizedDollarPnL = CurrencyPnL / parseFloat(tempRate.get(CounterProduct).Offer); } else { UnRealizedDollarPnL = CurrencyPnL / parseFloat(tempRate.get(CounterProduct).Bid); }; } else { if (CurrencyPnL > 0) { UnRealizedDollarPnL = CurrencyPnL * parseFloat(tempRate.get(CounterProduct).Bid); } else { UnRealizedDollarPnL = CurrencyPnL * parseFloat(tempRate.get(CounterProduct).Offer); }; }; }; aposition.UnrealizedinUSD = UnRealizedDollarPnL; if (Unrealized == null) { //console.log("UnRealizedDollarPnL " + UnRealizedDollarPnL); if (BasePair != "USD/USD") { var BaseProductNotation = this.ProductSettingObject.data.get(BasePair).Notation; //console.log("BaseProductNotation " + CounterProduct + " " + CounterProductNotation); if (BaseProductNotation == "E") { if (UnRealizedDollarPnL < 0) { Unrealized = UnRealizedDollarPnL * parseFloat(tempRate.get(BasePair).Offer); } else { Unrealized = UnRealizedDollarPnL * parseFloat(tempRate.get(BasePair).Bid); }; } else { if (UnRealizedDollarPnL < 0) { Unrealized = UnRealizedDollarPnL / parseFloat(tempRate.get(BasePair).Bid); } else { Unrealized = UnRealizedDollarPnL / parseFloat(tempRate.get(BasePair).Offer); }; }; } else { Unrealized = UnRealizedDollarPnL; }; }; aposition.Unrealized = Unrealized.toFixed(2); }; this.initEditOrder = function() { var temp = this.ProductSettingObject.data.get(0).Product; this.EditingOrder = { action: 'PLACE', orderID: null, isASSP: false, product: temp }; }; this.getProductAlias = function(product,contract) { if(this.ProductSettingObject!=null && this.ProductSettingObject.data!=null & this.ProductSettingObject.data.length>0) { var pro = this.ProductSettingObject.data.get(product); if(pro!=null){ if(contract && pro.AliasName.indexOf("/")>-1){ return pro.AliasName.substr(0,pro.AliasName.indexOf("/")); } else{return pro.AliasName;} } } return product; }; this.isFlagVisible = function(product) { if(this.ProductSettingObject!=null && this.ProductSettingObject.data!=null & this.ProductSettingObject.data.length>0) { var pro = this.ProductSettingObject.data.get(product); if(pro!=null){ if(pro.AliasName.indexOf("/")>-1){ return true; } else{return false;} } } return true; }; }; Ext.extend(GAIN.TradingManager, Ext.util.Observable, {});