diff --git a/resources/NBTest/NBTest_051_CPCBalancer.ipynb b/resources/NBTest/NBTest_051_CPCBalancer.ipynb index 4a8ff6bc8..06b8851f4 100644 --- a/resources/NBTest/NBTest_051_CPCBalancer.ipynb +++ b/resources/NBTest/NBTest_051_CPCBalancer.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 79, + "execution_count": 81, "id": "a448e212", "metadata": {}, "outputs": [ @@ -10,16 +10,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "ConstantProductCurve v3.1beta1 (25/Aug/2023)\n" + "ConstantProductCurve v3.1 (25/Aug/2023)\n" ] } ], "source": [ - "from fastlane_bot.tools.cpc import ConstantProductCurve as CPC\n", + "from fastlane_bot.tools.cpc import ConstantProductCurve as CPC, CurveBase\n", "from fastlane_bot.testing import *\n", + "# from flbtools.cpc import ConstantProductCurve as CPC, CurveBase\n", + "# from flbtesting import *\n", "\n", - "#from flbtools.cpc import ConstantProductCurve as CPC\n", - "#from flbtesting import *\n", "from math import sqrt\n", "print(\"{0.__name__} v{0.__VERSION__} ({0.__DATE__})\".format(CPC))\n", "# from fastlane_bot import __VERSION__\n", @@ -44,19 +44,23 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 82, "id": "5e055a74-6e99-4ffa-a450-5f53ae7695e5", "metadata": {}, "outputs": [], "source": [ "c0 = CPC.from_xy(100, 200)\n", "assert c0.tknx == \"TKNB\"\n", - "assert c0.tkny == \"TKNQ\"" + "assert c0.tkny == \"TKNQ\"\n", + "k0 = c0.invariant()\n", + "assert iseq(k0, sqrt(100*200))\n", + "k1, k2 = c0.invariant(include_target=True)\n", + "assert iseq(k0, k1, k2)" ] }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 83, "id": "22004cc1-b2c4-4486-b16f-f8a7b4fbd1b8", "metadata": {}, "outputs": [], @@ -67,13 +71,14 @@ "assert iseq(y, 200)\n", "assert iseq(xvec[c0.tknx], x)\n", "assert iseq(xvec[c0.tkny], y)\n", + "assert iseq(c0.invariant(), c0.invariant(xvec))\n", "assert raises(c0.xvecfrompvec_f, {c0.tknx: c0.p} ).startswith(\"pvec must contain\")\n", "assert raises(c0.xvecfrompvec_f, {c0.tkny: 1} ).startswith(\"pvec must contain\")" ] }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 84, "id": "4138bcb3-0054-4077-8a0d-df39cb46f98f", "metadata": {}, "outputs": [], @@ -88,12 +93,13 @@ "assert iseq(x, 81.64965809277261)\n", "assert iseq(y, 244.9489742783178)\n", "assert iseq(xvec[c0.tknx], x)\n", - "assert iseq(xvec[c0.tkny], y)" + "assert iseq(xvec[c0.tkny], y)\n", + "assert iseq(c0.invariant(), c0.invariant(xvec))" ] }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 85, "id": "ffa625ad-6ca5-40fe-b20a-bede3249cc7e", "metadata": {}, "outputs": [], @@ -110,7 +116,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 86, "id": "4975e630-45ba-4ac2-95ed-2809b6488f4a", "metadata": {}, "outputs": [], @@ -128,6 +134,64 @@ "assert iseq(dxvec[c0.tkny], dy)" ] }, + { + "cell_type": "markdown", + "id": "bc39d223-0e37-43f4-86f0-1c07105cb321", + "metadata": {}, + "source": [ + "## CurveBase" + ] + }, + { + "cell_type": "markdown", + "id": "1bb551a7-0e40-4758-ae87-1d1b30f12a3f", + "metadata": {}, + "source": [ + "Checking that `CurveBase` can only instantiate with all functions defined" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "5ba59a93-f792-428a-8381-56f3b4b3fd30", + "metadata": {}, + "outputs": [], + "source": [ + "class CB1(CurveBase):\n", + " pass\n", + "\n", + "class CB2(CurveBase):\n", + " def dxvecfrompvec_f(self, pvec, *, ignorebounds=False):\n", + " pass\n", + "\n", + "class CB3(CurveBase):\n", + " def xvecfrompvec_f(self, pvec, *, ignorebounds=False):\n", + " pass\n", + "\n", + "class CB4(CurveBase):\n", + " def xvecfrompvec_f(self, pvec, *, ignorebounds=False):\n", + " pass\n", + " def dxvecfrompvec_f(self, pvec, *, ignorebounds=False):\n", + " pass\n", + " def invariant(self, xvec=None, *, include_target=False):\n", + " pass\n", + " \n", + "assert raises(CB1).startswith(\"Can't instantiate abstract class\")\n", + "assert raises(CB2).startswith(\"Can't instantiate abstract class\")\n", + "assert raises(CB3).startswith(\"Can't instantiate abstract class\")\n", + "assert not raises(CB4)" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "afb61a79-c802-41f0-bafa-4d2ae4bc82d5", + "metadata": {}, + "outputs": [], + "source": [ + "assert isinstance(CPC.from_xy(100, 200), CurveBase)" + ] + }, { "cell_type": "markdown", "id": "521e4bc5-f003-4062-8978-18506ecff248", @@ -138,7 +202,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 89, "id": "c5cf94f7-77df-412c-9988-7085184bdd1f", "metadata": {}, "outputs": [ @@ -148,7 +212,7 @@ "ConstantProductCurve(k=20000, x=100, x_act=100, y_act=200.0, alpha=0.5, pair='TKNB/TKNQ', cid='None', fee=None, descr=None, constr='xy', params={})" ] }, - "execution_count": 85, + "execution_count": 89, "metadata": {}, "output_type": "execute_result" } @@ -169,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 90, "id": "9eb7484e-a09a-4184-bd59-073a7b399b8a", "metadata": {}, "outputs": [], @@ -191,7 +255,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 91, "id": "2b12d88d-6d7d-4d39-b9bc-def990500df7", "metadata": {}, "outputs": [ @@ -201,7 +265,7 @@ "ConstantProductCurve(k=20000.0, x=100, x_act=100, y_act=200.0, alpha=0.5, pair='TKNB/TKNQ', cid='None', fee=None, descr=None, constr='xyal', params={})" ] }, - "execution_count": 87, + "execution_count": 91, "metadata": {}, "output_type": "execute_result" } @@ -216,7 +280,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 92, "id": "fc98b1e0-0771-4cd6-877c-b70e74b548b8", "metadata": {}, "outputs": [], @@ -238,7 +302,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 93, "id": "f6ededab-423a-489e-8d1b-295162fda59b", "metadata": {}, "outputs": [ @@ -248,7 +312,7 @@ "ConstantProductCurve(k=20000.0, x=100, x_act=100, y_act=200.0, alpha=0.5, pair='TKNB/TKNQ', cid='None', fee=None, descr=None, constr='xyal', params={})" ] }, - "execution_count": 89, + "execution_count": 93, "metadata": {}, "output_type": "execute_result" } @@ -264,7 +328,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 94, "id": "203e5e7b-1cde-4154-a75c-35068f018a21", "metadata": {}, "outputs": [ @@ -274,7 +338,7 @@ "ConstantProductCurve(k=20000.0, x=100, x_act=100, y_act=200.0, alpha=0.5, pair='TKNB/TKNQ', cid='None', fee=None, descr=None, constr='xyal', params={})" ] }, - "execution_count": 90, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } @@ -290,7 +354,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 95, "id": "ee693b31-3278-46d2-a578-ba6e0b2c6333", "metadata": {}, "outputs": [], @@ -309,7 +373,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 96, "id": "2331941a-3aeb-4fa3-887a-6ed45c37307b", "metadata": {}, "outputs": [ @@ -319,7 +383,7 @@ "ConstantProductCurve(k=20000, x=100, x_act=100, y_act=200.0, alpha=0.5, pair='TKNB/TKNQ', cid='None', fee=None, descr=None, constr='xy', params={})" ] }, - "execution_count": 92, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } @@ -334,13 +398,22 @@ "assert c0.alpha == 0.5\n", "assert c0.eta == 1\n", "assert c0.constr == \"xy\"\n", + "assert iseq(c0.invariant(), c0.kbar)\n", "assert c0.is_constant_product() == True\n", "c0" ] }, { "cell_type": "code", - "execution_count": 93, + "execution_count": null, + "id": "00857b2e-d9af-41ce-a41f-c52b373fcd3a", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 97, "id": "db5f6d9a-87c3-4bba-9bbc-32993d2c09a7", "metadata": {}, "outputs": [ @@ -350,7 +423,7 @@ "ConstantProductCurve(k=20000.0, x=100, x_act=100, y_act=200.0, alpha=0.5, pair='TKNB/TKNQ', cid='None', fee=None, descr=None, constr='xyal', params={})" ] }, - "execution_count": 93, + "execution_count": 97, "metadata": {}, "output_type": "execute_result" } @@ -361,12 +434,13 @@ "assert c1.is_constant_product() == True\n", "assert c1 == c0\n", "assert c1.asdict()[\"alpha\"] == 0.5\n", + "assert iseq(c1.invariant(), c1.kbar)\n", "c1" ] }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 98, "id": "02dc9cc9-d30e-4daf-9f7d-11f9bd60ad04", "metadata": {}, "outputs": [ @@ -376,7 +450,7 @@ "ConstantProductCurve(k=800000000.0, x=100, x_act=100, y_act=199.99999999999994, alpha=0.25, pair='TKNB/TKNQ', cid='None', fee=None, descr=None, constr='xyal', params={})" ] }, - "execution_count": 94, + "execution_count": 98, "metadata": {}, "output_type": "execute_result" } @@ -388,6 +462,7 @@ "assert c2.alpha == 0.25\n", "assert c2.asdict()[\"alpha\"] == 0.25\n", "assert iseq(c2.eta, 0.25/0.75)\n", + "assert iseq(c2.invariant(), c2.kbar)\n", "assert c2 != c0\n", "assert c2 != c1\n", "c2" @@ -395,7 +470,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 99, "id": "f48bec87-6674-4a5c-8a15-562c5caff154", "metadata": {}, "outputs": [ @@ -405,7 +480,7 @@ "ConstantProductCurve(k=800000000.0, x=100, x_act=100, y_act=199.99999999999994, alpha=0.25, pair='TKNB/TKNQ', cid='None', fee=None, descr=None, constr='xyal', params={})" ] }, - "execution_count": 95, + "execution_count": 99, "metadata": {}, "output_type": "execute_result" } @@ -417,6 +492,7 @@ "assert iseq(c3.alpha, 0.8)\n", "assert c3.asdict()[\"alpha\"] == 0.8\n", "assert iseq(c3.eta, 0.8/0.2)\n", + "assert iseq(c3.invariant(), c3.kbar)\n", "assert c3 != c0\n", "assert c3 != c1\n", "assert c3 != c2\n", @@ -425,7 +501,7 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 100, "id": "4dc4a1f6-1d71-4f1d-b0a0-616f83fb58e8", "metadata": {}, "outputs": [ @@ -435,7 +511,7 @@ "ConstantProductCurve(k=376.06030930863926, x=100, x_act=100, y_act=200.0, alpha=0.8, pair='TKNB/TKNQ', cid='None', fee=None, descr=None, constr='xyal', params={})" ] }, - "execution_count": 96, + "execution_count": 100, "metadata": {}, "output_type": "execute_result" } @@ -448,7 +524,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 101, "id": "8caba1ff-65e7-496e-adcf-aea4cddcee4b", "metadata": {}, "outputs": [], @@ -461,7 +537,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 102, "id": "b54669fb-a128-41ae-a3ac-b9eff553f897", "metadata": {}, "outputs": [ @@ -471,7 +547,7 @@ "'alpha must be > 0 [0]'" ] }, - "execution_count": 98, + "execution_count": 102, "metadata": {}, "output_type": "execute_result" } @@ -482,7 +558,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 103, "id": "c4630e2a-b577-410b-8251-1c327866c9f1", "metadata": {}, "outputs": [], @@ -493,7 +569,7 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 104, "id": "c8c740c3-3ffb-4694-95dc-2932b7d39c6c", "metadata": {}, "outputs": [ @@ -503,7 +579,7 @@ "\"(34, 'Result too large')\"" ] }, - "execution_count": 100, + "execution_count": 104, "metadata": {}, "output_type": "execute_result" } @@ -524,7 +600,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 105, "id": "fd739374-c01b-432e-ab80-98e35fda3674", "metadata": {}, "outputs": [ @@ -534,7 +610,7 @@ "ConstantProductCurve(k=20000.0, x=100, x_act=100, y_act=200.0, alpha=0.5, pair='TKNB/TKNQ', cid='None', fee=None, descr=None, constr='xyal', params={})" ] }, - "execution_count": 101, + "execution_count": 105, "metadata": {}, "output_type": "execute_result" } @@ -547,7 +623,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 106, "id": "927269a5-0c16-4336-8fcf-3e22392c8847", "metadata": {}, "outputs": [ @@ -557,7 +633,7 @@ "ConstantProductCurve(k=800000000.0, x=100, x_act=100, y_act=199.99999999999994, alpha=0.25, pair='TKNB/TKNQ', cid='None', fee=None, descr=None, constr='xyal', params={})" ] }, - "execution_count": 102, + "execution_count": 106, "metadata": {}, "output_type": "execute_result" } @@ -595,7 +671,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 107, "id": "30239c29-9c2d-4a35-b24e-593e27d23013", "metadata": {}, "outputs": [], @@ -614,7 +690,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 108, "id": "31973a9d-f6fc-4ba6-8660-3ca6b3b31238", "metadata": {}, "outputs": [], @@ -625,7 +701,7 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 109, "id": "6da717da-0e96-4af5-a011-74c54c5a8033", "metadata": {}, "outputs": [], @@ -644,7 +720,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 110, "id": "22c10615-7636-4ec9-ba02-01e9c58ad20d", "metadata": {}, "outputs": [], @@ -655,7 +731,7 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 111, "id": "b63b25fa-8f4a-415d-b0c5-8d10be06f91b", "metadata": {}, "outputs": [], @@ -666,7 +742,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 112, "id": "1e2a97a5-240b-49b4-abdc-f30b3b1e2788", "metadata": {}, "outputs": [], @@ -677,7 +753,7 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": 113, "id": "98262a43-8c8e-4eb2-af03-6c7f082a9a5e", "metadata": {}, "outputs": [], @@ -688,7 +764,7 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": 114, "id": "b3049924-bc62-44c3-b7fc-0c5513544b87", "metadata": {}, "outputs": [], @@ -699,7 +775,7 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": 115, "id": "9809fe75-81eb-4117-b06f-ea80e8e5d1af", "metadata": {}, "outputs": [], @@ -718,7 +794,7 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": 116, "id": "d53b7a88-b1b2-488f-b33e-cf85af69dad2", "metadata": {}, "outputs": [], @@ -729,7 +805,7 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 117, "id": "47628578-dfab-4a28-a46f-b83b12af7745", "metadata": {}, "outputs": [], @@ -740,7 +816,7 @@ }, { "cell_type": "code", - "execution_count": 114, + "execution_count": 118, "id": "818af0e4", "metadata": {}, "outputs": [], @@ -751,7 +827,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": 119, "id": "bac20004", "metadata": {}, "outputs": [], @@ -762,7 +838,7 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 120, "id": "490db431", "metadata": {}, "outputs": [], @@ -773,7 +849,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 121, "id": "bc7fda17", "metadata": {}, "outputs": [], @@ -784,7 +860,7 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 122, "id": "d1637e16-ae56-45f6-bb90-b10cd5e83194", "metadata": {}, "outputs": [], @@ -803,7 +879,7 @@ }, { "cell_type": "code", - "execution_count": 119, + "execution_count": 123, "id": "4a70d47e-3a89-452d-80f3-d69028433648", "metadata": {}, "outputs": [], @@ -814,7 +890,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 124, "id": "a1bbada5-9cd2-4dd0-a226-fb7422614b53", "metadata": {}, "outputs": [], @@ -833,7 +909,7 @@ }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 125, "id": "d90a02d0-21c9-47dc-894c-82ea93b01779", "metadata": {}, "outputs": [], @@ -844,7 +920,7 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": 126, "id": "052b8feb-038e-457a-a3f1-8ff25b030a0e", "metadata": {}, "outputs": [], @@ -855,7 +931,7 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": 127, "id": "8872ba9c-2186-4176-add5-4a36b66162a4", "metadata": {}, "outputs": [], @@ -866,7 +942,7 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": 128, "id": "795eac9c-fe49-447f-b3fe-c0268cdd20de", "metadata": {}, "outputs": [], @@ -877,7 +953,7 @@ }, { "cell_type": "code", - "execution_count": 125, + "execution_count": 129, "id": "13b1f308-3062-4872-a91e-e89bccf17cf8", "metadata": {}, "outputs": [], @@ -889,7 +965,7 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 130, "id": "b90b56dd-bc10-4087-a3f4-5c0b8bcc681a", "metadata": {}, "outputs": [], @@ -901,7 +977,7 @@ }, { "cell_type": "code", - "execution_count": 127, + "execution_count": 131, "id": "9b797a1f-2d04-42ae-a03f-4b9675a65ed3", "metadata": {}, "outputs": [], @@ -913,7 +989,7 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 132, "id": "ee7cd754-fe9c-4fb8-848d-46dc2850c0cd", "metadata": {}, "outputs": [], @@ -933,7 +1009,7 @@ }, { "cell_type": "code", - "execution_count": 129, + "execution_count": 133, "id": "6373dfe5-6d20-4c55-9f0b-80a8ac6e1e05", "metadata": {}, "outputs": [], @@ -945,7 +1021,7 @@ }, { "cell_type": "code", - "execution_count": 130, + "execution_count": 134, "id": "81cb94ae-1fd1-4d63-9f59-e79274737d4d", "metadata": {}, "outputs": [], @@ -969,7 +1045,7 @@ }, { "cell_type": "code", - "execution_count": 131, + "execution_count": 135, "id": "0eac45f7-ef6d-419d-8254-098858e7a529", "metadata": {}, "outputs": [], @@ -993,7 +1069,7 @@ }, { "cell_type": "code", - "execution_count": 132, + "execution_count": 136, "id": "cc39342b-1304-4e7d-8594-950df18ffb6e", "metadata": {}, "outputs": [], @@ -1017,7 +1093,7 @@ }, { "cell_type": "code", - "execution_count": 133, + "execution_count": 137, "id": "80e3f97b-41b0-4263-b2e0-b02da963c60f", "metadata": {}, "outputs": [], @@ -1032,7 +1108,7 @@ }, { "cell_type": "code", - "execution_count": 134, + "execution_count": 138, "id": "593fb93f-5d95-4796-9da6-abd79206ff3d", "metadata": {}, "outputs": [], @@ -1060,7 +1136,7 @@ }, { "cell_type": "code", - "execution_count": 135, + "execution_count": 139, "id": "171b56b5-f270-4f51-8fd8-a185b3c0e91f", "metadata": {}, "outputs": [], @@ -1084,7 +1160,7 @@ }, { "cell_type": "code", - "execution_count": 136, + "execution_count": 140, "id": "01d3e5e2-aba4-434d-a118-15bd63e854db", "metadata": {}, "outputs": [], @@ -1096,7 +1172,7 @@ }, { "cell_type": "code", - "execution_count": 137, + "execution_count": 141, "id": "0ae9f182-0f46-4fdd-9879-619cf3f6ecd2", "metadata": {}, "outputs": [], @@ -1120,7 +1196,7 @@ }, { "cell_type": "code", - "execution_count": 138, + "execution_count": 142, "id": "9ff8935e-1898-40b9-802f-09a7b2cc608c", "metadata": {}, "outputs": [], @@ -1133,7 +1209,7 @@ }, { "cell_type": "code", - "execution_count": 139, + "execution_count": 143, "id": "081ec8c2-768c-45a5-a478-1df3a55590ab", "metadata": {}, "outputs": [], @@ -1146,7 +1222,7 @@ }, { "cell_type": "code", - "execution_count": 140, + "execution_count": 144, "id": "5f556205-299f-4f5c-b717-076a64137784", "metadata": {}, "outputs": [], @@ -1173,7 +1249,7 @@ }, { "cell_type": "code", - "execution_count": 141, + "execution_count": 145, "id": "bfbc589d-2124-4c51-9ac9-a7a3034ebd67", "metadata": {}, "outputs": [], @@ -1186,7 +1262,7 @@ }, { "cell_type": "code", - "execution_count": 142, + "execution_count": 146, "id": "fb276d33-72b0-406d-9f3d-9ce16354098b", "metadata": {}, "outputs": [], @@ -1199,7 +1275,7 @@ }, { "cell_type": "code", - "execution_count": 143, + "execution_count": 147, "id": "d55dd588-8a49-43c5-bf23-22fa207876fd", "metadata": {}, "outputs": [], @@ -1230,7 +1306,7 @@ }, { "cell_type": "code", - "execution_count": 144, + "execution_count": 148, "id": "eb60778c-edd0-42c5-8681-a259e02c1e91", "metadata": {}, "outputs": [], @@ -1242,7 +1318,7 @@ }, { "cell_type": "code", - "execution_count": 145, + "execution_count": 149, "id": "c242f036-c366-4c25-ab1d-9b13ff283d60", "metadata": {}, "outputs": [], @@ -1254,7 +1330,7 @@ }, { "cell_type": "code", - "execution_count": 146, + "execution_count": 150, "id": "1e39b3b6-ac20-4a7d-ab4a-f764cc949ee9", "metadata": {}, "outputs": [], @@ -1283,7 +1359,7 @@ }, { "cell_type": "code", - "execution_count": 147, + "execution_count": 151, "id": "8fd1d683-195a-414b-bfa2-2155462efcc1", "metadata": {}, "outputs": [], @@ -1296,7 +1372,7 @@ }, { "cell_type": "code", - "execution_count": 148, + "execution_count": 152, "id": "c2a77208-d1d6-4d99-8fab-d8b9f988dac9", "metadata": {}, "outputs": [], @@ -1322,7 +1398,7 @@ }, { "cell_type": "code", - "execution_count": 149, + "execution_count": 153, "id": "30a07eda-449f-471e-a609-f979d894d09a", "metadata": {}, "outputs": [], @@ -1348,7 +1424,7 @@ }, { "cell_type": "code", - "execution_count": 150, + "execution_count": 154, "id": "8826b280-00f7-4771-9bc6-2d5d344b197e", "metadata": {}, "outputs": [], @@ -1369,7 +1445,7 @@ }, { "cell_type": "code", - "execution_count": 151, + "execution_count": 155, "id": "853233ca-22bd-4d0e-a6d1-3dee13341b99", "metadata": {}, "outputs": [], @@ -1392,7 +1468,7 @@ }, { "cell_type": "code", - "execution_count": 152, + "execution_count": 156, "id": "73dfd68b-56ba-4154-a1f8-79c8bddf4169", "metadata": {}, "outputs": [], @@ -1403,7 +1479,7 @@ }, { "cell_type": "code", - "execution_count": 153, + "execution_count": 157, "id": "bf52087a-ffb7-4dad-bffa-753983249b51", "metadata": {}, "outputs": [], @@ -1418,7 +1494,7 @@ }, { "cell_type": "code", - "execution_count": 154, + "execution_count": 158, "id": "c3e74ed7-83cb-497d-82a8-4a8d38bf312d", "metadata": {}, "outputs": [ @@ -1446,7 +1522,7 @@ }, { "cell_type": "code", - "execution_count": 155, + "execution_count": 159, "id": "dcf6c394-8ad0-4ea1-a0fe-849374b16110", "metadata": {}, "outputs": [ @@ -1481,7 +1557,7 @@ }, { "cell_type": "code", - "execution_count": 156, + "execution_count": 160, "id": "3c8cbd59-5039-43bc-a52d-f05201e9e83b", "metadata": {}, "outputs": [ diff --git a/resources/NBTest/NBTest_051_CPCBalancer.py b/resources/NBTest/NBTest_051_CPCBalancer.py index 04bcea464..3557fd4a4 100644 --- a/resources/NBTest/NBTest_051_CPCBalancer.py +++ b/resources/NBTest/NBTest_051_CPCBalancer.py @@ -15,11 +15,11 @@ # --- # + -from fastlane_bot.tools.cpc import ConstantProductCurve as CPC +from fastlane_bot.tools.cpc import ConstantProductCurve as CPC, CurveBase from fastlane_bot.testing import * +# from flbtools.cpc import ConstantProductCurve as CPC, CurveBase +# from flbtesting import * -#from flbtools.cpc import ConstantProductCurve as CPC -#from flbtesting import * from math import sqrt print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(CPC)) # from fastlane_bot import __VERSION__ @@ -33,6 +33,10 @@ c0 = CPC.from_xy(100, 200) assert c0.tknx == "TKNB" assert c0.tkny == "TKNQ" +k0 = c0.invariant() +assert iseq(k0, sqrt(100*200)) +k1, k2 = c0.invariant(include_target=True) +assert iseq(k0, k1, k2) x,y,_ = c0.xyfromp_f(c0.p) xvec = c0.xvecfrompvec_f({c0.tknx: c0.p, c0.tkny: 1} ) @@ -40,6 +44,7 @@ assert iseq(y, 200) assert iseq(xvec[c0.tknx], x) assert iseq(xvec[c0.tkny], y) +assert iseq(c0.invariant(), c0.invariant(xvec)) assert raises(c0.xvecfrompvec_f, {c0.tknx: c0.p} ).startswith("pvec must contain") assert raises(c0.xvecfrompvec_f, {c0.tkny: 1} ).startswith("pvec must contain") @@ -54,6 +59,7 @@ assert iseq(y, 244.9489742783178) assert iseq(xvec[c0.tknx], x) assert iseq(xvec[c0.tkny], y) +assert iseq(c0.invariant(), c0.invariant(xvec)) dx,dy,_ = c0.dxdyfromp_f(c0.p) dxvec = c0.dxvecfrompvec_f({c0.tknx: c0.p, c0.tkny: 1} ) @@ -76,6 +82,39 @@ assert iseq(dxvec[c0.tknx], dx) assert iseq(dxvec[c0.tkny], dy) + +# ## CurveBase + +# Checking that `CurveBase` can only instantiate with all functions defined + +# + +class CB1(CurveBase): + pass + +class CB2(CurveBase): + def dxvecfrompvec_f(self, pvec, *, ignorebounds=False): + pass + +class CB3(CurveBase): + def xvecfrompvec_f(self, pvec, *, ignorebounds=False): + pass + +class CB4(CurveBase): + def xvecfrompvec_f(self, pvec, *, ignorebounds=False): + pass + def dxvecfrompvec_f(self, pvec, *, ignorebounds=False): + pass + def invariant(self, xvec=None, *, include_target=False): + pass + +assert raises(CB1).startswith("Can't instantiate abstract class") +assert raises(CB2).startswith("Can't instantiate abstract class") +assert raises(CB3).startswith("Can't instantiate abstract class") +assert not raises(CB4) +# - + +assert isinstance(CPC.from_xy(100, 200), CurveBase) + # ## Constant product constructor c0 = CPC.from_xy(100, 200) @@ -152,14 +191,18 @@ assert c0.alpha == 0.5 assert c0.eta == 1 assert c0.constr == "xy" +assert iseq(c0.invariant(), c0.kbar) assert c0.is_constant_product() == True c0 + + c1 = CPC.from_xyal(100, 200) assert c1.constr == "xyal" assert c1.is_constant_product() == True assert c1 == c0 assert c1.asdict()["alpha"] == 0.5 +assert iseq(c1.invariant(), c1.kbar) c1 c2 = CPC.from_xyal(100, 200, alpha=0.25) @@ -168,6 +211,7 @@ assert c2.alpha == 0.25 assert c2.asdict()["alpha"] == 0.25 assert iseq(c2.eta, 0.25/0.75) +assert iseq(c2.invariant(), c2.kbar) assert c2 != c0 assert c2 != c1 c2 @@ -178,6 +222,7 @@ assert iseq(c3.alpha, 0.8) assert c3.asdict()["alpha"] == 0.8 assert iseq(c3.eta, 0.8/0.2) +assert iseq(c3.invariant(), c3.kbar) assert c3 != c0 assert c3 != c1 assert c3 != c2