程序化止损的几种方式

1. Time-Based Stop Loss:

This strategy considers time as a valuable factor. If the return on a stock within a certain time period falls below a predefined threshold, the system triggers a sell order. While this method helps save time, it does not effectively reduce drawdowns.
Pseudocode:

if holding period > X days and return within the range is less than Y%:
    sell
else:
    continue holding

2. Price-Based Stop Loss:

In this approach, the purchase price serves as the reference point. If the stock price rises above X% or falls below Y%, the system executes a sell order. Similar to time-based stop loss, setting appropriate X and Y values can be challenging.
Pseudocode:

if current price > (1 + X%) * purchase price:
    sell (take profit)
else if current price < (1 - Y%) * purchase price:
    sell (stop loss)
else:
    continue holding

3. Trailing Stop Loss:

This strategy considers drawdowns. If the drawdown exceeds a predefined value (X%), the system triggers a sell order. The stop loss price adjusts based on the highest price during the holding period.
Pseudocode:

if current price < (1 - X%) * highest price during holding period:
    sell
else:
    continue holding

4. Step Stop Loss:

Step stop loss is a dynamic strategy. The stop loss price changes based on the highest price during the holding period. It performs well during market downturns.
Disadvantage: Parameter settings significantly impact performance.
Stop loss price calculation:

stop loss price = f(x) (highest price after purchase, initial stop loss price, step length, step change rate)
if current price < stop loss price:
    sell
else:
    continue holding

5. Time + Step Stop Loss:

Combines time value and dynamic stop loss. The stop loss price adjusts with the holding period. If the price falls below the stop loss, the system sells.
Similar to step stop loss, parameter settings are crucial.
Stop loss price calculation:

stop loss price = f(x) (holding period, expected return rate)
if current price < stop loss price:
    sell
else:
    continue holding

6. ATR (Average True Range) Stop Loss:

ATR stop loss calculates the average true range, which measures price volatility. The stop loss is based on this indicator.

Raw ATR = max(|today’s range|, |yesterday’s close - today’s high|, |yesterday’s close - today’s low|)
ATR = moving_average(ATR, N) (N is typically 22)
If current price < ATR * predefined factor:
Sell (stop loss)
Else:
Continue holding

用布林线下轨实现的跟踪止损

// 跟踪止损的EA示例
// 当价格上涨时,将止损价格移动到布林带下轨

extern double TrailingStopDistance = 25; // 设置跟踪止损的点数

void OnTick()
{
    double currentPrice = MarketInfo(Symbol(), MODE_BID);
    double bandLower = iBands(Symbol(), PERIOD_CURRENT, 20, 2, 0, PRICE_CLOSE, MODE_LOWER, 1);

    // 检查是否有持仓
    if (OrdersTotal() > 0)
    {
        for (int i = OrdersTotal() - 1; i >= 0; i--)
        {
            if (OrderSelect(i, SELECT_BY_POS))
            {
                if (OrderSymbol() == Symbol() && OrderType() == OP_BUY)
                {
                    // 如果布林带下轨的价格大于当前止损价,移动止损
                    if (bandLower > OrderStopLoss())
                    {
                        double newStopLoss = NormalizeDouble(bandLower, MarketInfo(Symbol(), MODE_DIGITS));
                        OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrRed);
                    }
                }
            }
        }
    }
}

ATR 跟踪止损

// ATR跟踪止损的EA示例
// 当价格上涨时,将止损价格移动到ATR的倍数位置

extern double ATRMultiplier = 2.0; // 设置ATR的倍数

void OnTick()
{
    double currentPrice = MarketInfo(Symbol(), MODE_BID);
    double atrValue = iATR(Symbol(), PERIOD_CURRENT, 14, 0); // 计算14周期的ATR值

    // 检查是否有持仓
    if (OrdersTotal() > 0)
    {
        for (int i = OrdersTotal() - 1; i >= 0; i--)
        {
            if (OrderSelect(i, SELECT_BY_POS))
            {
                if (OrderSymbol() == Symbol() && OrderType() == OP_BUY)
                {
                    // 计算新的止损价格
                    double newStopLoss = OrderOpenPrice() - ATRMultiplier * atrValue;
                    newStopLoss = NormalizeDouble(newStopLoss, MarketInfo(Symbol(), MODE_DIGITS));

                    // 如果新的止损价格低于当前止损价,移动止损
                    if (newStopLoss > OrderStopLoss())
                    {
                        OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrRed);
                    }
                }
            }
        }
    }
}

