Exemple #1
0
def get_sl(stock):

    output = []

    #get stock data
    stock_data = get_stock_data_wotd(stock)

    if (stock_data[0]["open"] <= 0) and (stock_data[0]["close"] <= 0):
        output.append("NO DATA")
        return output

    #indikators
    atr = get_atr(stock_data, 14)

    stops_15 = []
    for i in range(15):
        stops_15.append(0)


    for i in range(30, 0, -1):
        stops_15 = stops_15[1:]
        stops_15.append(stock_data[i]["high"]-5*atr[i])


    output.append(stock + " aktueller SL: " + str(max(stops_15)))
    output.append("aktueller Kurs: " + str(stock_data[0]["close"]))

    return output
Exemple #2
0
def check_signal(stock):

    stock_data = {}
    output = []

    #get stock data
    stock_data = get_stock_data_wotd(stock)

    if (stock_data[0]["open"] <= 0) and (stock_data[0]["close"] <= 0):
        output.append(stock + ' ZERO VALUES!')
        return output

    #set indicators

    sma_length = 50

    atr_1_length = 14

    rsi_length = 14

    #get indicator values
    sma = get_sma(stock_data, sma_length)
    atr_1 = get_atr(stock_data, atr_1_length)
    rsi = get_rsi(stock_data, rsi_length)

    # Alle Listen so lange machen, wie die kürzeste, damit von hinten durchiteriert werden kann
    min_length = min(len(sma), len(atr_1), len(rsi))

    sma = sma[:min_length]
    atr_1 = atr_1[:min_length]
    rsi = rsi[:min_length]

    stock_data = stock_data[:min_length]

    if ((stock_data[0]["close"] - stock_data[1]["close"]) >=
            1.5 * atr_1[1]) and rsi[0] > 50:
        if ((sma[0] - sma[1]) > 0) and ((sma[0] - sma[15]) > 0):

            vol_avg = 0
            for k in range(14):
                vol_avg += stock_data[k]["volume"]

            vol_avg = vol_avg / 14

            if (rsi[0] < 75 and stock_data[0]["volume"] > 2 * vol_avg
                ) or stock_data[0]["volume"] > 3.5 * vol_avg:

                stops_15 = []
                for i in range(15):
                    stops_15.append(stock_data[i]["high"] - 5 * atr_1[i])

                trade = {"EK": 0, "Anzahl": 0, "SL": 0}
                trade["EK"] = stock_data[0]["close"]
                trade["SL"] = max(stops_15)
                trade["Anzahl"] = round(300 / (trade["EK"] - trade["SL"]))

                output.append(stock + " - " + str(trade["Anzahl"]) +
                              " Stück für " + str(trade["EK"]) + "SL = " +
                              str(trade["SL"]))

    return output
Exemple #3
0
def backtest(stock):

    #set indicators
    atr_length = 14

    #get stock data
    stock_data = get_stock_data_wotd(stock)

    if (stock_data[0]["open"] <= 0) and (stock_data[0]["close"] <= 0):
        return

    atr = get_atr(stock_data, atr_length)
    atr = atr[:len(atr)]
    stock_data = stock_data[:len(atr)]

    trade = {"position": "$", "EK": 0, "Anzahl": 0, "SL": 0, "TP": 0}
    sum = 50000

    l_count1 = 0
    l_count2 = 0

    box = {"high": 0, "low": 0, "Trend": 'UP', "Done": True}

    box["high"] = stock_data[-1]["high"]
    box["low"] = stock_data[-1]["high"] - atr[-1]

    box_helper = 0
    cd_20 = 0
    box_count = 0

    highs_20 = []
    lows_20 = []

    print("Close: " + str(stock_data[0]))
    print("ATR1: " + str(atr[0]))
    print("box high: " + str(box["high"]))
    print("box low: " + str(box["low"]))

    for i in range(-1, (-1) * (len(stock_data)), -1):
        '''
        if stock_data[i]["date"] == '2015-09-17':

                print("Close: " + str(stock_data[i]))
                print("SMA50: " +  str(sma[i]))
                print("ATR1: " +  str(atr[i]))
                print("ATR2: " +  str(atr_2[i]))
                print("HMA1: " +  str(hma_1[i]))
                print("HMA2: " +  str(hma_2[i]))
                print("HMA3: " +  str(hma_3[i]))
                print("CCI: " +  str(cci[i]))
                vol_avg = 0
                for k in range(14):
                    vol_avg += stock_data[i+k]["volume"]
                vol_avg = vol_avg/14
                print("Vol " +  str(stock_data[i]["volume"]))
                print("Volavg " +  str(vol_avg))
        '''

        l_count1 += 1

        ##################WENN AUFWÄRTS################

        if (box["Done"] or box["Trend"]
                == "BUP") and stock_data[i]["high"] > box["high"] + atr[i]:

            if box["Trend"] == "DOWN" or box["Trend"] == "BUP":
                box_count = 0

            box["Done"] = False
            box["Trend"] = "UP"
            box["low"] = box["high"]
            box["high"] = stock_data[i]["high"]
            #print(stock_data[i]["date"] + ' ' + "new upbox " +str(box["high"]) + " " + str(box["low"]))

            cd_20 = 20

        if not box["Done"] and box["Trend"] == "UP":
            if stock_data[i]["high"] > box["high"]:
                box["high"] = stock_data[i]["high"]
                #print(stock_data[i]["date"] + " box high = " + str(box["high"]))
            elif stock_data[i]["high"] < box["high"] - 0.5 * atr[i]:
                box["Trend"] = "BDOWN"
                #print(stock_data[i]["date"] + ' ' + "back down")

        if not box["Done"] and box["Trend"] == "BDOWN" and stock_data[i][
                "low"] > stock_data[i + 1]["low"] + 0.5 * atr[i]:
            box["low"] = stock_data[i + 1]["low"]
            box["Done"] = True
            box["Trend"] = "UP"
            box_count += 1
            #print(stock_data[i]["date"] + ' ' + "upbox no. " + str(box_count) + " done")

