-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converted code to Deedle #528
Comments
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); |
@zyzhu, thanks for your answer. By following your way, kvp.Value.GetAs<decimal>("Rsi") returns This is the actual strategydef 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
|
You may either fill missing first or use 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); |
@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;
} |
That looks good. Just one feedback. You may return buy or sell |
@zyzhu, thanks a lot! :) |
I'm recreating this from python (pandas). I'm new to the library and I'd like to have a feedback, if something could be done better. And @zyzhu, thanks for your examples on SO and here in the issues, By the way, the strategy differs from the python code, but the idea stays.
I don't think
ZipInner
is the most human-readable way to represent the following. It would be nice if you know a better way.Full code
The text was updated successfully, but these errors were encountered: