# -*- coding: utf-8 -*-

import os
import sys
from pprint import pprint

# -----------------------------------------------------------------------------

this_folder = os.path.dirname(os.path.abspath(__file__))
root_folder = os.path.dirname(os.path.dirname(this_folder))
sys.path.append(root_folder + '/python')
sys.path.append(this_folder)

# -----------------------------------------------------------------------------

import ccxt  # noqa: E402

# -----------------------------------------------------------------------------


def main():

    exchange = ccxt.binanceusdm()
    exchange.load_markets()

    timeframe = '1h'
    symbols = exchange.symbols

    allSwapSymbols = []
    for i in range(0, len(symbols)):
        symbol = symbols[i]
        market = exchange.market(symbol)
        if market['swap']:
            allSwapSymbols.append(symbol)

    ohlcvs = []
    for i in range(0, len(allSwapSymbols)):
        symbol = allSwapSymbols[i]
        ohlcv = exchange.fetchOHLCV(symbol, timeframe, None, 1)
        ohlcv[0].append(symbol)
        ohlcvs.append(ohlcv[0])

    priceChanges = []
    for i in range(0, len(ohlcvs)):
        ohlcv = ohlcvs[i]
        open = ohlcv[1]
        close = ohlcv[4]
        symbol = ohlcv[6]
        priceIncrease = close - open
        increaseAsRatio = priceIncrease / open
        priceChanges.append([increaseAsRatio, symbol])

    priceChanges.sort()
    pprint(priceChanges)


main()

@@ -39,16 +39,31 @@
 ort sys%0A
+import asyncio%0A
 from ppr
@@ -81,16 +81,17 @@
 pprint%0A%0A
+%0A
 # ------
@@ -544,26 +544,8 @@
 --%0A%0A
-%0Adef main():%0A%0A    
 exch
@@ -570,20 +570,16 @@
 eusdm()%0A
-    
 exchange
@@ -594,21 +594,16 @@
 rkets()%0A
-%0A    
 timefram
@@ -610,16 +610,309 @@
 e = '1h'
+%0Aohlcvs = %5B%5D%0A%0A%0Aasync def fetchOHLCV(symbol):%0A    try:%0A        ohlcv = exchange.fetchOHLCV(symbol, timeframe, None, 1)%0A        ohlcv%5B0%5D.append(symbol)%0A        ohlcvs.append(ohlcv%5B0%5D)%0A    except Exception as e:%0A        print(f'%7Bsymbol%7D failed fetchOHLCV with exception %7Be%7D')%0A%0A%0Aasync def main():%0A
 %0A    sym
@@ -1144,224 +1144,78 @@
     
-ohlcvs = %5B%5D%0A    for i in range(0, len(allSwapSymbols)):%0A        symbol = allSwapSymbols%5Bi%5D%0A        ohlcv = exchange.fetchOHLCV(symbol, timeframe, None, 1)%0A        ohlcv%5B0%5D.append(symbol)%0A        ohlcvs.append(ohlcv%5B0
+await asyncio.gather(*%5BfetchOHLCV(symbol) for symbol in allSwapSymbols
 %5D)%0A%0A
@@ -1251,27 +1251,24 @@
  i in range(
-0, 
 len(ohlcvs))
@@ -1564,12 +1564,81 @@
 s)%0A%0A
-%0Amain
+loop = asyncio.get_event_loop()%0Aloop.run_until_complete(main())%0Aloop.close
 ()%0A

python sort-swap-markets-by-hourly-price-change is asynchronous