##############WENN RUNTER##########################

        if (box["Done"] or box["Trend"]
                == "BDOWN") and stock_data[i]["low"] < box["low"] - atr[i]:

            if box["Trend"] == "UP" or box["Trend"] == "BDOWN":
                box_count = 0

            box["Done"] = False
            box["Trend"] = "DOWN"
            box["high"] = box["low"]
            box["low"] = stock_data[i]["low"]
            #print(stock_data[i]["date"] + ' ' + "new downbox")

            cd_20 = 20

        if not box["Done"] and box["Trend"] == "DOWN":
            if stock_data[i]["low"] < box["low"]:
                box["low"] = stock_data[i]["low"]
            elif stock_data[i]["low"] > box["low"] + 0.5 * atr[i]:
                box["Trend"] = "BUP"
                #print(stock_data[i]["date"] + ' ' + "back up")

        if not box["Done"] and box["Trend"] == "BUP" and stock_data[i][
                "high"] < stock_data[i + 1]["high"] - 0.5 * atr[i]:
            box["high"] = stock_data[i + 1]["high"]
            box["Done"] = True
            box["Trend"] = "DOWN"
            box_count += 1
            #print(stock_data[i]["date"] + ' ' + "downbox no. " + str(box_count) + " done")

#########################################################################################

        if cd_20 < 0:

            if box["low"] < min(lows_20):
                box["low"] = min(lows_20)
            if box["high"] > max(highs_20):
                box["high"] = max(highs_20)

            lows_20 = lows_20[1:]
            highs_20 = highs_20[1:]

        lows_20.append(stock_data[i]["low"])
        highs_20.append(stock_data[i]["high"])
        cd_20 -= 1

        if trade["position"] == "$" and box["Done"] and box_count >= 3:

            if box["Trend"] == 'UP':

                trade["position"] = "L"
                trade["EK"] = stock_data[i]["close"]
                trade["Anzahl"] = round((0.005 * sum) / (2.5 * atr[i]))
                trade["SL"] = trade["EK"] - 2.5 * atr[i]

                print("--------------------!!!--------------------")
                print("Buy " + str(trade["Anzahl"]) + " Stück für " +
                      str(trade["EK"]) + " am " + str(stock_data[i]["date"]))
                print("Stop Loss: " + str(trade["SL"]))

###################################WENN LONG###########################################
        if trade["position"] == "L":

            l_count2 += 1
            '''
            if (stock_data[i]["low"] < trade["SL"]):

                print(str(stock_data[i]["date"]) + "Verkauf 1. Hälfte - Profit: " + str((trade["SL"] - trade["EK"])*trade["Anzahl"]) +"€ "+ str((trade["SL"] - trade["EK"]) / trade["EK"]*100) + "%")
                sum += (trade["SL"] - trade["EK"])*trade["Anzahl"]
                print("sum = " + str(sum))
                trade["SL"] = 0
                trade = {"position" : "$", "EK" : 0, "Anzahl" : 0, "SL" : 0, "TP": 0}

            '''
            if (box["Trend"] == "DOWN" and box_count == 1 and not box["Done"]):

                print(
                    str(stock_data[i]["date"]) + "Verkauf 2. Box - Profit: " +
                    str((box["high"] - trade["EK"]) * trade["Anzahl"]) + "€ " +
                    str((box["high"] - trade["EK"]) / trade["EK"] * 100) + "%")
                sum += (box["high"] - trade["EK"]) * trade[
                    "Anzahl"]  #box["high"] weil jetzt == altes box low
                print("sum = " + str(sum))
                trade = {
                    "position": "$",
                    "EK": 0,
                    "Anzahl": 0,
                    "SL": 0,
                    "TP": 0
                }

            if (stock_data[i]["low"] < trade["SL"]):

                print(
                    str(stock_data[i]["date"]) + "Verkauf SL - Profit: " +
                    str((trade["SL"] - trade["EK"]) * trade["Anzahl"]) + "€ " +
                    str((box["high"] - trade["EK"]) / trade["EK"] * 100) + "%")
                sum += (trade["SL"] - trade["EK"]) * trade["Anzahl"]
                print("sum = " + str(sum))
                trade = {
                    "position": "$",
                    "EK": 0,
                    "Anzahl": 0,
                    "SL": 0,
                    "TP": 0
                }

            #if trade["SL"] != 0:
            #trade["SL"] = box["low"] - atr[i]
            #print(stock_data[i]["date"] + ' ' + str(trade["SL"]<stock_data[i]["low"]))

    print(str(sum) + " " + str(l_count2) + "/" + str(l_count1))
