123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package main
- import (
- "git.beejay.kim/Gshopper/sentio"
- "time"
- )
- var Strategy = _ib_qqq{
- clock: sentio.ClockUTC{},
- }
- type _ib_qqq struct {
- clock sentio.Clock
- }
- func (strategy _ib_qqq) Name() string {
- return "IBKR: QQQ"
- }
- func (strategy _ib_qqq) Model() string {
- return "qqq400"
- }
- func (strategy _ib_qqq) MarketId() string {
- return "ibkr"
- }
- func (strategy _ib_qqq) IsMarketOpened() bool {
- utc := strategy.clock.Now()
- h := utc.Hour()
- m := utc.Minute()
- return (h == 13 && m >= 30 || h > 13) &&
- h < 20 &&
- utc.Weekday() != time.Saturday && utc.Weekday() != time.Sunday
- }
- func (strategy _ib_qqq) PositionSymbols() map[sentio.Side]string {
- return map[sentio.Side]string{
- sentio.LONG: "320227571", // QQQ@NASDAQ
- }
- }
- func (strategy _ib_qqq) ShouldClosePositions(portfolio sentio.Portfolio, proba float64) []string {
- panic("implement me")
- }
- func (strategy _ib_qqq) ShouldOpenPosition(portfolio sentio.Portfolio, proba float64) (*string, float64) {
- panic("implement me")
- }
- func (strategy _ib_qqq) Handle(proba float64, portfolio sentio.Portfolio) []sentio.StrategyOrder {
- if !strategy.IsMarketOpened() {
- return nil
- }
- var utc = strategy.clock.Now()
- for _, symbol := range strategy.PositionSymbols() {
- var (
- position sentio.Position
- ok bool
- )
- if portfolio != nil {
- position, ok = portfolio.Get(symbol)
- ok = ok && position.GetSize() != 0
- }
- // Close positions before market close
- if ok && utc.Hour() >= 19 && utc.Minute() >= 30 {
- return []sentio.StrategyOrder{
- {
- Symbol: position.GetSymbol(),
- Action: func() sentio.OrderAction {
- if position.GetSize() > 0 {
- return sentio.OrderSell
- } else {
- return sentio.OrderBuy
- }
- }(),
- Ratio: 1,
- },
- }
- }
- if proba < 0 {
- continue
- }
- // Close LONG position
- if ok && position.GetSize() > 0 && proba < 1 {
- return []sentio.StrategyOrder{
- {
- Symbol: position.GetSymbol(),
- Action: sentio.OrderSell,
- Ratio: 1,
- },
- }
- }
- // Close SHORT position
- if ok && position.GetSize() < 0 && proba > 1 {
- return []sentio.StrategyOrder{
- {
- Symbol: position.GetSymbol(),
- Action: sentio.OrderBuy,
- Ratio: 1,
- },
- }
- }
- if utc.Hour() == 19 && utc.Minute() >= 30 {
- continue
- }
- if proba > 1.002 {
- return []sentio.StrategyOrder{
- {
- Symbol: symbol,
- Action: sentio.OrderBuy,
- Ratio: func() float64 {
- if ok {
- return .3
- } else {
- return .6
- }
- }(),
- },
- }
- }
- if proba < .998 {
- return []sentio.StrategyOrder{
- {
- Symbol: symbol,
- Action: sentio.OrderSell,
- Ratio: func() float64 {
- if ok {
- return .3
- } else {
- return .6
- }
- }(),
- },
- }
- }
- }
- return nil
- }
|