Giter Site home page Giter Site logo

Comments (4)

sdcoffey avatar sdcoffey commented on August 19, 2024

Hey @Krisa,

sorry for the delayed response here. Can you provide an example?

from techan.

0cv avatar 0cv commented on August 19, 2024

@sdcoffey

here:

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/adshao/go-binance"
	"github.com/sdcoffey/big"
	"github.com/sdcoffey/techan"
)

func main() {
	series := getSeries()
	closePrices := techan.NewClosePriceIndicator(series)

	// Show EMAs
	EMA10 := techan.NewEMAIndicator(closePrices, 10)
	EMA20 := techan.NewEMAIndicator(closePrices, 20)
	EMA10Price := EMA10.Calculate(series.LastIndex() - 1).Float()
	EMA20Price := EMA20.Calculate(series.LastIndex() - 1).Float()

	log.Printf("EMA10Price: %v", EMA10Price)
	log.Printf("EMA20Price: %v", EMA20Price)

	// Show RSI
	rsi14 := techan.NewRelativeStrengthIndexIndicator(closePrices, 14)
	rsi14Calculated := rsi14.Calculate(series.LastIndex() - 1).Float()

	log.Printf("rsi14: %v", rsi14Calculated)
}

func getSeries() *techan.TimeSeries {
	binanceClient := binance.NewClient("", "")
	klines, err := binanceClient.
		NewKlinesService().
		Symbol("BTCUSDT").
		Interval("1h").
		Do(context.Background())

	if err != nil {
		fmt.Println(err)
		return nil
	}

	series := techan.NewTimeSeries()

	for _, kline := range klines {
		start := time.Unix(0, 1000000*kline.OpenTime)
		end := time.Unix(0, 1000000*kline.CloseTime)
		period := techan.NewTimePeriod(start, end.Add(1*time.Millisecond).Sub(start))

		candle := techan.NewCandle(period)
		candle.OpenPrice = big.NewFromString(kline.Open)
		candle.ClosePrice = big.NewFromString(kline.Close)
		candle.MaxPrice = big.NewFromString(kline.High)
		candle.MinPrice = big.NewFromString(kline.Low)
		candle.Volume = big.NewFromString(kline.Volume)
		candle.TradeCount = uint(kline.TradeNum)

		series.AddCandle(candle)
	}
	return series
}

You can compare to the real chart here: https://www.tradingview.com/symbols/BTCUSDT/?exchange=BINANCE

EMA 10 and 20 are both perfectly calculated, RSI, at the time of writing (since it takes time.Now() - 1 hour) is quite off (45.53 real, 51.26 calculated)

from techan.

sdcoffey avatar sdcoffey commented on August 19, 2024

Thanks for the snippet @Krisa, let me see if I can fix

from techan.

sdcoffey avatar sdcoffey commented on August 19, 2024

@Krisa sorry for taking so long to get to this. The issue should be fixed in v0.8.0, please give it a whirl and re-open if it persists.

from techan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.