Skip to content
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

Carets - Maria - ada-trader #38

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ <h2>Open Orders</h2>
<div class="columns large-4 small-12 trade-column">
<div class="order-entry-form columns small-10 small-offset-1">
<h3>Order Entry Form</h3>
<form>
<form id="add-order-form">
<label for="symbol">Symbol</label>
<select name="symbol">
<select id="symbol" name="symbol">
<option value="HUMOR">HUMOR</option>
<option value="CLOTH">CLOTH</option>
<option value="HABIT">HABIT</option>
<option value="SUPER">SUPER</option>
<!-- Option entries should be added here using JavaScript -->
</select>
<label for="price-target">Price</label>
Expand Down
33 changes: 32 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ import 'foundation-sites/dist/foundation.css';
import 'css/app.css';

import $ from 'jquery';
import _ from 'underscore';

import Simulator from 'models/simulator';
import QuoteList from 'collections/quote_list';

import Quote from './models/quote';
import QuoteList from './collections/quote_list';

import QuoteView from './views/quote_view';
import QuoteListView from './views/quote_list_view';

import Order from './models/order';
import OrderList from './collections/order_list';

import OrderView from './views/order_view';
import OrderListView from './views/order_list_view';

const quoteData = [
{
Expand All @@ -25,11 +37,30 @@ const quoteData = [
},
];


$(document).ready(function() {
const quotes = new QuoteList(quoteData);
const orders = new OrderList();
const simulator = new Simulator({
quotes: quotes,
});

const quoteListView = new QuoteListView({
model: quotes,
template: _.template($('#quote-template').html()),
tradesTemplate: _.template($('#trade-template').html()),
el: 'main'
});

const orderListView = new OrderListView({
model: orders,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest passing quotes in to the OrderListView and then the view can listen for changes in quotes and check to see if it's time to execute an order.

template: _.template($('#order-template').html()),
el: 'main'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that both views represent the SAME element in the DOM, this means it's possible they could interfere with each other.

Instead I would suggest making the QuoteListView's el element be #quotes-container and the OrderListView's el be #order-workspace.

});

quoteListView.render();
orderListView.render();

simulator.start();

});
8 changes: 8 additions & 0 deletions src/collections/order_list.js
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;
17 changes: 17 additions & 0 deletions src/models/order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Backbone from 'backbone';

const Order = Backbone.Model.extend({
defaults: {
symbol: 'UNDEF',
targetPrice: 0.00
},

buyOrder(){
this.set('buy', true);
},
sellOrder() {
this.set('buy', false);
},
});

export default Order;
4 changes: 4 additions & 0 deletions src/models/quote.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ const Quote = Backbone.Model.extend({
},

buy() {
this.set('price', this.get('price') + 1)
// Implement this function to increase the price by $1.00
this.set('buy', true);
},

sell() {
this.set('price', this.get('price') - 1)
// Implement this function to decrease the price by $1.00
this.set('buy', false);
},
});

Expand Down
31 changes: 31 additions & 0 deletions src/views/order_list_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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.template = params.template;
this.listenTo(this.model, 'update', this.render);
},
render() {
this.$('#orders').empty();
this.model.each((order) => {
const orderView = new OrderView({
model: order,
template: this.template,
tagName: 'li',
className: 'order'
});

this.$('#orders').append(orderView.render().$el);
});
return this;
}, // end of render
listOrders(orderView){
const compiledOrdersTemplate = orderView.template(orderView.model.toJSON());
this.$('#orders').prepend(compiledOrdersTemplate);
}
});

export default OrderListView;
35 changes: 35 additions & 0 deletions src/views/order_view.js
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.template;
this.orderTemplate = params.orderTemplate;
this.listenTo(this.model, "update", this.render);
},
render() {
const compiledTemplate = this.template(this.model.toJSON());
this.$el.html(compiledTemplate);

return this;
}, //end of render
events: {
'click button.alert': 'addOrderBuy',
'click button.success': 'addOrderSell',
},
addOrderSell(event) {
event.preventDefault();
const orderData = {};
['symbol', 'price-target'].forEach( (field) => {
const val = this.$(`#add-order-form input[name=${field}]`).val();
if (val != '') {
orderData[field] = val;
}
});
const newOrder = new Order(orderData);
this.model.add(newOrder);
this.trigger('listOrders', this);
},
}); // end of OrderView

export default OrderView;
35 changes: 35 additions & 0 deletions src/views/quote_list_view.js
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';
import Quote from '../models/quote';

const QuoteListView = Backbone.View.extend({
initialize(params) {
this.template = params.template;
this.tradesTemplate = params.tradesTemplate;
this.listenTo(this.model, 'update', this.render);
},
render() {
this.$('#quotes').empty();
this.model.each((quote) => {
const quoteView = new QuoteView({
model: quote,
template: this.template,
tradesTemplate: this.tradesTemplate,
tagName: 'li',
className: 'quote'
});

this.listenTo(quoteView, 'listTrades', this.listTrades)
this.$('#quotes').append(quoteView.render().$el);
});
return this;
}, // end of render
listTrades(quoteView){
const compiledTradesTemplate = quoteView.tradesTemplate(quoteView.model.toJSON());
this.$('#trades').prepend(compiledTradesTemplate);
}

}); //end of QuoteListView

export default QuoteListView
30 changes: 30 additions & 0 deletions src/views/quote_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Backbone from 'backbone';
import Quote from '../models/quote';

const QuoteView = Backbone.View.extend({
initialize(params) {
this.template = params.template;
this.tradesTemplate = params.tradesTemplate;
this.listenTo(this.model, "change", this.render);
},
render() {
const compiledTemplate = this.template(this.model.toJSON());
this.$el.html(compiledTemplate);

return this;
}, //end of render
events: {
'click button.alert': 'buyStock',
'click button.success': 'sellStock',
},
buyStock: function(e) {
this.model.buy();
this.trigger('listTrades', this);
},
sellStock: function(e) {
this.model.sell();
this.trigger('listTrades', this);
}
}); // end of QuoteView

export default QuoteView;