Comments (6)
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.
@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.
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.
@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.
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.
@zyzhu, thanks a lot! :)
from deedle.
Related Issues (20)
- Sample code snippet and introduction for the fslab website HOT 2
- Signature of Matrix.dot is unnecessarily limited HOT 3
- Breaking change for Min/Max stats function between v2.1.0 and 2.1.1 HOT 2
- Frame.mapColValues is weirdly slow compared to mapping columns as series and joining with Frame.ofColumns HOT 1
- Frame.toArray2D throws a System.Format exception HOT 1
- Optionalvalue ignores culture
- "Select not supported" for GetRowsAs<'T>
- FSLab page out of date, not working HOT 1
- Unable to load text file with space seperated
- Stats.cov throws if Stats.stdDev contains missing values HOT 2
- Change melt to have optional parameters in line with Pandas DateFrame and R's data.table
- Deedle Finance.ewmVol is returning the rolling mean not the standard deviation HOT 1
- Exporting frames to a json
- Suggestion to distinct rows by specified columns HOT 2
- Series.windowSize throws IndexOutOfRangeException when size is bigger than input series length HOT 1
- About latest .net interactive
- Frame.ofRecords fails silently when underlaying record type is internal or private
- Implement IDataView interface
- Performance of CSV writer HOT 1
- Add Series.Diff method that operates on DateTime to return series of TimeSpan
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deedle.