Skip to content

Pipes - Rebecca - Ada Trader #34

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
wants to merge 17 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
91 changes: 91 additions & 0 deletions spec/models/open_order_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import OpenOrder from 'models/open_order';
import Quote from 'models/quote';


describe('OpenOrder spec', () => {
let currentQuote;
let OrderAtQuotePrice;
let OrderAboveQuotePrice;
let OrderBelowQuotePrice;

beforeEach(() => {
currentQuote = new Quote({
symbol: 'HUMOR',
price: 85.00,
})

OrderAtQuotePrice = new OpenOrder({
targetPrice: 85.0,
symbol: 'HUMOR',
quote: currentQuote,
buy: true,
})

OrderAboveQuotePrice = new OpenOrder({
targetPrice: 99.0,
symbol: 'HUMOR',
quote: currentQuote,
buy: true,
})

OrderBelowQuotePrice = new OpenOrder({
targetPrice: 83.0,
symbol: 'HUMOR',
quote: currentQuote,
buy: true,
})
});

describe('validate', () => {
it ('does not allow for price to be blank', () => {

const invalidOrder = new OpenOrder({
targetPrice: '',
symbol: 'HUMOR',
quote: currentQuote,
buy: true,
})

expect(invalidOrder.isValid()).toBeFalsy();
});

// buy order validations
it ('does not allow for buy orders where target price is equal to the quote', () => {

expect(OrderAtQuotePrice.isValid()).toBeFalsy();
});

it ('does not allow for buy orders where target price is greater than the quote', () => {

expect(OrderAboveQuotePrice.isValid()).toBeFalsy();
});

it ('allows for buy orders where target price is less than the quote', () => {

expect(OrderBelowQuotePrice.isValid()).toBeTruthy(`error: ${OrderBelowQuotePrice.validationError}`);
});

// sell order validations
it ('does not allow for sell orders where target price is equal to the quote', () => {

OrderAtQuotePrice.set('buy', false);

expect(OrderAtQuotePrice.isValid()).toBeFalsy();
});

it ('does not allow for sell orders where target price is less than the quote', () => {

OrderBelowQuotePrice.set('buy', false);

expect(OrderBelowQuotePrice.isValid()).toBeFalsy();
});

it ('allows for sell orders where target price is greater than the quote', () => {

OrderAboveQuotePrice.set('buy', false)

expect(OrderAboveQuotePrice.isValid()).toBeTruthy(`error: ${OrderAboveQuotePrice.validationError}`);
});
});

});
51 changes: 51 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ import 'foundation-sites/dist/foundation.css';
import 'css/app.css';

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

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

import Quote from './models/quote';
import QuoteView from './views/quote_view';
import QuoteListView from './views/quote_list_view';
import TradeListView from './views/trade_list_view';
import OpenOrder from './models/open_order';
import OpenOrderView from './views/open_order_view';
import OpenOrderListView from './views/open_order_list_view';

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

const openOrderList = new OpenOrderList();

let quoteTemplate;
let tradeTemplate;
let orderTemplate;

$(document).ready(function() {
let bus = {};

bus = _.extend(bus, Backbone.Events);
quoteTemplate = _.template($('#quote-template').html());
orderTemplate = _.template($('#order-template').html());


tradeTemplate = _.template($('#trade-template').html());

const quotes = new QuoteList(quoteData);
const simulator = new Simulator({
quotes: quotes,
});

// Quotelistview will encompass the main tag
const quoteListView = new QuoteListView({
el: 'main',
model: quotes,
template: quoteTemplate,
bus: bus,
});

// TradelistView will encompass the main tag
const tradelistView = new TradeListView({
el: '#trades-container',
bus: bus,
template: tradeTemplate,
});

// OpenOrderListview will encompass the orderWorkspace div
const openOrderListView = new OpenOrderListView({
el: '#order-workspace',
model: openOrderList,
template: orderTemplate,
bus: bus,
});

quoteListView.render();

simulator.start();
});
8 changes: 8 additions & 0 deletions src/collections/open_order_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Backbone from 'backbone';
import OpenOrder from 'models/open_order';

const OpenOrderList = Backbone.Collection.extend({
model: OpenOrder,
});

export default OpenOrderList;
7 changes: 7 additions & 0 deletions src/collections/quote_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import Quote from 'models/quote';

const QuoteList = Backbone.Collection.extend({
model: Quote,
symbols() {
let symbols = [];
this.each((quote) => {
symbols.push(quote.get('symbol'));
});
return symbols;
}
});

export default QuoteList;
29 changes: 29 additions & 0 deletions src/models/open_order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Backbone from 'backbone';

const OpenOrder = Backbone.Model.extend({
initialize(attributes) {
},
validate(attributes) {
const errors = {};

if (!attributes.targetPrice) {
errors['target_price'] = ["Price cannot be blank."];
}

if (attributes.buy && attributes.targetPrice >= attributes.quote.get('price')) {
errors['target_price'] = ["The price you listed for a buy order is greater than or equal to the current quote price."];
}

if (!attributes.buy && attributes.targetPrice <= attributes.quote.get('price')) {
errors['target_price'] = ["The price you listed for a sell order is less than or equal to the current quote price."];
}

if ( Object.keys(errors).length > 0 ) {
return errors;
} else {
return false;
}
},
});

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

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

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

Expand Down
Loading