|
//定义初始变量和参数
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);
}
}
} |
|