forked from AdaGold/ada-trader
-
Notifications
You must be signed in to change notification settings - Fork 43
Laura Robertson -- Carets #28
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
LauraAddams
wants to merge
13
commits into
Ada-C8:master
Choose a base branch
from
LauraAddams: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
958c487
Added quote view and quote list view, import links added in app.js
LauraAddams 8cad806
Quotes are displayed in the view, clicking buy/sell increase/decrease…
LauraAddams 884bb85
Trade history shows bought and sold quotes to the user
LauraAddams cc4a9ac
Added order, order list and matching views
LauraAddams f57c4e6
Refactored formSelect
LauraAddams 995801c
A new order template object is made, form information currently undef…
LauraAddams 127ccc0
Changes removeOrder to cancelOrder for clarity
LauraAddams e205b69
New order form correctly passes inputs to a new order
LauraAddams f6c81e4
Form recieves all information, bus added where necessary
LauraAddams 8042de8
If quote reaches order value, order cancels
LauraAddams 565c958
An order buys/sells when a quote hits the right number(but buys all q…
LauraAddams 038e1f6
Validations added for orders
LauraAddams 7cc2483
Order only buy/sell correct quote
LauraAddams 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,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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import Backbone from 'backbone'; | ||
|
|
||
| const Order = Backbone.Model.extend({ | ||
| validate(attributes) { | ||
| const errors = {}; | ||
|
|
||
| if(!attributes.targetPrice || attributes.targetPrice <= 0) { | ||
| errors['price'] = ['Invalid target price']; | ||
| } | ||
|
|
||
| if (this.get('buy') && attributes.targetPrice >= this.get('quote').get('price')) { | ||
| errors['price'] = ['Price higher than market price!']; | ||
| } | ||
|
|
||
| if (!this.get('buy') && attributes.targetPrice <= this.get('quote').get('price')) { | ||
| errors['price'] = ['Price lower than 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,75 @@ | ||
| import Backbone from 'backbone'; | ||
| // import _ from 'underscore'; | ||
| // import OrderList from '../collections/order_list'; | ||
| import $ from 'jquery'; | ||
| import OrderView from './order_view'; | ||
| import Order from '../models/order'; | ||
|
|
||
| const OrderListView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.template; | ||
| this.bus = params.bus; | ||
| this.quotes = params.quotes; | ||
| this.listenTo(this.model, 'update', this.render); | ||
| }, | ||
| render() { | ||
| this.$('#orders').empty(); | ||
|
|
||
| this.model.each((order) => { | ||
| const orderView = new OrderView({ | ||
| model: order, | ||
| template: this.template, | ||
| bus: this.bus, | ||
| tagName: 'li', | ||
| quotes: this.quotes, | ||
| className: 'order', | ||
| }); | ||
| this.$('#orders').append(orderView.render().$el); | ||
| }); | ||
| return this; | ||
| }, | ||
| events: { | ||
| 'click .btn-buy': 'buyOrder', | ||
| 'click .btn-sell': 'sellOrder', | ||
| }, | ||
| createOrder(value) { | ||
| this.$('.form-errors').empty(); | ||
|
|
||
| const orderData = { | ||
| buy: value.buy, | ||
| symbol: this.$('select[name=symbol]').val(), | ||
| quote: this.quotes.find({symbol: this.$('select[name=symbol]').val()}), | ||
| targetPrice: parseFloat(this.$('input[name=price-target]').val()), | ||
| bus: this.bus | ||
| }; | ||
| return new Order(orderData); | ||
| }, | ||
| buyOrder: function(event) { | ||
| event.preventDefault(); | ||
| const order = this.createOrder({buy: true}); | ||
| this.validate(order); | ||
| }, | ||
| sellOrder: function(event) { | ||
| event.preventDefault(); | ||
| const order = this.createOrder({buy: false}); | ||
| this.validate(order); | ||
| }, | ||
| validate(order) { | ||
| if (order.isValid()) { | ||
| this.model.add(order); | ||
| this.$el.find('form').trigger('reset'); | ||
| } else { | ||
| const errors = order.validationError; | ||
| const errorSection = this.$('.form-errors'); | ||
|
|
||
| Object.keys(errors).forEach((field) => { | ||
| errors[field].forEach((error) => { | ||
| const html = `<h3>${error}</h3>`; | ||
| errorSection.append(html); | ||
| }); | ||
| }); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| 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,37 @@ | ||
| import Backbone from 'backbone'; | ||
|
|
||
| const OrderView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.template; | ||
| this.bus = params.bus; | ||
| this.listenTo(this.model, 'change', this.render); | ||
| this.listenTo(this.model.get('quote'), 'change', this.tradeOrder); | ||
| }, | ||
| render() { | ||
| const compiledTemplate = this.template(this.model.toJSON()); | ||
| this.$el.html(compiledTemplate); | ||
| return this; | ||
| }, | ||
| events: { | ||
| 'click .btn-cancel': 'cancelOrder', | ||
| }, | ||
| cancelOrder() { | ||
| this.model.destroy(); | ||
| this.remove(); | ||
| }, | ||
| tradeOrder() { | ||
| if (this.model.get('buy')) { | ||
| if (this.model.get('quote').get('price') <= this.model.get('targetPrice')) { | ||
| this.bus.trigger('buyOrder', this); | ||
| this.cancelOrder(); | ||
| } | ||
| } else { | ||
| if (this.model.get('quote').get('price') >= this.model.get('targetPrice')) { | ||
| this.bus.trigger('sellOrder', this); | ||
| this.cancelOrder(); | ||
| } | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| 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,34 @@ | ||
| import Backbone from 'backbone'; | ||
| // import _ from 'underscore'; | ||
| // import Quote from '../models/quote'; | ||
| import QuoteView from './quote_view'; | ||
|
|
||
|
|
||
| const QuoteListView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.template; | ||
| this.tradeTemplate = params.tradeTemplate; | ||
| this.bus = params.bus; | ||
| this.listenTo(this.model, 'update', this.render); | ||
| }, | ||
| render() { | ||
| this.model.each( (quote) => { | ||
| const quoteView = new QuoteView({ | ||
| model: quote, | ||
| template: this.template, | ||
| bus: this.bus, | ||
| tagName: 'li', | ||
| className: 'quote', | ||
| }); | ||
| this.listenTo(quoteView, 'quoteAction', this.trade); | ||
| this.$('#quotes').append(quoteView.render().$el); | ||
| }); | ||
| return this; | ||
| }, | ||
| trade: function(quoteView) { | ||
| const tradeTemplate = this.tradeTemplate(quoteView.model.toJSON()); | ||
| this.$('#trades').prepend(tradeTemplate); | ||
| }, | ||
| }); | ||
|
|
||
| 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,42 @@ | ||
| import Backbone from 'backbone'; | ||
|
|
||
| const QuoteView = Backbone.View.extend({ | ||
| initialize(params) { | ||
| this.template = params.template; | ||
| this.bus = params.bus; | ||
| this.listenTo(this.model, 'change', this.render); | ||
| this.listenTo(this.bus, 'buyOrder', this.buyOrder); | ||
| this.listenTo(this.bus, 'sellOrder', this.sellOrder); | ||
| }, | ||
| render() { | ||
| const compiledTemplate = this.template(this.model.toJSON()); | ||
| this.$el.html(compiledTemplate); | ||
| return this; | ||
| }, | ||
| events: { | ||
| 'click button.btn-buy': 'buyQuote', | ||
| 'click button.btn-sell': 'sellQuote', | ||
| }, | ||
| buyQuote: function(event) { | ||
| this.model.set('buy', true); | ||
| this.trigger('quoteAction', this); | ||
| this.model.buy(); | ||
| }, | ||
| sellQuote: function(event) { | ||
| this.model.set('buy', false); | ||
| this.trigger('quoteAction', this); | ||
| this.model.sell(); | ||
| }, | ||
| buyOrder: function(orderQuote) { | ||
| if(orderQuote.model.get('symbol') === this.model.get('symbol')){ | ||
| this.buyQuote(); | ||
| } | ||
| }, | ||
| sellOrder: function(orderQuote) { | ||
| if(orderQuote.model.get('symbol') === this.model.get('symbol')){ | ||
| this.sellQuote(); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| 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.
Neat, but you could also do:
this.$('form').trigger('reset');That way you don't need to executefind.