Created
January 2, 2016 09:56
-
-
Save vanrysss/cb8820b8cd3344f0deda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This is nothing fancy. I figured that the inside trader would have a high cash | |
* position, almost no inventory, and a low number of orders compared to their net | |
* asset value. I canceled a ton of orders (enough to shake out the 100 bots operating) | |
* and then kept internal state of what I was interested in using a map. I sub'd to | |
* each account's websocket feed and let the data accumulate. From there I eyeballed | |
* it in my terminal. | |
*/ | |
package main | |
import ( | |
"fmt" | |
"github.com/donovanhide/stockfighter" | |
"strings" | |
"time" | |
) | |
type Profits struct { | |
Inventory int64 | |
Cash int64 | |
Orders map[uint64]bool | |
} | |
func main() { | |
apiKey := "8cc647d44ea8c05bc5749b2fec23bbcdfeaf8d51" | |
levelId := "making_amends" | |
fighter := stockfighter.NewStockfighter(apiKey, false) | |
instance, _ := fighter.Start(levelId) | |
status := make(map[string]Profits) | |
time.Sleep(10 * time.Second) | |
for ind := 0; ind < 1000; ind++ { | |
time.Sleep(20 * time.Millisecond) | |
err := fighter.Cancel(instance.Venues[0], instance.Tickers[0], uint64(ind)) | |
if err != nil { | |
errSlice := strings.Split(err.Error(), " ") | |
actName := errSlice[len(errSlice)-1] | |
actName = strings.Replace(actName, ".", "", 1) | |
profitMap := make(map[uint64]bool) | |
status[string(actName)] = Profits{0, 0, profitMap} | |
} | |
} | |
for acct, _ := range status { | |
go func(acct string) { | |
messages, _ := fighter.Executions(acct, instance.Venues[0], instance.Tickers[0]) | |
go func() { | |
for exec := range messages { | |
newInventory := int64(exec.Filled) | |
newCash := int64(exec.Price) | |
if exec.Order.Direction == "sell" { | |
newInventory = newInventory * -1 | |
} | |
if exec.Order.Direction == "buy" { | |
newCash = newCash * -1 | |
} | |
currentProf := status[acct] | |
currentProf.Cash += newCash | |
currentProf.Inventory += newInventory | |
currentProf.Orders[exec.Order.Id] = true | |
status[acct] = currentProf | |
} | |
}() | |
}(acct) | |
} | |
go func() { | |
time.Sleep(1 * time.Minute) | |
quote, _ := fighter.Quote(instance.Venues[0], instance.Tickers[0]) | |
price := ((quote.Ask + quote.Bid) / 2) / 100 | |
for k, v := range status { | |
nav := v.Cash + (v.Inventory * int64(price)) | |
if nav > 0 { | |
fmt.Println(k, nav, len(v.Orders)) | |
} | |
} | |
}() | |
time.Sleep(20 * time.Minute) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment