inject stratvars
This commit is contained in:
50
testy/changeDict.py
Normal file
50
testy/changeDict.py
Normal file
@ -0,0 +1,50 @@
|
||||
from v2realbot.utils.utils import AttributeDict
|
||||
|
||||
stratvars_parsed_old = AttributeDict(maxpozic = 250,
|
||||
chunk = 10,
|
||||
MA = 3,
|
||||
Trend = 3,
|
||||
profit = 0.02,
|
||||
lastbuyindex=-6,
|
||||
pendingbuys={},
|
||||
limitka = None,
|
||||
jevylozeno=0,
|
||||
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],
|
||||
blockbuy = 0,
|
||||
ticks2reset = 0.04)
|
||||
|
||||
stratvars_parsed_new = AttributeDict(maxpozic = 250,
|
||||
chunk = 10,
|
||||
MA = 3,
|
||||
Trend = 3,
|
||||
profit = 0.02,
|
||||
lastbuyindex=-6,
|
||||
pendingbuys={},
|
||||
limitka = None,
|
||||
jevylozeno=0,
|
||||
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],
|
||||
blockbuy = 0,
|
||||
ticks2reset = 0.04)
|
||||
|
||||
|
||||
STRATVARS_UNCHANGEABLES = ['pendingbuys', 'blockbuy', 'jevylozeno', 'limitka']
|
||||
|
||||
changed_keys = []
|
||||
#get changed values
|
||||
for key,value in stratvars_parsed_new.items():
|
||||
if value != stratvars_parsed_old[key]:
|
||||
changed_keys.append(key)
|
||||
|
||||
print("changed before check", changed_keys)
|
||||
#remove keys that cannot be changed
|
||||
for k in changed_keys:
|
||||
if k in STRATVARS_UNCHANGEABLES:
|
||||
print(k, "cant be changed removing")
|
||||
changed_keys.remove(k)
|
||||
|
||||
print("clean changed keys", changed_keys)
|
||||
|
||||
for k in changed_keys:
|
||||
print("injecting",k, "value", stratvars_parsed_new[k])
|
||||
@ -4,31 +4,29 @@ print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from alpaca.data.historical import CryptoHistoricalDataClient, StockHistoricalDataClient
|
||||
from alpaca.data.requests import CryptoLatestTradeRequest, StockLatestTradeRequest, StockLatestBarRequest, StockTradesRequest
|
||||
from alpaca.data.enums import DataFeed
|
||||
from config import API_KEY, SECRET_KEY, MAX_BATCH_SIZE
|
||||
from v2realbot.config import ACCOUNT1_PAPER_API_KEY, ACCOUNT1_PAPER_SECRET_KEY
|
||||
from v2realbot.utils.utils import zoneNY
|
||||
from datetime import datetime, timezone, time, timedelta, date
|
||||
import pytz
|
||||
from rich import print
|
||||
|
||||
#práce s datumy
|
||||
|
||||
zone_NY = pytz.timezone('America/New_York')
|
||||
|
||||
parametry = {}
|
||||
symbol = ["BAC"]
|
||||
client = StockHistoricalDataClient(API_KEY, SECRET_KEY, raw_data=True)
|
||||
datetime_object_from = datetime(2023, 3, 16, 9, 30, 0, tzinfo=zone_NY)
|
||||
datetime_object_to = datetime(2023, 3, 16, 16, 00, 0, tzinfo=zone_NY)
|
||||
client = StockHistoricalDataClient(ACCOUNT1_PAPER_API_KEY, ACCOUNT1_PAPER_SECRET_KEY, raw_data=True)
|
||||
datetime_object_from = datetime(2023, 4, 12, 15, 45, 8, tzinfo=zoneNY)
|
||||
datetime_object_to = datetime(2023, 4, 12, 15, 45, 10, tzinfo=zoneNY)
|
||||
trades_request = StockTradesRequest(symbol_or_symbols=symbol, feed = DataFeed.SIP, start=datetime_object_from, end=datetime_object_to)
|
||||
|
||||
all_trades = client.get_stock_trades(trades_request)
|
||||
|
||||
#print(all_trades)
|
||||
print(all_trades)
|
||||
|
||||
print(len(all_trades['BAC']))
|
||||
# print(len(all_trades['BAC']))
|
||||
# print(all_trades['BAC'])
|
||||
|
||||
# for i in all_trades:
|
||||
# print(all_trades[i])
|
||||
|
||||
if __name__ == "__main__":
|
||||
# bar will be invoked if this module is being run directly, but not via import!
|
||||
print("hello")
|
||||
# if __name__ == "__main__":
|
||||
# # bar will be invoked if this module is being run directly, but not via import!
|
||||
# print("hello")
|
||||
@ -220,11 +220,11 @@ def main():
|
||||
name = os.path.basename(__file__)
|
||||
se = Event()
|
||||
pe = Event()
|
||||
s = StrategyOrderLimitVykladaci(name = name, symbol = "BAC", account=Account.ACCOUNT2, next=next, init=init, stratvars=stratvars, open_rush=40, close_rush=0, pe=pe, se=se)
|
||||
s.set_mode(mode = Mode.PAPER,
|
||||
s = StrategyOrderLimitVykladaci(name = name, symbol = "BAC", account=Account.ACCOUNT2, next=next, init=init, stratvars=stratvars, open_rush=10, close_rush=0, pe=pe, se=se)
|
||||
s.set_mode(mode = Mode.BT,
|
||||
debug = False,
|
||||
start = datetime(2023, 3, 30, 9, 30, 0, 0, tzinfo=zoneNY),
|
||||
end = datetime(2023, 3, 31, 16, 0, 0, 0, tzinfo=zoneNY),
|
||||
start = datetime(2023, 4, 10, 9, 30, 0, 0, tzinfo=zoneNY),
|
||||
end = datetime(2023, 4, 10, 9, 35, 0, 0, tzinfo=zoneNY),
|
||||
cash=100000)
|
||||
|
||||
#na sekundovem baru nezaokrouhlovat MAcko
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@ -43,7 +43,7 @@ from v2realbot.common.model import TradeUpdate, Order
|
||||
#from rich import print
|
||||
import threading
|
||||
import asyncio
|
||||
from v2realbot.config import BT_DELAYS
|
||||
from v2realbot.config import BT_DELAYS, DATA_DIR
|
||||
from v2realbot.utils.utils import AttributeDict, ltp, zoneNY, trunc, count_decimals,print
|
||||
from v2realbot.utils.tlog import tlog
|
||||
from datetime import datetime, timedelta
|
||||
@ -59,7 +59,6 @@ from v2realbot.utils.dash_save_html import make_static
|
||||
import dash_bootstrap_components as dbc
|
||||
from dash.dependencies import Input, Output
|
||||
from dash import dcc, html, dash_table, Dash
|
||||
from config import DATA_DIR
|
||||
""""
|
||||
LATENCY DELAYS
|
||||
.000 trigger - last_trade_time (.4246266)
|
||||
@ -563,8 +562,18 @@ class Backtester:
|
||||
hist_df = hist_df.set_index('time', drop=False)
|
||||
|
||||
#indicators
|
||||
#TODO vyresit if no indicators or no trades - pada na ValueError protoze pole obsahuje time, ale nikoliv indikatory
|
||||
#zatim jen takto workaround
|
||||
#print(state.indicators)
|
||||
try:
|
||||
ind_df = pd.DataFrame(state.indicators)
|
||||
ind_df = ind_df.set_index('time', drop=False)
|
||||
except ValueError as e:
|
||||
print("Value error", str(e))
|
||||
state.indicators = {'time': [] }
|
||||
ind_df = pd.DataFrame(state.indicators)
|
||||
ind_df = ind_df.set_index('time', drop=False)
|
||||
|
||||
#print("Indicators", ind_df)
|
||||
#print(state.indicators)
|
||||
|
||||
@ -801,7 +810,7 @@ class Backtester:
|
||||
if n_clicks == 0:
|
||||
return 'not saved'
|
||||
else:
|
||||
bt_dir = DATADIR + "/backtestresults/" + self.symbol + self.bp_from.strftime("%d-%m-%y-%H-%M-%S") + ' ' + self.bp_to.strftime("%d-%m-%y-%H-%M-%S") + ' ' + str(datetime.now().microsecond)
|
||||
bt_dir = DATA_DIR + "/backtestresults/" + self.symbol + self.bp_from.strftime("%d-%m-%y-%H-%M-%S") + ' ' + self.bp_to.strftime("%d-%m-%y-%H-%M-%S") + ' ' + str(datetime.now().microsecond)
|
||||
make_static(f'http://127.0.0.1:{port}/', bt_dir)
|
||||
return 'saved'
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@ from alpaca.data.enums import DataFeed
|
||||
from v2realbot.enums.enums import Mode, Account
|
||||
from appdirs import user_data_dir
|
||||
|
||||
STRATVARS_UNCHANGEABLES = ['pendingbuys', 'blockbuy', 'jevylozeno', 'limitka']
|
||||
DATA_DIR = user_data_dir("v2realbot")
|
||||
#BT DELAYS
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@ from v2realbot.common.model import StrategyInstance, Runner, RunRequest
|
||||
from v2realbot.utils.utils import AttributeDict, zoneNY, dict_replace_value, Store, parse_toml_string
|
||||
from datetime import datetime
|
||||
from threading import Thread, current_thread, Event, enumerate
|
||||
from v2realbot.config import STRATVARS_UNCHANGEABLES
|
||||
import importlib
|
||||
from queue import Queue
|
||||
db = Store()
|
||||
@ -89,25 +90,56 @@ def delete_stratin(id: UUID):
|
||||
return (0, i.id)
|
||||
return (-2, "not found")
|
||||
|
||||
def inject_stratvars(id: UUID, stratvars_parsed: AttributeDict):
|
||||
def inject_stratvars(id: UUID, stratvars_parsed_new: AttributeDict, stratvars_parsed_old: AttributeDict):
|
||||
for i in db.runners:
|
||||
if str(i.id) == str(id):
|
||||
i.run_instance.state.vars = AttributeDict(stratvars_parsed["stratvars"])
|
||||
i.run_instance.stratvars = AttributeDict(stratvars_parsed["stratvars"])
|
||||
return 0
|
||||
return -2
|
||||
#inject only those changed, some of them cannot be changed (for example pendingbuys)
|
||||
|
||||
changed_keys = []
|
||||
#get changed values
|
||||
for key,value in stratvars_parsed_new.items():
|
||||
if value != stratvars_parsed_old[key]:
|
||||
changed_keys.append(key)
|
||||
|
||||
#print("changed before check", changed_keys)
|
||||
#remove keys that cannot be changed
|
||||
for k in changed_keys:
|
||||
if k in STRATVARS_UNCHANGEABLES:
|
||||
#print(k, "cant be changed removing")
|
||||
changed_keys.remove(k)
|
||||
return -2, "Stratvar Key "+k+" cannot be changed"
|
||||
|
||||
#print("clean changed keys", changed_keys)
|
||||
#inject clean keys
|
||||
for k in changed_keys:
|
||||
print("INJECTING ",k, "value", stratvars_parsed_new[k])
|
||||
i.run_instance.state.vars[k] = stratvars_parsed_new[k]
|
||||
i.run_instance.stratvars[k] = stratvars_parsed_new[k]
|
||||
return 0, None
|
||||
return -2, "No runners found"
|
||||
|
||||
#allows change of set of parameters that are possible to change while it is running
|
||||
#also injects those parameters to instance
|
||||
def modify_stratin_running(si: StrategyInstance, id: UUID):
|
||||
try:
|
||||
#validate toml
|
||||
res,stp = parse_toml_string(si.stratvars_conf)
|
||||
if res < 0:
|
||||
return (-1, "stratvars invalid")
|
||||
return (-1, "new stratvars format invalid")
|
||||
for i in db.stratins:
|
||||
if str(i.id) == str(id):
|
||||
if not is_stratin_running(id=str(id)):
|
||||
return (-1, "not running")
|
||||
res,stp_old = parse_toml_string(i.stratvars_conf)
|
||||
if res < 0:
|
||||
return (-1, "current stratin stratvars invalid")
|
||||
#TODO reload running strat
|
||||
#print(stp)
|
||||
#print("starting injection", stp)
|
||||
res, msg = inject_stratvars(id=si.id, stratvars_parsed_new=stp["stratvars"], stratvars_parsed_old=stp_old["stratvars"])
|
||||
if res < 0:
|
||||
print("ajajaj inject se nepovedl", msg)
|
||||
return(-3, "inject failed: " + msg)
|
||||
i.id2 = si.id2
|
||||
i.name = si.name
|
||||
i.open_rush = si.open_rush
|
||||
@ -115,16 +147,11 @@ def modify_stratin_running(si: StrategyInstance, id: UUID):
|
||||
i.note = si.note
|
||||
i.history = si.history
|
||||
db.save()
|
||||
#TODO reload running strat
|
||||
print(stp)
|
||||
print("starting injection", stp)
|
||||
res = inject_stratvars(id=si.id, stratvars_parsed=stp)
|
||||
if res < 0:
|
||||
print("ajajaj inject se nepovedl")
|
||||
return(-3, "inject failed")
|
||||
return (0, i.id)
|
||||
return (-2, "not found")
|
||||
#controller.reload_params(si)
|
||||
except Exception as e:
|
||||
return (-2, "Error Exception" + str(e))
|
||||
|
||||
|
||||
##enable realtime chart - inject given queue for strategy instance
|
||||
##webservice listens to this queue
|
||||
@ -208,6 +235,7 @@ def capsule(target: object, db: object):
|
||||
reason = "SHUTDOWN OK"
|
||||
except Exception as e:
|
||||
reason = "SHUTDOWN Exception:" + str(e)
|
||||
print(reason)
|
||||
finally:
|
||||
# remove runners after thread is stopped and save results to stratin history
|
||||
for i in db.runners:
|
||||
|
||||
Binary file not shown.
@ -77,10 +77,11 @@ class TradeAggregator:
|
||||
#print(data)
|
||||
##implementing fitlers - zatim natvrdo a jen tyto: size: 1, cond in [O,C,4] opening,closed a derivately priced,
|
||||
## 22.3. - dal jsem pryc i contingency trades [' ', '7', 'V'] - nasel jsem obchod o 30c mimo
|
||||
## dán pryč P - prior reference time + 25centu mimo, {'t': '2023-04-12T19:45:08.63257344Z', 'x': 'D', 'p': 28.68, 's': 1000, 'c': [' ', 'P'], 'i': 71693108525109, 'z': 'A'},
|
||||
## Q - jsou v pohode, oteviraci trady, ale O jsou jejich duplikaty
|
||||
try:
|
||||
for i in data['c']:
|
||||
if i in ('C','O','4','B','7','V'): return 0
|
||||
if i in ('C','O','4','B','7','V','P'): return 0
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
|
||||
@ -228,7 +228,7 @@
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="cash" class="control-label">cash</label>
|
||||
<input type="number" class="form-control" id="cash" name="cash" placeholder="cash" value=0>
|
||||
<input type="number" class="form-control" id="cash" name="cash" placeholder="cash" value="100000">
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
|
||||
@ -3,7 +3,7 @@ var ws = null;
|
||||
function connect(event) {
|
||||
var runnerId = document.getElementById("runnerId")
|
||||
try {
|
||||
ws = new WebSocket("ws://localhost:8000/runners/" + runnerId.value + "/ws?api_key=" + API_KEY);
|
||||
ws = new WebSocket("ws://"+ window.location.hostname +":8000/runners/" + runnerId.value + "/ws?api_key=" + API_KEY);
|
||||
}
|
||||
catch (err) {
|
||||
console.log("nejaky error" + err)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@ -3,7 +3,7 @@ import os
|
||||
import requests
|
||||
from html.parser import HTMLParser
|
||||
import requests
|
||||
from config import DATA_DIR
|
||||
from v2realbot.config import DATA_DIR
|
||||
|
||||
def patch_file(file_path: str, content: bytes, extra: dict = None) -> bytes:
|
||||
if file_path == 'index.html':
|
||||
|
||||
@ -13,7 +13,7 @@ import os
|
||||
from v2realbot.common.model import StrategyInstance, Runner
|
||||
from typing import List
|
||||
import tomli
|
||||
from config import DATA_DIR
|
||||
from v2realbot.config import DATA_DIR
|
||||
|
||||
#datetime to timestamp
|
||||
def json_serial(obj):
|
||||
|
||||
Reference in New Issue
Block a user