Skip to content

Commit

Permalink
TradingView: custom indicators (#662)
Browse files Browse the repository at this point in the history
* getIsPaperPair util fn

* refactor

* ask for clear backtest results if user change pair

* add new properties for Chart

* use constructor labels

* generate custom study name for TV

* local TV url for testing

* Remove hardcoded close price source from MACD indicator

* Update config.js
  • Loading branch information
dmytroshch authored Jul 20, 2023
1 parent 979a93a commit 079025f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/components/Chart/Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Chart = ({
hideResolutions,
hideIndicators,
hideDeleteIndicator,
isStrategyChart,
onClosePosition,
position,
chartRange,
Expand All @@ -51,6 +52,7 @@ const Chart = ({
hideResolutions,
hideIndicators,
hideDeleteIndicator,
isStrategyChart,
}).toString()

useEffect(() => {
Expand Down Expand Up @@ -80,6 +82,7 @@ Chart.propTypes = {
hideResolutions: PropTypes.bool,
hideIndicators: PropTypes.bool,
hideDeleteIndicator: PropTypes.bool,
isStrategyChart: PropTypes.bool,
position: PropTypes.object, // eslint-disable-line react/forbid-prop-types
chartRange: PropTypes.object, // eslint-disable-line react/forbid-prop-types
onClosePosition: PropTypes.func,
Expand All @@ -98,6 +101,7 @@ Chart.defaultProps = {
hideResolutions: false,
hideIndicators: false,
hideDeleteIndicator: false,
isStrategyChart: false,
position: null,
chartRange: null,
onClosePosition: null,
Expand Down
10 changes: 6 additions & 4 deletions src/components/StrategyEditor/StrategyEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ const StrategyEditor = (props) => {

// Need to delete inherited execution data of parent strategy
delete newStrategy.executionId
delete newStrategy.results
newStrategy.results = {}
delete newStrategy.startedOn
delete newStrategy.stoppedOn

Expand Down Expand Up @@ -350,12 +350,14 @@ const StrategyEditor = (props) => {

const saveStrategyOptions = useCallback(
(newOptions) => {
onSaveAsStrategy({
const newStrategy = {
...strategy,
strategyOptions: { ...strategyOptions, ...newOptions },
})
}
saveStrategy(newStrategy)
onLoadStrategy(newStrategy, false)
},
[onSaveAsStrategy, strategy, strategyOptions],
[onLoadStrategy, saveStrategy, strategy, strategyOptions],
)

const onBacktestStart = useCallback(() => {
Expand Down
1 change: 1 addition & 0 deletions src/components/StrategyEditor/tabs/BacktestTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const BacktestTab = (props) => {
exitFullscreenChart={hideFullscreenChart}
trades={trades}
isBacktest
isExecuting={false}
/>
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const StrategyLiveTab = (props) => {
fullscreenChart={fullscreenChart}
exitFullscreenChart={unsetFullScreenChart}
trades={trades}
isExecuting={executing}
/>
)

Expand Down
24 changes: 9 additions & 15 deletions src/components/StrategyLiveChart/StrategyLiveChart.helpers.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import memoizeOne from 'memoize-one'
import _map from 'lodash/map'
import _find from 'lodash/find'
import _isEmpty from 'lodash/isEmpty'

const generateCustomStudyName = (name, args) => {
if (_isEmpty(args)) {
return `${name}_`
}
return `${name} ${args.join(' ')}`
}
export const prepareTVIndicators = (indicators) => {
return _map(indicators, (i) => {
const transformed = [
...i,
]

const instance = i[0] && new i[0]()
let name = instance?.label
if (instance?.label === 'EMA') {
name = 'Moving Average Exponential'
} else if (instance?.label === 'ROC') {
name = 'Rate Of Change'
}
const name = instance?.label
transformed[0] = name

if (instance?.label === 'MACD') {
transformed[1] = [
transformed?.[1]?.[0],
transformed?.[1]?.[1],
'close',
transformed?.[1]?.[2],
]
}
transformed[3] = generateCustomStudyName(name, transformed[1])
return transformed
})
}
Expand Down
16 changes: 13 additions & 3 deletions src/components/StrategyLiveChart/StrategyLiveChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const StrategyLiveChart = ({
lastOpenPosition,
trades,
isBacktest,
isExecuting,
}) => {
const {
strategyOptions: {
Expand All @@ -43,9 +44,13 @@ const StrategyLiveChart = ({
stoppedOn,
} = strategy
const start = isBacktest ? new Date(startDate).getTime() : startedOn
const end = isBacktest ? new Date(endDate).getTime() : stoppedOn
const end = isBacktest
? new Date(endDate).getTime()
: isExecuting
? null
: stoppedOn
const chartRange = useMemo(() => {
if (start && end) {
if (start || end) {
return {
start,
end,
Expand All @@ -59,7 +64,9 @@ const StrategyLiveChart = ({
() => prepareTVIndicators(indicators),
[indicators],
)
const interval = TIMEFRAME_INTERVAL_MAPPING[timeframe] || '15'
const interval = isBacktest
? TIMEFRAME_INTERVAL_MAPPING[timeframe] || '15'
: '1'

const {
wsID, uiID, base, quote,
Expand Down Expand Up @@ -134,6 +141,7 @@ const StrategyLiveChart = ({
hideResolutions
hideDeleteIndicator
hideIndicators
isStrategyChart
chartRange={chartRange}
key={executionId}
/>
Expand All @@ -151,12 +159,14 @@ StrategyLiveChart.propTypes = {
exitFullscreenChart: PropTypes.func.isRequired,
lastOpenPosition: PropTypes.object, // eslint-disable-line
isBacktest: PropTypes.bool,
isExecuting: PropTypes.bool,
}

StrategyLiveChart.defaultProps = {
indicators: [],
isBacktest: false,
lastOpenPosition: null,
isExecuting: false,
}

export default memo(StrategyLiveChart)
1 change: 0 additions & 1 deletion src/redux/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ const DISCUSSION_BOARD_URL = 'https://bit.ly/42p9YiV'
const DISCORD_URL = 'https://bit.ly/400Xj49'

const CHART_URL = isElectronApp ? 'https://bitfinexcom.github.io/bfx-hf-tradingview/' : process.env.REACT_APP_CHART_URL
// const CHART_URL = 'http://localhost:3001/bfx-hf-tradingview/'

const HONEY_AUTH_URL = `${process.env.REACT_APP_UFX_API_URL}/honey`

Expand Down

0 comments on commit 079025f

Please sign in to comment.