Skip to content

Instantly share code, notes, and snippets.

@gregoryyoung
Created February 24, 2012 07:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregoryyoung/1898785 to your computer and use it in GitHub Desktop.
Save gregoryyoung/1898785 to your computer and use it in GitHub Desktop.
quick kata impl
class stoploss {
private Dictionary<Guid, decimal> fifteen = new Dictionary>Guid, decimal>()
private Dictionary<Guid, decimal> thirty = new Dictionary>Guid, decimal>();;
bool alive = true;
decimal price
Guid id;
public stoploss(PositionAcquiredMessage m) {
id = m.PositionId;
}
public void Consume(PriceChangeMessage m) {
fifteen.Add(m.Id, m.Price);
thirty.Add(m.Id, m.Price);
Raise(SendMeInNSeconds(new RemoveFromFifteen(m.Id), 15);
Raise(SendMeInNSeconds(new RemoveFromThirty(m.Id), 30); }
}
public void Consume(RemoveFromFifteen m) {
fifteen.Remove(m.Id);
CheckMove();
}
public void Consume(RemoveFromThirty m) {
CheckSell();
thtirty.Remove(m.Id);
}
void CheckMove() {
if(fifteen.Values.Where(x => x > target).Count != fiften.Values.Count) return;
price = fifteen.Values.Minimum();
}
void CheckSell() {
if(!alive) return;
if(!thirty.Where(x.Value > price * .95).Count() == 0) return;
Raise(new SellPosition(this.Id));
alive = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment