寫一個MQL5程式,目的是實現SAR策略
- User
寫一個MQL5程式,目的是實現SAR策略
Assistant
以下是一個簡單的MQL5程式,實現SAR(Stop and Reverse)策略:
//+------------------------------------------------------------------+ //| SAR策略EA | //+------------------------------------------------------------------+ #property copyright "Copyright 2023, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.0" #property strict input double Acceleration = 0.02; // 加速係數 input double Maximum = 0.2; // 最大加速係數 input int Length = 14; // SAR周期 double sar[]; double ep[]; double af[]; int OnInit() { ArrayResize(sar, Length); ArrayResize(ep, Length); ArrayResize(af, Length); return (INIT_SUCCEEDED); } void OnTick() { // 計算SAR int i; for (i = 0; i < Length; i++) { if (i == 0) { sar[i] = SymbolInfoDouble(_Symbol, SYMBOL_BID); ep[i] = SymbolInfoDouble(_Symbol, SYMBOL_BID); af[i] = Acceleration; } else { if (Close[i] > ep[i-1]) { ep[i] = High[i]; af[i] = MathMin(af[i-1] + Acceleration, Maximum); } else { ep[i] = Low[i]; af[i] = MathMin(af[i-1] + Acceleration, Maximum); } sar[i] = sar[i-1] + af[i] * (ep[i] - sar[i-1]); } } // 判斷買賣信號 if (Close[0] > sar[0] && Close[1] < sar[1]) { // 買入 int ticket = OrderSend(_Symbol, OP_BUY, 0.1, Ask, 3, Bid-50*_Point, Bid+50*_Point, "SAR EA", 0, 0, Green); if (ticket > 0) { printf("買入成功,票號:%d", ticket); } } else if (Close[0] < sar[0] && Close[1] > sar[1]) { // 賣出 int ticket = OrderSend(_Symbol, OP_SELL, 0.1, Bid, 3, Ask+50*_Point, Ask-50*_Point, "SAR EA", 0, 0, Red); if (ticket > 0) { printf("賣出成功,票號:%d", ticket); } } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.