Trading Script Logic Overview
This document provides a non-technical explanation of the automated trading logic contained within the provided MetaTrader MQL script. The script is designed to manage "Buy" and "Sell" positions based on specific price extremes and profit milestones.
1. Core Configuration Parameters
The script operates using several predefined settings that determine its behavior:
- Trade Size (Lots): Defines the quantity of units to be traded in each transaction.
- Profit Target: Sets a specific monetary value. Once the combined profit of open positions reaches this number, the script takes action.
- Lookback Distance: Determines how many previous candles (bars) the script analyzes to identify price highs and lows.
- Magic Number: A unique identification code used to ensure the script only manages its own trades and does not interfere with other manual or automated trades on the same account.
2. Entry Logic (When to Open Trades)
The script constantly monitors the market to identify "extreme" price movements over a specific timeframe:
- Buy Signals: The script looks for the lowest price point reached within the defined lookback period. If the current market data confirms a new historical low, it automatically opens a Buy position. Simultaneously, it places a blue arrow on the chart to visually confirm the entry.
- Sell Signals: Conversely, the script monitors the highest price point reached over the same period. If a new high is detected, it automatically opens a Sell position and places a magenta arrow on the chart as a visual indicator.
3. Exit Logic (When to Close Trades)
The script includes an automated "Take Profit" mechanism that monitors the real-time performance of open positions:
- Profit Monitoring: The script continuously calculates the total floating profit (including swap costs) for all currently open positions associated with its unique Magic Number.
- Automatic Closing:
- If the total profit of all Buy positions meets or exceeds the defined Target, the script will automatically close all active Buy orders.
- If the total profit of all Sell positions meets or exceeds the defined Target, the script will automatically close all active Sell orders.
4. Execution Flow Summary
- Scan: The script scans the market for new price highs or lows based on the lookback distance.
- Action (Entry): If a new extreme is hit, it opens a corresponding trade and marks the chart.
- Monitor: It continuously adds up the profit of all open positions.
- Action (Exit): If those profits reach the user-defined target, it shuts down those specific trades to secure the gain.
//+------------------------------------------------------------------+
//| sell&buy.mq4 |
//+------------------------------------------------------------------+
extern double lots=0.1;
extern double target=4;
int cbars=0;
int magic=9348670;
int dist=24;
int start() {
double profit=0;
int j=OrdersTotal()-1;
for (int i=j;i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber()==magic && OrderSymbol()==Symbol())
profit=OrderProfit()+OrderSwap()+profit;
}
if (profit>=target)
{
j=OrdersTotal()-1;
for (i=j;i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
RefreshRates();
if(OrderType()==OP_BUY && OrderMagicNumber()==magic && OrderSymbol()==Symbol())
OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
}
}
double sig = Lowest(NULL,0,MODE_LOW,dist,0);
if(cbars!=Bars && sig==1)
{
RefreshRates();
OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"buy",magic,0,Blue);
string AN="ArrBuy "+TimeToStr(CurTime());
ObjectCreate(AN,OBJ_ARROW,0,Time[1],Low[1]-6*Point,0,0,0,0);
ObjectSet(AN, OBJPROP_ARROWCODE, 233);
ObjectSet(AN, OBJPROP_COLOR , Blue);
}
profit=0;
j=OrdersTotal()-1;
for (i=j;i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_SELL && OrderMagicNumber()==magic && OrderSymbol()==Symbol())
profit=OrderProfit()+OrderSwap()+profit;
}
if (profit>=target)
{
j=OrdersTotal()-1;
for (i=j;i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
RefreshRates();
if(OrderType()==OP_SELL && OrderMagicNumber()==magic && OrderSymbol()==Symbol())
OrderClose(OrderTicket(),OrderLots(),Ask,3,Magenta);
}
}
sig = Highest(NULL,0,MODE_HIGH,dist,0);
if(cbars!=Bars && sig==1)
{
RefreshRates();
OrderSend(Symbol(),OP_SELL,lots,Bid,3,0,0,"sell",magic,0,Magenta);
AN="ArrSell "+TimeToStr(CurTime());
ObjectCreate(AN,OBJ_ARROW,0,Time[1],High[1]+6*Point,0,0,0,0);
ObjectSet(AN, OBJPROP_ARROWCODE, 234);
ObjectSet(AN, OBJPROP_COLOR , Magenta);
}
cbars=Bars;
return(0);
}
Comments