Kaynağa Gözat

Proto

- add fun IsAction
- add fun ActionToSide
- refactor util.CreateOrder
Alexey Kim 4 ay önce
ebeveyn
işleme
5e644444fc
2 değiştirilmiş dosya ile 49 ekleme ve 28 silme
  1. 29 0
      probability.go
  2. 20 28
      util/order.go

+ 29 - 0
probability.go

@@ -21,3 +21,32 @@ func (x *Probability) Next(n int) (*Probability_Stamp, bool) {
 	slices.SortFunc(x.Stamps, CompareProbabilityStamps)
 	return x.Stamps[n], true
 }
+
+func IsAction(action Action, oneOf ...Action) bool {
+	if oneOf == nil || len(oneOf) == 0 {
+		return false
+	}
+
+	for i := range oneOf {
+		if action == oneOf[i] {
+			return true
+		}
+	}
+
+	return false
+}
+
+func ActionToSide(action Action) Side {
+	switch action {
+	case Action_DOUBLE_SELL:
+		return SHORT
+	case Action_SELL:
+		return SHORT
+	case Action_BUY:
+		return LONG
+	case Action_DOUBLE_BUY:
+		return LONG
+	default:
+		return BASE
+	}
+}

+ 20 - 28
util/order.go

@@ -9,24 +9,27 @@ import (
 func CreateOrder(
 	m sentio.Market,
 	s sentio.Strategy,
-	t sentio.Side,
-	stamp *sentio.Probability_Stamp,
+	action sentio.Action,
 	quotes map[string]sentio.Quote,
 	rm sentio.RiskManager,
 ) error {
 	var (
-		symbol    string
-		account   sentio.MarketAccount
-		threshold float32
-		size      uint
-		bid       float64
-		tp        float64
-		sl        float64
-		ok        bool
-		err       error
+		symbol  string
+		account sentio.MarketAccount
+		size    uint
+		side    sentio.Side
+		bid     float64
+		tp      float64
+		sl      float64
+		ok      bool
+		err     error
 	)
 
-	if symbol, ok = s.PositionSymbols()[t]; !ok {
+	if side = sentio.ActionToSide(action); sentio.BASE == side {
+		return errors.New("`CreateOrder`: do nothing while `sentio.BASE`")
+	}
+
+	if symbol, ok = s.PositionSymbols()[side]; !ok {
 		return errors.New("`CreateOrder`: unknown Side for the current strategy")
 	}
 
@@ -38,11 +41,6 @@ func CreateOrder(
 	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 tp < bid+0.02 {
 		return sentio.ErrLowTakeProfit
@@ -62,19 +60,13 @@ func CreateOrder(
 		return sentio.ErrTooSmallOrder
 	}
 
-	if sentio.LONG == t && stamp.Value > threshold ||
-		sentio.SHORT == t && stamp.Value < threshold {
-
-		// create a new order
-		if size = uint(math.Floor(account.GetCash() * .9 / bid)); size < 1 {
-			return sentio.ErrTooSmallOrder
-		}
-
-		_, err = m.CreateOrder(symbol, size, rm)
-		return err
+	// create a new order
+	if size = uint(math.Floor(account.GetCash() * .9 / bid)); size < 1 {
+		return sentio.ErrTooSmallOrder
 	}
 
-	return nil
+	_, err = m.CreateOrder(symbol, size, rm)
+	return err
 }
 
 func CloseAllOrders(m sentio.Market, s sentio.Strategy) error {