Advertisement
Guest User

triple band logic

a guest
Mar 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. ptltrader
  2. Overview
  3. Source
  4. Commits
  5. Branches
  6. Pull requests
  7. Pipelines
  8. Deployments
  9. Deployments
  10. Downloads
  11. Boards
  12. Boards
  13. Settings
  14.  
  15. Karel Vavra  ptltrader  Source
  16. TripleBandLogic.java
  17. Branchmaster   Check out branch Source  Diff  History ptltrader / src / main / java / com / pairtradinglab / ptltrader / trading / TripleBandLogic.java
  18.  0e62405 2017-11-06  Full commit Blame Raw   Edit   More file actions
  19.  1
  20.  2
  21.  3
  22.  4
  23.  5
  24.  6
  25.  7
  26.  8
  27.  9
  28. 10
  29. 11
  30. 12
  31. 13
  32. 14
  33. 15
  34. 16
  35. 17
  36. 18
  37. 19
  38. 20
  39. 21
  40. 22
  41. 23
  42. 24
  43. 25
  44. 26
  45. 27
  46. 28
  47. 29
  48. 30
  49. 31
  50. 32
  51. 33
  52. 34
  53. 35
  54. 36
  55. 37
  56. 38
  57. 39
  58. 40
  59. 41
  60. 42
  61. 43
  62. 44
  63. 45
  64. 46
  65. 47
  66. 48
  67. 49
  68. 50
  69. 51
  70. 52
  71. 53
  72. 54
  73. 55
  74. 56
  75. 57
  76. 58
  77. 59
  78. package com.pairtradinglab.ptltrader.trading;
  79.  
  80. import net.jcip.annotations.Immutable;
  81.  
  82. @Immutable
  83. class TripleBandLogic {
  84.     final double inThreshold;
  85.     final double outThreshold;
  86.     final double downTickThreshold;
  87.     final double maxScore;
  88.     final int entryMode;
  89.  
  90.     TripleBandLogic(double inThreshold, double outThreshold, double downTickThreshold, double maxScore, int entryMode) {
  91.         this.inThreshold = inThreshold;
  92.         this.outThreshold = outThreshold;
  93.         this.downTickThreshold = downTickThreshold;
  94.         this.maxScore = maxScore;
  95.         this.entryMode = entryMode;
  96.     }
  97.  
  98.     EntrySignal entryLogic(double scoreAsk, double scoreBid, double lastScoreAsk, double lastScoreBid) {
  99.         int out = PairTradingModel.SIGNAL_NONE;
  100.  
  101.         if (entryMode == PairTradingModel.ENTRY_MODE_UPTICK) {
  102.             // uptick
  103.             if (scoreAsk <= -inThreshold && (maxScore<0.01 || scoreAsk>-maxScore) && lastScoreAsk > -inThreshold) out = PairTradingModel.SIGNAL_LONG;
  104.             else if (scoreBid >= inThreshold && (maxScore<0.01 || scoreBid<maxScore) && lastScoreBid < inThreshold) out = PairTradingModel.SIGNAL_SHORT;
  105.  
  106.         } else if (entryMode == PairTradingModel.ENTRY_MODE_DOWNTICK) {
  107.             // downtick
  108.             if (scoreAsk > -inThreshold && scoreAsk<-downTickThreshold && (maxScore<0.01 || scoreAsk>-maxScore) && lastScoreAsk <= -inThreshold) out = PairTradingModel.SIGNAL_LONG;
  109.             else if (scoreBid < inThreshold && scoreBid>downTickThreshold && (maxScore<0.01 || scoreBid<maxScore) && lastScoreBid >= inThreshold) out = PairTradingModel.SIGNAL_SHORT;
  110.         } else {
  111.             // simple
  112.             if (scoreAsk <= -inThreshold && (maxScore<0.01 || scoreAsk>-maxScore)) out = PairTradingModel.SIGNAL_LONG;
  113.             else if (scoreBid >= inThreshold && (maxScore<0.01 || scoreBid<maxScore)) out = PairTradingModel.SIGNAL_SHORT;
  114.         }
  115.  
  116.         double zscoreInvolved = 0;
  117.         if (out == PairTradingModel.SIGNAL_LONG) zscoreInvolved = scoreAsk;
  118.         else if (out == PairTradingModel.SIGNAL_SHORT) zscoreInvolved = scoreBid;
  119.  
  120.         return new EntrySignal(out, zscoreInvolved);
  121.     }
  122.  
  123.     ExitSignal exitLogic(int position, double scoreAsk, double scoreBid) {
  124.         if (position==PairTradingModel.SIGNAL_SHORT && scoreAsk<=outThreshold) {
  125.             return new ExitSignal(true, scoreAsk);
  126.         } else if (position==PairTradingModel.SIGNAL_LONG && scoreBid>=-outThreshold) {
  127.             return new ExitSignal(true, scoreBid);
  128.         } else {
  129.             return new ExitSignal(false, 0);
  130.         }
  131.     }
  132.  
  133.     boolean checkReversal(double score, double lastExitScore) {
  134.         return Math.abs(lastExitScore)>=inThreshold && Math.abs(score)>=inThreshold && score*lastExitScore>=0;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement