event_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package response
  2. import (
  3. "fmt"
  4. "testing"
  5. "encoding/xml"
  6. )
  7. func TestOrderBookEvent(t *testing.T) {
  8. 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"
  9. event := NewOrderBookEvent(eventData)
  10. if event.InstrumentId != 4001 {
  11. t.Errorf("OrderBookEvent.InstrumentId Error")
  12. }
  13. data, err := event.Encode()
  14. if err != nil {
  15. t.Error(err)
  16. }
  17. if string(data) != "<ob2>" + eventData + "</ob2>"{
  18. t.Errorf(string(data))
  19. }
  20. }
  21. func TestInstrument(t *testing.T) {
  22. 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>"
  23. insts := NewInstruments(data)
  24. ins := insts.Data[0]
  25. if ins.Id != 4001 {
  26. t.Errorf("Id Error")
  27. }
  28. if ins.Name != "EUR/USD" {
  29. t.Errorf("Name Error")
  30. }
  31. if ins.Symbol != "EUR/USD" {
  32. t.Errorf("Symbol Error")
  33. }
  34. if ins.Isin != "" {
  35. t.Errorf("Isin Error")
  36. }
  37. if ins.AssetClass != "CURRENCY" {
  38. t.Errorf("Symbol Error")
  39. }
  40. if ins.StartTime == nil {
  41. t.Error("StartTime Error")
  42. }
  43. if ins.OpenOffset != -415 {
  44. t.Errorf("Open Error")
  45. }
  46. if ins.CloseOffset != 1020 {
  47. t.Errorf("Close Error")
  48. }
  49. if ins.TimeZone != "America/New_York" {
  50. t.Errorf("Time Error")
  51. }
  52. if ins.TradingDays[2] != "WEDNESDAY" {
  53. t.Errorf("TeadingDays Error")
  54. }
  55. if ins.PriceIncrement != 0.00001 {
  56. t.Errorf("PriceIncrement Error")
  57. }
  58. if ins.OrderQuantityIncrement != 0.1 {
  59. t.Errorf("QuantityIncrement Error")
  60. }
  61. if ins.VolatilityBandPercentage != 2 {
  62. t.Errorf("VolatilityBandPercentage Error")
  63. }
  64. if ins.Currency != "USD" {
  65. t.Errorf("Current Error")
  66. }
  67. if ins.UnitPrice != 10000 {
  68. t.Errorf("UPrice Error")
  69. }
  70. if ins.UnitOfMeasure != "EUR" {
  71. t.Errorf("UnitOfMeasure Error")
  72. }
  73. if ins.ContractSize != 10000 {
  74. t.Errorf("ContractSize Error")
  75. }
  76. if ins.MinimumCommission != 0 {
  77. t.Errorf("MinCommission Error")
  78. }
  79. if ins.AggressiveCommissionRate != 0.25 {
  80. t.Errorf("aggressiveCommissionRate Error")
  81. }
  82. if ins.PassiveCommissionRate != 0.25 {
  83. t.Errorf("passiveCommissionRate Error")
  84. }
  85. if ins.DailyInterestRateBasis != 360 {
  86. t.Errorf("MinCommission Error")
  87. }
  88. if ins.LongSwapPoints != 0 {
  89. t.Errorf("MinCommission Error")
  90. }
  91. if ins.ShortSwapPoints != 0 {
  92. t.Errorf("MinCommission Error")
  93. }
  94. if ins.MarginRate != 0.01 {
  95. t.Errorf("Margin Error")
  96. }
  97. if ins.MaximumPosition != 50000 {
  98. t.Errorf("TeadingDays Error")
  99. }
  100. fmt.Println(ins.Name)
  101. }
  102. func TestRejectedEvent(t *testing.T) {
  103. data := "<accountId>1334343885</accountId><instrumentId>4001</instrumentId><instructionId>1235</instructionId><reason>DUPLICATE_ORDER</reason>"
  104. event := NewInstructionRejectedEvent(data)
  105. if event.AccountId != 1334343885 {
  106. t.Errorf("RejectedEvent.AccountId Error")
  107. }
  108. if event.InstrumentId != 4001 {
  109. t.Errorf("RejectedEvent.InstrumentId Error")
  110. }
  111. if event.InstructionId != "1235" {
  112. t.Errorf("RejectedEvent.InstructionId Error")
  113. }
  114. if event.Reason != "DUPLICATE_ORDER" {
  115. t.Errorf("RejectedEvent.Reason Error")
  116. }
  117. }
  118. func TestAccountEvent(t *testing.T) {
  119. 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>"
  120. event := NewAccountStateEvent(data)
  121. if event.AccountId != 1334343885 {
  122. t.Errorf("AccountId Error:", event.AccountId)
  123. return
  124. }
  125. if event.Wallets[0].Balance != 9998.03181 {
  126. t.Errorf("Account.Wallets[0].balance Error", event.Wallets[0].Balance)
  127. return
  128. }
  129. data2, err := xml.Marshal(event)
  130. if err != nil {
  131. t.Error(err)
  132. return
  133. }
  134. if string(data2) != data {
  135. t.Errorf(string(data2))
  136. return
  137. }
  138. }
  139. func TestOrderEventExecution(t *testing.T) {
  140. 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>"
  141. ord, ex := NewOrderEvent(data)
  142. if ord == nil {
  143. t.Errorf("OrderExecution Error order nil")
  144. }
  145. if ex == nil {
  146. t.Errorf("OrderExecution Error Execution nil")
  147. }
  148. if ex.InstrumentId != 4001 {
  149. t.Errorf("OrderExecution Error Execution.Order.InstrumentId")
  150. }
  151. if ex.Quantity != -1000 {
  152. t.Errorf("OrderExecution Error Execution.Quantity:", ex.Quantity)
  153. }
  154. data2, err := xml.Marshal(ex)
  155. if err != nil {
  156. t.Error(err)
  157. return
  158. }
  159. if string(data2) != data {
  160. t.Errorf(string(data2))
  161. return
  162. }
  163. }
  164. func TestOrderEvent(t *testing.T) {
  165. 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>"
  166. event, ex := NewOrderEvent(data)
  167. if ex != nil {
  168. t.Errorf("OrderEvent Error Execution:", ex)
  169. }
  170. if event.InstructionId != "7285635440332458499" {
  171. t.Errorf("OrderEvent.InstructionId Error", event.InstructionId)
  172. }
  173. if event.Quantity != 1 {
  174. t.Errorf("OrderEvent.Quantity Error", event.Quantity)
  175. }
  176. if event.LimitPrice != 0 {
  177. t.Errorf("OrderEvent.LimitPrice Error", event.LimitPrice)
  178. }
  179. if event.StopReferencePrice != 1.33957 {
  180. t.Errorf("OrderEvent.StopReferencePrice Error", event.StopReferencePrice)
  181. }
  182. if event.StopProfitOffset != 0 {
  183. t.Errorf("OrderEvent.StopProfitOffset Error", event.StopProfitOffset)
  184. }
  185. if OrderType(event.OrderType) != "MARKET" {
  186. t.Errorf("OrderEvent.OrderType Error", event.OrderType)
  187. }
  188. data2, err := xml.Marshal(event)
  189. if err != nil {
  190. t.Error(err)
  191. return
  192. }
  193. if string(data2) != data {
  194. t.Errorf(string(data2))
  195. return
  196. }
  197. }
  198. func TestPositionEvent(t *testing.T) {
  199. 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>"
  200. event := NewPositionEvent(data)
  201. fmt.Println(event)
  202. if event.AccountId != 1334343885 {
  203. t.Errorf("PositionEvent. Error", event.AccountId)
  204. }
  205. if event.Valuation != -1834.965 {
  206. t.Errorf("PositionEvent.Valuation Error", event.Valuation)
  207. }
  208. if event.OpenCost != 66795.3 {
  209. t.Errorf("PositionEvent.OpenCose Error", event.OpenCost)
  210. }
  211. }
  212. func TestHistoricEvent(t *testing.T) {
  213. 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>"
  214. event := NewHistoricMarketData(data)
  215. fmt.Println(event.InstructionId)
  216. fmt.Println(event.Urls[0])
  217. if len(event.Urls) != 2 {
  218. t.Errorf("HistoricMarketData.urls Error")
  219. }
  220. }
  221. func TestAccountDetail(t *testing.T) {
  222. 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>"
  223. event := NewAccountDetails(data)
  224. data2, err := xml.Marshal(event)
  225. if err != nil {
  226. t.Error(err)
  227. return
  228. }
  229. if string(data2) != data {
  230. t.Error("TestAccountDetail error", string(data2))
  231. }
  232. }
  233. func TestOrders(t *testing.T) {
  234. 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>"
  235. orders := &Orders{}
  236. err := xml.Unmarshal([]byte(data), orders)
  237. if err != nil {
  238. t.Error(err)
  239. return
  240. }
  241. //fmt.Println(orders.Data[0], orders.Data[1])
  242. data2, err := xml.Marshal(orders)
  243. if err != nil {
  244. t.Error(err)
  245. return
  246. }
  247. if string(data2) != data {
  248. t.Error("TestOrders error", string(data2))
  249. }
  250. }