TTF_MW_expert
Download scriptThis automated trading script is designed to manage and execute trades based on specific signals generated by an external technical indicator. It automates the decision-making process for entering, exiting, and adjusting positions in the financial market.
Core Trading Logic
The script operates by constantly monitoring two distinct types of data from a custom indicator: "Opening Signals" and "Closing Signals."
1. Signal Detection:
- Entry Signals: The script looks for specific numerical thresholds provided by the indicator to determine when it is an appropriate time to open a new trade. If the indicator exceeds a predefined "comparison level," the script triggers a command to either Buy or Sell.
- Exit Signals: Simultaneously, the script monitors the indicator for opposite signals. If a "Sell" signal appears while a "Buy" position is currently open (or vice versa), the script is programmed to close the existing trade immediately.
2. Trade Execution and Management:
- Automated Entries: When a valid entry signal is detected and no other trades are currently active, the script automatically opens a position with a specific volume (lot size).
- Risk Controls: Every time a trade is opened, the script automatically calculates and attaches a Stop Loss (to limit potential losses) and a Take Profit (to secure gains). It accounts for market spreads and minimum distance requirements to ensure these orders are placed correctly by the broker.
- Dynamic Trailing Stop: To protect profits as a trade moves in a favorable direction, the script includes a "Trailing" mechanism. If the price moves significantly past a certain point, the script will automatically move the Stop Loss closer to the current price, "locking in" gains while allowing the trade to continue running.
3. Safety and Validation:
- Trade Permission: Before performing any action, the script verifies that trading is permitted on the account.
- Margin Check: The script includes a safety feature that checks your available free margin. If there are insufficient funds to open a new position based on your set lot size, it will trigger an alert rather than attempting a failing trade.
- Unique Identification: It uses a specific "Magic Number" to identify its own trades, ensuring it only manages the positions it opened and does not interfere with other manual or automated trades on the same account.
Summary of Operation Flow
The script follows a continuous loop: it reads the indicator values, checks if any open trades need to be closed based on those values, adjusts trailing stops for active trades, and then looks for opportunities to open new trades if the account balance allows.
//+------------------------------------------------------------------+
//| Adviser working by some indicator
//| Copyright 2007, Victor Lukashuk aka lukas1
//|
//| version 01 25.07.2007
//+------------------------------------------------------------------+
//
// Ýêñïåðò îòêðûâàåò è çàêðûâàåò ïîçèöèè ïî ïðîïèñàííîìó èíäèêàòîðó "TTF_MW.mq4"
// à ìîæåò è ïî äðóãîìó èíäèêàòîðó - ýòà âåðñèÿ äëÿ èíäèêàòîðîâ, ðèñóþùèõ òî÷êè
#property copyright "Copyright © 2007, lukas1"
#include <stdlib.mqh> // áèáëèîòåêà îòîáðàæåíèÿ îøèáîê
extern int Take_profit=120;
extern int Stop_loss=40;
extern int TrailingStep=95; // øàã òðåéëèíãà
extern double TrailingStop=15; // ñòîïû ïî òðåéëèíãó
// ïàðàìåòðû äëÿ 1 ÷àñîâîê äëÿ EURUSD
extern string indicator = "TTF_MW";
extern double open_param1=80;
extern double open_param2=18;
extern double open_param3=27.1;
extern int buffer_for_open_buy=0;
extern int buffer_for_open_sell=1;
extern double close_param4=78;
extern double close_param5=54;
extern double close_param6=4.6;
extern int buffer_for_close_buy=0;
extern int buffer_for_close_sell=0;
extern double level_comparity=0.5;
extern double Lots =0.1;
extern int MAGIC=111222;
int digits; // êîëè÷åñòâî çíàêîâ äëÿ ðàáîòû ñ äàííîé âàë. ïàðîé
int stoplevel; // ìèíèìàëüíûé îòñòóï óñòàíîâêè ñòîïîâûõ îðäåðîâ
int spred;
double f1=0.01;
double s1=0.01;
int Buy=0;
int Sell=0;
double b=1;
int cnt=0;
double PROF=0;
double STOP=0;
int ticket,st,pr;
//+------------------------------------------------------------------+
//|
//-------------------------------------------------------------------------------------------------------------
int start()
{
// â ñàìîì íà÷àëå 20 ðàç ïðîâåðÿåì ÷òî òîðãîâëÿ ðàçðåøåíà
if (!IsTradeAllowed()) return(0);
Buy=0;Sell=0;
f1 = iCustom(NULL,0,indicator,close_param4,close_param5,close_param6,buffer_for_close_buy, 1);
s1 = iCustom(NULL,0,indicator,close_param4,close_param5,close_param6,buffer_for_close_sell, 1);
if (f1>level_comparity) {Buy=1; }
if (s1>level_comparity) {Sell=1;}
//Comment (" f1= ",f1, " s1= ",s1, " b= ",b) ;
if(b<Bars)
{
// çàêðûâàåì îòêðûòûå ïîçèöèè äëÿ ÁÀÉ
for(cnt=OrdersTotal();cnt>=0;cnt--)
{OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol()==Symbol() || OrderMagicNumber()==MAGIC )
if(OrderType()==OP_BUY)
{// åñëè ÑÅËË äëèííóþ ïîçèöèþ çàêðûòü
if((Sell==1)&&(b<Bars))
{OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
s1=0.01;
return(0);
}
// check for trailing stop for BUY
if(TrailingStop>0) // åñëè ïàðàìåòð TrailingStop çàäàí è áîëüøå 0
{
if(OrderOpenPrice()<(Bid-Point*TrailingStep))
{
if(OrderStopLoss()<(Bid-Point*(TrailingStop+TrailingStep+1)))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,DodgerBlue);
// òî ïåðåíîñèì ñòîï óðîâåíü îðäåðà íà óðîâåíü Bid-Point*TrailingStop
return(0);
} } } } }
// çàêðûâàåì îòêðûòûå ïîçèöèè äëÿ ÑÅËË
for(cnt=OrdersTotal();cnt>=0;cnt--)
{OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol()==Symbol() || OrderMagicNumber()==MAGIC)
if(OrderType()==OP_SELL)
{// åñëè ÁÀÉ êîðîòêóþ ïîçèöèþ çàêðûòü
if ((Buy==1)&&(b<Bars))
{OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
f1=0.01;
return(0);
}
// check for trailing stop for SELL
if(TrailingStop>0)
{
if(OrderOpenPrice()>(Ask+Point*TrailingStep))
{
if((OrderStopLoss()>(Ask+Point*(TrailingStop+TrailingStep+1))))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Coral);
return(0);
} } } } }
Buy=0;Sell=0;
f1 = iCustom(NULL,0,indicator,open_param1,open_param2,open_param3,buffer_for_open_buy, 1);
s1 = iCustom(NULL,0,indicator,open_param1,open_param2,open_param3,buffer_for_open_sell, 1);
if (f1>level_comparity) {Buy=1; }
if (s1>level_comparity) {Sell=1;}
//Comment (" f1= ",f1, " s1= ",s1, " b= ",b) ;
// îòêðûâàåì íîâûå îðäåðà ÑÅËË
if (OrdersTotal()<1)
if((Sell==1)&&(b<Bars))
{b=Bars;
// ðàñ÷åò ñòîïîâûõ îðäåðîâ
PROF=0; STOP=0;
if(Stop_loss>0)
{if(Stop_loss-spred<=stoplevel) st=stoplevel; else st=Stop_loss-spred;
STOP=NormalizeDouble(Ask+st*Point,digits);
}
if(Take_profit>0)
{pr=Take_profit+spred; //ðàñ÷åò TP îò Ask
PROF=NormalizeDouble(Ask-pr*Point,digits);
}
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,
NormalizeDouble(STOP,digits), // SL
NormalizeDouble(PROF,digits), // TP
"sell_",MAGIC,0,Red);
if(ticket<1)
{Print("Oøèáêà ",ErrorDescription(GetLastError()));
return(0);
}
else s1=0.01;
return(0); // exit
}
// îòêðûâàåì íîâûå îðäåðà ÁÀÉ
if (OrdersTotal()<1)
if((Buy==1)&&(b<Bars))
{b=Bars;
// ðàñ÷åò ñòîïîâûõ îðäåðîâ
PROF=0; STOP=0;
stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
spred =MarketInfo(Symbol(),MODE_SPREAD);
digits =MarketInfo(Symbol(),MODE_DIGITS);
if (Stop_loss>0)
{if(Stop_loss-spred<=stoplevel) st=stoplevel; else st=Stop_loss-spred;
STOP=NormalizeDouble(Bid-st*Point,digits);
}
if(Take_profit>0)
{pr=Take_profit+spred; //ðàñ÷åò TP îò Bid
PROF=NormalizeDouble(Bid+pr*Point,digits);
}
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,
NormalizeDouble(STOP,digits), // SL
NormalizeDouble(PROF,digits), // TP
"buy_",MAGIC,0,Blue);
if(ticket<1)
{Print("Oøèáêà ",ErrorDescription(GetLastError()));
return(0);
}
else f1=0.01;
return(0); // exit
}
if(AccountFreeMargin()<(300*Lots)) // Ïðîâåðêà íàëè÷èÿ ñâîáîäíûõ ñðåäñòâ äëÿ òîðãîâëè
{Alert("Ìû íå èìååì äåíåã. Free Margin = ", AccountFreeMargin()); /*ÿ çàìåíèë ôóíêöèþ Print
âûâîäÿùóþ ñîîáùåíèÿ â ôàéë îò÷¸òà, êîòîðûé íåïîíÿòíî ãäå ñìîòðåòü íà ôóíêöèþ Alert*/
return(0);
}
return(0);
}
}
Comments