def macd(coin_name, amount):

    model = MLPRegressor(coin=coin_name.lower(), model=MODEL)
    model.to(DEVICE)

    dataloaderX = cryptoData(coin_name.lower(), DEVICE=DEVICE)
    DAYS = len(dataloaderX)
    model.eval(dataloaderX[33][0])

    macd_line = []
    signal_line = []
    x = []

    start_amount = amount
    no_of_coins = 0
    print("\n", coin_name.upper(), ":")

    for i, (x_input, target) in enumerate(
            dataloaderX
    ):  # TODO: Discuss standard time slot across all algorithms
        if i < 34:  # let dataloader catchup with macd range
            continue
        if i == DAYS:
            break

        predictedPrice = model(x_input) * dataloaderX.pmax

        (macd, macd_9, ema_12, ema_26, price) = dataloaderX.getMacDData(i)

        ema_12 = ema_12 + (predictedPrice - ema_12) * dataloaderX.multiplier_12
        ema_26 = ema_26 + (predictedPrice - ema_26) * dataloaderX.multiplier_26
        macd = ema_12 - ema_26
        macd_9 = macd_9 + (macd - macd_9) * dataloaderX.multiplier_9

        x.append(i)
        macd_line.append(macd)
        signal_line.append(macd_9)

        # If MACD crosses over Signal Line then bullish market
        if macd - macd_9 > 0 and no_of_coins == 0:
            no_of_coins = amount / price
            amount = 0

        # If MACD crosses below Signal Line then bearish market
        if macd - macd_9 <= 0 and no_of_coins != 0:
            amount = no_of_coins * price
            no_of_coins = 0

    if amount == 0:
        amount = no_of_coins * price

    print("\nStart: ", start_amount, " End: ", amount)

    macd_line = np.asarray(macd_line)
    signal_line = np.asarray(signal_line)
    x = np.asarray(x)
Esempio n. 2
0
DEVICE = torch.device("cpu")
MODE = "test"
COIN = "btc"
MODEL = "norm"
STOCHASTIC = True

if __name__ == "__main__":

    if MODE == "train":
        test = False

    else:
        test = True

    model = MLPRegressor(coin=COIN, model=MODEL, stochastic=STOCHASTIC)
    model.to(DEVICE)

    dataloader = cryptoData(COIN, test=test, DEVICE=DEVICE, model=MODEL)
    breaker = len(dataloader)

    for j in range(100):
        model.eval(dataloader[0][0])
        t, h = [], []
        z = 0
        m = 0
        r = 0
        for i, data in enumerate(dataloader):
            if i == breaker:
                break
            x, target = data
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, myPath + '/../')

import torch
import matplotlib.pyplot as plt

from data.trader import hourly
from models.model import SeqRegressor, MLPRegressor

DEVICE = torch.device("cpu")
COIN = "ltc"
MODEL = "norm"

if __name__ == "__main__":

    model = MLPRegressor(coin=COIN, model=MODEL)
    model.to(DEVICE)

    dataloader = hourly(COIN, DEVICE=DEVICE, model=MODEL)
    DAYS = len(dataloader)
    model.eval(dataloader[0][0])

    cash = 1000
    coin = 0
    transactions = 0
    state = "CASH"
    for i, (x, hourly, target) in enumerate(dataloader):
        if i <= DAYS / 2:
            continue
        if i == DAYS:
            break
Esempio n. 4
0
def main(COIN1, COIN2):
    model_coin1 = MLPRegressor(coin=COIN1, model=MODEL)
    model_coin1.to(DEVICE)
    model_coin2 = MLPRegressor(coin=COIN2, model=MODEL)
    model_coin2.to(DEVICE)

    dataloader_coin1 = cryptoData(COIN1, DEVICE=DEVICE, model=MODEL)
    DAYS_coin1 = len(dataloader_coin1)
    dataloader_coin2 = cryptoData(COIN2, DEVICE=DEVICE, model=MODEL)
    DAYS_coin2 = len(dataloader_coin2)

    model_coin1.eval(dataloader_coin1[0][0])
    model_coin2.eval(dataloader_coin2[0][0])

    residualModel = Residual(dataloader_coin1, dataloader_coin2)

    coin1_amt = 0
    coin2_amt = 0
    cash = 0

    startDay = 34
    trendThreshold = 1
    shorts = longs = holds = 0
    time = 0
    for i in range(startDay, min(DAYS_coin1, DAYS_coin2)):
        time += 1
        x_coin1, target_coin1 = dataloader_coin1[i]
        x_coin2, target_coin2 = dataloader_coin2[i]
        price_coin1 = dataloader_coin1.getDataFrame(i, 1)
        price_coin2 = dataloader_coin2.getDataFrame(i, 1)

        if i == startDay:
            coin1_amt = 5000 / price_coin1
            coin2_amt = 5000 / price_coin2

        out_coin1 = model_coin1(x_coin1)
        out_coin2 = model_coin2(x_coin2)

        zScore, risk = residualModel.zScore(i, out_coin1, out_coin2)
        trend_coin1 = getGeneralTrends(dataloader_coin1, i)
        trend_coin2 = getGeneralTrends(dataloader_coin2, i)

        if not risk:
            if zScore > 1:
                shorts += 1
                if trend_coin2 > trendThreshold:
                    temp = coin1_amt * price_coin1
                    coin1_amt = 0
                    coin2_amt += (temp / price_coin2)
                # print("\t",i,"Transaction: short at ",price_coin1.item(),price_coin2.item())
            elif zScore < -1:
                longs += 1
                if trend_coin1 > trendThreshold:
                    temp = coin2_amt * price_coin2
                    coin2_amt = 0
                    coin1_amt += (temp / price_coin1)
                # print("\t",i,"Transaction: long at ",price_coin1.item(),price_coin2.item())
            else:
                holds += 1

        out_coin1 = out_coin1.item() * dataloader_coin1.pmax.item()
        out_coin2 = out_coin2.item() * dataloader_coin2.pmax.item()
    print(COIN1, COIN2, "\n\t",
          (coin1_amt * price_coin1) + (coin2_amt * price_coin2) + cash)
    print(time, '\n')
