Fixed Ichimoku bugs

This commit is contained in:
Luke I. Wilson 2023-05-24 16:31:13 -05:00
parent b5434fb5de
commit 74855ba4b1
2 changed files with 29 additions and 13 deletions

View File

@ -34,6 +34,7 @@ func (s *IchimokuStrategy) Next(t *auto.Trader) {
// - price closed above the cloud at the current time // - price closed above the cloud at the current time
// - conversion above baseline // - conversion above baseline
// - future cloud must be green (LeadingA > LeadingB) // - future cloud must be green (LeadingA > LeadingB)
// - lagging span above lagging cloud
// Oposite conditions for sell... // Oposite conditions for sell...
@ -52,18 +53,33 @@ func (s *IchimokuStrategy) Next(t *auto.Trader) {
t.CloseOrdersAndPositions() t.CloseOrdersAndPositions()
} }
} else { } else {
// v := a - b , then comparing v > 0 is the same as a > b
closeToLeadA := data.CloseIndex(now) - leadA.FloatIndex(now)
convToBase := conv.FloatIndex(now) - base.FloatIndex(now)
leadAToLeadB := leadA.FloatIndex(now) - leadB.FloatIndex(now)
futureLeadAToLeadB := leadA.Float(-1) - leadB.Float(-1)
laggingToLeadA := lagging.FloatIndex(*laggingTime) - leadA.FloatIndex(*laggingTime)
// tw := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
// fmt.Fprintf(tw, "closeToLeadA\t%v\n", closeToLeadA)
// fmt.Fprintf(tw, "convToBase\t%v\n", convToBase)
// fmt.Fprintf(tw, "leadAToLeadB\t%v\n", leadAToLeadB)
// fmt.Fprintf(tw, "futureLeadAToLeadB\t%v\n", futureLeadAToLeadB)
// fmt.Fprintf(tw, "laggingToLeadA\t%v\n\n", laggingToLeadA)
// tw.Flush()
// Look to enter a trade // Look to enter a trade
if data.CloseIndex(now) > leadA.FloatIndex(now) && if closeToLeadA > 0 &&
leadA.FloatIndex(now) > leadB.FloatIndex(now) && leadAToLeadB > 0 &&
conv.FloatIndex(now) > base.FloatIndex(now) && convToBase > 0 &&
leadA.Float(-1) > leadB.Float(-1) && futureLeadAToLeadB > 0 &&
lagging.FloatIndex(*laggingTime) > leadA.FloatIndex(*laggingTime) { laggingToLeadA > 0 {
t.Buy(10000, 0, 0) t.Buy(10000, 0, 0)
} else if data.CloseIndex(now) < leadA.FloatIndex(now) && } else if closeToLeadA < 0 &&
leadA.FloatIndex(now) < leadB.FloatIndex(now) && leadAToLeadB < 0 &&
conv.FloatIndex(now) < base.FloatIndex(now) && convToBase < 0 &&
leadA.Float(-1) < leadB.Float(-1) && futureLeadAToLeadB < 0 &&
lagging.FloatIndex(*laggingTime) < leadA.FloatIndex(*laggingTime) { laggingToLeadA < 0 {
t.Sell(10000, 0, 0) t.Sell(10000, 0, 0)
} }
} }
@ -74,7 +90,7 @@ func main() {
auto.Backtest(auto.NewTrader(auto.TraderConfig{ auto.Backtest(auto.NewTrader(auto.TraderConfig{
Broker: auto.NewTestBroker(broker, nil, 10000, 50, 0.0002, 0), Broker: auto.NewTestBroker(broker, nil, 10000, 50, 0.0002, 0),
Strategy: &IchimokuStrategy{convPeriod: 9, basePeriod: 26, leadingPeriods: 52}, Strategy: &IchimokuStrategy{convPeriod: 9, basePeriod: 26, leadingPeriods: 52},
Symbol: "USD_JPY", Symbol: "EUR_USD",
Frequency: "M15", Frequency: "M15",
CandlesToKeep: 2500, CandlesToKeep: 2500,
})) }))

View File

@ -54,9 +54,9 @@ func Ichimoku(price *IndexedFrame[UnixTime], convPeriod, basePeriod, leadingPeri
conv := price.Highs().Copy().Rolling(convPeriod).Max().Add(price.Lows().Copy().Rolling(convPeriod).Min()).DivFloat(2) conv := price.Highs().Copy().Rolling(convPeriod).Max().Add(price.Lows().Copy().Rolling(convPeriod).Min()).DivFloat(2)
base := price.Highs().Copy().Rolling(basePeriod).Max().Add(price.Lows().Copy().Rolling(basePeriod).Min()).DivFloat(2) base := price.Highs().Copy().Rolling(basePeriod).Max().Add(price.Lows().Copy().Rolling(basePeriod).Min()).DivFloat(2)
lagging := price.Closes().Copy()
leadingA := conv.Copy().Add(base).DivFloat(2) leadingA := conv.Copy().Add(base).DivFloat(2)
leadingB := price.Highs().Copy().Rolling(leadingPeriods).Max().Add(price.Lows().Copy().Rolling(leadingPeriods).Min()).DivFloat(2) leadingB := price.Highs().Copy().Rolling(leadingPeriods).Max().Add(price.Lows().Copy().Rolling(leadingPeriods).Min()).DivFloat(2)
lagging := price.Closes().Copy()
// Return a DataFrame of the results. // Return a DataFrame of the results.
return NewIndexedFrame( return NewIndexedFrame(
@ -64,6 +64,6 @@ func Ichimoku(price *IndexedFrame[UnixTime], convPeriod, basePeriod, leadingPeri
base.SetName("Base"), base.SetName("Base"),
leadingA.SetName("LeadingA").ShiftIndex(leadingPeriods, UnixTimeStep(frequency)), leadingA.SetName("LeadingA").ShiftIndex(leadingPeriods, UnixTimeStep(frequency)),
leadingB.SetName("LeadingB").ShiftIndex(leadingPeriods, UnixTimeStep(frequency)), leadingB.SetName("LeadingB").ShiftIndex(leadingPeriods, UnixTimeStep(frequency)),
lagging.SetName("Lagging").ShiftIndex(-leadingPeriods, UnixTimeStep(frequency)), lagging.SetName("Lagging").ShiftIndex(-basePeriod, UnixTimeStep(frequency)),
) )
} }