Код: Выделить всё
def simpleRSIstrategy(indicator, threshold1 = 50, threshold2 = 40, days = 3):
buySellTable = dict()
# simple RSI strategy
hold = False
count = 0
for i in range(len(indicator)):
# buying strategy
if indicator['RSI7'][i] > threshold1 and not hold:
date = indicator.index[i]
buySellTable[date] = 'Buy'
hold = True
# selling strategy
if indicator['RSI7'][i] < threshold1 and hold:
count += 1
if count == days or indicator['RSI7'][i] < threshold2:
date = indicator.index[i]
buySellTable[date] = 'Sell'
hold = False
count = 0
if indicator['RSI7'][i] > threshold1 and hold:
count = 0
return buySellTable
Код: Выделить всё
def simpleRSIstrategy_split(indicator, threshold1 = 50, threshold2 = 40, days = 3):
buySellTable = dict()
hold = False
count = 0
startIdx = 0
while True:
simpleBuyStrategy_split(indicator, threshold1)
simpleSellStrategy_split(indicator, threshold1, threshold2, days)
if startIdx == len(indicator)-1:
break
return buySellTable
Код: Выделить всё
def simpleBuyStrategy_split(indicator, threshold1):
global startIdx, hold, buySellTable
for i in range(startIdx, len(indicator)):
if indicator['RSI7'][i] > threshold1 and not hold:
date = indicator.index[i]
buySellTable[date] = 'Buy'
hold = True
startIdx = i+1
break
def simpleSellStrategy_split(indicator, threshold1, threshold2, days):
global startIdx, count, hold, buySellTable
for i in range(startIdx, len(indicator)):
if indicator['RSI7'][i] < threshold1 and hold:
count += 1
if count == days or indicator['RSI7'][i] < threshold2:
date = indicator.index[i]
buySellTable[date] = 'Sell'
hold = False
count = 0
startIdx = i+1
break
if indicator['RSI7'][i] > threshold1 and hold:
count = 0
Код: Выделить всё
for i in range(startIdx, len(indicator)):
Подробнее здесь: https://stackoverflow.com/questions/725 ... -in-python