随着利润增加,如何在止损和当前价格之间保持 10 点的利润差距

2024-03-28

我试图在解决方案中添加另一个条件。当交易盈利 10 点时,我希望止损移动 10 点。更具体地说,假设我设置了一个挂单买单,止损是低于开盘价 10 点,止盈是 50 点。如果交易盈利 10 点,则止损将向上移动 10 点,如果交易盈利 20 点,则止损将再向上移动 10 点,对于 30 和 40 点也会发生同样的情况。获利直至达到 50 点止盈。这里的想法是,当利润增加 10 点时,止损增加 10 点,但止损不会下降。因此,如果止损位于利润 10 点,而价格位于利润 23 点,且突然下跌,则应在 10 点利润止损处退出交易。

设置上述条件对我来说似乎相当复杂。我没能完成它。

下面是我试图解决的代码的相关部分(请注意,代码的其余部分与上面链接的问题解决方案相同)。

//=========================================================
    //CLOSE EXPIRED STOP/EXECUTED ORDERS
    //---------------------------------------------------------
    for( int i=OrdersTotal()-1; i>=0; i-- ) {
        if(OrderSelect( i, SELECT_BY_POS, MODE_TRADES ))
            if( OrderSymbol() == Symbol() )
                if( OrderMagicNumber() == viMagicId) {
                    if( (OrderType() == OP_BUYSTOP) || (OrderType() == OP_SELLSTOP) )
                        if((TimeCurrent()-OrderOpenTime()) >= viDeleteStopOrderAfterInSec)
                            OrderDelete(OrderTicket());

                    if( (OrderType() == OP_BUY) || (OrderType() == OP_SELL) )
                        if((TimeCurrent()-OrderOpenTime()) >= viDeleteOpenOrderAfterInSec) {
                            // For executed orders, need to close them
                            double closePrice = 0;
                            RefreshRates();
                            if(OrderType() == OP_BUY)
                                closePrice  = Bid;
                            if(OrderType() == OP_SELL)
                                closePrice  = Ask;
                            OrderClose(OrderTicket(), OrderLots(), closePrice, int(viMaxSlippageInPip*viPipsToPoint), clrWhite);
                        }

                        // WORKING ON 10 pip Gap for to increase stop loss by 10 pips as profits increase by 10 pips
                        int incomePips = (int) ((OrderProfit() - OrderSwap() - OrderCommission()) / OrderLots());

                        if (incomePips >= 10)   {
                           if(OrderType() == OP_BUY)
                              OrderModify(OrderTicket(), 0, OrderStopLoss() + 10*Point, OrderTakeProfit(), 0);
                           if(OrderType() == OP_SELL)
                              OrderModify(OrderTicket(), 0, OrderStopLoss() - 10*Point, OrderTakeProfit(), 0);
                        }

                }
    }

块尾随您正在寻找的称为“块追踪”。与 MT4 附带的普通追踪止损不同,您(将)需要:

  1. 仅开始跟踪 x 点利润。
  2. 区块大小(以点为单位)(每次区块移动后仅跳转 SL)。
  3. 每次止损调整需要移动 x 点。

Note: 随之而来的一个常见问题是交易者将轨迹设置得离当前市场太近/太紧。 MT4 不是高频交易平台。不要将其剥得太近。大多数经纪商都有最小冻结和止损距离。如果您将其设置得太接近价格边缘,您将收到“ERROR 130 Invalid Stop”错误。检查经纪商在合约规范中的符号设置。


参数

vsTicketIdsInCSV:要处理的 OPENED TicketID 列表(例如 123、124、123123、1231、1)

viProfitToActivateBlockTrailInPip:追踪仅在 OrderProfit > 此点后开始。

viTrailShiftProfitBlockInPip:每当利润增加此点数时,止损就会跳动。

viTrailOnProfitInPip 移动:将止损增加此点数。


Example:

