File size: 2,022 Bytes
63bad2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Model deepseek-reasoner
import backtrader as bt

class TripleMACross(bt.Strategy):
    """
    Triple moving average crossover strategy with 10% position sizing.
    - Entry: Fast SMA (5) crosses above Medium SMA (10) and Medium SMA is above Slow SMA (20)
    - Exit: Fast SMA crosses below Medium SMA OR Medium SMA crosses below Slow SMA
    - Position sizing: 10% of portfolio per trade
    """
    params = (
        ('fast_period', 5),
        ('medium_period', 10),
        ('slow_period', 20),
    )

    def __init__(self):
        # Three moving averages
        self.sma_fast = bt.indicators.SMA(period=self.p.fast_period)
        self.sma_medium = bt.indicators.SMA(period=self.p.medium_period)
        self.sma_slow = bt.indicators.SMA(period=self.p.slow_period)
        
        # Crossover indicators
        self.cross_fast_medium = bt.indicators.CrossOver(self.sma_fast, self.sma_medium)
        self.cross_medium_slow = bt.indicators.CrossOver(self.sma_medium, self.sma_slow)
        
        # Track position for conditional logic
        self.position_open = False

    def next(self):
        # Entry condition: Fast crosses above Medium AND Medium > Slow (no existing position)
        if not self.position:
            if self.cross_fast_medium > 0 and self.sma_medium[0] > self.sma_slow[0]:
                self.buy(size=self.get_target_size())  # Use dynamic sizing
                self.position_open = True
                
        # Exit conditions: Fast crosses below Medium OR Medium crosses below Slow
        elif self.position_open:
            if self.cross_fast_medium < 0 or self.cross_medium_slow < 0:
                self.close()
                self.position_open = False

    def get_target_size(self):
        """Calculate 10% of current portfolio value"""
        return int((self.broker.getvalue() * 0.90) / self.data.close[0])

# Initialize Cerebro with strategy and sizer
cerebro = bt.Cerebro()
cerebro.addstrategy(TripleMACross, fast_period=5, medium_period=10, slow_period=20)