Esempio n. 5
0
def boll_band(coin, amount):

    model = MLPRegressor(coin=coin, model=MODEL)
    model.to(DEVICE)

    print("\n", coin.upper(), ":")
    dataloader = cryptoData(coin, DEVICE=DEVICE)
    model.eval(dataloader[28][0])
    start = amount
    cash = amount
    no_of_coins = 0

    mean = []
    ub = []
    lb = []
    x = []

    buyAndHold = 10000
    first = False
    time = 0
    for i, (x_input, target) in enumerate(
            dataloader
    ):  # TODO: Discuss standard time slot across all algorithms
        # Wait for the Moving Average
        if (i < 34):
            continue
        (sma_20, sma_30, sma_5, ubb, lbb,
         price) = dataloader.getBollBandData(i)

        if not first:
            first = True
            buyAndHold = buyAndHold / target

        trend = sma_5 - sma_30
        x.append(i)
        ub.append(ubb)
        lb.append(lbb)
        mean.append(sma_20)

        predictedPrice = model(x_input) * dataloader.pmax

        if trend > 0:
            # If Upward Trend and price touches Upper Bollinger Band then Buy
            if predictedPrice - ubb >= 0 and no_of_coins == 0:
                # print("Buy\n")
                no_of_coins = cash / price
                cash = 0

        else:
            # If Downward Trend and price touches Lower Bollinger Band then Sell
            if predictedPrice - lbb <= 0 and no_of_coins != 0:
                # print("Sell\n")
                cash = no_of_coins * price
                no_of_coins = 0
        time += 1

    if no_of_coins != 0:
        cash = no_of_coins * price
        no_of_coins = 0

    print("\nStart: ", start, " End: ", cash)
    print(buyAndHold * target, time)
Esempio n. 6
0
from data.norm_loader import cryptoData
from models.model import  MLPRegressor

DEVICE = torch.device("cuda:0")
MODE = "train"
# MODE = "test"

if __name__ == "__main__":

    if MODE == "train":
        test = False
        
    else:
        test = True

    model = MLPRegressor()
    # model.load_state_dict(torch.load("weights/mlpreg_final.pth"))
    
    optimizer = Adam(model.parameters(), lr=0.00001, weight_decay=0.00001)
    lossfn = nn.MSELoss(reduction='mean')

    dataloader = cryptoData("btc",test=test,DEVICE=DEVICE)

    model.to(DEVICE)

    breaker = len(dataloader)
    
    for j in range(700):
        tots = 0
        for i,(x,target) in enumerate(dataloader):
            if i == breaker:
Esempio n. 7
0
def macd(coin_name, amount):

    model = MLPRegressor(coin=coin_name.lower(), model=MODEL)
    model.to(DEVICE)

    dataloaderX = cryptoData(coin_name.lower(), DEVICE=DEVICE)
    DAYS = len(dataloaderX)
    model.eval(dataloaderX[33][0])

    sma5_line = []
    sma34_line = []
    ao_line = []

    start_amount = amount
    no_of_coins = 0
    print("\n", coin_name.upper(), ":")

    buyAndHold = 10000
    first = False
    time = 0
    for i, (x_input, target) in enumerate(
            dataloaderX
    ):  # TODO: Discuss standard time slot across all algorithms
        if i < 34:  # let dataloader catchup with macd range
            continue
        if i == DAYS:
            break

        if not first:
            first = True
            buyAndHold = buyAndHold / target

        predictedPrice = model(x_input) * dataloaderX.pmax

        sma_5, sma_34, AO, price, (new_sma5_lower,
                                   new_sma34_lower) = dataloaderX.getAOData(i)
        sma5_line.append(sma_5)
        sma34_line.append(sma_34)
        ao_line.append(AO)

        predicted_sma5 = (new_sma5_lower + predictedPrice) / 2
        predicted_sma34 = (new_sma34_lower + predictedPrice) / 2

        # Averaging out AO and predicted AO
        AO = (predicted_sma5 - predicted_sma34 + AO) / 2

        if AO > 0 and no_of_coins == 0:
            no_of_coins = amount / price
            amount = 0

        if AO <= 0 and no_of_coins != 0:
            amount = no_of_coins * price
            no_of_coins = 0
        # print(predicted_sma5.item())
        time += 1

    if amount == 0:
        amount = no_of_coins * price

    print("\nStart: ", start_amount, " End: ", amount)
    print(buyAndHold * target, time)
    sma5_line = np.asarray(sma5_line)
    sma34_line = np.asarray(sma34_line)
    ao_line = np.asarray(ao_line)