123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- package response
- import (
- "fmt"
- "testing"
- "encoding/xml"
- )
- func TestOrderBookEvent(t *testing.T) {
- eventData := "4001|141bc6fee7a|20@1.34889;300@1.34888;210@1.34887;240@1.34886;670@1.34885|20@1.34892;90@1.34893;382.5@1.34894;530@1.34895;230@1.34896|1.35608;141b8c4b080|1.35713|1.34798|1.34889|1.34892|1.3489"
- event := NewOrderBookEvent(eventData)
- if event.InstrumentId != 4001 {
- t.Errorf("OrderBookEvent.InstrumentId Error")
- }
- data, err := event.Encode()
- if err != nil {
- t.Error(err)
- }
- if string(data) != "<ob2>" + eventData + "</ob2>"{
- t.Errorf(string(data))
- }
- }
- func TestInstrument(t *testing.T) {
- data := "<res><header><status>OK</status></header><body><instruments><instrument><id>4001</id><name>EUR/USD</name><startTime>2010-07-09T13:09:00</startTime><tradingHours><openingOffset>-415</openingOffset><closingOffset>1020</closingOffset><timezone>America/New_York</timezone></tradingHours><margin>1</margin><currency>USD</currency><unitPrice>10000</unitPrice><minimumOrderQuantity>0.1</minimumOrderQuantity><orderQuantityIncrement>0.1</orderQuantityIncrement><minimumPrice>0</minimumPrice><maximumPrice>99999</maximumPrice><trustedSpread>0.0018</trustedSpread><priceIncrement>0.00001</priceIncrement><stopBuffer>0</stopBuffer><assetClass>CURRENCY</assetClass><underlyingIsin/><symbol>EUR/USD</symbol><maximumPositionThreshold>50000</maximumPositionThreshold><aggressiveCommissionRate>0.250</aggressiveCommissionRate><passiveCommissionRate>0.250</passiveCommissionRate><minimumCommission>0.00</minimumCommission><longSwapPoints>0.00</longSwapPoints><shortSwapPoints>0.00</shortSwapPoints><dailyInterestRateBasis>360</dailyInterestRateBasis><contractUnitOfMeasure>EUR</contractUnitOfMeasure><contractSize>10000.00</contractSize><tradingDays><tradingDay>MONDAY</tradingDay><tradingDay>TUESDAY</tradingDay><tradingDay>WEDNESDAY</tradingDay><tradingDay>THURSDAY</tradingDay><tradingDay>FRIDAY</tradingDay></tradingDays><retailVolatilityBandPercentage>2</retailVolatilityBandPercentage></instrument></instruments><hasMoreResults>true</hasMoreResults></body></res>"
- insts := NewInstruments(data)
- ins := insts.Data[0]
- if ins.Id != 4001 {
- t.Errorf("Id Error")
- }
- if ins.Name != "EUR/USD" {
- t.Errorf("Name Error")
- }
- if ins.Symbol != "EUR/USD" {
- t.Errorf("Symbol Error")
- }
- if ins.Isin != "" {
- t.Errorf("Isin Error")
- }
- if ins.AssetClass != "CURRENCY" {
- t.Errorf("Symbol Error")
- }
- if ins.StartTime == nil {
- t.Error("StartTime Error")
- }
- if ins.OpenOffset != -415 {
- t.Errorf("Open Error")
- }
- if ins.CloseOffset != 1020 {
- t.Errorf("Close Error")
- }
- if ins.TimeZone != "America/New_York" {
- t.Errorf("Time Error")
- }
- if ins.TradingDays[2] != "WEDNESDAY" {
- t.Errorf("TeadingDays Error")
- }
- if ins.PriceIncrement != 0.00001 {
- t.Errorf("PriceIncrement Error")
- }
- if ins.OrderQuantityIncrement != 0.1 {
- t.Errorf("QuantityIncrement Error")
- }
- if ins.VolatilityBandPercentage != 2 {
- t.Errorf("VolatilityBandPercentage Error")
- }
- if ins.Currency != "USD" {
- t.Errorf("Current Error")
- }
- if ins.UnitPrice != 10000 {
- t.Errorf("UPrice Error")
- }
- if ins.UnitOfMeasure != "EUR" {
- t.Errorf("UnitOfMeasure Error")
- }
- if ins.ContractSize != 10000 {
- t.Errorf("ContractSize Error")
- }
- if ins.MinimumCommission != 0 {
- t.Errorf("MinCommission Error")
- }
- if ins.AggressiveCommissionRate != 0.25 {
- t.Errorf("aggressiveCommissionRate Error")
- }
- if ins.PassiveCommissionRate != 0.25 {
- t.Errorf("passiveCommissionRate Error")
- }
- if ins.DailyInterestRateBasis != 360 {
- t.Errorf("MinCommission Error")
- }
- if ins.LongSwapPoints != 0 {
- t.Errorf("MinCommission Error")
- }
- if ins.ShortSwapPoints != 0 {
- t.Errorf("MinCommission Error")
- }
- if ins.MarginRate != 0.01 {
- t.Errorf("Margin Error")
- }
- if ins.MaximumPosition != 50000 {
- t.Errorf("TeadingDays Error")
- }
- fmt.Println(ins.Name)
- }
- func TestRejectedEvent(t *testing.T) {
- data := "<accountId>1334343885</accountId><instrumentId>4001</instrumentId><instructionId>1235</instructionId><reason>DUPLICATE_ORDER</reason>"
- event := NewInstructionRejectedEvent(data)
- if event.AccountId != 1334343885 {
- t.Errorf("RejectedEvent.AccountId Error")
- }
- if event.InstrumentId != 4001 {
- t.Errorf("RejectedEvent.InstrumentId Error")
- }
- if event.InstructionId != "1235" {
- t.Errorf("RejectedEvent.InstructionId Error")
- }
- if event.Reason != "DUPLICATE_ORDER" {
- t.Errorf("RejectedEvent.Reason Error")
- }
- }
- func TestAccountEvent(t *testing.T) {
- data := "<accountState><accountId>1334343885</accountId><balance>9998.03181</balance><availableFunds>7951.93686</availableFunds><availableToWithdraw>7951.93686</availableToWithdraw><unrealisedProfitAndLoss>-1237.21777</unrealisedProfitAndLoss><margin>808.87718</margin><wallets><wallet><currency>USD</currency><balance>9998.03181</balance></wallet></wallets><active>true</active></accountState>"
- event := NewAccountStateEvent(data)
- if event.AccountId != 1334343885 {
- t.Errorf("AccountId Error:", event.AccountId)
- return
- }
- if event.Wallets[0].Balance != 9998.03181 {
- t.Errorf("Account.Wallets[0].balance Error", event.Wallets[0].Balance)
- return
- }
- data2, err := xml.Marshal(event)
- if err != nil {
- t.Error(err)
- return
- }
- if string(data2) != data {
- t.Errorf(string(data2))
- return
- }
- }
- func TestOrderEventExecution(t *testing.T) {
- data := "<order><timeInForce>ImmediateOrCancel</timeInForce><instructionId>7214477869579157657</instructionId><originalInstructionId>7214477869579157657</originalInstructionId><orderId>AAIm0gAAAAAP779F</orderId><accountId>108640298</accountId><instrumentId>4001</instrumentId><quantity>-1000</quantity><matchedQuantity>-908.5</matchedQuantity><matchedCost>-1.217019227e+07</matchedCost><cancelledQuantity>-91.5</cancelledQuantity><timestamp>2013-11-11T11:53:56</timestamp><orderType>STOP_COMPOUND_MARKET</orderType><openQuantity>-908.5</openQuantity><openCost>-1.217019227e+07</openCost><cumulativeCost>-1.217019227e+07</cumulativeCost><commission>304.25481</commission><stopReferencePrice>1.33825</stopReferencePrice><executions><executionId>517113623</executionId><execution><price>1.33964</price><quantity>-20</quantity></execution><execution><price>1.33963</price><quantity>-260</quantity></execution><execution><price>1.33962</price><quantity>-320</quantity></execution><execution><price>1.33961</price><quantity>-80</quantity></execution><execution><price>1.3396</price><quantity>-180</quantity></execution><execution><price>1.33913</price><quantity>-47.9</quantity></execution><execution><price>1.33875</price><quantity>-0.3</quantity></execution><execution><price>1.33825</price><quantity>-0.3</quantity></execution><orderCancelled><quantity>-91.5</quantity></orderCancelled></executions></order>"
- ord, ex := NewOrderEvent(data)
- if ord == nil {
- t.Errorf("OrderExecution Error order nil")
- }
- if ex == nil {
- t.Errorf("OrderExecution Error Execution nil")
- }
- if ex.InstrumentId != 4001 {
- t.Errorf("OrderExecution Error Execution.Order.InstrumentId")
- }
- if ex.Quantity != -1000 {
- t.Errorf("OrderExecution Error Execution.Quantity:", ex.Quantity)
- }
- data2, err := xml.Marshal(ex)
- if err != nil {
- t.Error(err)
- return
- }
- if string(data2) != data {
- t.Errorf(string(data2))
- return
- }
- }
- func TestOrderEvent(t *testing.T) {
- data := "<order><timeInForce>ImmediateOrCancel</timeInForce><instructionId>7285635440332458499</instructionId><originalInstructionId>7285635440332458499</originalInstructionId><orderId>AAIm0gAAAAAEAVVR</orderId><accountId>1334343885</accountId><instrumentId>4001</instrumentId><quantity>1</quantity><matchedQuantity>1</matchedQuantity><matchedCost>13395.7</matchedCost><cancelledQuantity>0</cancelledQuantity><timestamp>2013-06-19T08:07:45</timestamp><orderType>STOP_COMPOUND_MARKET</orderType><openQuantity>1</openQuantity><openCost>13395.7</openCost><cumulativeCost>13395.7</cumulativeCost><commission>0.33489</commission><stopReferencePrice>1.33957</stopReferencePrice><executions></executions></order>"
- event, ex := NewOrderEvent(data)
- if ex != nil {
- t.Errorf("OrderEvent Error Execution:", ex)
- }
- if event.InstructionId != "7285635440332458499" {
- t.Errorf("OrderEvent.InstructionId Error", event.InstructionId)
- }
- if event.Quantity != 1 {
- t.Errorf("OrderEvent.Quantity Error", event.Quantity)
- }
- if event.LimitPrice != 0 {
- t.Errorf("OrderEvent.LimitPrice Error", event.LimitPrice)
- }
- if event.StopReferencePrice != 1.33957 {
- t.Errorf("OrderEvent.StopReferencePrice Error", event.StopReferencePrice)
- }
- if event.StopProfitOffset != 0 {
- t.Errorf("OrderEvent.StopProfitOffset Error", event.StopProfitOffset)
- }
- if OrderType(event.OrderType) != "MARKET" {
- t.Errorf("OrderEvent.OrderType Error", event.OrderType)
- }
- data2, err := xml.Marshal(event)
- if err != nil {
- t.Error(err)
- return
- }
- if string(data2) != data {
- t.Errorf(string(data2))
- return
- }
- }
- func TestPositionEvent(t *testing.T) {
- data := "<position><accountId>1334343885</accountId><instrumentId>4001</instrumentId><valuation>-1834.965</valuation><shortUnfilledCost>0</shortUnfilledCost><longUnfilledCost>0</longUnfilledCost><openQuantity>5</openQuantity><cumulativeCost>66795.3</cumulativeCost><openCost>66795.3</openCost></position>"
- event := NewPositionEvent(data)
- fmt.Println(event)
- if event.AccountId != 1334343885 {
- t.Errorf("PositionEvent. Error", event.AccountId)
- }
- if event.Valuation != -1834.965 {
- t.Errorf("PositionEvent.Valuation Error", event.Valuation)
- }
- if event.OpenCost != 66795.3 {
- t.Errorf("PositionEvent.OpenCose Error", event.OpenCost)
- }
- }
- func TestHistoricEvent(t *testing.T) {
- data := "<historicMarketData><instructionId>1</instructionId><urls><url>https://testapi.LMAXtrader.com/marketdata/orderbook/4001/2011/05/2011-05-10-21-00-00-000-4001-bid-ask-tick-depth1.csv.gz</url><url>https://testapi.LMAXtrader.com/marketdata/orderbook/4001/2011/05/2011-05-11-21-00-00-000-4001-bid-ask-tick-depth1.csv.gz</url></urls></historicMarketData>"
- event := NewHistoricMarketData(data)
- fmt.Println(event.InstructionId)
- fmt.Println(event.Urls[0])
- if len(event.Urls) != 2 {
- t.Errorf("HistoricMarketData.urls Error")
- }
- }
- func TestAccountDetail(t *testing.T) {
- data := "<res><header><status>OK</status></header><body><username>niniwzw</username><currency>USD</currency><accountId>108640298</accountId><accountType>STANDARD_TRADER</accountType><productType>CFD_DEMO</productType><registrationLegalEntity>UK</registrationLegalEntity><displayLocale>en_GB</displayLocale><fundingDisallowed>false</fundingDisallowed><availableAccounts><account><accountId>108640298</accountId><accountName>null king wang</accountName></account></availableAccounts><availableSchemes></availableSchemes><userPreferences><version>4</version><category><name>instrument_preferences</name><item><id>quantity-100437</id><value>0.1</value></item></category><category><name>trading</name><item><id>qtyInc-RATE</id><value>10</value></item><item><id>qtyInc-INDEX</id><value>10</value></item><item><id>qtyInc-EQUITY</id><value>10</value></item><item><id>qtyInc-CURRENCY</id><value>10</value></item><item><id>qtyInc-COMMODITY</id><value>10</value></item><item><id>qtyInc-SYSTEM_EXCHANGE_RATE</id><value>10</value></item><item><id>oneClickClose</id><value>false</value></item><item><id>oneClickTrading</id><value>false</value></item><item><id>ladderOnQuickTicket</id><value>false</value></item><item><id>quickTicketSweepControls</id><value>false</value></item><item><id>contingentOrdersOnQuickTicket</id><value>false</value></item><item><id>activeOrderType</id><value>MARKET</value></item><item><id>quickTicketTooltips</id><value>true</value></item><item><id>notificationsPanelShown</id><value>true</value></item><item><id>individualPositionsPanelShown</id><value>true</value></item></category></userPreferences></body></res>"
- event := NewAccountDetails(data)
- data2, err := xml.Marshal(event)
- if err != nil {
- t.Error(err)
- return
- }
- if string(data2) != data {
- t.Error("TestAccountDetail error", string(data2))
- }
- }
- func TestOrders(t *testing.T) {
- data := "<orders><page><order><timeInForce>ImmediateOrCancel</timeInForce><instructionId>7213612489500704526</instructionId><originalInstructionId>7213612489500704526</originalInstructionId><orderId>AAIm0gAAAAAEelMb</orderId><accountId>1334343885</accountId><instrumentId>4001</instrumentId><quantity>1</quantity><matchedQuantity>1</matchedQuantity><matchedCost>13130.1</matchedCost><cancelledQuantity>0</cancelledQuantity><timestamp>2013-06-25T11:17:06</timestamp><orderType>STOP_COMPOUND_MARKET</orderType><openQuantity>1</openQuantity><openCost>13130.1</openCost><cumulativeCost>13130.1</cumulativeCost><commission>0.32825</commission><stopReferencePrice>1.31301</stopReferencePrice><executions><executionId>144887230</executionId><execution><price>1.31301</price><quantity>1</quantity></execution></executions></order><order><timeInForce>ImmediateOrCancel</timeInForce><instructionId>7213612489500704526</instructionId><originalInstructionId>7213612489500704526</originalInstructionId><orderId>AAIm0gAAAAAEelMb</orderId><accountId>1334343885</accountId><instrumentId>4001</instrumentId><quantity>1</quantity><matchedQuantity>1</matchedQuantity><matchedCost>13130.1</matchedCost><cancelledQuantity>0</cancelledQuantity><timestamp>2013-06-25T11:17:06</timestamp><orderType>STOP_COMPOUND_MARKET</orderType><openQuantity>1</openQuantity><openCost>13130.1</openCost><cumulativeCost>13130.1</cumulativeCost><commission>0.32825</commission><stopReferencePrice>1.31301</stopReferencePrice><executions><executionId>144887230</executionId><execution><price>1.31301</price><quantity>1</quantity></execution></executions></order></page><hasMoreResults>false</hasMoreResults><correlationId>-1</correlationId></orders>"
- orders := &Orders{}
- err := xml.Unmarshal([]byte(data), orders)
- if err != nil {
- t.Error(err)
- return
- }
- //fmt.Println(orders.Data[0], orders.Data[1])
- data2, err := xml.Marshal(orders)
- if err != nil {
- t.Error(err)
- return
- }
- if string(data2) != data {
- t.Error("TestOrders error", string(data2))
- }
- }
|