Exemple #4
0
def get_sl(symbol, date, ek):

    output = []

    #get stock data
    stock_data = get_stock_data_wotd(symbol)

    if (stock_data[0]["open"] <= 0) and (stock_data[0]["close"] <= 0):
        output.append("NO DATA")
        return output

    #indikators
    atr = get_atr(stock_data, 14)
    sma = get_sma(stock_data, 50)

    #get pos of buy date
    date_index = 0

    last_high = 0
    trade = {"EK": ek, "SL": 0, "TP": 0, "Anzahl": 0}

    for i in range(len(stock_data)):
        if (stock_data[i]["date"] == date):
            date_index = i
            trade["SL"] = ek - 2.5 * atr[i]
            #output.append("initialer SL: " + str(trade["SL"]))
            trade["TP"] = ek + 2.5 * atr[i]
            #output.append("initialer TP: " + str(trade["TP"]))
            break

    output.append("---------" + symbol + "----------")

    #output.append(date_index)

    for i in range(date_index, -1, -1):

        if last_high < stock_data[i]["high"]:
            last_high = stock_data[i]["high"]

            if (trade["TP"] != 0) and (trade["TP"] < last_high - 3 * atr[i]):
                trade["TP"] = last_high - 3 * atr[i]

        if (stock_data[i]["low"] < trade["SL"]):

            output.append(stock_data[i]["date"] + " - SL hit - Sold at " +
                          str(trade["SL"]))
            trade["SL"] = 0
            trade["TP"] = 0
            break

        #output.append(stock_data[i]["date"] + ' ' + str(stock_data[i]["high"]) + str(i))

        if (stock_data[i]["high"] > trade["TP"]) and trade["SL"] < trade["EK"]:

            trade["SL"] = trade["EK"]
            trade["TP"] = last_high - 3 * atr[i]
            output.append(
                str(stock_data[i]["date"]) +
                " - TP überschritten -> Anpassung SL auf EK: " +
                str(trade["SL"]))
            output.append(
                str(stock_data[i]["date"]) +
                " - TP überschritten -> Anpassung TP - 3ATR: " +
                str(trade["TP"]))

        if (stock_data[i]["low"] < trade["TP"]) and trade["SL"] > trade[
                "EK"]:  #SL > EK damit Gewinnmitnahme erst im Profit

            output.append(stock_data[i]["date"] +
                          " - Gewinnmitnahme 50% verkauft (3ATR)")
            #output.append("TP = 0")
            trade["TP"] = 0

        if ((sma[i] - 1.5 * atr[i]) > trade["SL"]
            ) and stock_data[i]["high"] > (sma[i] - 1.5 * atr[i]):

            trade["SL"] = (sma[i] - 1.5 * atr[i])
            #output.append(stock_data[i]["date"] + " - Anpassung SL auf SMA50: " + str(trade["SL"]))

    output.append("aktueller SL: " + str(trade["SL"]))
    output.append("aktueller TP: " + str(trade["TP"]))
    output.append("aktueller Kurs: " + str(stock_data[0]["close"]))

    return output
