This automated trading script is designed to manage currency trades by analyzing multiple technical indicators across several different timeframes simultaneously. Its primary goal is to identify high-probability market trends and execute trades based on a "confluence" of signals—meaning it only acts when several different mathematical conditions align at once.
Core Trading Logic
The script functions by constantly scanning the market for specific patterns. It looks at data from five different timeframes (Daily, 4-Hour, 1-Hour, 30-Minute, and 5-Minute) to ensure that the short-term movement of the price is supported by the long-term trend.
Entry Requirements
To open a new position, the script requires a specific "checklist" of conditions to be met:
- Trend Analysis: It uses momentum indicators (like Bulls/Bears Power and MACD) to determine if the market is trending upward or downward on both the Daily and 4-Hour charts.
- Momentum & Volatility: It utilizes the Relative Strength Index (RSI) and Williams Percent Range (WPR) across multiple timeframes. For a "Buy" signal, it looks for specific price strength levels; for a "Sell" signal, it looks for specific weakness levels.
- Multi-Timeframe Alignment: A trade is only triggered if the 5-minute, 15-minute, 30-minute, and 1-hour charts all agree with the primary trend established on the larger timeframes.
Trade Management & Safety
Once a trade is opened, the script actively manages it to protect your capital:
- Dynamic Profit Protection (Trailing Stop): As a trade moves into profit, the script automatically moves the "Stop Loss" order along behind the price. This allows the system to lock in gains while still giving the trade room to continue moving in your favor.
- Automatic Exits: The script monitors specific technical changes (such as Moving Average crossovers or RSI extremes on the 4-hour chart) to decide when a trade should be closed manually before it hits a hard limit, aiming to exit during optimal market conditions.
- Risk Management: It includes built-in safety checks to ensure there is enough "Free Margin" (available funds) in your account before opening a trade. It also features a sophisticated lot-sizing engine that can calculate your trade size based on a fixed amount, a percentage of your balance, or a progression system based on your current account equity.
Summary of Operation
Essentially, this script acts as an automated "multi-lens" analyst. Instead of looking at just one chart, it watches five different charts at once. It waits for the 5-minute, 15-minute, and 30-minute charts to "agree" with the 1-hour and Daily trends before entering a trade, then uses trailing stops to protect your profit as the price moves.
//+------------------------------------------------------------------+
//| Copyright 2005, Larionov P.V. |
//| http:\\www.lolion.narod.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright 2005, Larionov P.V."
#property link "http:\\www.lolion.narod.ru "
#define MAGIC 721328
extern double lStopLoss = 500;
extern double sStopLoss = 500;
extern double lTakeProfit = 40;
extern double sTakeProfit = 40;
extern double lTrailingStop = 500;
extern double sTrailingStop = 500;
extern color clOpenBuy = Blue;
extern color clCloseBuy = Aqua;
extern color clOpenSell = Red;
extern color clCloseSell = Violet;
extern color clModiBuy = Blue;
extern color clModiSell = Red;
extern string Name_Expert = "ABBMD GBP/JPY";
extern int Slippage = 5;
extern bool UseSound = False;
extern string NameFileSound = "alert.wav";
extern double Lots =0.10;
extern double Margin_Level =500;
extern double WilliamsH1_BUY =10;
extern double WilliamsH1_SELL =-200;
extern double WilliamsM30_BUY =10;
extern double WilliamsM30_SELL =-200;
extern double WilliamsM15_BUY =10;
extern double WilliamsM15_SELL =-200;
extern double WilliamsM5_BUY =-70;
extern double WilliamsM5_SELL =-30;
extern double RSIH1_BUY =75;
extern double RSIH1_SELL =25;
extern double RSIH4_BUY =99;
extern double RSIH4_SELL =11;
extern double RSID1_BUY =99;
extern double RSID1_SELL =11;
extern double MACDH4_BUY_FastEma =15;
extern double MACDH4_BUY_SlowEma =35;
extern double MACDH4_BUY_SMA =12;
extern double MACDH4_SELL_FastEma =15;
extern double MACDH4_SELL_SlowEma =35;
extern double MACDH4_SELL_SMA =12;
extern double MACDD1_BUY_FastEma =15;
extern double MACDD1_BUY_SlowEma =35;
extern double MACDD1_BUY_SMA =12;
extern double MACDD1_SELL_FastEma =15;
extern double MACDD1_SELL_SlowEma =35;
extern double MACDD1_SELL_SMA =12;
extern double BEARS =15;
extern double BULLS =15;
extern double FastMA =24;
extern double SlowMA =24;
extern double CLOSE_FastEma =15;
extern double CLOSE_SlowEma =35;
extern double CLOSE_SMA =12;
extern double CLOSE_RSI_LevelBUY =70;
extern double Close_RSI_LevelSELL =30;
extern double Close_RSI_Period = 21;
extern string _Parameters_b_Lots = "Ïàðàìåòðû ìîäóëÿ ðàñ÷¸òà ëîòà";
extern int LotsWayChoice = 1; // Ñïîñîá âûáîðà ðàáî÷åãî ëîòà:
// 0-ôèêñèðîâàííûé,
// 1-ïðîöåíò îò äåïîçèòà,
// 2-ôðàêöèîííî-ïðîïîðöèîíàëüíûé,
// 3-ôðàêöèîííî-ôèêñèðîâàííûé,
extern int LotsPercent = 40; // Ïðîöåíò îò äåïîçèòà
extern int LotsDeltaDepo = 500; // Êîýôôèöèåíò ïðèðàùåíèÿ äåïîçèòà
extern int LotsDepoForOne = 500; // Ðàçìåð äåïîçèòà äëÿ îäíîãî ìèíèëîòà
extern int LotsMax = 1000; // Ìàêñèìàëüíîå êîëè÷åñòâî ìèíèëîòîâ
void deinit() {
Comment("");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start(){
Print("Account balance = ",AccountBalance());
Print("Point = ",Point);
if(Bars<100){
Print("bars less than 100");
return(0);
}
if(lStopLoss<10){
Print("StopLoss less than 10");
return(0);
}
if(lTakeProfit<10){
Print("TakeProfit less than 10");
return(0);
}
if(sStopLoss<10){
Print("StopLoss less than 10");
return(0);
}
if(sTakeProfit<10){
Print("TakeProfit less than 10");
return(0);
}
//Indicators on Daily Timeframe ***********************************************************************************
//Buy
double diBullsPower0 =iBullsPower(NULL,1440,BULLS,PRICE_TYPICAL,0);
double diBullsPower24 =iBullsPower(NULL,1440,BULLS,PRICE_TYPICAL,1);
double diMACD1_D1BUY =iMACD(NULL,1440,MACDD1_BUY_FastEma,MACDD1_SELL_SlowEma,MACDD1_BUY_SMA,PRICE_TYPICAL,MODE_MAIN,0);
double diMACD2_D1BUY =iMACD(NULL,1440,MACDD1_BUY_FastEma,MACDD1_SELL_SlowEma,MACDD1_BUY_SMA,PRICE_TYPICAL,MODE_MAIN,1);
//Sell
double diBearsPower8 =iBearsPower(NULL,1440,BEARS,PRICE_TYPICAL,0);
double diBearsPower25 =iBearsPower(NULL,1440,BEARS,PRICE_TYPICAL,1);
double diMACD9_D1SELL =iMACD(NULL,1440,MACDD1_SELL_FastEma,MACDD1_SELL_SlowEma,MACDD1_SELL_SMA,PRICE_TYPICAL,MODE_MAIN,0);
double diMACD10_D1SELL =iMACD(NULL,1440,MACDD1_SELL_FastEma,MACDD1_SELL_SlowEma,MACDD1_SELL_SMA,PRICE_TYPICAL,MODE_MAIN,1);
//Indicators on H4 timeframe ***************************************************************************
//BUY
double diMACD3_H4BUY =iMACD(NULL,240,MACDH4_BUY_FastEma,MACDH4_BUY_SlowEma,MACDH4_BUY_SMA,PRICE_TYPICAL,MODE_MAIN,0);
double diMACD4_H4BUY =iMACD(NULL,240,MACDH4_BUY_FastEma,MACDH4_BUY_SlowEma,MACDH4_BUY_SMA,PRICE_TYPICAL,MODE_MAIN,1);
double diMACD5_H4BUY =iMACD(NULL,240,MACDH4_BUY_FastEma,MACDH4_BUY_SlowEma,MACDH4_BUY_SMA,PRICE_TYPICAL,MODE_MAIN,1);
double diMACD6_H4BUY =iMACD(NULL,240,MACDH4_BUY_FastEma,MACDH4_BUY_SlowEma,MACDH4_BUY_SMA,PRICE_TYPICAL,MODE_MAIN,2);
//SELL
double diMACD11_H4SELL =iMACD(NULL,240,MACDH4_SELL_FastEma,MACDH4_SELL_SlowEma,MACDH4_SELL_SMA,PRICE_TYPICAL,MODE_MAIN,0);
double diMACD12_H4SELL =iMACD(NULL,240,MACDH4_SELL_FastEma,MACDH4_SELL_SlowEma,MACDH4_SELL_SMA,PRICE_TYPICAL,MODE_MAIN,1);
double diMACD13_H4SELL =iMACD(NULL,240,MACDH4_SELL_FastEma,MACDH4_SELL_SlowEma,MACDH4_SELL_SMA,PRICE_TYPICAL,MODE_MAIN,1);
double diMACD14_H4SELL =iMACD(NULL,240,MACDH4_SELL_FastEma,MACDH4_SELL_SlowEma,MACDH4_SELL_SMA,PRICE_TYPICAL,MODE_MAIN,2);
//COMMON INDICATORS
//Indicators on D1 timeframe
double diRSI36_COMD1 =iRSI(NULL,1440,14,PRICE_CLOSE,0);
double diWPR55_COMD1 =iWPR(NULL,1440,15,0);
//Indicators on 4H timeframe
double diRSI18_COMH4 =iRSI(NULL,240,14,PRICE_CLOSE,0);
double diWPR56_COMH4 =iWPR(NULL,240,15,0);
//Indicators on 1H timeframe
double diRSI35_COMH1 =iRSI(NULL,60,14,PRICE_CLOSE,0);
double diWPR37_COMH1 =iWPR(NULL,60,15,0);
//Indicators on 30 min timeframe
double diWPR7_COMM30 =iWPR(NULL,30,15,0);
double diWPR15_COMM30 =iWPR(NULL,30,15,0);
double diRSI30_COMM30 =iRSI(NULL,30,14,PRICE_CLOSE,0);
//Indicators on 15 min timeframe
double diWPR54_COMM15 =iWPR(NULL,15,15,0);
//Indicators on 5 min timeframe
double diWPR55_COMM5 =iWPR(NULL,5,15,0);
double diRSI56_COMM5 =iRSI(NULL,5,14,PRICE_CLOSE,0);
// Closing positions
// At 4H timeframe
double diRSI38_COMH4 =iRSI(NULL,240,Close_RSI_Period,PRICE_TYPICAL,0);
double diRSI44_COMH4 =iRSI(NULL,240,Close_RSI_Period,PRICE_TYPICAL,1);
double diRSI45_COMH4 =iRSI(NULL,240,Close_RSI_Period,PRICE_TYPICAL,2);
double diRSI46_COMH4 =iRSI(NULL,240,Close_RSI_Period,PRICE_TYPICAL,3);
double diRSI47_COMH4 =iRSI(NULL,240,Close_RSI_Period,PRICE_TYPICAL,4);
double diRSI48_COMH4 =iRSI(NULL,240,Close_RSI_Period,PRICE_TYPICAL,5);
double diRSI49_COMH4 =iRSI(NULL,240,Close_RSI_Period,PRICE_TYPICAL,6);
double diRSI50_COMH4 =iRSI(NULL,240,Close_RSI_Period,PRICE_TYPICAL,7);
double diRSI51_COMH4 =iRSI(NULL,240,Close_RSI_Period,PRICE_TYPICAL,8);
double diRSI52_COMH4 =iRSI(NULL,240,Close_RSI_Period,PRICE_TYPICAL,9);
double diRSI53_COMH4 =iRSI(NULL,240,Close_RSI_Period,PRICE_TYPICAL,10);
double diMACD39_COMH4 =iMACD(NULL,240,CLOSE_FastEma,CLOSE_SlowEma,CLOSE_SMA,PRICE_TYPICAL,MODE_MAIN,0);
double diMACD40_COMH4 =iMACD(NULL,240,CLOSE_FastEma,CLOSE_SlowEma,CLOSE_SMA,PRICE_TYPICAL,MODE_MAIN,1);
double diMACD41_COMH4 =iMACD(NULL,240,CLOSE_FastEma,CLOSE_SlowEma,CLOSE_SMA,PRICE_TYPICAL,MODE_MAIN,2);
double diMACD42_COMH4 =iMACD(NULL,240,CLOSE_FastEma,CLOSE_SlowEma,CLOSE_SMA,PRICE_TYPICAL,MODE_MAIN,3);
double diMACD43_COMH4 =iMACD(NULL,240,CLOSE_FastEma,CLOSE_SlowEma,CLOSE_SMA,PRICE_TYPICAL,MODE_MAIN,4);
//MA Initialization for close positions 4H timeframe
double diMA28 =iMA(NULL,240,FastMA,0,MODE_EMA,PRICE_CLOSE,0); //fast MA positions closing when fast MA increase slow MA at 4H timeframe or when slow MA increase fast MA
double diMA29 =iMA(NULL,240,SlowMA,0,MODE_EMA,PRICE_CLOSE,0); //slow MA
double diMA31 =iMA(NULL,240,FastMA,0,MODE_EMA,PRICE_CLOSE,1); //
double diMA32 =iMA(NULL,240,SlowMA,0,MODE_EMA,PRICE_CLOSE,1); //
double diMA33 =iMA(NULL,240,FastMA,0,MODE_EMA,PRICE_CLOSE,2); //
double diMA34 =iMA(NULL,240,SlowMA,0,MODE_EMA,PRICE_CLOSE,2); //
if(AccountFreeMargin()<(Margin_Level*Lots)){
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (!ExistPositions()){
if ((diBullsPower0 > 0.01 && diMACD1_D1BUY>diMACD2_D1BUY && diRSI36_COMD1 < RSID1_BUY && diMACD3_H4BUY > diMACD4_H4BUY && diMACD5_H4BUY > diMACD6_H4BUY && diRSI18_COMH4 < RSIH4_BUY && diRSI35_COMH1 < RSIH1_BUY && diWPR37_COMH1<WilliamsH1_BUY && diWPR15_COMM30<WilliamsM30_BUY && diWPR54_COMM15<WilliamsM15_BUY && diWPR55_COMM5<WilliamsM5_BUY)){ //&& diMA28>diMA29&& diBullsPower24>0.001 && diWPR7< - 85 && diMACD1>diMACD2
OpenBuy();
return(0);
}
if ((diBearsPower8 < -0.01 && diMACD9_D1SELL<diMACD10_D1SELL && diRSI36_COMD1>RSID1_SELL && diRSI18_COMH4 > RSIH4_SELL && diMACD11_H4SELL<diMACD12_H4SELL && diMACD13_H4SELL < diMACD14_H4SELL && diRSI35_COMH1 > RSIH1_SELL && diWPR37_COMH1>WilliamsH1_SELL && diWPR15_COMM30>WilliamsM30_SELL && diWPR54_COMM15>WilliamsM15_SELL && diWPR55_COMM5>WilliamsM5_SELL )){ // //&& diMA28<diMA29 && diBearsPower25 < -0.001 && diWPR15> -15&& diMACD9<diMACD10
OpenSell();
return(0);
}
}
if (ExistPositions()){
if(OrderType()==OP_BUY){
if ((diMA28<diMA29 && diMA31>=diMA32 && diMA33>diMA34)||((diMACD39_COMH4<diMACD40_COMH4 && diMACD40_COMH4<diMACD41_COMH4 && diMACD41_COMH4 < diMACD42_COMH4) && ( diRSI38_COMH4>CLOSE_RSI_LevelBUY ||diRSI44_COMH4>CLOSE_RSI_LevelBUY ||diRSI45_COMH4>CLOSE_RSI_LevelBUY ||diRSI46_COMH4>CLOSE_RSI_LevelBUY || diRSI47_COMH4>CLOSE_RSI_LevelBUY || diRSI48_COMH4>CLOSE_RSI_LevelBUY || diRSI49_COMH4>CLOSE_RSI_LevelBUY || diRSI50_COMH4>CLOSE_RSI_LevelBUY || diRSI51_COMH4>CLOSE_RSI_LevelBUY || diRSI52_COMH4>CLOSE_RSI_LevelBUY || diRSI53_COMH4>CLOSE_RSI_LevelBUY ))){ // && diMACD42_COMH4 < diMACD43_COMH4 diMACD16<diMACD17 && diMACD17<diMACD26'diMACD16<diMACD17 && diMACD17<diMACD26|| (diRSI18 < diRSI19 && diRSI20 >70 || diRSI21 >70))'
CloseBuy();
return(0);
}
}
if(OrderType()==OP_SELL){
if ((diMA28>diMA29 && diMA31<=diMA32 && diMA33<diMA34)||((diMACD39_COMH4>diMACD40_COMH4 && diMACD40_COMH4>diMACD41_COMH4 && diMACD41_COMH4 > diMACD42_COMH4) && (diRSI38_COMH4 < Close_RSI_LevelSELL ||diRSI44_COMH4 < Close_RSI_LevelSELL ||diRSI45_COMH4<Close_RSI_LevelSELL ||diRSI46_COMH4<Close_RSI_LevelSELL || diRSI47_COMH4<Close_RSI_LevelSELL || diRSI48_COMH4<Close_RSI_LevelSELL || diRSI49_COMH4<Close_RSI_LevelSELL || diRSI50_COMH4<Close_RSI_LevelSELL || diRSI51_COMH4<Close_RSI_LevelSELL || diRSI52_COMH4<Close_RSI_LevelSELL || diRSI53_COMH4>Close_RSI_LevelSELL))){ // && diMACD42_COMH4 > diMACD43_COMH4 diMACD22>diMACD23 && diMACD23>diMACD27
CloseSell();
return(0);
}
}
}
TrailingPositionsBuy(lTrailingStop);
TrailingPositionsSell(sTrailingStop);
return (0);
}
bool ExistPositions() {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) {
return(True);
}
}
}
return(false);
}
void TrailingPositionsBuy(int trailingStop) {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) {
if (OrderType()==OP_BUY) {
if (Bid-OrderOpenPrice()>trailingStop*Point) {
if (OrderStopLoss()<Bid-trailingStop*Point)
ModifyStopLoss(Bid-trailingStop*Point);
}
}
}
}
}
}
void TrailingPositionsSell(int trailingStop) {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) {
if (OrderType()==OP_SELL) {
if (OrderOpenPrice()-Ask>trailingStop*Point) {
if (OrderStopLoss()>Ask+trailingStop*Point || OrderStopLoss()==0)
ModifyStopLoss(Ask+trailingStop*Point);
}
}
}
}
}
}
void ModifyStopLoss(double ldStopLoss) {
bool fm;
fm = OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
if (fm && UseSound) PlaySound(NameFileSound);
}
void CloseBuy() {
bool fc;
fc=OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clCloseBuy);
if (fc && UseSound) PlaySound(NameFileSound);
}
void CloseSell() {
bool fc;
fc=OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clCloseSell);
if (fc && UseSound) PlaySound(NameFileSound);
}
void OpenBuy() {
double ldLot, ldStop, ldTake;
string lsComm;
ldLot = GetSizeLot();
ldStop = GetStopLossBuy();
ldTake = GetTakeProfitBuy();
lsComm = GetCommentForOrder();
OrderSend(Symbol(),OP_BUY,ldLot,Ask,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenBuy);
if (UseSound) PlaySound(NameFileSound);
}
void OpenSell() {
double ldLot, ldStop, ldTake;
string lsComm;
ldLot = GetSizeLot();
ldStop = GetStopLossSell();
ldTake = GetTakeProfitSell();
lsComm = GetCommentForOrder();
OrderSend(Symbol(),OP_SELL,ldLot,Bid,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenSell);
if (UseSound) PlaySound(NameFileSound);
}
string GetCommentForOrder() { return(Name_Expert); }
double GetSizeLot() {
double dLot;
if (LotsWayChoice==0) dLot=Lots;
// ôèêñèðîâàííûé ïðîöåíò îò äåïîçèòà
if (LotsWayChoice==1) {
dLot=MathCeil(AccountFreeMargin()/10000*LotsPercent)/10;
}
// ôðàêöèîííî-ïðîïîðöèîíàëüíûé
if (LotsWayChoice==2) {
int k=LotsDepoForOne;
for (double i=2; i<=LotsMax; i++) {
k=k+i*LotsDeltaDepo;
if (k>AccountFreeMargin()) {
dLot=(i-1)/10; break;
}
}
}
// ôðàêöèîííî-ôèêñèðîâàííûé
if (LotsWayChoice==3) {
dLot=MathCeil((AccountFreeMargin()-LotsDepoForOne)/LotsDeltaDepo)/10;
}
if (dLot<0.1) dLot=0.1;
return(dLot); }
double GetStopLossBuy() { return (Bid-lStopLoss*Point);}
double GetStopLossSell() { return(Ask+sStopLoss*Point); }
double GetTakeProfitBuy() { return(Ask+lTakeProfit*Point); }
double GetTakeProfitSell() { return(Bid-sTakeProfit*Point); }
Comments