Bladeren bron

CompareStrategyOrders

- add
Alexey Kim 9 maanden geleden
bovenliggende
commit
5cb250bece
4 gewijzigde bestanden met toevoegingen van 157 en 7 verwijderingen
  1. 14 0
      strategy.go
  2. 4 4
      strategy/alpaca/qqq04f/strategy.go
  3. 13 3
      strategy/portfolio_test.go
  4. 126 0
      strategy_test.go

+ 14 - 0
strategy.go

@@ -1,5 +1,7 @@
 package sentio
 
+import "cmp"
+
 type Strategy interface {
 	Name() string
 
@@ -15,3 +17,15 @@ type StrategyOrder struct {
 	Action OrderAction `yaml:"action"`
 	Ratio  float64     `yaml:"ratio"`
 }
+
+func CompareStrategyOrders(a, b StrategyOrder) int {
+	if a.Action == b.Action {
+		return cmp.Compare(b.Ratio, a.Ratio)
+	}
+
+	if OrderSell == a.Action {
+		return -1
+	}
+
+	return 1
+}

+ 4 - 4
strategy/alpaca/qqq04f/strategy.go

@@ -101,9 +101,9 @@ func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.Strateg
 				Action: sentio.OrderBuy,
 				Ratio: func() float64 {
 					if ok {
-						return .3
-					} else {
 						return .6
+					} else {
+						return .3
 					}
 				}(),
 			})
@@ -120,9 +120,9 @@ func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.Strateg
 				Action: sentio.OrderBuy,
 				Ratio: func() float64 {
 					if ok {
-						return .3
-					} else {
 						return .6
+					} else {
+						return .3
 					}
 				}(),
 			})

+ 13 - 3
strategy/portfolio_test.go

@@ -27,9 +27,11 @@ func (p PortfolioStub) Positions() []sentio.Position {
 }
 
 type PositionStub struct {
-	symbol string
-	size   float64
-	price  float64
+	symbol       string
+	size         float64
+	price        float64
+	pnl          float64
+	currentPrice float64
 }
 
 func (p PositionStub) GetSize() float64 {
@@ -43,3 +45,11 @@ func (p PositionStub) GetAvgPrice() float64 {
 func (p PositionStub) GetSymbol() string {
 	return p.symbol
 }
+
+func (p PositionStub) GetPnL() float64 {
+	return p.pnl
+}
+
+func (p PositionStub) GetCurrentPrice() float64 {
+	return p.currentPrice
+}

+ 126 - 0
strategy_test.go

@@ -0,0 +1,126 @@
+package sentio
+
+import (
+	"reflect"
+	"slices"
+	"testing"
+)
+
+func TestCompareStrategyOrders(t *testing.T) {
+	tests := []struct {
+		name   string
+		orders []StrategyOrder
+		want   []StrategyOrder
+	}{
+		{
+			name: "Sell first",
+			orders: []StrategyOrder{
+				{
+					Action: OrderBuy,
+					Ratio:  .3,
+				},
+				{
+					Action: OrderSell,
+					Ratio:  1,
+				},
+			},
+			want: []StrategyOrder{
+				{
+					Action: OrderSell,
+					Ratio:  1,
+				},
+				{
+					Action: OrderBuy,
+					Ratio:  .3,
+				},
+			},
+		},
+		{
+			name: "Buy largest first",
+			orders: []StrategyOrder{
+				{
+					Action: OrderBuy,
+					Ratio:  .3,
+				},
+				{
+					Action: OrderBuy,
+					Ratio:  .6,
+				},
+			},
+			want: []StrategyOrder{
+				{
+					Action: OrderBuy,
+					Ratio:  .6,
+				},
+				{
+					Action: OrderBuy,
+					Ratio:  .3,
+				},
+			},
+		},
+		{
+			name: "Sell largest first",
+			orders: []StrategyOrder{
+				{
+					Action: OrderSell,
+					Ratio:  .3,
+				},
+				{
+					Action: OrderSell,
+					Ratio:  .6,
+				},
+			},
+			want: []StrategyOrder{
+				{
+					Action: OrderSell,
+					Ratio:  .6,
+				},
+				{
+					Action: OrderSell,
+					Ratio:  .3,
+				},
+			},
+		},
+		{
+			name: "Sell largest first then buy",
+			orders: []StrategyOrder{
+				{
+					Action: OrderSell,
+					Ratio:  .3,
+				},
+				{
+					Action: OrderBuy,
+					Ratio:  .6,
+				},
+				{
+					Action: OrderSell,
+					Ratio:  .6,
+				},
+			},
+			want: []StrategyOrder{
+				{
+					Action: OrderSell,
+					Ratio:  .6,
+				},
+				{
+					Action: OrderSell,
+					Ratio:  .3,
+				},
+				{
+					Action: OrderBuy,
+					Ratio:  .6,
+				},
+			},
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			slices.SortFunc(tt.orders, CompareStrategyOrders)
+
+			if ok := reflect.DeepEqual(tt.want, tt.orders); !ok {
+				t.Errorf("SortFunc = %v, want %v", tt.orders, tt.want)
+			}
+		})
+	}
+}