ChatCrawlersearch across public Telegram Open the app
P

Python 中文交流群

сообщение · 2024-10-31 04:17 UTC
R
#include <Trade/Trade.mqh> // Initialize trading class CTrade trade; // Input parameters for moving averages input int fastLength = 10; // Fast MA period input int slowLength = 20; // Slow MA period // Input parameters for position management input double tp1Percent = 2.0; // Take Profit 1 (% from entry price) input double tp2Percent = 4.0; // Take Profit 2 (% from entry price) input double tp3Percent = 6.0; // Take Profit 3 (% from entry price) input double initialStopLossPercent = 2.0; // Initial Stop-Loss (% from entry price) input double trailingStopPercent = 3.0; // Trailing Stop (% from current price) input double positionSize = 0.1; // Position size in lots // Global variables double fastMA[], slowMA[]; double entryPrice, currentStopLoss; double tp1Price, tp2Price, tp3Price; bool trailingStopActive = false; // Indicator handles int fastMAHandle; int slowMAHandle; // Initialization function int OnInit() { // Create handles for moving averages fastMAHandle = iMA(_Symbol, PERIOD_CURRENT, fastLength, 0, MODE_EMA, PRICE_CLOSE); slowMAHandle = iMA(_Symbol, PERIOD_CURRENT, slowLength, 0, MODE_EMA, PRICE_CLOSE); if (fastMAHandle == INVALID_HANDLE || slowMAHandle == INVALID_HANDLE) { Print("Failed to get indicator handles."); return INIT_FAILED; } return INIT_SUCCEEDED; } // OnTick function - main strategy logic void OnTick() { // Copy moving average values into the arrays if (CopyBuffer(fastMAHandle, 0, 0, 1, fastMA) <= 0 || CopyBuffer(slowMAHandle, 0, 0, 1, slowMA) <= 0) { Print("Failed to copy buffer data."); return; } // Retrieve the current position double currentPositionSize = PositionGetDouble(POSITION_VOLUME); int positionType = (currentPositionSize > 0) ? POSITION_TYPE_BUY : (currentPositionSize < 0) ? POSITION_TYPE_SELL : -1; // Long condition: Fast MA crosses above Slow MA if (fastMA[0] > slowMA[0] && positionType != POSITION_TYPE_BUY) { // Close short position if exists if (positionType == POSITION_TYPE_SELL) { trade.PositionClose(_Symbol); } // Open long position entryPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID); currentStopLoss = entryPrice * (1 - initialStopLossPercent / 100); tp1Price = entryPrice * (1 + tp1Percent / 100); tp2Price = entryPrice * (1 + tp2Percent / 100); tp3Price = entryPrice * (1 + tp3Percent / 100); trailingStopActive = true; // Enable trailing stop trade.Buy(positionSize, NULL, entryPrice, currentStopLoss, 0); } // Short condition: Fast MA crosses below Slow MA if (fastMA[0] < slowMA[0] && positionType != POSITION_TYPE_SELL) { // Close long position if exists if (positionType == POSITION_TYPE_BUY) { trade.PositionClose(_Symbol); } // Open short position entryPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK); currentStopLoss = entryPrice * (1 + initialStopLossPercent / 100); tp1Price = entryPrice * (1 - tp1Percent / 100); tp2Price = entryPrice * (1 - tp2Percent / 100); tp3Price = entryPrice * (1 - tp3Percent / 100); trailingStopActive = true; // Enable trailing stop trade.Sell(positionSize, NULL, entryPrice, currentStopLoss, 0); } // Trailing stop logic if (currentPositionSize > 0) // Long position { double newStopLoss = SymbolInfoDouble(_Symbol, SYMBOL_BID) * (1 - trailingStopPercent / 100); if (newStopLoss > currentStopLoss) { currentStopLoss = newStopLoss; trade.PositionModify(_Symbol, currentStopLoss, 0); } }

Вся лента · оригинал в Telegram

Open in Telegram Каталог площадок Искать в ChatCrawler

A snapshot of an open public feed from the search index ChatCrawler — “Google for public Telegram”; refreshed as the venue is crawled. Times are UTC.

Public content only, official Telegram API. About · FAQ · What we do not do · Remove a page · Catalog