jxch-capital / K-MLLC

Machine Learning: Lorentzian Classification
1 stars 0 forks source link

Thanks for sharing #1

Closed kiranmaya closed 4 months ago

kiranmaya commented 4 months ago

Thanks for the indicator in Python, I'm a c# developer, and not good in Python, with chatbots.I can manage somewhat executing the code, I already had a c# trading setup, and I'm thinking about, wrapping your main prediction function with Flask server , I have observed that only backtesting is available in test.ipynb . not for run-time trading. will you add the realtime function which takes candles and returns signal?

Thanks kiran

kiranmaya commented 4 months ago

able to do it with chatgpt. `from matplotlib import dates import yfinance as yf from proxy import enable from lorentzian import lorentzian_distance, lorentzian_k_nearest_neighbor, Diction, KNode, test_data_by_df,train_data_by_df, \ prediction2node from features import n_rsi, n_adx, n_cci, n_wt, HLC3, FeaturesX, FeaturesDefinition import ta.wrapper import matplotlib.pyplot as plt import mplfinance as mpf import pandas as pd from lightweight_charts import Chart

from flask import Flask, request, jsonify from lightweight_charts import Chart

app = Flask(name)

@app.route('/signals', methods=['POST']) def get_signals(): data = request.json # Get the JSON data from the request signals = process_candlesticks(data) # Process the candlestick data return jsonify(signals) # Return the signals as JSON

def buy_signal(the_current, the_test): return (

the_test.info['Close-SMA'] > 0 and

            the_test.info['Close-EMA'] > 0 
            # and the_test.info['Close'] >the_test.info['Open'] 
            and the_current.diction == Diction.LONG and 8 >= the_current.prediction > 2)

def sell_signal(the_current, the_test): return (

the_test.info['Close-SMA'] < 0 and

            the_test.info['Close-EMA'] < 0 
            # and the_test.info['Close'] < the_test.info['Open'] 
            and the_current.diction == Diction.SHORT and -8 <= the_current.prediction < -2)

def process_candlesticks(candlesticks): all_signals = {"prediction":0,"closeDateTime":'',"Close-EMA":0} candle_dicts = [] for candle in candlesticks: candle_dicts.append({ 'closeDateTime': candle['closeDateTime'], 'openPrice': candle['openPrice'], 'highPrice': candle['highPrice'], 'lowPrice': candle['lowPrice'], 'closePrice': candle['closePrice'], 'volume': candle['volume'], })

# Create DataFrame from list of dictionaries
df = pd.DataFrame(candle_dicts)
df.rename(columns={'closeDateTime': 'Datetime', 'openPrice': 'Open', 'highPrice': 'High', 'lowPrice': 'Low', 'closePrice': 'Close'}, inplace=True)

print(df)
# Assuming 'his' is supposed to be the DataFrame of candlesticks
his = df
his['SMA20'] = ta.wrapper.SMAIndicator(his['Close'], window=20).sma_indicator()
his['EMA20'] = ta.wrapper.EMAIndicator(his['Close'], window=20).ema_indicator()

his['Close-SMA'] = his["Close"] - his["SMA20"]
his['Close-EMA'] = his["Close"] - his["EMA20"]

features_definition = FeaturesDefinition.default(high=his['High'], close=his['Close'], open=his['Open'], low=his['Low'], vol=his['volume'])
his = features_definition.calculate(his)
print('==============')
trainData = test_data_by_df(his, features_definition, future_count=4)

print(his.iloc[-1]['Datetime'])

current_node = prediction2node(his.iloc[-1], trainData, features_definition, k=12)

all_signals['Close-EMA']=his.iloc[-1]['Close-EMA'] 
all_signals['prediction']=current_node.prediction

all_signals['closeDateTime']=his.iloc[-1]['Datetime']

print(all_signals)
print('==============')
return all_signals

if name == 'main': app.run(port=9000, debug=True)

    `