@tradejs/connectors 3.1.28-beta.254 → 3.1.28-beta.256
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +83 -1
- package/dist/index.mjs +83 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -785,7 +785,7 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
785
785
|
tpSize: isFullMode ? void 0 : tpSizeStr,
|
|
786
786
|
tpslMode: isFullMode ? "Full" : "Partial",
|
|
787
787
|
takeProfit: tpPriceNorm.priceStr,
|
|
788
|
-
tpTriggerBy: "
|
|
788
|
+
tpTriggerBy: "LastPrice",
|
|
789
789
|
tpOrderType: "Market",
|
|
790
790
|
positionIdx: 0
|
|
791
791
|
});
|
|
@@ -880,6 +880,87 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
880
880
|
}
|
|
881
881
|
return isTradingStopAccepted(slRes);
|
|
882
882
|
};
|
|
883
|
+
const setPositionProtection = async ({
|
|
884
|
+
symbol,
|
|
885
|
+
direction,
|
|
886
|
+
qty,
|
|
887
|
+
takeProfits,
|
|
888
|
+
stopLossPrice
|
|
889
|
+
}) => {
|
|
890
|
+
const hasFullTakeProfit = takeProfits.length === 1 && takeProfits[0]?.rate === 1;
|
|
891
|
+
const hasStopLoss = typeof stopLossPrice === "number" && Number.isFinite(stopLossPrice);
|
|
892
|
+
if (hasFullTakeProfit && hasStopLoss) {
|
|
893
|
+
const client = await getPrivateClient();
|
|
894
|
+
const marketDataClient = await getPublicClient();
|
|
895
|
+
if (!client || !marketDataClient) {
|
|
896
|
+
return false;
|
|
897
|
+
}
|
|
898
|
+
const meta = await getSymbolMeta(marketDataClient, symbol);
|
|
899
|
+
const isLong = direction === "LONG";
|
|
900
|
+
const takeProfit = normalizePrice(
|
|
901
|
+
takeProfits[0].price,
|
|
902
|
+
isLong ? "TP_LONG" : "TP_SHORT",
|
|
903
|
+
meta
|
|
904
|
+
);
|
|
905
|
+
const stopLoss = normalizePrice(
|
|
906
|
+
stopLossPrice,
|
|
907
|
+
isLong ? "SL_LONG" : "SL_SHORT",
|
|
908
|
+
meta
|
|
909
|
+
);
|
|
910
|
+
const protectionRes = await client.setTradingStop({
|
|
911
|
+
category: BYBIT_CATEGORY2,
|
|
912
|
+
symbol,
|
|
913
|
+
tpslMode: "Full",
|
|
914
|
+
takeProfit: takeProfit.priceStr,
|
|
915
|
+
stopLoss: stopLoss.priceStr,
|
|
916
|
+
tpTriggerBy: "LastPrice",
|
|
917
|
+
slTriggerBy: "LastPrice",
|
|
918
|
+
tpOrderType: "Market",
|
|
919
|
+
slOrderType: "Market",
|
|
920
|
+
positionIdx: 0
|
|
921
|
+
});
|
|
922
|
+
if (isTradingStopNotModified(protectionRes)) {
|
|
923
|
+
logger.log(
|
|
924
|
+
"debug",
|
|
925
|
+
"position protection unchanged: %s %s takeProfit=%s stopLoss=%s",
|
|
926
|
+
symbol,
|
|
927
|
+
direction,
|
|
928
|
+
takeProfit.priceStr,
|
|
929
|
+
stopLoss.priceStr
|
|
930
|
+
);
|
|
931
|
+
} else if (protectionRes.retCode === 0) {
|
|
932
|
+
logger.log(
|
|
933
|
+
"info",
|
|
934
|
+
"position protection updated: %s %s takeProfit=%s stopLoss=%s",
|
|
935
|
+
symbol,
|
|
936
|
+
direction,
|
|
937
|
+
takeProfit.priceStr,
|
|
938
|
+
stopLoss.priceStr
|
|
939
|
+
);
|
|
940
|
+
} else {
|
|
941
|
+
logger.log(
|
|
942
|
+
"error",
|
|
943
|
+
"position protection failed: %s %s takeProfit=%s stopLoss=%s %s",
|
|
944
|
+
symbol,
|
|
945
|
+
direction,
|
|
946
|
+
takeProfit.priceStr,
|
|
947
|
+
stopLoss.priceStr,
|
|
948
|
+
(0, import_data.toJson)(protectionRes, true)
|
|
949
|
+
);
|
|
950
|
+
}
|
|
951
|
+
return isTradingStopAccepted(protectionRes);
|
|
952
|
+
}
|
|
953
|
+
const takeProfitsOk = await setTakeProfits({
|
|
954
|
+
symbol,
|
|
955
|
+
direction,
|
|
956
|
+
qty,
|
|
957
|
+
takeProfits
|
|
958
|
+
});
|
|
959
|
+
if (!takeProfitsOk) {
|
|
960
|
+
return false;
|
|
961
|
+
}
|
|
962
|
+
return setStopLoss({ symbol, direction, stopLossPrice });
|
|
963
|
+
};
|
|
883
964
|
return {
|
|
884
965
|
capabilities: BYBIT_CAPABILITIES,
|
|
885
966
|
universe,
|
|
@@ -1270,6 +1351,7 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
1270
1351
|
invalidatePositionSnapshot(symbol);
|
|
1271
1352
|
return true;
|
|
1272
1353
|
},
|
|
1354
|
+
setPositionProtection,
|
|
1273
1355
|
setTakeProfits,
|
|
1274
1356
|
setStopLoss,
|
|
1275
1357
|
closePosition: async ({ symbol, direction }) => {
|
package/dist/index.mjs
CHANGED
|
@@ -737,7 +737,7 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
737
737
|
tpSize: isFullMode ? void 0 : tpSizeStr,
|
|
738
738
|
tpslMode: isFullMode ? "Full" : "Partial",
|
|
739
739
|
takeProfit: tpPriceNorm.priceStr,
|
|
740
|
-
tpTriggerBy: "
|
|
740
|
+
tpTriggerBy: "LastPrice",
|
|
741
741
|
tpOrderType: "Market",
|
|
742
742
|
positionIdx: 0
|
|
743
743
|
});
|
|
@@ -832,6 +832,87 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
832
832
|
}
|
|
833
833
|
return isTradingStopAccepted(slRes);
|
|
834
834
|
};
|
|
835
|
+
const setPositionProtection = async ({
|
|
836
|
+
symbol,
|
|
837
|
+
direction,
|
|
838
|
+
qty,
|
|
839
|
+
takeProfits,
|
|
840
|
+
stopLossPrice
|
|
841
|
+
}) => {
|
|
842
|
+
const hasFullTakeProfit = takeProfits.length === 1 && takeProfits[0]?.rate === 1;
|
|
843
|
+
const hasStopLoss = typeof stopLossPrice === "number" && Number.isFinite(stopLossPrice);
|
|
844
|
+
if (hasFullTakeProfit && hasStopLoss) {
|
|
845
|
+
const client = await getPrivateClient();
|
|
846
|
+
const marketDataClient = await getPublicClient();
|
|
847
|
+
if (!client || !marketDataClient) {
|
|
848
|
+
return false;
|
|
849
|
+
}
|
|
850
|
+
const meta = await getSymbolMeta(marketDataClient, symbol);
|
|
851
|
+
const isLong = direction === "LONG";
|
|
852
|
+
const takeProfit = normalizePrice(
|
|
853
|
+
takeProfits[0].price,
|
|
854
|
+
isLong ? "TP_LONG" : "TP_SHORT",
|
|
855
|
+
meta
|
|
856
|
+
);
|
|
857
|
+
const stopLoss = normalizePrice(
|
|
858
|
+
stopLossPrice,
|
|
859
|
+
isLong ? "SL_LONG" : "SL_SHORT",
|
|
860
|
+
meta
|
|
861
|
+
);
|
|
862
|
+
const protectionRes = await client.setTradingStop({
|
|
863
|
+
category: BYBIT_CATEGORY2,
|
|
864
|
+
symbol,
|
|
865
|
+
tpslMode: "Full",
|
|
866
|
+
takeProfit: takeProfit.priceStr,
|
|
867
|
+
stopLoss: stopLoss.priceStr,
|
|
868
|
+
tpTriggerBy: "LastPrice",
|
|
869
|
+
slTriggerBy: "LastPrice",
|
|
870
|
+
tpOrderType: "Market",
|
|
871
|
+
slOrderType: "Market",
|
|
872
|
+
positionIdx: 0
|
|
873
|
+
});
|
|
874
|
+
if (isTradingStopNotModified(protectionRes)) {
|
|
875
|
+
logger.log(
|
|
876
|
+
"debug",
|
|
877
|
+
"position protection unchanged: %s %s takeProfit=%s stopLoss=%s",
|
|
878
|
+
symbol,
|
|
879
|
+
direction,
|
|
880
|
+
takeProfit.priceStr,
|
|
881
|
+
stopLoss.priceStr
|
|
882
|
+
);
|
|
883
|
+
} else if (protectionRes.retCode === 0) {
|
|
884
|
+
logger.log(
|
|
885
|
+
"info",
|
|
886
|
+
"position protection updated: %s %s takeProfit=%s stopLoss=%s",
|
|
887
|
+
symbol,
|
|
888
|
+
direction,
|
|
889
|
+
takeProfit.priceStr,
|
|
890
|
+
stopLoss.priceStr
|
|
891
|
+
);
|
|
892
|
+
} else {
|
|
893
|
+
logger.log(
|
|
894
|
+
"error",
|
|
895
|
+
"position protection failed: %s %s takeProfit=%s stopLoss=%s %s",
|
|
896
|
+
symbol,
|
|
897
|
+
direction,
|
|
898
|
+
takeProfit.priceStr,
|
|
899
|
+
stopLoss.priceStr,
|
|
900
|
+
toJson(protectionRes, true)
|
|
901
|
+
);
|
|
902
|
+
}
|
|
903
|
+
return isTradingStopAccepted(protectionRes);
|
|
904
|
+
}
|
|
905
|
+
const takeProfitsOk = await setTakeProfits({
|
|
906
|
+
symbol,
|
|
907
|
+
direction,
|
|
908
|
+
qty,
|
|
909
|
+
takeProfits
|
|
910
|
+
});
|
|
911
|
+
if (!takeProfitsOk) {
|
|
912
|
+
return false;
|
|
913
|
+
}
|
|
914
|
+
return setStopLoss({ symbol, direction, stopLossPrice });
|
|
915
|
+
};
|
|
835
916
|
return {
|
|
836
917
|
capabilities: BYBIT_CAPABILITIES,
|
|
837
918
|
universe,
|
|
@@ -1222,6 +1303,7 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
1222
1303
|
invalidatePositionSnapshot(symbol);
|
|
1223
1304
|
return true;
|
|
1224
1305
|
},
|
|
1306
|
+
setPositionProtection,
|
|
1225
1307
|
setTakeProfits,
|
|
1226
1308
|
setStopLoss,
|
|
1227
1309
|
closePosition: async ({ symbol, direction }) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tradejs/connectors",
|
|
3
|
-
"version": "3.1.28-beta.
|
|
3
|
+
"version": "3.1.28-beta.256",
|
|
4
4
|
"description": "MIT-licensed exchange connectors and market data providers for the TradeJS framework.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"tradejs",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@tradejs/core": "^3.1.28-beta.
|
|
43
|
-
"@tradejs/types": "^3.1.28-beta.
|
|
42
|
+
"@tradejs/core": "^3.1.28-beta.256",
|
|
43
|
+
"@tradejs/types": "^3.1.28-beta.256",
|
|
44
44
|
"bybit-api": "^4.7.1",
|
|
45
45
|
"chalk": "4.1.2",
|
|
46
46
|
"lodash": "^4.18.1"
|