分形低点止损

// 分形低点止损的EA示例
void OnTick()
{
    double currentPrice = MarketInfo(Symbol(), MODE_BID);
    double fractalLow = iFractals(Symbol(), PERIOD_CURRENT, MODE_LOWER, 1); // 获取最近的分形低点

    // 检查是否有持仓
    if (OrdersTotal() > 0)
    {
        for (int i = OrdersTotal() - 1; i >= 0; i--)
        {
            if (OrderSelect(i, SELECT_BY_POS))
            {
                if (OrderSymbol() == Symbol() && OrderType() == OP_BUY)
                {
                    // 如果当前价格低于分形低点,移动止损到分形低点之上
                    if (currentPrice < fractalLow)
                    {
                        double newStopLoss = fractalLow;
                        OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrRed);
                    }
                }
            }
        }
    }
}

Zigzag 跟踪止损

// 基于 ZigZag 的跟踪止损的EA示例
void OnTick()
{
    double currentPrice = MarketInfo(Symbol(), MODE_BID);
    double zigzagLow = iCustom(Symbol(), PERIOD_CURRENT, "ZigZag", 12, 5, 3, 0, 1); // 获取最近的 ZigZag 波谷

    // 检查是否有持仓
    if (OrdersTotal() > 0)
    {
        for (int i = OrdersTotal() - 1; i >= 0; i--)
        {
            if (OrderSelect(i, SELECT_BY_POS))
            {
                if (OrderSymbol() == Symbol() && OrderType() == OP_BUY)
                {
                    // 如果当前价格低于 ZigZag 波谷,移动止损到 ZigZag 波谷之上
                    if (currentPrice < zigzagLow)
                    {
                        double newStopLoss = zigzagLow;
                        OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrRed);
                    }
                }
            }
        }
    }
}

移动平均线止损

// 基于移动平均线的跟踪止损的EA示例
void OnTick()
{
    double currentPrice = MarketInfo(Symbol(), MODE_BID);
    double ma25 = iMA(Symbol(), PERIOD_CURRENT, 25, 0, MODE_EMA, PRICE_CLOSE, 1); // 获取25周期的EMA值
    double ma60 = iMA(Symbol(), PERIOD_CURRENT, 60, 0, MODE_EMA, PRICE_CLOSE, 1); // 获取60周期的EMA值

    // 检查是否有持仓
    if (OrdersTotal() > 0)
    {
        for (int i = OrdersTotal() - 1; i >= 0; i--)
        {
            if (OrderSelect(i, SELECT_BY_POS))
            {
                if (OrderSymbol() == Symbol() && OrderType() == OP_BUY)
                {
                    // 如果当前价格低于25EMA,移动止损到25EMA之上
                    if (currentPrice < ma25)
                    {
                        double newStopLoss = ma25;
                        OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrRed);
                    }
                }
            }
        }
    }
}

肯特纳通道跟踪止损

// 基于肯特纳通道的跟踪止损的EA示例
void OnTick()
{
    double currentPrice = MarketInfo(Symbol(), MODE_BID);
    double keltnerUpper = iCustom(Symbol(), PERIOD_CURRENT, "Keltner Channels", 10, 1, 1, 0, 1); // 获取肯特纳通道的上轨
    double keltnerLower = iCustom(Symbol(), PERIOD_CURRENT, "Keltner Channels", 10, 1, -1, 0, 1); // 获取肯特纳通道的下轨

    // 检查是否有持仓
    if (OrdersTotal() > 0)
    {
        for (int i = OrdersTotal() - 1; i >= 0; i--)
        {
            if (OrderSelect(i, SELECT_BY_POS))
            {
                if (OrderSymbol() == Symbol() && OrderType() == OP_BUY)
                {
                    // 如果当前价格低于肯特纳通道的下轨,移动止损到下轨之上
                    if (currentPrice < keltnerLower)
                    {
                        double newStopLoss = keltnerLower;
                        OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrRed);
                    }
                }
            }
        }
    }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