Expert4x Grid Trend Multiplier Online
def update_multiplier(self, trend_strength: float): """ Update position multiplier based on trend strength """ if trend_strength > 50: # Strong trend - increase multiplier self.total_multiplier = min( self.max_multiplier, self.total_multiplier * self.trend_multiplier ) elif trend_strength < 25: # Weak trend - decrease multiplier self.total_multiplier = max( 1.0, self.total_multiplier / self.trend_multiplier ) def check_grid_execution(self, current_price: float, grid_levels: List[float], atr: float) -> Optional[Dict]: """ Check if price hit a grid level and execute order Returns: Order details if executed, None otherwise """ for level in grid_levels: # Check if price crossed a grid level if abs(current_price - level) / level < 0.0001: # Within 0.01% # Determine direction based on trend if self.current_trend == "BULLISH": direction = "BUY" stop_loss = level * (1 - 0.02) # 2% stop loss take_profit = level * (1 + self.grid_distance_pct / 100) elif self.current_trend == "BEARISH": direction = "SELL" stop_loss = level * (1 + 0.02) take_profit = level * (1 - self.grid_distance_pct / 100) else: # Neutral - alternate direction = "BUY" if len(self.open_positions) % 2 == 0 else "SELL" stop_loss = level * (1 - 0.02) if direction == "BUY" else level * (1 + 0.02) take_profit = level * (1 + self.grid_distance_pct / 100) if direction == "BUY" else level * (1 - self.grid_distance_pct / 100) position_size = self.calculate_position_size(level) order = { 'type': direction, 'entry_price': level, 'position_size': position_size, 'stop_loss': stop_loss, 'take_profit': take_profit, 'timestamp': datetime.now(), 'grid_level': level, 'multiplier': self.total_multiplier } return order return None
print("\n" + "="*50) print("GRID TREND MULTIPLIER STRATEGY RESULTS") print("="*50) for key, value in metrics.items(): if isinstance(value, float): print(f"{key.replace('_', ' ').title()}: {value:.2f}") else: print(f"{key.replace('_', ' ').title()}: {value}") return strategy, metrics if == " main ": strategy, metrics = run_backtest() expert4x grid trend multiplier
def detect_trend(self, prices: pd.Series, volume: Optional[pd.Series] = None) -> Tuple[str, float]: """ Detect market trend using multiple indicators Returns: (trend_direction, trend_strength) """ # Calculate EMAs ema_fast = prices.ewm(span=20, adjust=False).mean() ema_slow = prices.ewm(span=50, adjust=False).mean() # Calculate ADX for trend strength high = prices.rolling(window=14).max() low = prices.rolling(window=14).min() plus_dm = high.diff() minus_dm = -low.diff() plus_dm[plus_dm < 0] = 0 minus_dm[minus_dm < 0] = 0 tr = self.calculate_atr( high, low, prices ) if hasattr(self, 'calculate_atr') else pd.Series(index=prices.index) plus_di = 100 * (plus_dm.rolling(14).mean() / tr) minus_di = 100 * (minus_dm.rolling(14).mean() / tr) dx = 100 * abs(plus_di - minus_di) / (plus_di + minus_di) adx = dx.rolling(14).mean() # Determine trend current_ema_fast = ema_fast.iloc[-1] current_ema_slow = ema_slow.iloc[-1] current_adx = adx.iloc[-1] if not pd.isna(adx.iloc[-1]) else 25 if current_ema_fast > current_ema_slow and current_adx > 25: trend = "BULLISH" trend_strength = min(100, current_adx) elif current_ema_fast < current_ema_slow and current_adx > 25: trend = "BEARISH" trend_strength = min(100, current_adx) else: trend = "NEUTRAL" trend_strength = 0 return trend, trend_strength atr: float) ->