Exemple #5
0
def backtest(stock):

    #set indicators
    ema_co_long = 50
    ema_co_short = 25

    cci_length = 20

    ppo_length_short = 12
    ppo_length_long = 26

    atr_length = 14

    min_ppo_change = 0.2
    min_cci_change = 0

    #get stock data
    stock_data = get_stock_data_wotd(stock)

    if (stock_data["open"][0] <= 0) and (stock_data["close"][0] <= 0):
        return

    #get indicator values
    cci = get_cci(stock_data, cci_length)

    ema_short = get_ema(stock_data, ema_co_short)
    ema_long = get_sma(stock_data, ema_co_long)

    ppo = get_ppo(stock_data, ppo_length_short, ppo_length_long)

    atr = get_atr(stock_data, atr_length)

    # Alle Listen so lange machen, wie die kürzeste, damit von hinten durchiteriert werden kann
    min_length = min(len(cci),len(ema_short),len(ema_long),len(ppo),len(atr))
    cci = cci[:min_length]
    ema_short = ema_short[:min_length]
    ppo = ppo[:min_length]
    atr = atr[:min_length]

    stock_data["open"] = stock_data["open"][:min_length]
    stock_data["high"] = stock_data["high"][:min_length]
    stock_data["low"] = stock_data["low"][:min_length]
    stock_data["close"] = stock_data["close"][:min_length]
    stock_data["volume"] = stock_data["volume"][:min_length]
    stock_data["date"] = stock_data["date"][:min_length]


    trade = {"position" : "$", "EK" : 0, "SL" : 0}
    sum = 10000
                                                        #ema_co_long größte Zahl
    for i in range(-1, (-1)*(len(stock_data["open"]) - ema_co_long),-1):

        cci_reversal = False

        if trade["position"] == "L":
            if (cci[i]<=-100) or ((cci[i] < 100) and ((ppo[i+1]-ppo[i]) >= min_ppo_change) and ((ppo[i+1]-ppo[i]) > (ppo[i+2]-ppo[i+1])) and ((cci[i+1]-cci[i]) >= min_cci_change)):
                print("CCI < -100 - Sold at " + str(stock_data["open"][i]) + " - Profit: " + str((stock_data["open"][i] - trade["EK"]) / trade["EK"]*100) + "%")
                sum += sum * ((stock_data["open"][i+1] - trade["EK"]) / trade["EK"])
                trade = {"position" : "$", "EK" : 0, "SL" : 0}

        elif trade["position"] == "S":
            if (cci[i]>=100) or ((cci[i]>=-100) and ((ppo[i]-ppo[i+1]) >= min_ppo_change) and ((ppo[i]-ppo[i+1]) > (ppo[i+1]-ppo[i+2])) and ((cci[i]-cci[i+1]) >= min_cci_change) and ((cci[i]-cci[i+1]) > 0)):
                print("CCI < -100 - Sold at " + str(stock_data["open"][i]) + " - Profit: " + str((trade["EK"]-stock_data["open"][i]) / trade["EK"]*100) + "%")
                sum +=  sum * ((trade["EK"]-stock_data["open"][i+1]) / trade["EK"])
                trade = {"position" : "$", "EK" : 0, "SL" : 0}

        #check trend with ema crossover
        if ema_short[i] > ema_long[i]:

            if trade["position"] == "S":
                print("EMA Cross against Trade - Sold at " + str(stock_data["open"][i]) + " - Profit: " + str((trade["EK"] - stock_data["open"][i]) / trade["EK"]*100) + "%")
                trade = {"position" : "$", "EK" : 0, "SL" : 0}
            #Kommt CCI aus Short-Trend und ist jetzt neutral/long?
            for k in range(1,3):
                if i+k > len(cci)-1:
                    cci_reversal = False

                elif (cci[i] > -100) and (cci[i+k] < -100):
                    cci_reversal = True
                    break

            #wenn CCI im Longtrend oder Reversal aus -100
            if (trade["position"] == "$") and ((cci[i] >= 100) or (cci_reversal)):

                #Wenn PPO und CCI steigen (mindestens um X) + PPO/CCI-Momentum -> PPO/CCI steigt stärker als am Vortag
                if ((ppo[i]-ppo[i+1]) >= min_ppo_change) and (ppo[i]-ppo[i+1] > ppo[i+1]-ppo[i+2]) and ((cci[i]-cci[i+1]) >= min_cci_change):

                    print("--------------------!!!--------------------")
                    print("GO LONG " + stock + " am " + str(stock_data["date"][i]) + " at " + str(stock_data["close"][i]))
                    print("Stop Loss 2D Low - 1 ATR: "+str(min(stock_data["low"][i], stock_data["low"][i+1], stock_data["low"][i+2])-atr[i]))
                    print("Stop Loss 2 ATR: "+str(stock_data["low"][i]-(2*atr[i])))
                    print("--------------------!!!--------------------")

                    trade["position"] = "L"
                    trade["EK"] = stock_data["close"][i]
                    trade["SL"] = min(stock_data["low"][i], stock_data["low"][i+1], stock_data["low"][i+2])-atr[i]

                elif ((ppo[i]-ppo[i+1]) >= min_ppo_change*0.7) and (ppo[i]-ppo[i+1] > ppo[i+1]-ppo[i+2]) and ((cci[i]-cci[i+1]) >= min_cci_change*0.7) and (cci[i]-cci[i+1] > cci[i+1]-cci[i+2]):
                    print("--------------------!!!--------------------")
                    print(stock+": about to show LONG signal")
                    print("--------------------!!!--------------------")



        elif ema_short[i] < ema_long[i]:

            if trade["position"] == "L":
                print("EMA Cross against Trade - Sold at " + str(stock_data["open"][i]) + " - Profit: " + str((stock_data["open"][i] - trade["EK"]) / trade["EK"]*100) + "%")
                trade = {"position" : "$", "EK" : 0, "SL" : 0}

            #Kommt CCI aus Long-Trend und ist jetzt neutral/short?
            for k in range(1,3):
                if (cci[i] < 100) and (cci[i+k] > 100):
                    cci_reversal = True
                    break

            #wenn CCI im Shorttrend oder Reversal aus 100
            if (trade["position"] == "$") and ((cci[i] <= -100) or (cci_reversal)):

                #Wenn PPO und CCI fallen (mindestens um X) + PPO-Momentum -> PPO fällt stärker als am Vortag
                if ((ppo[i+1]-ppo[i]) >= min_ppo_change) and (ppo[i+1]-ppo[i] > ppo[i+2]-ppo[i+1]) and ((cci[i+1]-cci[i]) >= min_cci_change):

                    print("--------------------!!!--------------------")
                    print("GO SHORT " + stock + " am " + str(stock_data["date"][i]) + " at " + str(stock_data["close"][i]))
                    print("Stop Loss 2D High - 1 ATR: "+str(max(stock_data["high"][i], stock_data["high"][i+1], stock_data["high"][i+2])+atr[i]))
                    print("Stop Loss 2 ATR: "+str(stock_data["high"][i]+(2*atr[i])))
                    print("--------------------!!!--------------------")

                    trade["position"] = "S"
                    trade["EK"] = stock_data["close"][i]
                    trade["SL"] = min(stock_data["high"][i], stock_data["high"][i+1], stock_data["high"][i+2])-atr[i]


                elif ((ppo[i+1]-ppo[i]) >= min_ppo_change*0.7) and (ppo[i+1]-ppo[i] > ppo[i+2]-ppo[i+1]) and ((cci[i+1]-cci[i]) >= min_cci_change*0.7) and (cci[i+1]-cci[i] > cci[i+2]-cci[i+1]):
                    print("--------------------!!!--------------------")
                    print(stock+": about to show SHORT signal")
                    print("--------------------!!!--------------------")

    print(sum)
