דלגו לתוכן
פתיחת RaamTrade

RC2_FOR_ITER

תוכן זה אינו זמין עדיין בשפה שלך.

for loop iterable must be range(...) or a persistent-state list/dict.

What it means

There are exactly two things you can loop over: an integer range(...), and a list or dict declared in the class body (for x in self.recent_highs:, for k in self.counts:). Those are the only iterables the language has — RaamCode cannot build a collection on the fly, and there is no generator, comprehension, or iterator protocol behind the loop. Anything else has no defined element sequence, so it is refused.

Why it happens

The usual cause is a list built inline, which looks harmless but is a collection RaamCode has no way to hold:

class Bad(Strategy):
trade = Symbol()
sizing = FixedCapital()
total = 0.0
def execute(self):
for x in [self.trade.high, self.trade.low, self.trade.close]: # ❌ list literal
self.total = self.total + x

Others that land here:

  • for x in self.trade: — a Symbol is not a sequence.
  • for c in "abc": — strings are not iterable.
  • for x in self.counts.values(): — .items(), .keys() and .values() don’t exist; dict iteration already yields keys.
  • for x in map(...), zip(...), sorted(...) — blocked builtins (SDK012).

How to fix it

Declare the collection in the class body and loop over the slot, or loop over indices with range(...).

class Good(Strategy):
trade = Symbol()
sizing = FixedCapital()
recent_highs = [0.0]*5
counts = {"up": 0, "down": 0}
total = 0.0
def execute(self):
self.total = 0.0
for x in self.recent_highs: # ✅ a declared list — yields elements
self.total = self.total + x
for k in self.counts: # ✅ a declared dict — yields keys
self.total = self.total + self.counts[k]
for i in range(5): # ✅ an integer range
self.total = self.total + self.recent_highs[i]
if self.total > 0 and self.trade.is_flat:
self.buy(self.trade)

range(len(self.recent_highs)) works too, if the length should follow the declaration rather than being written out.

  • For Loops — the three shapes and the rolling-window idiom.
  • Persistent State — declaring the lists and dicts these loops walk.
  • RC2_FOR_RANGE — range(...) with the wrong number of arguments.
  • SDK012 — map, zip, enumerate, sorted and friends.