|
@@ -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
|
|
|
}
|
|
|
|