Action Directory / Tradervue Journal entry

Install

Create journal entries and notes in Tradervue

Shared by Tradervue

Script

// Enter your Tradervue username and password below
username = "";
password = "";
//

api_root = "https://www.tradervue.com/api/v1";

headers = {
			'Accept': 'application/json',
			'User-Agent': '1Writer journal app (gregr@rassoc.com)'
};

function open_journal_entry()
{
	d = new Date();
	dq = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
	http.request({
	    url: api_root + '/journal?d=' + encodeURIComponent(dq),
		headers: headers,
		username: username,
		password: password
	}, function(data, error) {
		if (error) {
			if (error[0] == 401)
				ui.alert("Tradervue username or password incorrect.")
			else
				ui.alert(error);
		}
		else if (data['journal_entries'].length > 0)
			editor.setText(data['journal_entries'][0]['notes']);
		else
			editor.setText("");
	});
}

function save_journal_entry()
{
	d = new Date();
	dq = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
	http.request({
    	url: api_root + '/journal?d=' + encodeURIComponent(dq),
		headers: headers,
		username: username,
		password: password
	}, function(data, error) {
		if (error) {
			if (error[0] == 401)
				ui.alert("Tradervue username or password incorrect.")
			else
				ui.alert(error);
		}
		else if (data['journal_entries'].length > 0)
			save_to_entry(data['journal_entries'][0]['id'], editor.getText());
		else
			create_journal_entry(d, editor.getText());
	});
}

function save_to_entry(entry_id, new_notes) {
	http.request({
	    url: api_root + '/journal/' + entry_id,
		type: 'PUT',
		headers: headers,
		username: username,
		password: password,
		data: {"notes": new_notes}
	}, function(data, error) {
		if (error)
			ui.alert(error);
		else
			ui.alert("Journal entry saved.")
	});
}

function create_journal_entry(d, new_notes) {
	date_str = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
	http.request({
	    url: api_root + '/journal',
		type: 'POST',
		headers: headers,
		username: username,
		password: password,
		data: {"date": date_str, "notes": new_notes}
	}, function(data, error) {
		if (error) {
			ui.alert(error);
		}
		else
			ui.alert("Journal entry saved.")
	});
}

function create_journal_note() {
	http.request({
	    url: api_root + '/notes',
		type: 'POST',
		headers: headers,
		username: username,
		password: password,
		data: {"notes": editor.getText()}
	}, function(data, error) {
		if (error) {
			if (error[0] == 401)
				ui.alert("Tradervue username or password incorrect.")
			else
				ui.alert(error);
		}
		else
			ui.alert("Journal note saved.")
	});
}

if (username == "") {
	ui.alert("Before using this action, you must edit it and enter your Tradervue username and password.");
}
else {
	ui.alert("What would you like to do?",
				"Tradervue",
				"Open today's journal entry",
				"Save today's journal entry",
				"Save new journal note",
				"Cancel", 
				function(selected) {
					if (selected == 0)
						open_journal_entry();
					else if (selected == 1)
						save_journal_entry();
					else if (selected == 2)
						create_journal_note();
				});
}