viProfitToActivateBlockTrailInPip=100,viTrailShiftProfitBlockInPip=30,viTrailShiftOnProfitInPip=20。

当订单浮动利润 130 点 (100+30) 时,区块追踪将开始。止损将设置为保证 20 点利润。

当浮动利润达到160pips(100+30+30)时,止损将设置为保证40pips(2x20pips)。


我已将其添加到 GitHub:https://github.com/fhlee74/mql4-BlockTrailer https://github.com/fhlee74/mql4-BlockTrailerETH 或 BTC 的贡献将不胜感激。

这是它的完整代码:

//+------------------------------------------------------------------+
//|                                                   SO56177003.mq4 |
//|                Copyright 2019, Joseph Lee, TELEGRAM @JosephLee74 |
//|                                             http://www.fs.com.my |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Joseph Lee, TELEGRAM @JosephLee74"
#property link      "http://www.fs.com.my"
#property version   "1.00"
#property strict

#include <stderror.mqh> 
#include <stdlib.mqh> 

//-------------------------------------------------------------------
// APPLICABLE PARAMETERS
//-------------------------------------------------------------------
extern string       vsEAComment1                                = "Telegram @JosephLee74";      // Ego trip
extern string       vsTicketIdsInCSV                            = "123 , 124, 125 ,126 ";       // List of OPENED TicketIDs to process.
extern int          viProfitToActivateBlockTrailInPip   = 10;                                   // Order must be in profit by this to activate BlockTrail
extern int          viTrailShiftProfitBlockInPip            = 15;                                   // For every pip in profit (Profit Block), shift the SL
extern int          viTrailShiftOnProfitInPip               = 10;                                   // by this much
extern int          viMaxSlippageInPip                      = 2;                                    // Max Slippage (pip)


//-------------------------------------------------------------------
// System Variables
//-------------------------------------------------------------------
double  viPipsToPrice               = 0.0001;
double  viPipsToPoint               = 1;
int     vaiTicketIds[];
string  vsDisplay                   = "";

//-------------------------------------------------------------------



//+------------------------------------------------------------------+
//| EA Initialization function
//+------------------------------------------------------------------+
int init() {
    ObjectsDeleteAll(); Comment("");
    // Caclulate PipsToPrice & PipsToPoints (old sytle, but works)
    if((Digits == 2) || (Digits == 3)) {viPipsToPrice=0.01;}
    if((Digits == 3) || (Digits == 5)) {viPipsToPoint=10;}

    // ---------------------------------
    // Transcribe the list of TicketIDs from CSV (comma separated string) to an Int array.
    string vasTickets[];
    StringSplit(vsTicketIdsInCSV, StringGetCharacter(",", 0), vasTickets);
    ArrayResize(vaiTicketIds, ArraySize(vasTickets));
    for(int i=0; i<ArraySize(vasTickets); i++) {
        vaiTicketIds[i] = StringToInteger(StringTrimLeft(StringTrimRight(vasTickets[i])));
    }
    // ---------------------------------
    start();
    return(0);
}
//+------------------------------------------------------------------+
//| EA Stand-Down function
//+------------------------------------------------------------------+
int deinit() {
    ObjectsDeleteAll();
    return(0);
}


//============================================================
// MAIN EA ROUTINE
//============================================================
int start() {

    // ========================================
    // Process all the tickets in the list
    vsDisplay                   = "BLOCK-TRAILER v1.1 (Please note the Minimum Freeze/StopLoss level in Contract Specification to AVOID error 130 Invalid Stop when trailing).\n";
    double  viPrice         = 0;
    for(int i=0; i<ArraySize(vaiTicketIds); i++) {
        if(OrderSelect( vaiTicketIds[i], SELECT_BY_TICKET, MODE_TRADES ))
            if(OrderCloseTime() == 0 ) {
                // Only work on Active orders
                if(OrderType() == OP_BUY) {
                    RefreshRates();
                    double  viCurrentProfitInPip        = (Bid-OrderOpenPrice()) / viPipsToPrice;
                    double  viNewSLinPip                = ((viCurrentProfitInPip - viProfitToActivateBlockTrailInPip)/viTrailShiftProfitBlockInPip) * viTrailShiftOnProfitInPip;
                    double  viSLinPrice                 = NormalizeDouble(OrderOpenPrice() + (viNewSLinPip * viPipsToPrice), Digits);
                    double  viNewSLFromCurrentPrice = NormalizeDouble((Bid-viSLinPrice)/viPipsToPrice, 1);
                    vsDisplay   =   vsDisplay 
                                        + "\n[" + IntegerToString(OrderTicket()) 
                                        + "] BUY: Open@ " + DoubleToStr(OrderOpenPrice(), Digits) 
                                        + " | P/L: $" + DoubleToStr(OrderProfit(), 2) 
                                        + " / " + DoubleToStr(viCurrentProfitInPip, 1) + "pips.";
                    if(viCurrentProfitInPip < (viProfitToActivateBlockTrailInPip+viTrailShiftProfitBlockInPip))
                        vsDisplay   = vsDisplay + " " + int(((viProfitToActivateBlockTrailInPip+viTrailShiftProfitBlockInPip))-viCurrentProfitInPip) + " pips to start Trail.";
                    if(viCurrentProfitInPip >= (viProfitToActivateBlockTrailInPip+viTrailShiftProfitBlockInPip)) {
                        ResetLastError();
                        vsDisplay   = vsDisplay + " TRAILING to [" + DoubleToStr(viSLinPrice, Digits) + " which is " + DoubleToStr(viNewSLFromCurrentPrice, 1) + " pips from Bid]";
                        if((viSLinPrice > OrderStopLoss()) || (OrderStopLoss() == 0))
                            if(OrderModify(OrderTicket(), OrderOpenPrice(), viSLinPrice, OrderTakeProfit(), OrderExpiration())) {
                                vsDisplay = vsDisplay + " --Trailed SL to " + DoubleToStr(viSLinPrice, Digits);
                            }
                            else {
                                int errCode = GetLastError();
                                Print(" --ERROR Trailing " + IntegerToString(OrderTicket()) + " to " + DoubleToStr(viSLinPrice, Digits) + ". [" + errCode + "]: " + ErrorDescription(errCode));
                                vsDisplay = vsDisplay + " --ERROR Trailing to " + DoubleToStr(viSLinPrice, Digits);
                                vsDisplay = vsDisplay + " [" + errCode + "]: " + ErrorDescription(errCode);
                            }
                    }
                }
                if(OrderType() == OP_SELL) {
                    RefreshRates();
                    double  viCurrentProfitInPip    = (OrderOpenPrice()-Ask) / viPipsToPrice;
                    double  viNewSLinPip                = int((viCurrentProfitInPip - viProfitToActivateBlockTrailInPip)/viTrailShiftProfitBlockInPip) * viTrailShiftOnProfitInPip;
                    double  viSLinPrice                 = NormalizeDouble(OrderOpenPrice() - (viNewSLinPip * viPipsToPrice), Digits);
                    double  viNewSLFromCurrentPrice = NormalizeDouble((viSLinPrice-Ask)/viPipsToPrice, 1);
                    vsDisplay   =   vsDisplay 
                                        + "\n[" + IntegerToString(OrderTicket()) 
                                        + "] SELL: Open@ " + DoubleToStr(OrderOpenPrice(), Digits) 
                                        + " | P/L: $" + DoubleToStr(OrderProfit(), 2) 
                                        + " / " + DoubleToStr(viCurrentProfitInPip, 1) + "pips.";
                    if(viCurrentProfitInPip < (viProfitToActivateBlockTrailInPip+viTrailShiftProfitBlockInPip))
                        vsDisplay   = vsDisplay + " " + int(((viProfitToActivateBlockTrailInPip+viTrailShiftProfitBlockInPip))-viCurrentProfitInPip) + " pips to start Trail.";
                    if(viCurrentProfitInPip >= (viProfitToActivateBlockTrailInPip+viTrailShiftProfitBlockInPip)) {
                        ResetLastError();
                        vsDisplay   = vsDisplay + " TRAILING to [" + DoubleToStr(viSLinPrice, Digits) + " which is " + DoubleToStr(viNewSLFromCurrentPrice, 1) + " pips from Ask]";
                        if((viSLinPrice < OrderStopLoss()) || (OrderStopLoss()==0) )
                            if(OrderModify(OrderTicket(), OrderOpenPrice(), viSLinPrice, OrderTakeProfit(), OrderExpiration())) {
                                vsDisplay = vsDisplay + " --Trailed SL to " + DoubleToStr(viSLinPrice, Digits);
                            }
                            else {
                                int errCode = GetLastError();
                                Print(" --ERROR Trailing " + IntegerToString(OrderTicket()) + " to " + DoubleToStr(viSLinPrice, Digits) + ". [" + errCode + "]: " + ErrorDescription(errCode));
                                vsDisplay = vsDisplay + " --ERROR Trailing to " + DoubleToStr(viSLinPrice, Digits);
                                vsDisplay = vsDisplay + " [" + errCode + "]: " + ErrorDescription(errCode);
                            }
                    }
                }
            }
    }
    Comment(vsDisplay);
    return(0);
}

And here is the screen shots showing how it works: enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

随着利润增加,如何在止损和当前价格之间保持 10 点的利润差距 的相关文章

随机推荐

  • QMainWindow::splitDockWidget 的 QDockWidget 拉伸因子?

    我正在使用 QMainWindow 在 C 中手动布局 Qt 应用程序 我想要在屏幕底部有两个并排停靠的小部件 但我希望它们具有不成比例的宽度 目前 我只能让它们具有相同的宽度 有没有办法设置拉伸因子或其他机制来获得不均匀的码头分割 以下是
  • 显示所有数据库名称

    有没有办法使用主机地址和端口显示所有数据库名称 喜欢SELECT current database 显示当前连接的数据库 我需要显示所有数据库名称 提前致谢 有一个表显示所有数据库 SELECT FROM pg database
  • 使用express.js 处理猫鼬连接的正确方法是什么?

    我有一个非常简单的 server js 设置 我正在尝试运行 var express require express wines require routes testscripts var app express app get firs
  • 关于如何制作影响 Angular 中所有组件的主题机制的指南?

    问题 我需要有关如何在 Angular 中编写机制以在我的应用程序中全局设置组件的 外观和感觉 的指导 请注意 我正在努力学习 ngrx 平台 https github com ngrx platform我认为这将是一个有趣的设计约束 然而
  • 为什么 tabindex='-1' 阻止键盘

    经过几个小时的尝试找出键盘输入在引导模式中不起作用的原因后 我终于成功地找出了问题 这是我从未想到过的事情 但通过纯粹的消除过程发现了它 有了tabindex 1 存在于 div 对于引导程序中的模态 它完全停止键盘输入 我本以为数据属性d
  • 在 Laravel 5 中安装 Guzzle

    如何将 Guzzle 安装到 Laravel 5 中 我在我的项目中使用 laravel 但我需要像 guzzle 这样的库来让我在 laravel 中轻松使用curl 任何机构可以帮忙吗 打开终端 切换到你的 laravel 项目根目录并
  • 检索 DynamoDB 上以指定文本开头的列的所有项目

    我在 DynamoDB 中有一个表 Id int hash key Name string 还有很多列 但我省略了 通常 我只是根据项目的 ID 提取和更新项目 这个模式非常适合这种情况 然而 要求之一是有一个基于名称的自动完成下拉框 我希
  • ANTLR 4 - 树模式匹配

    我试图理解 ANTLR 4 中的解析树匹配 所以为此 我有以下java代码 package sampleCodes public class fruits public static void main String args int a
  • 如何检查正则表达式是否完全匹配字符串,即字符串不包含任何额外字符?

    我有两个问题 1 我有一个正则表达式 A Z a z 0 2 d 我正在使用Python的re finditer 匹配适当的字符串 我的问题是 我只想匹配不包含额外字符的字符串 否则我想引发异常 我想捕捉以下模式 大写字母 后跟 0 1 或
  • 如何从一个 Instagram 帐户获取关注者列表?

    我正在建立一个网站 我需要的只是一个 Instagram 帐户的关注者列表 我已经完成了使用 auth 2 0 验证我的网络应用程序的步骤 我刚刚意识到 通过此身份验证 我只能访问每个访问令牌所属帐户的关注者 有没有其他方法可以从我想要的帐
  • 如何在 ubuntu 12.04 中安装 python-matplotlib?

    当我尝试时 sudo apt get install python matplotlib 我收到以下错误 Reading package lists Done Building dependency tree Reading state i
  • `yield from foo()` 和 `for x in foo(): Yield x` 之间的区别

    在Python中 大多数yield from的例子都是这样解释的 yield from foo 类似于 for x in foo yield x 另一方面 它似乎并不完全相同 并且有一些魔法 我对使用一个执行我不理解的魔法的函数感到有点不安
  • 无法使用 Require.js 调用函数

    我尝试使用 require js 为我的 node js 服务器编写一个模块 它只返回我想从 url 获取的对象 但不知何故 我无法返回用我的方法获得的值 http get 在我返回值后执行 所以我只是得到 未定义 但为什么呢 请你帮助我好
  • 如何检测 2 的补码乘法溢出?

    在我正在阅读的一本书中 以下函数用于确定 2 的补码整数乘法溢出 int tmult ok int x int y int p x y return x p x y 虽然这有效 但我如何证明它在所有情况下的正确性 当发生溢出时如何确保 p
  • 在 Windows Server 上运行的 Java 应用程序可以通过 Windows 身份验证连接到 SQL Server

    在提出问题之前 让我先介绍一些背景知识 我在一家主要运行 Windows 的商店 我们有几个批处理应用程序在 Windows 服务器上运行 主要是 2003 年 大多数批处理应用程序都是用 C 和 C 编写的 然而 我们有一些用 Java
  • 如何通过命令行检查 Visual Studio 更新?

    为了简化我的虚拟环境设置 我正在使用巧克力味 http chocolatey org自动化我的虚拟机 因为我可以运行cinst安装 Visual Studio 的命令 c gt cinst VisualStudio2012Professio
  • tangelgram 的彩色线 - 包 ape 函数 cophyloplot

    我正在尝试对包含相同分类单元的两棵树进行系统发育比较 我想根据隔离站点为连接着色 我原以为我已经成功执行了此操作 但我的工作流程中存在错误 即彩色线与隔离站点不准确对应 我想知道您是否有任何见解 请在下面找到我的可复制示例 site lt
  • firebase导入服务抛出错误

    我正在使用 firebase 函数 我想使用服务帐户密钥 json 来初始化App 并将其放入凭证中 但出现错误 类型参数 type string project id 字符串 private key id 字符串 私钥 字符串 clien
  • 从字节数组中读取 C# 中的 C/C++ 数据结构

    从数据来自 C C 结构的 byte 数组填充 C 结构的最佳方法是什么 C 结构看起来像这样 我的 C 很生锈 typedef OldStuff CHAR Name 8 UInt32 User CHAR Location 8 UInt32
  • 随着利润增加,如何在止损和当前价格之间保持 10 点的利润差距

    我试图在解决方案中添加另一个条件 当交易盈利 10 点时 我希望止损移动 10 点 更具体地说 假设我设置了一个挂单买单 止损是低于开盘价 10 点 止盈是 50 点 如果交易盈利 10 点 则止损将向上移动 10 点 如果交易盈利 20