consolidation process added
This commit is contained in:
46
testy/moduleTests/backtestInterfaceTest.py
Normal file
46
testy/moduleTests/backtestInterfaceTest.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
from v2realbot.config import Keys, get_key
|
||||||
|
from v2realbot.enums.enums import Mode, Account, OrderSide
|
||||||
|
from alpaca.trading.enums import OrderSide, OrderStatus, TradeEvent, OrderType
|
||||||
|
from v2realbot.common.model import TradeUpdate, Order
|
||||||
|
from v2realbot.interfaces.live_interface import LiveInterface
|
||||||
|
from v2realbot.interfaces.backtest_interface import BacktestInterface
|
||||||
|
from v2realbot.backtesting.backtester import Backtester
|
||||||
|
from datetime import datetime
|
||||||
|
from v2realbot.utils.utils import zoneNY
|
||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
def callback():
|
||||||
|
print("callback entry")
|
||||||
|
|
||||||
|
|
||||||
|
start = datetime(2023, 4, 10, 9, 30, 0, 0, tzinfo=zoneNY)
|
||||||
|
end = datetime(2023, 4, 10, 9, 35, 0, 0, tzinfo=zoneNY)
|
||||||
|
btdata: list = []
|
||||||
|
cash=10000
|
||||||
|
#key = get_key(mode=Mode.PAPER, account=Account.ACCOUNT1)
|
||||||
|
symbol = "BAC"
|
||||||
|
|
||||||
|
bt = Backtester(symbol = symbol, order_fill_callback= callback, btdata=btdata, cash=cash, bp_from=start, bp_to=end)
|
||||||
|
|
||||||
|
bt.open_orders = [Order(id=uuid4(),
|
||||||
|
submitted_at = datetime(2023, 3, 17, 9, 30, 0, 0, tzinfo=zoneNY),
|
||||||
|
symbol = "BAC",
|
||||||
|
qty = 1,
|
||||||
|
status = OrderStatus.ACCEPTED,
|
||||||
|
order_type = OrderType.LIMIT,
|
||||||
|
side = OrderSide.SELL,
|
||||||
|
limit_price=22.4),
|
||||||
|
Order(id=uuid4(),
|
||||||
|
submitted_at = datetime(2023, 3, 17, 9, 30, 00, 0, tzinfo=zoneNY),
|
||||||
|
symbol = "BAC",
|
||||||
|
qty = 1,
|
||||||
|
order_type = OrderType.MARKET,
|
||||||
|
status = OrderStatus.ACCEPTED,
|
||||||
|
side = OrderSide.BUY)]
|
||||||
|
|
||||||
|
bt_interface = BacktestInterface(symbol=symbol, bt=bt)
|
||||||
|
|
||||||
|
orderlist = bt.get_open_orders(symbol=symbol, side=None)
|
||||||
|
|
||||||
|
print(orderlist)
|
||||||
|
print(len(orderlist))
|
||||||
57
testy/moduleTests/liveInterfaceTest.py
Normal file
57
testy/moduleTests/liveInterfaceTest.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
from v2realbot.config import Keys, get_key
|
||||||
|
from v2realbot.enums.enums import Mode, Account, OrderSide
|
||||||
|
from v2realbot.interfaces.live_interface import LiveInterface
|
||||||
|
from msgpack import packb, unpackb
|
||||||
|
key = get_key(mode=Mode.PAPER, account=Account.ACCOUNT1)
|
||||||
|
symbol = "BAC"
|
||||||
|
li = LiveInterface(symbol=symbol, key=key)
|
||||||
|
|
||||||
|
|
||||||
|
##tady jsem skoncil - otestovat tento kod na variantach
|
||||||
|
# pendinbuys na PAPER
|
||||||
|
#pak dat do Vykladaci ENTRY a otestovat i na BT
|
||||||
|
data = {}
|
||||||
|
data["index"] = 30
|
||||||
|
consolidation_bar_count = 10
|
||||||
|
pendingbuys = {'22dd3fe21-2c61-4ddd-b7df-cbdb3c1f7b79': '29.63', 'fe7b4baa-ef61-4867-b111-b1c3fc016dce': '29.59', '40253d42-bc00-4476-8f7a-28449fc00080': '29.57'}
|
||||||
|
|
||||||
|
##konzolidace kazdy Nty bar dle nastaveni
|
||||||
|
if int(data["index"])%int(consolidation_bar_count) == 0:
|
||||||
|
print("***Consolidation ENTRY***")
|
||||||
|
|
||||||
|
orderlist = li.get_open_orders(symbol=symbol, side=None)
|
||||||
|
#print(orderlist)
|
||||||
|
pendingbuys_new = {}
|
||||||
|
limitka = None
|
||||||
|
jevylozeno = 1
|
||||||
|
for o in orderlist:
|
||||||
|
if o.side == OrderSide.SELL:
|
||||||
|
print("Puvodni LIMITKA", limitka)
|
||||||
|
limitka = o.id
|
||||||
|
print("Přepsaná LIMITKA", limitka)
|
||||||
|
if o.side == OrderSide.BUY:
|
||||||
|
pendingbuys_new[str(o.id)]=o.limit_price
|
||||||
|
|
||||||
|
if pendingbuys_new != pendingbuys:
|
||||||
|
print("ROZDILNA PENDINGBUYS přepsána")
|
||||||
|
print("OLD",pendingbuys)
|
||||||
|
pendingbuys = unpackb(packb(pendingbuys_new))
|
||||||
|
print("NEW", pendingbuys)
|
||||||
|
print("OLD jevylozeno",jevylozeno)
|
||||||
|
if len(pendingbuys) > 0:
|
||||||
|
jevylozeno = 1
|
||||||
|
else:
|
||||||
|
jevylozeno = 0
|
||||||
|
print("NEW jevylozeno",jevylozeno)
|
||||||
|
|
||||||
|
#print(limitka)
|
||||||
|
#print(pendingbuys_new)
|
||||||
|
#print(pendingbuys)
|
||||||
|
#print(len(pendingbuys))
|
||||||
|
#print(len(pendingbuys_new))
|
||||||
|
#print(jevylozeno)
|
||||||
|
print("***CONSOLIDATION EXIT***")
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("no time for consolidation", data["index"])
|
||||||
|
|
||||||
@ -2,13 +2,14 @@ import os,sys
|
|||||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
from v2realbot.strategy.base import StrategyState
|
from v2realbot.strategy.base import StrategyState
|
||||||
from v2realbot.strategy.StrategyOrderLimitVykladaci import StrategyOrderLimitVykladaci
|
from v2realbot.strategy.StrategyOrderLimitVykladaci import StrategyOrderLimitVykladaci
|
||||||
from v2realbot.enums.enums import RecordType, StartBarAlign, Mode, Account
|
from v2realbot.enums.enums import RecordType, StartBarAlign, Mode, Account, OrderSide
|
||||||
from v2realbot.indicators.indicators import ema
|
from v2realbot.indicators.indicators import ema
|
||||||
from v2realbot.utils.utils import ltp, isrising, isfalling,trunc,AttributeDict, zoneNY, price2dec, dict_replace_value
|
from v2realbot.utils.utils import ltp, isrising, isfalling,trunc,AttributeDict, zoneNY, price2dec, dict_replace_value
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from icecream import install, ic
|
from icecream import install, ic
|
||||||
from rich import print
|
from rich import print
|
||||||
from threading import Event
|
from threading import Event
|
||||||
|
from msgpack import packb, unpackb
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
import tomli
|
import tomli
|
||||||
@ -22,6 +23,10 @@ Vykladaci strategie refactored z původního engine
|
|||||||
Params:
|
Params:
|
||||||
(maxpozic = 200, chunk = 50, MA = 6, Trend = 6, profit = 0.02, lastbuyindex=-6, pendingbuys={},limitka = None, jevylozeno=0, ticks2reset = 0.04, blockbuy=0)
|
(maxpozic = 200, chunk = 50, MA = 6, Trend = 6, profit = 0.02, lastbuyindex=-6, pendingbuys={},limitka = None, jevylozeno=0, ticks2reset = 0.04, blockbuy=0)
|
||||||
|
|
||||||
|
Pozor na symbolu nesmi byt dalsi cizí otevrene objednavky:
|
||||||
|
Pravidelny konzolidacni process da SELL order da do limitka, BUY - do pole pendingbuys
|
||||||
|
|
||||||
|
consolidation_bar_count - pocet baru po kterych se triggeruje konzolidační proces
|
||||||
|
|
||||||
Více nakupuje oproti Dokupovaci. Tady vylozime a nakupujeme 5 pozic hned. Pri dokupovaci se dokupuje, az na zaklade dalsich triggeru.
|
Více nakupuje oproti Dokupovaci. Tady vylozime a nakupujeme 5 pozic hned. Pri dokupovaci se dokupuje, az na zaklade dalsich triggeru.
|
||||||
Do budoucna vice ridit nakupy pri klesani - napr. vyložení jen 2-3 pozic a další dokupy až po triggeru.
|
Do budoucna vice ridit nakupy pri klesani - napr. vyložení jen 2-3 pozic a další dokupy až po triggeru.
|
||||||
@ -39,7 +44,8 @@ stratvars = AttributeDict(maxpozic = 250,
|
|||||||
vykladka=5,
|
vykladka=5,
|
||||||
curve = [0.01, 0.01, 0.01, 0, 0.02, 0.02, 0.01,0.01, 0.01,0.03, 0.01, 0.01, 0.01,0.04, 0.01,0.01, 0.01,0.05, 0.01,0.01, 0.01,0.01, 0.06,0.01, 0.01,0.01, 0.01],
|
curve = [0.01, 0.01, 0.01, 0, 0.02, 0.02, 0.01,0.01, 0.01,0.03, 0.01, 0.01, 0.01,0.04, 0.01,0.01, 0.01,0.05, 0.01,0.01, 0.01,0.01, 0.06,0.01, 0.01,0.01, 0.01],
|
||||||
blockbuy = 0,
|
blockbuy = 0,
|
||||||
ticks2reset = 0.04)
|
ticks2reset = 0.04,
|
||||||
|
consolidation_bar_count = 10)
|
||||||
##toto rozparsovat a strategii spustit stejne jako v main
|
##toto rozparsovat a strategii spustit stejne jako v main
|
||||||
toml_string = """
|
toml_string = """
|
||||||
[[strategies]]
|
[[strategies]]
|
||||||
@ -171,6 +177,7 @@ def next(data, state: StrategyState):
|
|||||||
res = asyncio.run(state.cancel_pending_buys())
|
res = asyncio.run(state.cancel_pending_buys())
|
||||||
|
|
||||||
|
|
||||||
|
#PENDING BUYS SPENT - PART
|
||||||
#pokud mame vylozeno a pendingbuys se vyklepou a
|
#pokud mame vylozeno a pendingbuys se vyklepou a
|
||||||
# 1 vykladame idned znovu
|
# 1 vykladame idned znovu
|
||||||
# vyloz()
|
# vyloz()
|
||||||
@ -183,7 +190,57 @@ def next(data, state: StrategyState):
|
|||||||
state.vars.blockbuy = 0
|
state.vars.blockbuy = 0
|
||||||
state.vars.jevylozeno = 0
|
state.vars.jevylozeno = 0
|
||||||
|
|
||||||
#TODO toto dodelat
|
#TODO toto dodelat konzolidaci a mozna lock na limitku a pendingbuys a jevylozeno ??
|
||||||
|
|
||||||
|
#kdykoliv se muze notifikace ztratit
|
||||||
|
# - pendingbuys - vsechny open orders buy
|
||||||
|
# - limitka - open order sell
|
||||||
|
|
||||||
|
##CONSOLIDATION PART kazdy Nty bar dle nastaveni
|
||||||
|
if int(data["index"])%int(state.vars.consolidation_bar_count) == 0:
|
||||||
|
print("***Consolidation ENTRY***")
|
||||||
|
|
||||||
|
orderlist = state.interface.get_open_orders(symbol=state.symbol, side=None)
|
||||||
|
#print(orderlist)
|
||||||
|
pendingbuys_new = {}
|
||||||
|
limitka_old = state.vars.limitka
|
||||||
|
print("Puvodni LIMITKA", limitka_old)
|
||||||
|
state.vars.limitka = None
|
||||||
|
for o in orderlist:
|
||||||
|
if o.side == OrderSide.SELL:
|
||||||
|
state.vars.limitka = o.id
|
||||||
|
if o.side == OrderSide.BUY:
|
||||||
|
pendingbuys_new[str(o.id)]=o.limit_price
|
||||||
|
|
||||||
|
print("Nová LIMITKA", state.vars.limitka)
|
||||||
|
if pendingbuys_new != state.vars.pendingbuys:
|
||||||
|
print("ROZDILNA PENDINGBUYS přepsána")
|
||||||
|
print("OLD",state.vars.pendingbuys)
|
||||||
|
state.vars.pendingbuys = unpackb(packb(pendingbuys_new))
|
||||||
|
print("NEW", state.vars.pendingbuys)
|
||||||
|
else:
|
||||||
|
print("PENDINGBUYS sedí - necháváme", state.vars.pendingbuys)
|
||||||
|
print("OLD jevylozeno", state.vars.jevylozeno)
|
||||||
|
if len(state.vars.pendingbuys) > 0:
|
||||||
|
state.vars.jevylozeno = 1
|
||||||
|
else:
|
||||||
|
state.vars.jevylozeno = 0
|
||||||
|
print("NEW jevylozeno", state.vars.jevylozeno)
|
||||||
|
|
||||||
|
#print(limitka)
|
||||||
|
#print(pendingbuys_new)
|
||||||
|
#print(pendingbuys)
|
||||||
|
#print(len(pendingbuys))
|
||||||
|
#print(len(pendingbuys_new))
|
||||||
|
#print(jevylozeno)
|
||||||
|
print("***CONSOLIDATION EXIT***")
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("no time for consolidation", data["index"])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#pokud je vylozeno a mame pozice a neexistuje limitka - pak ji vytvorim
|
#pokud je vylozeno a mame pozice a neexistuje limitka - pak ji vytvorim
|
||||||
# if int(state.oe.poz)>0 and state.oe.limitka == 0:
|
# if int(state.oe.poz)>0 and state.oe.limitka == 0:
|
||||||
# #pro jistotu updatujeme pozice
|
# #pro jistotu updatujeme pozice
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@ -516,8 +516,10 @@ class Backtester:
|
|||||||
res = []
|
res = []
|
||||||
#with lock:
|
#with lock:
|
||||||
for o in self.open_orders:
|
for o in self.open_orders:
|
||||||
if str(o.side) == side and o.symbol == symbol:
|
#print(o)
|
||||||
res.append(o)
|
if o.symbol == symbol:
|
||||||
|
if side is None or o.side == side:
|
||||||
|
res.append(o)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def display_backtest_result(self, state):
|
def display_backtest_result(self, state):
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@ -7,7 +7,7 @@ from alpaca.trading.enums import OrderSide, TimeInForce, OrderClass, OrderStatus
|
|||||||
from alpaca.trading.models import Order, Position
|
from alpaca.trading.models import Order, Position
|
||||||
from alpaca.common.exceptions import APIError
|
from alpaca.common.exceptions import APIError
|
||||||
from v2realbot.config import Keys
|
from v2realbot.config import Keys
|
||||||
from interfaces.general_interface import GeneralInterface
|
from v2realbot.interfaces.general_interface import GeneralInterface
|
||||||
"""""
|
"""""
|
||||||
Live interface with Alpaca for LIVE and PAPER trading.
|
Live interface with Alpaca for LIVE and PAPER trading.
|
||||||
"""""
|
"""""
|
||||||
@ -149,7 +149,7 @@ class LiveInterface(GeneralInterface):
|
|||||||
#order doesnt exist
|
#order doesnt exist
|
||||||
if e.code == 40410000: return 0,0
|
if e.code == 40410000: return 0,0
|
||||||
else:
|
else:
|
||||||
print("nepovedlo se zrusit objednavku")
|
print("nepovedlo se zrusit objednavku", str(e))
|
||||||
#raise Exception(e)
|
#raise Exception(e)
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
|||||||
@ -31,7 +31,7 @@ ic.configureOutput(includeContext=True)
|
|||||||
def threadName():
|
def threadName():
|
||||||
return '%s |> ' % str(current_thread().name)
|
return '%s |> ' % str(current_thread().name)
|
||||||
ic.configureOutput(prefix=threadName)
|
ic.configureOutput(prefix=threadName)
|
||||||
ic.disable()
|
#ic.disable()
|
||||||
"""""
|
"""""
|
||||||
Main entry point of the bot. Starts strategies according to config file, each
|
Main entry point of the bot. Starts strategies according to config file, each
|
||||||
in separate thread.
|
in separate thread.
|
||||||
|
|||||||
Reference in New Issue
Block a user