Created
October 29, 2020 08:36
-
-
Save unclebean/0792e2c70f13d1d1df1b1b9a5a8ffecd to your computer and use it in GitHub Desktop.
useContext
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useState, useCallback} from "react"; | |
export const AllForexPairs = [ | |
{ | |
key: "USD_JPY", | |
label: "USD/JPY", | |
}, | |
{ | |
key: "AUD_USD", | |
label: "AUD/USD", | |
}, | |
{ | |
key: "EUR_USD", | |
label: "EUR/USD", | |
}, | |
{ | |
key: "USD_CAD", | |
label: "USD/CAD", | |
}, | |
{ | |
key: "USD_CHF", | |
label: "USD/CHF", | |
}, | |
{ | |
key: "CAD_CHF", | |
label: "CAD/CHF", | |
}, | |
{ | |
key: "NZD_USD", | |
label: "NZD/USD", | |
}, | |
]; | |
export const ForexPairContext = React.createContext({ | |
symbol: {}, | |
setSymbol: () => {}, | |
tradingStrategySummary: {}, | |
setTradingStrategySummary: () => {}, | |
isBackTest: false, | |
onToggleBackTest: () => {}, | |
refresh: false, | |
toggleRefresh: () => {}, | |
endDate: "", | |
setEndDate: () => {}, | |
timeframe: "", | |
setTimeframe: () => {}, | |
strategy: "", | |
setStrategy: () => {}, | |
}); | |
export const useForexPairContext = () => { | |
const [symbol, updateSymbol] = useState(AllForexPairs[0]); | |
const [summary, updateSummary] = useState({win: 0, lose: 0, totalPips: 0}); | |
const [isBackTest, updateBackTest] = useState(false); | |
const [refresh, updateRefreshFlag] = useState(false); | |
const [endDate, updateEndDate] = useState(""); | |
const [timeframe, updateTimeframe] = useState("H1"); | |
const [strategy, updateStrategy] = useState("mama"); | |
const setSymbol = useCallback((symbol) => { | |
updateSymbol(symbol); | |
}, []); | |
const setTradingStrategySummary = useCallback((summary) => { | |
updateSummary(summary); | |
}, []); | |
const onToggleBackTest = useCallback((backTestFlag) => { | |
updateBackTest(backTestFlag); | |
}, []); | |
const toggleRefresh = useCallback((refreshFlag) => { | |
updateRefreshFlag(refreshFlag); | |
}, []); | |
const setEndDate = useCallback((ed) => { | |
updateEndDate(ed); | |
}, []); | |
const setTimeframe = useCallback((tf) => { | |
updateTimeframe(tf); | |
}, []); | |
const setStrategy = useCallback((stg) => { | |
updateStrategy(stg); | |
}, []); | |
return { | |
symbol, | |
setSymbol, | |
tradingStrategySummary: summary, | |
setTradingStrategySummary, | |
isBackTest, | |
onToggleBackTest, | |
refresh, | |
toggleRefresh, | |
endDate, | |
setEndDate, | |
timeframe, | |
setTimeframe, | |
strategy, | |
setStrategy, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment