登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

煦风沐月

壁立千仞,无欲则刚,海纳百川,有容乃大! 宝剑锋从磨砺出,梅花香自苦寒来!

 
 
 

日志

 
 

智能移动止损的 EA 脚本  

2013-06-30 13:08:50|  分类: 金融证券 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
学习mt4,发个智能移动止损的 EA 脚本。

在网上找了一个老外写的StepStopExpert_v1.1,貌似还是2007年写的,功能完全不满意。干脆自己做一个。

算法如下:

1. 检测所下单子,若单子未设置止损和止盈,则根据默认输入参数设置止损和止盈。
2. 若单子发生盈利,并达到移动止损触发线,则动态提高原有止损线,进入追踪止损状态。
3. 若单子一直盈利,止损的范围也会随着盈利的比例变大而变大。
4. 若单子盈利已经达到预期止盈目标的 75%,则动态提高止盈线,给出更多上涨空间。

总而言之,即是上涨时尽量扩大盈利目标,跌落时尽量保住既有盈利。

欢迎测试指证。

[font=courier new][code]#property copyright "CN.ineztia"
#property link "http://www.metaquotes.net"

extern bool g_debug = true;
extern double g_initStop = 27;
extern double g_breakEven = 20;
extern double g_stepSize = 3;
extern double g_measure = 10;
extern double g_stopMax = 200;
extern double g_profitExtendThreshold = 0.75;
extern double g_profitMax = 0; // close order if profit reaches the pre-defined max value

int doStepStop() {
    int total = OrdersTotal();
    for (int i = 0; i < total; i++) {
        if (OrderSelect(i, SELECT_BY_POS)) {
            if (OrderSymbol() != Symbol()) {
                continue;
            }
            double flag = 0;
            double price = 0; // current price
            double desiredProfit = 0;
            double realProfit = 0;
            double profitModifier = 0;
            double takeProfit = OrderTakeProfit();
            double stopLoss = OrderStopLoss();
            double stepStopTrigger = (g_breakEven + g_initStop) * Point;
            if (OrderType() == OP_BUY) {
                flag = 1;
                price = Bid;
            }    else if (OrderType() == OP_SELL) {
                flag = -1;
                price = Ask;
            } else {
                continue;
            }
            if (takeProfit <= 0) {
                takeProfit = OrderOpenPrice() + flag * (g_breakEven + g_initStop + g_stepSize) * Point;
            }
            if (stopLoss <= 0) {
                stopLoss = OrderOpenPrice() - flag * g_initStop * Point;
            }
            desiredProfit = MathAbs(takeProfit - OrderOpenPrice());
            if (desiredProfit <= Point) {
                continue;
            }
            realProfit = flag * (price - OrderOpenPrice()); // could be a negtive number!
            if (realProfit > 0) {
                if ((realProfit / desiredProfit > g_profitExtendThreshold) || (desiredProfit - realProfit < 2 * g_stepSize * Point)) {
                    // profitModifier is always a positive number
                    profitModifier = MathMax(desiredProfit / g_profitExtendThreshold + g_stepSize * Point, desiredProfit + 2 * g_stepSize * Point);
                }
                if (desiredProfit - stepStopTrigger > 0) {
                    if ((desiredProfit - realProfit > 0) && (realProfit - stepStopTrigger > 0)) {
                        double k = (desiredProfit - realProfit) / (g_measure * Point);
                        stopLoss = price - flag * (k * g_stepSize + g_initStop) * Point;
                        if (flag * (stopLoss - OrderStopLoss()) < 0 || MathAbs(stopLoss - OrderStopLoss()) < g_stepSize * Point) {
                            stopLoss = OrderStopLoss();
                        }
                    }
                }
            }
            stopLoss = NormalizeDouble(stopLoss, Digits);
            if (MathAbs(takeProfit - OrderOpenPrice()) < profitModifier) {
                if (g_profitMax > 0 && profitModifier - g_profitMax * Point > 0) {
                    profitModifier = g_profitMax * Point;
                }
                takeProfit = OrderOpenPrice() + flag * profitModifier;
            }
            takeProfit = NormalizeDouble(takeProfit, Digits);
            if (MathAbs(stopLoss - OrderStopLoss()) >= Point || MathAbs(takeProfit - OrderTakeProfit()) >= Point) {
                Print("order before modify: tk(" + OrderTicket() + "), sl(" + DoubleToStr(OrderStopLoss(), 4) + "), tp(" + DoubleToStr(OrderTakeProfit(), 4) + ")");
                OrderModify(OrderTicket(), OrderOpenPrice(), stopLoss, takeProfit, 0);
            }
        }
    }
    return(0);
}

int validateParams() {
    if (g_initStop < 0 || g_stepSize < 0 || g_measure < 0 || g_breakEven < 0) {
        Alert("g_initStop, g_stepSize, g_measure & g_breakEven can not be negtive.");
        return(-1);
    }
    if (g_measure <= g_stepSize) {
        Alert("g_measure must be greater than g_stepSize.");
        return(-1);
    }
    if (g_profitExtendThreshold <= 0 || g_profitExtendThreshold >= 1) {
        Alert("range of g_extendProfitThreshold is (0, 1).");
        return(-1);
    }
    return(0);
}

int init() {
    doStepStop();
    return(0);
}

int deinit() {
    return(0);
}

int start() {
    if (0 == validateParams()) {
        doStepStop();
    }
    return(0);
}[/code][/font] 

  评论这张
 
阅读(252)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018