How to use the cycle analysis API with Python

Call the raw CycleExplorer endpoint.
The example will pull eod data from yahoo api first and send to the cycle analsis api to get current dominant cycle:


import requests
import json
import yfinance as yf

#put your cycle.tools API key
cycleAPIkey = 'YOUR_API_KEY'

#set cycle.tools API url endpoint
url = f"https://api.cycle.tools/api/cycles/CycleExplorer?
       dynamicInSampleMethod=true
       &api_key={cycleAPIkey}"

#define the yahoo ticker symbol, e.g. SP 500 = ^GSPC
tickerSymbol = "^GSPC"

#get data on this ticker
tickerData = yf.Ticker(tickerSymbol)

#get the historical prices for this ticker
tickerDf = tickerData.history(interval="1d", period="5y")

#get close data as json doubele array
jsondata = tickerDf['Close'].to_json(orient='records')

#call CycleExplorer endpoint to get current dominant cycle
r= requests.post(url, data=jsondata, 
         headers={'Content-Type':'application/json'})

#get the dominant cycle return data
dominantCycle = r.json()
dominantCycleLength = dominantCycle['length']

#print the current dominant cycle length
print(f"Dominant Cycle Length: {dominantCycleLength}")

#print the json the results
print()
print(json.dumps(r.json(), indent=4, sort_keys=True))

Example output looks like:

Dominant Cycle Length: 165.0

{
 "amplitude": 146.00174004966559,
 "analysisEndDate": "2020-05-03T17:02:20.6771012+00:00",
 "analysisStartDate": "0001-01-01T00:00:00",
 "barsAvailable": 0,
 "barsused": 750,
 "currentPrice": 2830.71,
 "cycleProfitability": 0.75,
 "lastlow": -5.6668009766674174,
 "lasttop": -88.16680097666742,
 "length": 165.0,
 "license": "api.cycle.tools :You agree to the API usage terms and conditions from host ",
 "minbaroffset": 170.66680097666742,
 "nextlow": 159.33319902333258,
 "nexttop": 76.83319902333258,
 "phase": -1.3169251392646713,
 "phase_score": -100,
 "phase_status": "BOTTOM_Departure",
 "phasingScore": 135,
 "statusCode": null,
 "symbol": "manual",
 "timeSeries": []
}

Call the MarketCycles endpoint:
The example will use the integrated crypto datafeed to get the current dominant cycle in Bitcoin.


import requests
import json

#put your cycle.tools API key
cycleAPIkey = 'YOUR_API_KEY'
symbol="BTC" # Bitcoin
market="CDS" # Crypto

#set cycle.tools API url endpoint
url = f"https://api.cycle.tools/api/cycles/MarketCycles/{symbol}?marketType={market}&api_key={cycleAPIkey}"

#call MarketCycles endpoint to get current dominant cycle
r= requests.get(url)

#get the dominant cycle return data
marketCycle = r.json()
marketCycleLength = marketCycle['length']

#print the current dominant cycle length
print(f"Dominant Cycle Length: {marketCycleLength}")

#print the json the results
print()
print(json.dumps(r.json(), indent=4, sort_keys=True))

Example output:

Dominant Cycle Length: 39.0

{
    "amplitude": 418.6291962393411,
    "analysisEndDate": "2020-05-01T00:00:00",
    "analysisStartDate": "2016-11-28T00:00:00Z",
    "barsAvailable": 750,
    "barsused": 750,
    "currentPrice": 8728.58,
    "cycleProfitability": 0.7105263157894737,
    "lastlow": -7.758826601781372,
    "lasttop": -27.258826601781372,
    "length": 39.0,
    "license": "****** : You agree to the API usage terms and conditions from api.cycle.tools",
    "minbaroffset": 46.75882660178137,
    "nextlow": 31.241173398218628,
    "nexttop": 11.741173398218628,
    "phase": -0.15968528545011673,
    "phase_score": 40,
    "phase_status": "Uptrend_Neutral",
    "phasingScore": 0,
    "statusCode": "OK",
    "symbol": "BTC",
    "timeSeries": []
}