{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "nil" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ns clojure-backtesting.demo\n", " (:require [clojure-backtesting.data :refer :all]\n", " [clojure-backtesting.data-management :refer :all]\n", " [clojure-backtesting.portfolio :refer :all]\n", " [clojure-backtesting.order :refer :all]\n", " [clojure-backtesting.evaluate :refer :all]\n", " [clojure-backtesting.plot :refer :all]\n", " [clojure-backtesting.counter :refer :all]\n", " [clojure-backtesting.automation :refer :all]\n", " [clojure-backtesting.parameters :refer :all]\n", " [clojure-backtesting.indicators :refer :all]\n", " [clojure-backtesting.direct :refer :all]\n", " [clojure.string :as str]\n", " [clojure.java.io :as io]\n", " [clojure.pprint :as pprint]\n", " ) ;; require all libriaries from core\n", ")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The dataset is already furnished by add-aprc. No more modification is needed.\n" ] }, { "data": { "text/plain": [ "\"Date: 1986-01-09 Cash: $2000\"" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(load-dataset \"/Volumes/T7/CRSP\" \"main\" add-aprc)\n", "(init-portfolio \"1986-01-09\" 2000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Limit Order\n", "#### Stop Buy" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(defn stop-buy\n", " \"This function executes a stop buy order.\"\n", " [tic prc qty]\n", " (set-automation\n", " ; check if ticker adjusted price is greater than prc\n", " (condition #(and (get (deref portfolio) tic) (> (get-in (deref portfolio) [tic :aprc]) prc)))\n", " (action #(order tic qty))\n", " :max-dispatch nil\n", " :expiration nil))\n", "\n", "(stop-buy \"30154\" 32 10)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(defn limit-buy\n", " \"This function executes a limit buy order.\"\n", " [tic prc qty]\n", " (set-automation\n", " ; check if ticker adjusted price is smaller than prc\n", " (condition #(and (get (deref portfolio) tic) (< (get-in (deref portfolio) [tic :aprc]) prc)))\n", " (action #(order tic qty))\n", " :max-dispatch nil\n", " :expiration nil))\n", "\n", "(limit-buy \"30155\" 35 10)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(defn stop-sell\n", " \"This function executes a stop sell order.\"\n", " [tic prc qty]\n", " (set-automation\n", " ; check if ticker adjusted price is smaller than prc\n", " (condition #(and (get (deref portfolio) tic) (< (get-in (deref portfolio) [tic :aprc]) prc)))\n", " (action #(order tic (- qty)))\n", " :max-dispatch nil\n", " :expiration nil))\n", "\n", "(stop-sell \"30154\" 35 10)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(defn limit-sell\n", " \"This function executes a limit sell order.\"\n", " [tic prc qty]\n", " (set-automation\n", " ; check if ticker adjusted price is smaller than prc\n", " (condition #(and (get (deref portfolio) tic) (> (get-in (deref portfolio) [tic :aprc]) prc)))\n", " (action #(order tic (- qty)))\n", " :max-dispatch nil\n", " :expiration nil))\n", "\n", "(limit-sell \"30155\" 35 10)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "| :id | :condition | :action | :max-dispatch | :expire-date |\n", "|-----+-------------------------------------------------------------------------------------------+------------------------------+---------------+--------------|\n", "| 1 | (fn* [] (and (get (deref portfolio) tic) (> (get-in (deref portfolio) [tic :aprc]) prc))) | (fn* [] (order tic qty)) | Unlimited | Never |\n", "| 2 | (fn* [] (and (get (deref portfolio) tic) (< (get-in (deref portfolio) [tic :aprc]) prc))) | (fn* [] (order tic qty)) | Unlimited | Never |\n", "| 3 | (fn* [] (and (get (deref portfolio) tic) (< (get-in (deref portfolio) [tic :aprc]) prc))) | (fn* [] (order tic (- qty))) | Unlimited | Never |\n", "| 4 | (fn* [] (and (get (deref portfolio) tic) (> (get-in (deref portfolio) [tic :aprc]) prc))) | (fn* [] (order tic (- qty))) | Unlimited | Never |\n" ] }, { "data": { "text/plain": [ "nil" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(print-automation-list)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"1986-01-10\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(next-date)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "nil" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(print-order-record)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Clojure (backtesting_clojure)", "language": "clojure", "name": "backtesting_clojure" }, "language_info": { "file_extension": ".clj", "mimetype": "text/x-clojure", "name": "clojure", "version": "1.10.1" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" } } }, "nbformat": 4, "nbformat_minor": 2 }