Code Monkey home page Code Monkey logo

Comments (6)

zyzhu avatar zyzhu commented on June 20, 2024

The following snippet only checks close and open prices. You may expand it to make more complex logic assuming you already calculated ema indicators

var buy = df.Rows.Select(kvp => {
    var sr = kvp.Value.As<float>();
    var open = sr.Get("Open");
    var close = sr.Get("Close");
    return open > close ? 1.0 : 0.0;
});
df.AddColumn("Buy", buy);

from deedle.

Hulkstance avatar Hulkstance commented on June 20, 2024

@zyzhu, thanks for your answer. By following your way,

kvp.Value.GetAs<decimal>("Rsi")

returns Deedle.MissingValueException: 'Value at the key Rsi is missing' because the values are probably <missing> and not filled with zeroes.

This is the actual strategy

def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
	dataframe['rsi'] = ta.RSI(dataframe['close'], timeperiod=2)
	return dataframe

def informative_pairs(self):
	informative_pairs = []
	return informative_pairs

def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
	
	dataframe.loc[
		(dataframe['rsi'] < 45
		& (dataframe['rsi'] > dataframe['rsi'].shift(1))),
		'buy'] = 1
	return dataframe

def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
	
	dataframe.loc[                
		(dataframe['rsi'].gt(70)),
		'sell'] = 0
	return dataframe

Zip works fine, but Item1 is not descriptive.

csharp
var buy = a.ZipInner(b).Select(kvp => kvp.Value.Item1 < 45 && kvp.Value.Item1 > kvp.Value.Item2);

from deedle.

zyzhu avatar zyzhu commented on June 20, 2024

You may either fill missing first or use TryGet to get an OptionalValue. Then check it like the following. The actual logic is up to you.

var buy = df.Rows.Select(kvp => {
    var sr = kvp.Value.As<float>();
    var open = sr.TryGet("Open");
    var close = sr.TryGet("Close");
    return open.HasValue && close.HasValue && open.Value > close.Value ? 1.0 : 0.0;
});
df.AddColumn("Buy", buy);

from deedle.

Hulkstance avatar Hulkstance commented on June 20, 2024

@zyzhu, it seems like I need to use the Zip anyway. This looks good? P.S. I don't want to use TryGet because it's not generic.

public override Frame<int, string> PopulateIndicators(Frame<int, string> df)
{
	var candles = df.Rows.Select(kvp => new Ohlcv
	{
		Timestamp = kvp.Value.GetAs<DateTime>("Timestamp"),
		Open = kvp.Value.GetAs<decimal>("Open"),
		High = kvp.Value.GetAs<decimal>("High"),
		Low = kvp.Value.GetAs<decimal>("Low"),
		Close = kvp.Value.GetAs<decimal>("Close"),
		Volume = kvp.Value.GetAs<decimal>("Volume")
	}).Observations.Select(e => e.Value).ToList<IOhlcv>();

	df.AddColumn("Rsi", candles.Rsi(2));

	return df;
}

public override Frame<int, string> PopulateBuyTrend(Frame<int, string> df)
{
	var a = df.GetColumn<decimal>("Rsi").Realign(Enumerable.Range(0, df.RowCount)).FillMissing(0m);
	var b = df.GetColumn<decimal>("Rsi").Shift(1).Realign(Enumerable.Range(0, df.RowCount)).FillMissing(0m);

	var buy = a.ZipInner(b).Select(kvp =>
	{
		var rsi = kvp.Value.Item1;
		var rsiShifted = kvp.Value.Item2;

		return rsi < 45 && rsi > rsiShifted;
	});
	df.AddColumn("Buy", buy);

	return df;
}

public override Frame<int, string> PopulateSellTrend(Frame<int, string> df)
{
	var a = df.GetColumn<decimal>("Rsi").Realign(Enumerable.Range(0, df.RowCount)).FillMissing(0m);

	var sell = a.Select(kvp =>
	{
		var rsi = kvp.Value;

		return rsi > 70;
	});
	df.AddColumn("Sell", sell);

	return df;
}

from deedle.

zyzhu avatar zyzhu commented on June 20, 2024

That looks good. Just one feedback. You may return buy or sell series directly instead of adding it into the df so that you can keep the original df intact. If "Buy" column exists in df already the next time you run it, AddColumn will throw error.

from deedle.

Hulkstance avatar Hulkstance commented on June 20, 2024

@zyzhu, thanks a lot! :)

from deedle.

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.