소스 검색

Utils

- CreateOrder refactoring
Alexey Kim 5 달 전
부모
커밋
9c972b24f0
1개의 변경된 파일15개의 추가작업 그리고 14개의 파일을 삭제
  1. 15 14
      util/order.go

+ 15 - 14
util/order.go

@@ -10,17 +10,18 @@ func CreateOrder(
 	m sentio.Market,
 	s sentio.Strategy,
 	t sentio.Side,
-	probability *sentio.Probability,
-	q map[string]sentio.Quote,
-	sl map[string]float64,
-	tp map[string]float64,
+	stamp *sentio.Probability_Stamp,
+	quotes map[string]sentio.Quote,
+	rm sentio.RiskManager,
 ) error {
 	var (
 		symbol    string
 		account   sentio.MarketAccount
 		threshold float32
-		stamp     *sentio.Probability_Stamp
 		size      uint
+		bid       float64
+		tp        float64
+		sl        float64
 		ok        bool
 		err       error
 	)
@@ -29,19 +30,23 @@ func CreateOrder(
 		return errors.New("`CreateOrder`: unknown Side for the current strategy")
 	}
 
+	bid = sentio.ToFixed(quotes[symbol].BidPrice, 2)
+	tp = sentio.ToFixed(rm.TakeProfit(symbol, bid), 2)
+	sl = sentio.ToFixed(rm.StopLoss(symbol, bid), 2)
+
 	// define threshold
 	if threshold, ok = s.PositionProbabilities()[t]; !ok {
 		return nil
 	}
 
 	// Prevent orders those have too small expected profit
-	if sentio.ToFixed(tp[symbol], 2)/sentio.ToFixed(q[symbol].BidPrice, 2) < 1.0009 {
+	if tp < bid+0.02 {
 		return sentio.ErrLowTakeProfit
 	}
 
 	// Prevent cases when order will be closed ASAP they were opened.
 	// Also, Alpaca requires at least 0.01 gap against base_price
-	if sentio.ToFixed(sl[symbol], 2) > sentio.ToFixed(q[symbol].BidPrice, 2)-0.01 {
+	if sl > bid-0.01 {
 		return sentio.ErrHighStoploss
 	}
 
@@ -49,23 +54,19 @@ func CreateOrder(
 		return err
 	}
 
-	if account.GetCash() < q[symbol].BidPrice {
+	if account.GetCash() < bid {
 		return sentio.ErrTooSmallOrder
 	}
 
-	if stamp, ok = probability.First(); !ok {
-		return nil
-	}
-
 	if sentio.LONG == t && stamp.Value > threshold ||
 		sentio.SHORT == t && stamp.Value < threshold {
 
 		// create a new order
-		if size = uint(math.Floor(account.GetCash() * .8 / q[symbol].BidPrice)); size < 1 {
+		if size = uint(math.Floor(account.GetCash() * .8 / bid)); size < 1 {
 			return sentio.ErrTooSmallOrder
 		}
 
-		_, err = m.CreateOrder(symbol, size, sl[symbol], tp[symbol])
+		_, err = m.CreateOrder(symbol, size, rm)
 		return err
 	}