Skip to content

Commit

Permalink
Add websocket subscription symbols list
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Wu Fei authored and Carlos Wu Fei committed Oct 16, 2023
1 parent 01b6794 commit a0abfcd
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 4 deletions.
49 changes: 48 additions & 1 deletion api/research/controller.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from db import setup_db
from datetime import datetime
from time import sleep
from tools.handle_error import json_response, json_response_error
from tools.handle_error import json_response, json_response_error, json_response_message
from pymongo.errors import DuplicateKeyError
from apis import ThreeCommasApi
from tools.round_numbers import round_numbers
from pymongo import ASCENDING
from fastapi.encoders import jsonable_encoder

class Controller:
"""
Expand Down Expand Up @@ -134,4 +135,50 @@ def get_3commas_signals(self):

return json_response({"message": "Successfully retrieved profitable 3commas signals", "data": signals})

"""
Get pairs that binbot-research signals are subscribed to
receive cryptodata
To merge with blacklist
"""
def get_subscribed_symbols(self):
query_result = self.db.subscribed_symbols.find({}).sort("pair", ASCENDING)
all_symbols = list(query_result)
return json_response(
{"message": "Successfully retrieved blacklist", "data": all_symbols}
)

def delete_all_subscribed_symbols(self):
query_result = self.db.subscribed_symbols.delete_many({})

return json_response(
{"message": "Successfully deleted all symbols", "data": {
"total": 0
}}
)

def bulk_upsert_all(self, data):
symbols = jsonable_encoder(data)
try:
query_result = self.db.subscribed_symbols.update_many(
symbols,
upsert=True,
)
return json_response(
{"message": "Successfully created new susbcribed list", "data": {
"total": len(query_result.inserted_ids) + 1
}}
)
except Exception as error:
return json_response_error(f"Failed to update symbol in the subscribed list {error}")

def edit_subscribed_symbol(self, symbol):
symbol = jsonable_encoder(symbol)
try:
self.db.subscribed_symbols.update_one(
symbol,
upsert=True,
)
return json_response_message("Successfully update symbol in the subscribed list")
except Exception as error:
return json_response_error(f"Failed to update symbol in the subscribed list {error}")
16 changes: 15 additions & 1 deletion api/research/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from apis import ThreeCommasApi
from research.controller import Controller
from research.schemas import BlacklistSchema, BlacklistResponse
from research.schemas import BlacklistSchema, BlacklistResponse, SubscribedSymbolsSchema

research_blueprint = APIRouter()

Expand Down Expand Up @@ -42,3 +42,17 @@ def get_blacklisted():
@research_blueprint.get("/3commas-presets", tags=["blacklist and research"])
def three_commas_presets():
return ThreeCommasApi().get_marketplace_presets()


@research_blueprint.get("/subscribed", tags=["blacklist and research"])
def get_subscribed_symbols():
return Controller().get_subscribed_symbols()


@research_blueprint.post("/subscribed", tags=["blacklist and research"])
def create_subscribed_symbols(data: list[SubscribedSymbolsSchema]):
return Controller().bulk_upsert_all(data)

@research_blueprint.put("/subscribed/{symbol}", tags=["blacklist and research"])
def edit_subscribed_symbol(symbol: str):
return Controller().edit_subscribed_symbol(symbol)
25 changes: 25 additions & 0 deletions api/research/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,28 @@ class Config:

class BlacklistResponse(StandardResponse):
data: list[BlacklistSchema]


"""
Database control for symbols that are used
in signals.
"""
class SubscribedSymbolsSchema(BaseModel):
_id: str
pair: str
blacklisted: bool = False
blacklisted_reason: str = ""

class Config:
schema_extra = {
"example": {
"_id": "BNBBTC",
"pair": "BNBBTC",
"blacklisted": False,
"blacklisted_reason": "Overtraded"
}
}


class SubscribedSymbolsResponse(StandardResponse):
data: list[SubscribedSymbolsSchema]
1 change: 1 addition & 0 deletions web/.env
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ REACT_APP_TICKER=/account/ticker
REACT_APP_TICKER_24="https://api.binance.com/api/v3/ticker/24hr"
REACT_APP_RESEARCH_BLACKLIST=/research/blacklist
REACT_APP_RESEARCH_CONTROLLER=/autotrade-settings/bots
REACT_APP_SUBSCRIBED="/research/subscribed"
REACT_APP_NO_CANNIBALISM_SYMBOLS=/account/symbols/no-cannibal
REACT_APP_BINANCE_INFO="https://api.binance.com/api/v3/exchangeInfo"
REACT_APP_HEDGE_GBP=/account/hedge-gbp
Expand Down
24 changes: 24 additions & 0 deletions web/src/pages/research/ControllerTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const ControllerTab = ({
addToBlacklist,
removeFromBlacklist,
triggerGbpHedge,
subscribedSymbols,
}) => {
const [addBlacklist, setAddBlacklist] = useImmer({ reason: "", pair: "" });
const [removeBlacklist, setRemoveBlacklist] = useState("");
Expand Down Expand Up @@ -124,6 +125,29 @@ export const ControllerTab = ({
</Button>{" "}
</Col>
</Row>
<br />
<h3>Subscribed</h3>
<Row>
{subscribedSymbols && subscribedSymbols.length > 0 && (
<Col md="6">
<FormGroup>
<Label for="subscribed">Following list of cryptos are listened on by websockets</Label>
<Input
type="select"
name="subscribed"
id="blacklisted"
>
<option value={""}> </option>
{subscribedSymbols.map((x, i) => (
<option key={i} value={x._id}>
{x.pair} ({x.reason})
</option>
))}
</Input>
</FormGroup>
</Col>
)}
</Row>
</>
</Col>
</Row>
Expand Down
9 changes: 7 additions & 2 deletions web/src/pages/research/Research.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Nav, NavItem, NavLink, TabContent, TabPane } from "reactstrap";
import { addNotification, checkValue } from "../../validations";
import { loadCandlestick, getSymbols } from "../bots/actions";
import { getBalanceRaw } from "../../state/balances/actions";
import { getSubscribedListApi } from "./actions";
import {
getBlacklist,
addBlackList,
Expand All @@ -31,10 +32,12 @@ class Research extends React.Component {
};
}

componentDidMount = () => {
componentDidMount = async () => {
this.props.getBlacklist();
this.props.getSymbols();
this.props.getBalanceRaw();
const subscribedList = await getSubscribedListApi();
this.setState({ subscribedList: subscribedList.data })
};

componentDidUpdate = (p, s) => {
Expand Down Expand Up @@ -105,6 +108,7 @@ class Research extends React.Component {
<TabPane tabId="controllerTab">
<ControllerTab
blacklistData={this.state.blacklistData}
subscribedSymbols={this.state.subscribedList}
symbols={this.props.symbols}
addToBlacklist={(data) => {
this.props.addBlackList(data);
Expand Down Expand Up @@ -136,6 +140,7 @@ const mapStateToProps = (state) => {
symbols: symbols,
blacklistData: blacklistData,
balance_raw: balanceRaw,

};
};

Expand All @@ -145,5 +150,5 @@ export default connect(mapStateToProps, {
getBlacklist,
addBlackList,
deleteBlackList,
getBalanceRaw
getBalanceRaw,
})(Research);
9 changes: 9 additions & 0 deletions web/src/pages/research/actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import request from "../../request";
import { addNotification } from "../../validations";

export const GET_RESEARCH = "GET_RESEARCH";
Expand Down Expand Up @@ -129,3 +130,11 @@ export function deleteBlackListFailed() {
type: DELETE_BLACKLIST_ERROR
}
}

/**
* Websocket subscribed list of cryptos
*/
export async function getSubscribedListApi() {
const symbols = request(process.env.REACT_APP_SUBSCRIBED)
return symbols;
}
1 change: 1 addition & 0 deletions web/src/pages/research/saga.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,4 @@ export function* deleteBlacklistApi({ pair }) {
export function* watchDeleteBlackListApi() {
yield takeLatest(DELETE_BLACKLIST, deleteBlacklistApi);
}

0 comments on commit a0abfcd

Please sign in to comment.