Browse Source

Position

- prevent extra orders if current price > avg price
Alexey Kim 5 months ago
parent
commit
a95f5d374f
2 changed files with 21 additions and 5 deletions
  1. 13 5
      position.go
  2. 8 0
      strategy/alpaca/qqq/strategy.go

+ 13 - 5
position.go

@@ -10,15 +10,19 @@ type Position interface {
 	// GetSymbol returns the ticker symbol of the traded contract
 	GetSymbol() string
 
-	// GetPnL unrealized profit/loss in dollars
+	// GetPnL returns unrealized profit/loss in dollars
 	GetPnL() float64
+
+	// GetCurrentPrice returns current asset price per share
+	GetCurrentPrice() float64
 }
 
 type PositionStub struct {
-	Symbol string
-	Size   float64
-	Price  float64
-	PnL    float64
+	Symbol  string
+	Size    float64
+	Price   float64
+	PnL     float64
+	Current float64
 }
 
 func (p PositionStub) GetSize() float64 {
@@ -36,3 +40,7 @@ func (p PositionStub) GetSymbol() string {
 func (p PositionStub) GetPnL() float64 {
 	return p.PnL
 }
+
+func (p PositionStub) GetCurrentPrice() float64 {
+	return p.Current
+}

+ 8 - 0
strategy/alpaca/qqq/strategy.go

@@ -94,6 +94,10 @@ func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.Strateg
 		}
 
 		if sentio.LONG == side && proba > 1.001 {
+			if position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
+				continue
+			}
+
 			orders = append(orders, sentio.StrategyOrder{
 				Symbol: symbol,
 				Action: sentio.OrderBuy,
@@ -109,6 +113,10 @@ func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.Strateg
 		}
 
 		if sentio.SHORT == side && proba < .9998 {
+			if position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
+				continue
+			}
+
 			orders = append(orders, sentio.StrategyOrder{
 				Symbol: symbol,
 				Action: sentio.OrderBuy,