普乐智能

查看: 4214|回复: 0

下面是一个示例盈利加仓EA的MQL4代码:

[复制链接]

338

主题

424

帖子

1670

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1670
发表于 2023-6-14 17:23:19 | 显示全部楼层 |阅读模式
//定义初始变量和参数
double Lots = 0.01;
double MaxLots = 0.1;
double Step = 100;
double ProfitTarget = 100.0; //目标盈利
double StopLoss = 30.0;
double TrailingStop = 15.0;

int max_orders = 10; //最大订单数
int order_count = 0; //当前订单数
bool in_trade = false; //是否在交易中

//定义交易函数
void OpenOrder(double lot_size, double sl, double tp)
{
    in_trade = true;
    OrderSend(Symbol(), OP_BUY, lot_size, Ask, 3, sl, tp, "Martin EA", 0, 0, Lime);
}

//定义调整交易量函数
void AdjustLots()
{
    if (in_trade) {
        int current_order = order_count - 1;
        double profit = OrderProfit() + OrderCommission();
        double target_profit = (current_order + 1) * ProfitTarget;
        if (profit > target_profit) {
            double new_lots = MathMin(Lots * pow(Step, current_order), MaxLots);
            OpenOrder(new_lots, StopLoss, TrailingStop); //再次开仓
        }
    }
}

//在OnInit()函数中初始化参数
void OnInit()
{
    SetStopLoss(StopLoss);
    SetTakeProfit(ProfitTarget);
    SetTrailingStop(TrailingStop);
}

//在OnTick()函数中处理交易逻辑
void OnTick()
{
    if (!in_trade) { //如果没有订单则下单
        OpenOrder(Lots, StopLoss, TrailingStop);
        order_count++;
    } else {
        AdjustLots(); //如果有订单则调整交易量
    }
}

//在OnDeinit()函数中关闭订单
void OnDeinit(const int reason)
{
    if (in_trade) {
        for (int i = 0; i < order_count; i++) {
            OrderClose(i, OrderLots(), Bid, 3, Red);
        }
    }
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|11wo.com

Copyright © 2001-2013 Comsenz Inc.Template by Comsenz Inc.All Rights Reserved.

Powered by Discuz!X3.4( 备案号:桂ICP备18000909号-1 )QQ

快速回复 返回顶部 返回列表