Exemple #6
0
def check_signal(stock):

    stock_data = {}
    output = []

    #get stock data
    sma_length = 50

    atr_1_length = 14
    vcci_length = 20

    #get stock data
    stock_data = get_stock_data_wotd(stock)

    if (stock_data[0]["open"] <= 0) and (stock_data[0]["close"] <= 0):
        return

    #get indicator values
    sma = get_sma(stock_data, sma_length)

    atr_1 = get_atr(stock_data, atr_1_length)

    vcci = get_vcci(stock_data, vcci_length)

    # Alle Listen so lange machen, wie die kürzeste, damit von hinten durchiteriert werden kann
    min_length = min(len(sma), len(atr_1), len(vcci))

    sma = sma[:min_length]
    atr_1 = atr_1[:min_length]
    vcci = vcci[:min_length]

    stock_data = stock_data[:min_length]

    if (vcci[0] > 200
            and (stock_data[0]["close"] - stock_data[1]["close"]) >= atr_1[1]):
        l_high = 0
        vol_avg = 0

        for i in range(20):
            vol_avg += stock_data[i]["volume"]

        vol_avg = vol_avg / 20

        if stock_data[0]["volume"] > vol_avg:

            for i in range(1, 100):
                if l_high < stock_data[i]["close"]:
                    l_high = stock_data[i]["close"]

            if (stock_data[0]["close"] > l_high):

                stops_15 = []
                for i in range(15):
                    stops_15.append(stock_data[i]["high"] - 5 * atr_1[i])

                trade = {"EK": 0, "Anzahl": 0, "SL": 0}
                trade["EK"] = stock_data[0]["close"]
                trade["SL"] = max(stops_15)
                trade["Anzahl"] = round(300 / (trade["EK"] - trade["SL"]))

                output.append(stock + " - " + str(trade["Anzahl"]) +
                              " Stück für " + str(trade["EK"]) + "SL = " +
                              str(trade["SL"]))

    return output
Exemple #7
0
def backtest(stock):

    #set indicators

    sma_length = 50

    cci_length = 100

    hma_1_length = 12
    hma_2_length = 25
    hma_3_length = 100

    atr_1_length = 14
    atr_2_length = 1

    #get stock data
    stock_data = get_stock_data_wotd(stock)

    if (stock_data[0]["open"] <= 0) and (stock_data[0]["close"] <= 0):
        return

    #get indicator values
    sma = get_sma(stock_data, sma_length)

    atr_1 = get_atr(stock_data, atr_1_length)
    atr_2 = get_atr(stock_data, atr_2_length)

    hma_1 = get_hma(stock_data, hma_1_length)
    hma_2 = get_hma(stock_data, hma_2_length)
    hma_3 = get_hma(stock_data, hma_3_length)

    cci = get_cci(stock_data, cci_length)

    # Alle Listen so lange machen, wie die kürzeste, damit von hinten durchiteriert werden kann
    min_length = min(len(hma_3), len(hma_2), len(hma_1), len(sma), len(atr_1),
                     len(atr_2), len(cci))

    sma = sma[:min_length]
    atr_1 = atr_1[:min_length]
    atr_2 = atr_2[:min_length]
    hma_1 = hma_1[:min_length]
    hma_2 = hma_2[:min_length]
    hma_3 = hma_3[:min_length]
    cci = cci[:min_length]

    stock_data = stock_data[:min_length]

    trade = {"position": "$", "EK": 0, "Anzahl": 0, "SL": 0, "TP": 0}
    sum = 50000

    l_count1 = 0
    l_count2 = 0
    last_high = 0
    last_low = 0

    print("Close: " + str(stock_data[0]))
    print("SMA50: " + str(sma[0]))
    print("ATR1: " + str(atr_1[0]))
    print("ATR2: " + str(atr_2[0]))
    print("HMA1: " + str(hma_1[0]))
    print("HMA2: " + str(hma_2[0]))
    print("HMA3: " + str(hma_3[0]))
    print("CCI: " + str(cci[0]))

    for i in range(-1, (-1) * (len(stock_data)), -1):
        '''
        if stock_data[i]["date"] == '2015-09-17':

                print("Close: " + str(stock_data[i]))
                print("SMA50: " +  str(sma[i]))
                print("ATR1: " +  str(atr_1[i]))
                print("ATR2: " +  str(atr_2[i]))
                print("HMA1: " +  str(hma_1[i]))
                print("HMA2: " +  str(hma_2[i]))
                print("HMA3: " +  str(hma_3[i]))
                print("CCI: " +  str(cci[i]))
                vol_avg = 0
                for k in range(14):
                    vol_avg += stock_data[i+k]["volume"]
                vol_avg = vol_avg/14
                print("Vol " +  str(stock_data[i]["volume"]))
                print("Volavg " +  str(vol_avg))
        '''

        l_count1 += 1

        if last_high < stock_data[i]["high"]:
            last_high = stock_data[i]["high"]

        if last_low > stock_data[i]["low"]:
            last_low = stock_data[i]["low"]

