forked from AdaGold/ada-trader
-
Notifications
You must be signed in to change notification settings - Fork 43
Lauren Cardella -- Carets #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
enigmagnetic
wants to merge
13
commits into
Ada-C8:master
Choose a base branch
from
enigmagnetic:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f3f9761
Initial install. Add quote and quote list view files. Write initializ…
enigmagnetic 5ce1f83
Quotes list is showing but prices don't update. Add event listeners a…
enigmagnetic a9a40f4
Implement buy and sell methods in Quote model. Tests pass. Wave 1 com…
enigmagnetic e422dd8
Build QuoteListView and clean up app.js.
enigmagnetic 77c52b1
Build custom event to add trades. Add buy attribute and set within bu…
enigmagnetic 9caed0a
Add to collection render function to dynamically load quote symbols t…
enigmagnetic 2f8f4d6
Add files for order model and views.
enigmagnetic c736ae7
Add validation logic to order model.
enigmagnetic 968a03d
Add render functions to order and orderList views. OrderList is now r…
enigmagnetic cecebc9
Order form is creating new orders, which is rendering in the list vie…
enigmagnetic 560ae6b
Write tests for order validations. All tests passing.
enigmagnetic e944753
Missing code in the validations function was causing tests to fail. N…
enigmagnetic b11106b
Implement event bus to create trades from orders. Trade is added to l…
enigmagnetic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import Order from 'models/order'; | ||
|
|
||
| describe('Order spec', () => { | ||
| describe('Order validations', () => { | ||
| let order; | ||
| beforeEach(() => { | ||
| order = new Order({ | ||
| symbol: 'HUMOR', | ||
| targetPrice: '78.70', | ||
| currentPrice: '88.50', | ||
| buy: true, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns false if order is valid', () => { | ||
|
|
||
| expect(order.validate(order.attributes)).toEqual(false); | ||
| }); | ||
|
|
||
| it('requires a symbol to be valid', () => { | ||
| order.set('symbol', ''); | ||
|
|
||
| expect(order.isValid()).toBeFalsy(); | ||
| }); | ||
|
|
||
| it('requires a targetPrice to be valid', () => { | ||
| order.set('targetPrice', ''); | ||
|
|
||
| expect(order.isValid()).toBeFalsy(); | ||
| }); | ||
|
|
||
| it('is invalid if the buy price is higher than the current price', () =>{ | ||
| order.set('targetPrice', '92'); | ||
|
|
||
| expect(order.isValid()).toBeFalsy(); | ||
| }); | ||
|
|
||
| it('is invalid if the sell price is lower than the current price', () => { | ||
| order.set('buy', false); | ||
|
|
||
| expect(order.isValid()).toBeFalsy(); | ||
| }); | ||
|
|
||
| it('is invalid if the target price is 0 or not a number', () => { | ||
| order.set('targetPrice', '0'); | ||
|
|
||
| expect(order.isValid()).toBeFalsy(); | ||
|
|
||
| order.set('targetPrice', 'pfue'); | ||
|
|
||
| expect(order.isValid()).toBeFalsy(); | ||
| }); | ||
| }); | ||
| }); |
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import Backbone from 'backbone'; | ||
| import Order from '../models/order'; | ||
|
|
||
| const OrderList = Backbone.Collection.extend({ | ||
| model: Order, | ||
| }); | ||
|
|
||
| export default OrderList; |
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import Backbone from 'backbone'; | ||
|
|
||
| const Order = Backbone.Model.extend({ | ||
| validate: function(attributes) { | ||
| const errors = {}; | ||
|
|
||
| if (!attributes.symbol) { | ||
| errors['symbol'] = ["Symbol is required"]; | ||
| } | ||
|
|
||
| if (!attributes.targetPrice || attributes.targetPrice <= 0) { | ||
| errors['price_target'] = ["Target price is required"]; | ||
| } | ||
|
|
||
| if (attributes.buy && attributes.targetPrice >= attributes.currentPrice) { | ||
| errors['price_target'] = ["Buy order target price cannot be greater than the current market price."] | ||
| } | ||
|
|
||
| if (!attributes.buy && attributes.targetPrice <= attributes.currentPrice) { | ||
| errors['price_target'] = ["Sell order target price cannot be less than the current market price."] | ||
| } | ||
|
|
||
| if (Object.keys(errors).length > 0) { | ||
| return errors; | ||
| } else { | ||
| return false; | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| export default Order; |
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import Backbone from 'backbone'; | ||
| import _ from 'underscore'; | ||
| import OrderView from '../views/order_view'; | ||
| import Order from '../models/order'; | ||
|
|
||
| const OrderListView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.orderTemplate = params.orderTemplate; | ||
| this.quotes = params.quotes; | ||
| this.bus = params.bus; | ||
| this.listenTo(this.model, 'update', this.render); | ||
| }, | ||
| render() { | ||
| this.$('#orders').empty(); | ||
| this.model.each((order) => { | ||
| const orderView = new OrderView({ | ||
| model: order, | ||
| orderTemplate: this.orderTemplate, | ||
| tagName: 'li', | ||
| className: 'order', | ||
| bus: this.bus, | ||
| }); | ||
| this.$('#orders').append(orderView.render().$el); | ||
| }); | ||
| return this; | ||
| }, | ||
| events: { | ||
| 'click button.btn-buy': 'addOrder', | ||
| 'click button.btn-sell': 'addOrder', | ||
| }, | ||
| addOrder: function(event) { | ||
| event.preventDefault(); | ||
| let symbol = this.$('.order-entry-form [name=symbol]').val(); | ||
| const newOrder = new Order({ | ||
| symbol: symbol, | ||
| targetPrice: Number(this.$('.order-entry-form [name=price-target]').val()), | ||
| quote: this.quotes.findWhere({symbol: symbol}), | ||
| }); | ||
|
|
||
| if (event.target.innerHTML === 'Buy') { | ||
| newOrder.set('buy', true); | ||
| } else { | ||
| newOrder.set('buy', false) | ||
| } | ||
| newOrder.set('currentPrice', this.quotes.findWhere({symbol: symbol}).attributes['price']); | ||
|
|
||
| if (newOrder.isValid()) { | ||
| this.model.add(newOrder); | ||
| this.updateStatusMessageWith(`New order for ${newOrder.get('symbol')} has been saved.`) | ||
| this.clearForm(); | ||
| } else { | ||
| this.updateStatusMessageFrom(newOrder.validationError); | ||
| } | ||
| }, | ||
| updateStatusMessageFrom: function(messageHash) { | ||
| const statusMessagesEl = this.$('.form-errors'); | ||
| statusMessagesEl.empty(); | ||
| _.each(messageHash, (messageType) => { | ||
| messageType.forEach((message) => { | ||
| statusMessagesEl.append(`<p>${message}</p>`); | ||
| }); | ||
| }); | ||
| }, | ||
| updateStatusMessageWith: function(message) { | ||
| const statusMessagesEl = this.$('.form-errors'); | ||
| statusMessagesEl.empty(); | ||
| statusMessagesEl.append(`<p>${message}</p>`); | ||
| }, | ||
| clearForm: function() { | ||
| this.$('.order-entry-form input').val('') | ||
| }, | ||
| }); | ||
|
|
||
| export default OrderListView; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import Backbone from 'backbone'; | ||
| import Order from '../models/order'; | ||
|
|
||
| const OrderView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.orderTemplate; | ||
| this.bus = params.bus; | ||
| this.listenTo(this.bus, `check${this.model.get('symbol')}`, this.checkQuote); | ||
| }, | ||
| render() { | ||
| const compiledTemplate = this.template(this.model.toJSON()); | ||
| this.$el.html(compiledTemplate); | ||
| return this; | ||
| }, | ||
| events: { | ||
| 'click button.btn-cancel': 'cancelOrder', | ||
| }, | ||
| checkQuote: function(quote) { | ||
| if (this.model.get('buy') && quote.get('price') < this.model.get('targetPrice')) { | ||
| quote.buy(); | ||
| this.cancelOrder(); | ||
| } | ||
|
|
||
| if (!this.model.get('buy') && quote.get('price') > this.model.get('targetPrice')) { | ||
| quote.sell(); | ||
| this.cancelOrder(); | ||
| } | ||
| }, | ||
| cancelOrder: function(event) { | ||
| this.model.destroy(); | ||
| this.remove(); | ||
| }, | ||
| }); | ||
|
|
||
| export default OrderView; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import Backbone from 'backbone'; | ||
| import _ from 'underscore'; | ||
| import QuoteView from '../views/quote_view'; | ||
|
|
||
| const QuoteListView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.quoteTemplate = params.quoteTemplate; | ||
| this.tradeTemplate = params.tradeTemplate; | ||
| this.bus = params.bus; | ||
| this.listenTo(this.model, 'update', this.render); | ||
| this.listenTo(this.model, 'tradeMe', this.addTrade); | ||
| }, | ||
| render() { | ||
| this.$('#quotes').empty(); | ||
| this.$('form select').empty(); | ||
| this.model.each((quote) => { | ||
| const quoteView = new QuoteView({ | ||
| model: quote, | ||
| template: this.quoteTemplate, | ||
| tagName: 'li', | ||
| className: 'quote', | ||
| bus: this.bus, | ||
| }); | ||
| this.$('#quotes').append(quoteView.render().$el); | ||
| this.$('form select').append(`<option value="${ quoteView.model.get('symbol') }">${ quoteView.model.get('symbol') }</option>`); | ||
| }); | ||
| return this; | ||
| }, | ||
| addTrade: function(quote) { | ||
| const compiledTemplate = this.tradeTemplate(quote.toJSON()); | ||
| this.$('#trades').prepend(compiledTemplate); | ||
| }, | ||
| }); | ||
|
|
||
| export default QuoteListView; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import Backbone from 'backbone'; | ||
| import Quote from '../models/quote'; | ||
|
|
||
| const QuoteView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.template; | ||
| this.bus = params.bus; | ||
| this.listenTo(this.model, "change", this.render); | ||
| }, | ||
| render() { | ||
| const compiledTemplate = this.template(this.model.toJSON()); | ||
| this.$el.html(compiledTemplate); | ||
| this.bus.trigger(`check${this.model.get('symbol')}`, this.model); | ||
| return this; | ||
| }, | ||
| events: { | ||
| 'click button.btn-buy': 'buyQuote', | ||
| 'click button.btn-sell': 'sellQuote', | ||
| }, | ||
| buyQuote: function(event) { | ||
| this.model.buy(); | ||
| }, | ||
| sellQuote: function(event) { | ||
| this.model.sell(); | ||
| }, | ||
| }); | ||
|
|
||
| export default QuoteView; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like business logic and would make some sense to put into the Model