This script is designed to automatically place and manage pending and active trades in a trading platform. It works by setting up "buy stop" and "sell stop" orders based on the recent high and low prices of a specific trading instrument (like a currency pair).
Here's the breakdown of what it does, step-by-step:
-
Setting the Stage (Initial Setup): The script starts by defining some adjustable settings, such as the size of the trades ("Lots"), how far the "trailing stop" should move ("TralUp"), how far away from the high/low price to place orders ("EnterFiltr"), and how far back in time to look for the high and low prices ("InHistory"). It also sets a fixed initial stop loss ("SL").
-
Finding Highs and Lows: The script identifies the highest high and lowest low price of the trading instrument over a specified historical period. This range is determined by the 'InHistory' variable, allowing the script to consider a certain number of past price bars. The script considers the market spread (difference between buy and sell price) when placing buy orders.
-
Placing Pending Orders: Based on these highs and lows, it automatically creates two types of pending orders:
- Buy Stop Order: An order to automatically buy the instrument if the price reaches a level slightly above the recent high (defined using the 'EnterFiltr' variable). This anticipates the price continuing to rise if it breaks through the high.
- Sell Stop Order: An order to automatically sell the instrument if the price falls to a level slightly below the recent low (defined using the 'EnterFiltr' variable). This anticipates the price continuing to fall if it breaks through the low.
- The script only uses buy stop and sell stop orders and no buy limit and sell limit orders.
- The script set's a Stop Loss for both Buy Stop and Sell Stop orders and the stop loss level is defined by the SL variable
-
Managing Active Trades (Trailing Stop): The script also manages existing buy and sell trades. It checks if the trades are "profitable" (meaning the current price is moving in the desired direction after the trade was opened). If a trade is profitable, it adjusts the "stop loss" level to lock in profits. This is called a "trailing stop." The stop loss is moved closer to the current price as the price moves in a favorable direction. The Trailing stop adjustment is defined by the TralUp variable
-
Trade Management Logic:
- The script counts all active Buy and Sell orders with its magic number.
- The script also counts all active Buy Stop and Sell Stop orders with its magic number.
- If the script detects any active Buy and Sell orders, it will automatically delete all Buy Stop and Sell Stop orders. This is done to prevent conflicting orders.
- Finally, the script checks that both the count of active Buy and Sell orders, and the count of active Buy Stop and Sell Stop orders are zero. If both are zero, it means that there are no active trades, and no pending orders, therefore the script places new Buy Stop and Sell Stop orders
In essence, this script aims to automatically enter trades based on price breakouts and manage those trades by using a trailing stop to secure profits as the price moves in a favorable direction.
//+------------------------------------------------------------------+
//| kolbas ch by Maloma |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
#include <stderror.mqh>
extern double Lots=0.1;
extern int TralUp=11;
extern int EnterFiltr=6;
extern int InHistory=5;
extern double SL=45;
int StopLev;
int Tral;
double MA, MAP;
double Hich, Loch;
int i, CurTot, StopTot;
int OpenOrders()
{
Hich=High[Highest(Symbol(),NULL,MODE_HIGH,InHistory,0)]+(EnterFiltr+MarketInfo(Symbol(),MODE_SPREAD))*Point;
Loch=Low[Lowest(Symbol(),NULL,MODE_LOW,InHistory,0)]-EnterFiltr*Point;
OrderSend(Symbol(),OP_BUYSTOP,Lots,Hich,3,Hich-SL*Point,0,NULL,753,0,CLR_NONE);
OrderSend(Symbol(),OP_SELLSTOP,Lots,Loch,3,Loch+SL*Point,0,NULL,753,0,CLR_NONE);
// OrderSend(Symbol(),OP_SELLLIMIT,Lots,Bid+EnterFiltr*Point,3,Ask+2*EnterFiltr*Point,0,NULL,753,0,CLR_NONE);
// OrderSend(Symbol(),OP_BUYLIMIT,Lots,Ask-EnterFiltr*Point,3,Bid-2*EnterFiltr*Point,0,NULL,753,0,CLR_NONE);
return(0);
}
int start()
{
StopLev=MarketInfo(Symbol(),MODE_STOPLEVEL);
Tral=StopLev+TralUp;
CurTot=0;
StopTot=0;
for (i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if ((Symbol()==OrderSymbol())&&(OrderMagicNumber()==753)&&((OrderType()==OP_BUY)||(OrderType()==OP_SELL)))
{
CurTot++;
if (OrderType()==OP_BUY)
{
if ((OrderOpenPrice()+Tral*Point)<Bid)
{
if ((OrderStopLoss()+Tral*Point)<Bid) {OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Tral*Point,Bid+Tral*Point,OrderExpiration(),CLR_NONE);}
}
}
if (OrderType()==OP_SELL)
{
if (Ask<(OrderOpenPrice()-Tral*Point))
{
if (Ask<(OrderStopLoss()-Tral*Point)) {OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Tral*Point,Ask-Tral*Point,OrderExpiration(),CLR_NONE);}
}
}
}
if ((Symbol()==OrderSymbol())&&(OrderMagicNumber()==753)&&(OrderType()>1)) {StopTot++;}
}
for (i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if ((CurTot>0)&&(Symbol()==OrderSymbol())&&(OrderMagicNumber()==753)&&(OrderType()>1)) {OrderDelete(OrderTicket());}
}
if ((CurTot==0)&&(StopTot==0)) {OpenOrders();}
return(0);
}
Comments