###################################WENN LONG###########################################
        if trade["position"] == "L":

            l_count2 += 1

            if (trade["TP"] != 0) and (trade["TP"] < last_high - 4 * atr_1[i]):
                trade["TP"] = last_high - 4 * atr_1[i]

            if (stock_data[i]["low"] < trade["SL"]):
                print("SL hit - Sold at " + str(trade["SL"]) + " am " +
                      str(stock_data[i]["date"]))
                last_high = 0
                last_low = 0
                if (trade["TP"] > 0):
                    print("Verkauf - Profit: " +
                          str((trade["SL"] - trade["EK"]) * trade["Anzahl"]) +
                          "€ " + str((trade["SL"] - trade["EK"]) /
                                     trade["EK"] * 100) + "%")
                    sum += (trade["SL"] - trade["EK"]) * trade["Anzahl"]
                    print("sum = " + str(sum))
                else:
                    print("Verkauf zweite Hälfte - Profit: " +
                          str((trade["SL"] - trade["EK"]) * trade["Anzahl"]) +
                          "€ " + str((trade["SL"] - trade["EK"]) /
                                     trade["EK"] * 100) + "%")
                    sum += (trade["SL"] - trade["EK"]) * trade["Anzahl"]
                    print("sum = " + str(sum))
                trade = {
                    "position": "$",
                    "EK": 0,
                    "SL": 0,
                    "TP": 0,
                    "Anzahl": 0
                }

            if (trade["SL"] < trade["EK"]) and (stock_data[i]["high"] >
                                                trade["TP"]):
                '''
                print("Gewinnmitnahme (2.5 ATR) - Profit: " + str((trade["TP"] - trade["EK"])*trade["Anzahl"]) +"€ "+ str((trade["TP"] - trade["EK"]) / trade["EK"]*100) + "%")
                sum += (trade["TP"] - trade["EK"])*trade["Anzahl"]

                trade["TP"] = 0
                trade["Anzahl"] = trade["Anzahl"]/2
                '''
                trade["SL"] = trade["EK"]

            if (trade["TP"] > 0) and (
                    stock_data[i]["low"] <
                    trade["TP"]) and trade["SL"] > trade[
                        "EK"]:  #SL > EK damit Gewinnmitnahme erst im Profit

                print("Gewinnmitnahme (ATR) - Profit: " +
                      str((trade["TP"] - trade["EK"]) * trade["Anzahl"]) +
                      "€ " +
                      str((trade["TP"] - trade["EK"]) / trade["EK"] * 100) +
                      "%")
                sum += (trade["TP"] - trade["EK"]) * trade["Anzahl"]
                print("sum = " + str(sum))
                #trade["TP"] = 0
                #trade["Anzahl"] = trade["Anzahl"]/2
                trade = {
                    "position": "$",
                    "EK": 0,
                    "SL": 0,
                    "TP": 0,
                    "Anzahl": 0
                }

            if ((sma[i] - 1.5 * atr_1[i]) > trade["SL"]
                ) and stock_data[i]["high"] > (sma[i] - 1.5 * atr_1[i]):

                trade["SL"] = (sma[i] - 1.5 * atr_1[i])

#####################################WENN SHORT#########################################
        '''
        elif trade["position"] == "S":
            l_count2 += 1

            if (trade["TP"] != 0) and (trade["TP"] > last_low + 3*atr_1[i]):
                trade["TP"] = last_low + 3*atr_1[i]

            if (stock_data[i]["high"] > trade["SL"]):
                print("SL hit - Sold at " + str(trade["SL"]) + " am " + str(stock_data[i]["date"]))
                last_high = 0
                last_low = 0
                if (trade["TP"] > 0):
                    print("Verkauf - Profit: " + str((trade["EK"] - trade["SL"])*trade["Anzahl"]) +"€ "+ str((trade["EK"] - trade["SL"]) / trade["EK"]*100) + "%")
                    sum += (trade["EK"] - trade["SL"])*trade["Anzahl"]
                    print("sum = " + str(sum))
                else:
                    print("Verkauf zweite Hälfte - Profit: " + str((trade["EK"] - trade["SL"])*trade["Anzahl"]) +"€ "+ str((trade["EK"] - trade["SL"]) / trade["EK"]*100) + "%")
                    sum += (trade["EK"] - trade["SL"])*trade["Anzahl"]
                    print("sum = " + str(sum))
                trade = {"position" : "$", "EK" : 0, "SL" : 0, "TP": 0, "Anzahl" : 0}

            if (trade["SL"] < trade["EK"]) and (stock_data[i]["high"] > trade["TP"]):

                trade["SL"] = trade["EK"]

            if (stock_data[i]["high"] > trade["TP"]) and trade["SL"] < trade["EK"]: #SL < EK damit Gewinnmitnahme erst im Profit

                print("Gewinnmitnahme (2.5 ATR) - Profit: " + str((trade["EK"] - trade["TP"])*trade["Anzahl"]/2) +"€ "+ str((trade["EK"] - trade["TP"]) / trade["EK"]*100) + "%")
                sum += (trade["EK"] - trade["TP"])*trade["Anzahl"]/2
                print("sum = " + str(sum))
                trade["TP"] = 0
                trade["Anzahl"] = trade["Anzahl"]/2


            if ((sma[i] + 1.5 * atr_1[i]) < trade["SL"]) and stock_data[i]["low"] < (sma[i] + 1.5 * atr_1[i]):

                trade["SL"] = (sma[i] + 1.5 * atr_1[i])
        '''
        #####################################GO LONG##################################################
        if (cci[i] > 100) and (atr_2[i] > atr_1[i]):

            #if (hma_1[i] > hma_2[i]) and (hma_1[i] > hma_3[i]):

            if (((hma_1[i] - hma_1[i + 1]) > 0)
                    and ((hma_2[i] - hma_2[i + 1]) > 0)
                    and ((hma_3[i] - hma_3[i + 1]) > 0)
                    and ((sma[i] - sma[i + 1]) > 0)
                    and ((hma_1[i] - hma_1[i + 1]) >
                         (hma_1[i + 1] - hma_1[i + 2]))):

                vol_avg = 0

                for k in range(14):
                    vol_avg += stock_data[i + k]["volume"]

                vol_avg = vol_avg / 14

                if ((stock_data[i]["volume"] * atr_2[i] > vol_avg * atr_1[i])
                        and (stock_data[i]["close"] > stock_data[i]["open"])
                        and
                    (stock_data[i]["close"] > stock_data[i + 1]["close"])):

                    if (trade["position"] != "L"):

                        trade["position"] = "L"
                        trade["EK"] = stock_data[i]["close"]
                        trade["Anzahl"] = round(
                            (0.005 * sum) / (2.5 * atr_1[i]))
                        trade["SL"] = stock_data[i]["close"] - (2.5 * atr_1[i])
                        trade["TP"] = stock_data[i]["close"] + (2.5 * atr_1[i])

                        print("--------------------!!!--------------------")
                        print("Buy " + str(trade["Anzahl"]) + " Stück für " +
                              str(trade["EK"]) + " am " +
                              str(stock_data[i]["date"]))
                        print("Stop Loss 2.5 ATR: " + str(trade["SL"]))

                        break_even = trade["TP"]
                    '''
                    if (trade["SL"] > break_even) and (stock_data[i]["close"] > break_even):

                        l_anzahl = 0
                        l_anzahl = round((0.02*sum)/(stock_data[i]["close"] - trade["SL"])) # 1% Risiko (Risiko = Abstand zum SL)

                        trade ["EK"] = ((trade["EK"] * trade["Anzahl"]) + (stock_data[i]["close"] * l_anzahl)) / (trade["Anzahl"] + l_anzahl)
                        trade["Anzahl"] += l_anzahl
                        trade["TP"] = stock_data[i]["close"] + (2.5*atr_1[i])

                        print("NACHKAUF " + str(l_anzahl) + " Stück für " + str(stock_data[i]["close"]) + " am " + str(stock_data[i]["date"]))
                        print("TP: " + str(trade["TP"]))

                        break_even = trade["TP"]
                    '''

