MQL5 Expert Advisor: Scalping Strategy with Profit Target and Position Locking
//+------------------------------------------------------------------+
//| 7777.mq4 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright 'Copyright 2023, MetaQuotes Ltd.'
#property link 'https://www.mql5.com'
#property version '1.00'
#property strict
//+------------------------------------------------------------------+
//| Global variables |
//+------------------------------------------------------------------+
int MAGIC_NUMBER = 92133;
double INITIAL_LOTS = 0.5;
double REPLENISH_MULTIPLIER = 0.6;
int REPLENISH_ATTEMPTS = 5;
double PROFIT_TARGET = 40;
double MIN_FLUCTUATION = MarketInfo(Symbol(), MODE_POINT) * 100;
bool hasLongPosition = false;
bool hasShortPosition = false;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Close all open orders
CloseOrders();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check if all positions have been closed
if (!hasLongPosition && !hasShortPosition)
{
// Determine the prices for the pending orders
double askPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits);
double bidPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_BID), Digits);
double buyPrice = askPrice + MIN_FLUCTUATION;
double sellPrice = bidPrice - MIN_FLUCTUATION;
// Place pending orders if there are no open positions
if (!hasLongPosition && CountOrders(OP_BUYSTOP) == 0 && CountOrders(OP_BUYLIMIT) == 0)
{
if (CountOrders(OP_SELLSTOP) > 0)
{
DeleteOrders(OP_SELLSTOP);
}
if (CountOrders(OP_SELLLIMIT) > 0)
{
DeleteOrders(OP_SELLLIMIT);
}
if (OpenBuyOrder(INITIAL_LOTS, buyPrice))
{
Print('Opened buy order');
hasLongPosition = true;
}
else
{
Print('Failed to open buy order');
}
}
if (!hasShortPosition && CountOrders(OP_SELLSTOP) == 0 && CountOrders(OP_SELLLIMIT) == 0)
{
if (CountOrders(OP_BUYSTOP) > 0)
{
DeleteOrders(OP_BUYSTOP);
}
if (CountOrders(OP_BUYLIMIT) > 0)
{
DeleteOrders(OP_BUYLIMIT);
}
if (OpenSellOrder(INITIAL_LOTS, sellPrice))
{
Print('Opened sell order');
hasShortPosition = true;
}
else
{
Print('Failed to open sell order');
}
}
}
else
{
// Check if any positions have reached the profit target
double totalProfit = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER)
{
totalProfit += OrderProfit();
}
}
if (totalProfit >= PROFIT_TARGET)
{
CloseOrders();
}
else
{
double currentBid = NormalizeDouble(MarketInfo(Symbol(), MODE_BID), Digits);
double currentAsk = NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits);
// Check if the market has moved against the positions by 100 points
if (hasLongPosition && currentBid <= GetEntryPrice(OP_BUYSTOP) - (100 * MIN_FLUCTUATION))
{
// Lock the long position by opening a sell order
double replenishLots = GetLastOrderLots(OP_BUYSTOP) * REPLENISH_MULTIPLIER;
double replenishPrice = currentBid + MIN_FLUCTUATION;
if (OpenSellOrder(replenishLots, replenishPrice))
{
Print('Locked long position by opening sell order');
}
else
{
Print('Failed to open sell order to lock long position');
}
}
else if (hasShortPosition && currentAsk >= GetEntryPrice(OP_SELLSTOP) + (100 * MIN_FLUCTUATION))
{
// Lock the short position by opening a buy order
double replenishLots = GetLastOrderLots(OP_SELLSTOP) * REPLENISH_MULTIPLIER;
double replenishPrice = currentAsk - MIN_FLUCTUATION;
if (OpenBuyOrder(replenishLots, replenishPrice))
{
Print('Locked short position by opening buy order');
}
else
{
Print('Failed to open buy order to lock short position');
}
}
}
}
}
//+------------------------------------------------------------------+
//| Open buy order function |
//+------------------------------------------------------------------+
bool OpenBuyOrder(double lots, double price)
{
int ticket = OrderSend(Symbol(), OP_BUYSTOP, lots, price, 0, 0, 0, 'Buy Order', MAGIC_NUMBER, 0, Green);
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET))
{
return true;
}
else
{
Print('Failed to select order with ticket ', ticket);
}
}
else
{
Print('Failed to send buy order');
}
return false;
}
//+------------------------------------------------------------------+
//| Open sell order function |
//+------------------------------------------------------------------+
bool OpenSellOrder(double lots, double price)
{
int ticket = OrderSend(Symbol(), OP_SELLSTOP, lots, price, 0, 0, 0, 'Sell Order', MAGIC_NUMBER, 0, Red);
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET))
{
return true;
}
else
{
Print('Failed to select order with ticket ', ticket);
}
}
else
{
Print('Failed to send sell order');
}
return false;
}
//+------------------------------------------------------------------+
//| Close all orders function |
//+------------------------------------------------------------------+
void CloseOrders()
{
for (int i = OrdersTotal() - 1;
原文地址: http://www.cveoy.top/t/topic/b35o 著作权归作者所有。请勿转载和采集!