Package: netrade
NetradeNetrade is an AI trading assistant with human trader approachTable of contentsHow it worksPerformance ResultInstallationUsageTrainingInference Mode or TestingPrepare Real-time DataRun in real world and real-time dataHow it worksThis AI model will predict the price willgo uporgo downbased on chart pattern and candlestick patternThe data is availablehereChart PatternChart pattern is enough for analyzing price will go up or go down as usualy traders do, here's some example:Candle StickBechause of chart pattern has a limitation we need candle stick pattern to decide when we should buy or sell, here is some examplefirst row isbearishcandle stick pattern and the second rowbulishcandle stickPerformanceWe've been tested this model in 1 week and here's the result:model accuracy & lossprofitWin & loss rate ( 61% win 39% loss )Installationgithubgitclonehttps://github.com/rizki4106/netrade.gitcdnetrade&&pip3installtorchtorchmetricsscikit-imagePillowtorchvisionpypipip3installnetradeUsageThis step devided into 2 stepTrainingdownload pre-trained model herehttps://github.com/rizki4106/netrade/releases/but
if you want to train with your own data, here's the stepPrepare The dataYou should put your image in this pattern:chart:
----up:
------image1.png
------image2.png
----down:
------image1.png
------image2.png
candle:
------image1.png
------image2.png
----down:
------image1.png
------image2.pngMake csv file that contain this fieldchart_namecandle_namepathlabelfilename.pngfilename.pngdown0filename.pngfilename.pngup1filename.pngfilename.pngdown0filename.pngfilename.pngdown0filename.pngfilename.pngup1you can do it by using data preprocessing helper easlyfromnetrade.dataimportDataPreprocessing# initialize classchart="/path/to/somwhere/chart/"candle="/path/to/somewhere/candle"prep=DataPreprocessing(chart_path=chart,candle_path=candle)# create dataframeframe=prep.create_frame()frame.head()Create image transformerfromtorchvisionimporttransforms# this is for chart patternchart_transformer=transforms.Compose([transforms.Resize((224,224)),transforms.ToTensor()])# this is for candlestick patterncandle_transformer=transforms.Compose([transforms.Resize((64,64)),transforms.ToTensor()])Load your data with data loaderfromnetrade.dataimportNetradeDataLoader# supposed you have created csv file like i mention aboveframe=pd.read_csv("file_training.csv")# load data and turn it into tensortrain_data=NetradeDataLoader(chart_dir="/path/to/root/chart-pattern/",candle_dir="/path/to/root/candle-stick/",frame=frame,chart_transform=chart_transformer,candle_transform=candle_transformer)# this data loader will return [chart image, candle image and labels]Create bathesfromtorch.utils.dataimportDataLoaderdataloader=DataLoader(train_data,batch_size=16,shuffle=True)Run training loopfromnetrade.coreimportNetradeimporttorch# initialize the modelnetrade=Netrade()# run trainingmodel,history=netrade.train(X_train=dataloader,epochs=10)# model is pure pytorch nn module that has been trained with your own data you can check it model.parameters()# history is the result from training loopprint(history)# save the model's statetorch.save(model.state_dict(),"name-it.pth")Inference mode / TestingReal-time DataIf you want to use this model in real time data, you should prepare the comodity price history i.ebitcoinorteslastock price. in this example I'll be usingyfinanceto grab the historical datafromnetrade.dataimportdata_creationimportyfinanceasyfimportmatplotlib.pyplotasplt#ticker=yf.Ticker('BTC-USD')data=ticker.history(period="7d",interval="15m")# create chart image from realtime history data# take the last 50 candle stick barchart_image=data_creation.create_image(data=data[-50:,:])# take the last 3 candle stick barcandle_image=data_creation.create_image(data=data[-3:,:])# create_image returns PIL image class that you can use directly with pytorch# you can show it by the wayplt.imshow(chart_image)plt.imshow(candle_image)Final PreparationIt's time to predict real - time price, let's put everything togetherfromnetrade.dataimportdata_creationfromnetrade.coreimportNetradefromtorchvisionimporttransformsfromPILimportImageimportyfinanceasyf# initialize the modelnetrade=Netrade(saved_state_path="path-to-saved-state.pth")# create image transformer# this is for chart patternchart_transformer=transforms.Compose([transforms.Resize((224,224)),transforms.ToTensor()])# this is for candlestick patterncandle_transformer=transforms.Compose([transforms.Resize((64,64)),transforms.ToTensor()])# load realtime dataticker=yf.Ticker('BTC-USD')data=ticker.history(period="7d",interval="15m")# create chart image from real-time datachart_image=data_creation.create_image(data=data[-50:,:])candle_image=data_creation.create_image(data=data[-3:,:])# turn image into tensorchart_image=chart_transformer(chart_image)candle_image=candle_transformer(candle_image)# run predictionpreds=netrade.predict(chart_image=chart_image,candle_image=candle_image)# print the resultprint(preds.argmax(1))# 0 price will go down, 1 price will go up