########################GO SHORT##################################
        '''
        elif (cci[i] < -100) and (atr_2[i] > atr_1[i]):

            #if (hma_1[i] > hma_2[i]) and (hma_1[i] > hma_3[i]):

            if (((hma_1[i] - hma_1[i+1]) < 0) and ((hma_2[i] - hma_2[i+1]) < 0) and ((hma_3[i] - hma_3[i+1]) < 0) and ((sma[i] - sma[i+1]) < 0) and ((hma_1[i] - hma_1[i+1]) < (hma_1[i+1] - hma_1[i+2]))):

                vol_avg = 0

                for k in range(14):
                    vol_avg += stock_data[i+k]["volume"]

                vol_avg = vol_avg/14

                if ((stock_data[i]["volume"]*atr_2[i] > vol_avg*atr_1[i]) and (stock_data[i]["close"] < stock_data[i]["open"]) and (stock_data[i]["close"] < stock_data[i+1]["close"])):

                    if (trade["position"] != "S"):

                        trade["position"] = "S"
                        trade["EK"] = stock_data[i]["close"]
                        trade["Anzahl"] = round((0.02*sum)/(2.5*atr_1[i]))
                        trade["SL"] = stock_data[i]["close"] + (2.5*atr_1[i])
                        trade["TP"] = stock_data[i]["close"] - (2.5*atr_1[i])


                        print("--------------------!!!--------------------")
                        print("SHORT " + str(trade["Anzahl"]) + " Stück für " + str(trade["EK"]) + " am " + str(stock_data[i]["date"]))
                        print("Stop Loss 2.5 ATR: "+str(trade["SL"]))

                        break_even = trade["TP"]

                    if (trade["SL"] < break_even) and (stock_data[i]["close"] < break_even):

                        l_anzahl = 0
                        l_anzahl = round((0.02*sum)/(trade["SL"] - stock_data[i]["close"])) # 1% Risiko (Risiko = Abstand zum SL)

                        trade ["EK"] = ((trade["EK"] * trade["Anzahl"]) + (stock_data[i]["close"] * l_anzahl)) / (trade["Anzahl"] + l_anzahl)
                        trade["Anzahl"] += l_anzahl
                        trade["TP"] = stock_data[i]["close"] - (2.5*atr_1[i])

                        print("NACHKAUF " + str(l_anzahl) + " Stück für " + str(stock_data[i]["close"]) + " am " + str(stock_data[i]["date"]))
                        print("TP: " + str(trade["TP"]))

                        break_even = trade["TP"]
        '''

    print(str(sum) + " " + str(l_count2) + "/" + str(l_count1))
