[ea]FrameCloser_Ron_MT4_v04
Download scriptThis automated trading tool, known as the FrameCloser, is designed to act as a profit-taking manager for your trading account. Its primary purpose is to monitor your open positions and automatically close them once a specific financial goal is met.
How It Works
The script operates based on a "Basket Profit" logic. Instead of looking at individual trades one by one, it looks at the overall health of your active trading portfolio. Here is the step-by-step breakdown of its behavior:
1. Continuous Monitoring The tool constantly scans your account every time the market moves (on every "tick"). It calculates the real-time difference between your account's current equity and your actual balance. This represents your floating profit or loss for all active trades combined.
2. The Profit Target
You set a specific monetary goal (the BasketProfit value). As long as your combined open positions are in a losing state or haven't reached that amount, the script remains idle and does not take any action.
3. Automatic Execution The moment your total profit exceeds the target you have defined, the script triggers an immediate "sell-all" command. It identifies every active buy and sell order currently running on your account and closes them simultaneously.
- For Buy Positions: It executes a closing action at the current market "Bid" price.
- For Sell Positions: It executes a closing action at the current market "Ask" price.
4. Reliability and Error Handling To ensure high reliability, the script includes a built-in safety check for every transaction it attempts. If a trade fails to close due to a connection glitch or a momentary market fluctuation, the script captures that error, logs it, and will automatically attempt to retry the closure on the very next price movement.
Key Summary for the User
- No Entry Logic: This script does not open any new trades; it is strictly a management tool for positions you have already opened.
- Basket Approach: It treats all your active trades as one single "basket." Once that basket hits your profit goal, everything is cleared out.
- Simplicity: It provides a hands-off way to ensure you lock in profits automatically without having to manually monitor the screen and click "close" when your targets are met.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
/*
+------------+
|FrameCloser |
+------------+
Theory of operation
===================
Close all open (not pending) orders at BasketProfit
TIME FRAME
==========
M1
PAIRS
=====
EURUSD (most active)
ENTRY LONG
==========
None
ENTRY SHORT
===========
None
EXIT
====
Sell all OPEN (not pending) orders
MONEY MANAGEMENT
================
None
RISK MANAGEMENT
===============
None
FAILURE MANAGEMENT
==================
GetLastError on every transaction
All missed transactions retried on next tick
VERSION HISTORY
===============
00 - initial concept
*/
// variables declared here are GLOBAL in scope
#property copyright "Ron Thompson"
#property link "http://www.lightpatch.com/forex"
// user input
extern int BasketProfit=350;
// other settings
int Slippage=3;
// naming and numbering
int MagicNumber = 16384;
//+-------------+
//| Custom init |
//|-------------+
// Called ONCE when EA is added to chart or recompiled
int init()
{
Print("Init happened ",CurTime());
Comment(" ");
}
//+----------------+
//| Custom DE-init |
//+----------------+
// Called ONCE when EA is removed from chart
int deinit()
{
Print("DE-Init happened ",CurTime());
Comment(" ");
}
//+-----------+
//| Main |
//+-----------+
// Called EACH TICK and each Bar[]
int start()
{
double currBasket;
string mySymbol;
double myAsk;
double myBid;
int cnt=0;
int gle=0;
int OrdersOpenCount=0;
currBasket=AccountEquity()-AccountBalance();
Comment("Profit/Loss = ",currBasket, " of ",BasketProfit);
if(currBasket>BasketProfit)
{
// CLOSE order if profit target made
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
//if( OrderMagicNumber()==MagicNumber )
if( OrderType()==OP_BUY || OrderType()==OP_SELL )
{
mySymbol=OrderSymbol();
if(OrderType()==OP_BUY)
{
myBid=MarketInfo(mySymbol,MODE_BID);
OrderClose(OrderTicket(),OrderLots(),myBid,Slippage,White);
gle=GetLastError();
if(gle==0)
{
Print("CLOSE BUY Bid=",myBid);
}
else
{
Print("-----ERROR----- CLOSE BUY Bid=",myBid," error=",gle);
}
} // if BUY
if(OrderType()==OP_SELL)
{
myAsk=MarketInfo(mySymbol,MODE_ASK);
OrderClose(OrderTicket(),OrderLots(),myAsk,Slippage,Red);
gle=GetLastError();
if(gle==0)
{
Print("CLOSE SELL Ask=",myAsk);
}
else
{
Print("-----ERROR----- CLOSE SELL Ask=",myAsk," error=",gle);
}
} //if SELL
} // if(OrderSymbol)
} // for
} //currBasket
} // start()
Comments