code.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package common
  2. import (
  3. "fmt"
  4. "tickserver/api/lmaxapi"
  5. "tickserver/api/lmaxapi/request"
  6. "tickserver/api/lmaxapi/response"
  7. "os"
  8. "time"
  9. )
  10. func EnableLog(filename string) (*os.File, error) {
  11. logw, e := os.Create(filename)
  12. if e != nil {
  13. return nil, e
  14. }
  15. lmaxapi.EnableLog(logw)
  16. return logw, nil
  17. }
  18. func CreateSession() (*lmaxapi.Session, error) {
  19. lmaxapi.SetBaseUrl("https://trade.lmaxtrader.com")
  20. req := request.NewLoginRequest("wave3366", "Tg417395", request.ProductType.CFD_DEMO)
  21. session, err := lmaxapi.Login(req)
  22. if err == nil {
  23. fmt.Println("My accountId is:", session.GetAccountDetails().AccountId)
  24. }
  25. return session, err
  26. }
  27. func Heartbeat(session *lmaxapi.Session) {
  28. req2 := request.NewHeartbeatSubscriptionRequest()
  29. session.Subscribe(req2, nil)
  30. req3 := request.NewHeartbeatRequest("hello")
  31. go func() {
  32. for {
  33. time.Sleep(10 * time.Second)
  34. session.RequestHeartbeat(req3, nil)
  35. }
  36. }()
  37. }
  38. func DefaultSubscribeCB(err error) {
  39. if err != nil {
  40. fmt.Println("Failed:", err)
  41. }
  42. }
  43. func DefaultStreamFailureCB(s *lmaxapi.Session, err error) {
  44. fmt.Println("Error occured on the stream ", err)
  45. s.Stop()
  46. }
  47. func DefaultOrderCB(s *lmaxapi.Session, event *response.OrderEvent) {
  48. fmt.Println("OrderEvent:",event)
  49. }
  50. func DefaultInstructionRejectedCB(s *lmaxapi.Session, event *response.InstructionRejectedEvent) {
  51. fmt.Println("Rejection Received:",event)
  52. }
  53. func DefaultExecutionCB(s *lmaxapi.Session, event *response.ExecutionEvent) {
  54. fmt.Println("ExecutionEvent:",event)
  55. }
  56. func DefaultPositionCB(s *lmaxapi.Session, event *response.PositionEvent) {
  57. fmt.Println("Position Received:", event)
  58. }