Exemple #8
0
def backtest(stock):

    #set indicators
    sma_long_len = 100
    ema_short_len = 38

    cci_length = 20

    atr_length = 14

    #get stock data
    stock_data = get_stock_data_wotd(stock)

    if (stock_data["open"][0] <= 0) and (stock_data["close"][0] <= 0):
        return

    #get indicator values
    cci = get_cci(stock_data, cci_length)

    ema_short = get_ema(stock_data, ema_short_len)
    sma_long = get_sma(stock_data, sma_long_len)

    atr = get_atr(stock_data, atr_length)

    # Alle Listen so lange machen, wie die kürzeste, damit von hinten durchiteriert werden kann
    min_length = min(len(cci), len(ema_short), len(sma_long), len(atr))
    cci = cci[:min_length]
    ema_short = ema_short[:min_length]
    atr = atr[:min_length]

    stock_data["open"] = stock_data["open"][:min_length]
    stock_data["high"] = stock_data["high"][:min_length]
    stock_data["low"] = stock_data["low"][:min_length]
    stock_data["close"] = stock_data["close"][:min_length]
    stock_data["volume"] = stock_data["volume"][:min_length]
    stock_data["date"] = stock_data["date"][:min_length]

    trade = {"position": "$", "EK": 0, "SL": 0}
    sum = 10000
    cci_reversal = False
    #sma_long_len größte Zahl
    for i in range(-1, (-1) * (len(stock_data["open"])), -1):

        if (trade["position"] == "$"):
            cci_reversal = False
        elif (trade["position"] == "L") and (cci[i] > 0):
            cci_reversal = False
        elif (trade["position"] == "S") and (cci[i] < 0):
            cci_reversal = False

        if trade["position"] == "L":
            if ((cci[i] < 0) and (cci[i + 1] < 0) and
                (cci[i] < cci[i + 1]) and not cci_reversal) or (cci[i] < -100):
                print("CCI = " + str(cci[i]) + " - Sold at " +
                      str(stock_data["open"][i - 1]) + " am " +
                      str(stock_data["date"][i]) + " - Profit: " +
                      str((stock_data["open"][i - 1] - trade["EK"]) /
                          trade["EK"] * 100) + "%")
                sum += sum * (
                    (stock_data["open"][i - 1] - trade["EK"]) / trade["EK"])
                trade = {"position": "$", "EK": 0, "SL": 0}
                cci_reversal = False

        elif trade["position"] == "S":
            if ((cci[i] > 0) and
                (cci[i + 1] > 0) and not cci_reversal) or (cci[i] > 100):
                print("CCI = " + str(cci[i]) + " - Sold at " +
                      str(stock_data["open"][i - 1]) + " - Profit: " +
                      str((trade["EK"] - stock_data["open"][i - 1]) /
                          trade["EK"] * 100) + "%")
                sum += sum * (
                    (trade["EK"] - stock_data["open"][i - 1]) / trade["EK"])
                trade = {"position": "$", "EK": 0, "SL": 0}
                cci_reversal = False

        #check trend with ema crossover
        if ema_short[i] > sma_long[i]:

            if trade["position"] == "S":
                print("EMA Cross against Trade - Sold at " +
                      str(stock_data["open"][i - 1]) + " - Profit: " +
                      str((stock_data["open"][i - 1] - trade["EK"]) /
                          trade["EK"] * 100) + "%")
                trade = {"position": "$", "EK": 0, "SL": 0}

            #Kommt CCI aus Short-Trend und ist jetzt neutral/long?
            for k in range(1, 3):
                if i + k > len(cci) - 1:
                    cci_reversal = False

                elif (cci[i] > -100) and (cci[i + k] < -100):
                    cci_reversal = True
                    break
            '''
            print("LONG")
            print("DATE " + str(stock_data["date"][i]))
            print("POS " + trade["position"])
            print("CCI " + str(cci[i]))
            print("CCI Rev " + str(cci_reversal))
            print("EMA heute " + str(ema_short[i]))
            print("EMA gestern " + str(ema_short[i+1]))
            print("SMA heute " + str(sma_long[i]))
            '''

            ema_momentum = ((ema_short[i] - ema_short[i + 1]) > 0) and (
                (ema_short[i] - ema_short[i + 1]) >
                (ema_short[i + 1] - ema_short[i + 2])) and (
                    (ema_short[i + 1] - ema_short[i + 2]) >
                    (ema_short[i + 2] - ema_short[i + 3]))
            sma_momentum = ((sma_long[i] - sma_long[i + 1]) > 0.0015) and (
                (sma_long[i] - sma_long[i + 1]) >
                (sma_long[i + 1] - sma_long[i + 2])) and (
                    (sma_long[i + 1] - sma_long[i + 2]) >
                    (sma_long[i + 2] - sma_long[i + 3]))

            rel_abstand_ema_sma = (
                (ema_short[i] - sma_long[i]) / sma_long[i] > 0.05) and (
                    (ema_short[i] - sma_long[i]) >
                    (ema_short[i + 1] - sma_long[i + 1]))

            #wenn CCI im Longtrend oder Reversal aus -100 und ema steigt
            if (trade["position"] == "$") and (
                (cci[i] > 0) or (cci_reversal)) and (cci[i] > cci[i + 1]) and (
                    ema_momentum) and (sma_momentum) and (rel_abstand_ema_sma):

                print("--------------------!!!--------------------")
                print("GO LONG am " + str(stock_data["date"][i]) + " at " +
                      str(stock_data["close"][i]))
                print("Stop Loss 2D Low - 1 ATR: " + str(
                    min(stock_data["low"][i], stock_data["low"][i + 1],
                        stock_data["low"][i + 2]) - atr[i]))
                print("Stop Loss 2 ATR: " + str(stock_data["low"][i] -
                                                (2 * atr[i])))
                print("--------------------!!!--------------------")

                trade["position"] = "L"
                trade["EK"] = stock_data["close"][i]
                trade["SL"] = min(stock_data["low"][i],
                                  stock_data["low"][i + 1],
                                  stock_data["low"][i + 2]) - atr[i]