//+------------------------------------------------------------------+ //| Reduce_risks.mq4 | //| Copyright 2017, Alexander Masterskikh | //| | //+------------------------------------------------------------------+ #property copyright "2017, Alexander Masterskikh" #property link "https://www.mql5.com/en/users/a.masterskikh" input double TakeProfit = 600; //take profit input double StopLoss = 300; //stop loss input double Lots = 1; //number of lots input int Depo_first = 10000; //initial client deposit in monetary terms input int Percent_risk_depo = 5; //maximum allowed risk per deposit (in % to the client's initial deposit) input bool test = false; //set: true - for test (default is false for trading) //------------------------------------------------------------------------------------------------- void OnTick(void) { //---define variables--- int f,numb,kol; double sl_buy, tp_buy, sl_sell, tp_sell; double L_prev_m15, L_2p_m15, L_3p_m15; double H_prev_m15, H_2p_m15, H_3p_m15; double O_cur_m15, O_prev_m15, O_2p_m15, C_prev_m15, C_2p_m15; double MA4_cur_m15, MA4_prev_m15, MA4_2p_m15, MA5_prev_m15, MA8_cur_m15, MA8_prev_m15, MA8_2p_m15; double MA8_cur, MA8_prev, MA8_2p, MA8_3p, MA5_cur, MA5_prev, MA5_2p, MA13_cur, MA13_prev, MA13_2p, MA60_cur, MA60_prev, MA60_2p, MA24_cur_h1; double C_prev_h1, O_prev_h1, H_prev_h1, L_prev_h1, H_2p_h1, L_2p_h1; datetime Time_cur, Time_cur_m15; double shift_buy, shift_sell; double Max_pos, Min_pos; //-------- //MANAGING TECHNICAL PARAMETERS (STATES AND PERMISSIONS) - ON BROKER'S AND CLIENT TERMINAL'S SIDES------------------------------- if(test==false) //check: test or trade (test - true, trade - false) {//parameters that are not checked when testing on history data--- if(IsConnected() == false) //check the status of the main connection of the client terminal with the broker server { Print("Entry disabled-broker server offline"); Alert("Entry disabled-broker server offline"); return; } if(IsTradeAllowed() == false) //check ability to trade using EAs (broker flow, permission to trade) { Print("Entry disabled-broker trade flow busy and/or EA trading permission disabled :", IsTradeAllowed()); Alert("Entry disabled-broker trade flow busy and/or EA trading permission disabled :", IsTradeAllowed()); return; } if( !AccountInfoInteger(ACCOUNT_TRADE_EXPERT) ) //check trade account properties { Print("Auto trading disabled for account :", AccountInfoInteger(ACCOUNT_LOGIN), "on trade server side"); Alert("Auto trading disabled for account :", AccountInfoInteger(ACCOUNT_LOGIN), "on trade server side"); return; } if(IsExpertEnabled() == false) //check if launching EAs is allowed in the client terminal { Print("Entry disabled- robot's trading permission disabled in the terminal :", IsExpertEnabled()); Alert("Entry disabled- robot's trading permission disabled in the terminal :", IsExpertEnabled()); return; } if(IsStopped() == true) //check the arrival of the command to complete mql4 program execution { Print("Entry disabled-command to complete mql4 program execution triggered"); Alert("Entry disabled-command to complete mql4 program execution triggered"); return; } } //parameters not checked when testing on history data end here //Manage the presence of a sufficient number of quotes in the terminal by a used symbol--- if(Bars<100) { Print("insufficient number of bars on the current chart"); return; } //Manage placing an EA on a necessary timeframe---- if(Period() != PERIOD_M1) { Print("EA placed incorrectly, place EA on tf :", PERIOD_M1); Alert("EA placed incorrectly, place EA on tf :", PERIOD_M1); return; } //Manage placing the EA to necessary financial instruments---- if(Symbol() != "EURUSD" && Symbol() != "USDCHF" && Symbol() != "USDJPY") { Print("EA placed incorrectly-invalid financial instrument"); Alert("EA placed incorrectly-invalid financial instrument"); return; } //---------- //MANAGE FINANCIAL PARAMETERS RELATED TO LIMITING THE TOTAL RISK FOR THE CLIENT'S DEPOSIT------------------------------- if(kol < 1) //no orders { if(AccountBalance() <= NormalizeDouble( (Depo_first*((100 - Percent_risk_depo)/100)), 0))//if the risk limit for the entire deposit have been previously exceeded { Print("Entry disabled-risk limit reached earlier=",Percent_risk_depo, " % for the entire deposit=", Depo_first); Alert("Entry disabled-risk limit reached earlier=",Percent_risk_depo, " % for the entire deposit=", Depo_first); return; } if(AccountFreeMargin() < (1000*Lots)) //if margin funds allowed for opening orders on the current account are insufficient { Print("Insufficient margin funds. Free account margin = ",AccountFreeMargin()); Alert("Insufficient margin funds. Free account margin = ",AccountFreeMargin()); return; //...then exit } } //-------------- //Variable values: L_prev_m15 = iLow(NULL,PERIOD_M15,1); L_2p_m15 = iLow(NULL,PERIOD_M15,2); L_3p_m15 = iLow(NULL,PERIOD_M15,3); H_prev_m15 = iHigh(NULL,PERIOD_M15,1); H_2p_m15 = iHigh(NULL,PERIOD_M15,2); H_3p_m15 = iHigh(NULL,PERIOD_M15,3); O_cur_m15 = iOpen(NULL,PERIOD_M15,0); O_prev_m15 = iOpen(NULL,PERIOD_M15,1); O_2p_m15 = iOpen(NULL,PERIOD_M15,2); C_prev_m15 = iClose(NULL,PERIOD_M15,1); C_2p_m15 = iClose(NULL,PERIOD_M15,2); Time_cur_m15 = iTime(NULL,PERIOD_M15,0); C_prev_h1 = iClose(NULL,PERIOD_H1,1); O_prev_h1 = iOpen(NULL,PERIOD_H1,1); H_prev_h1 = iHigh(NULL,PERIOD_H1,1); L_prev_h1 = iLow(NULL,PERIOD_H1,1); H_2p_h1 = iHigh(NULL,PERIOD_H1,2); L_2p_h1 = iLow(NULL,PERIOD_H1,2); MA4_cur_m15 = iMA(NULL,PERIOD_M15,4,0,MODE_SMA,PRICE_TYPICAL,0); MA4_prev_m15 = iMA(NULL,PERIOD_M15,4,0,MODE_SMA,PRICE_TYPICAL,1); MA4_2p_m15 = iMA(NULL,PERIOD_M15,4,0,MODE_SMA,PRICE_TYPICAL,2); MA5_prev_m15 = iMA(NULL,PERIOD_M15,5,0,MODE_SMA,PRICE_TYPICAL,1); MA8_cur_m15 = iMA(NULL,PERIOD_M15,8,0,MODE_SMA,PRICE_TYPICAL,0); MA8_prev_m15 = iMA(NULL,PERIOD_M15,8,0,MODE_SMA,PRICE_TYPICAL,1); MA8_2p_m15 = iMA(NULL,PERIOD_M15,8,0,MODE_SMA,PRICE_TYPICAL,2); MA8_cur = iMA(NULL,PERIOD_M1,8,0,MODE_SMA,PRICE_TYPICAL,0); MA8_prev = iMA(NULL,PERIOD_M1,8,0,MODE_SMA,PRICE_TYPICAL,1); MA8_2p = iMA(NULL,PERIOD_M1,8,0,MODE_SMA,PRICE_TYPICAL,2); MA8_3p = iMA(NULL,PERIOD_M1,8,0,MODE_SMA,PRICE_TYPICAL,3); MA5_cur = iMA(NULL,PERIOD_M1,5,0,MODE_SMA,PRICE_TYPICAL,0); MA5_prev = iMA(NULL,PERIOD_M1,5,0,MODE_SMA,PRICE_TYPICAL,1); MA5_2p = iMA(NULL,PERIOD_M1,5,0,MODE_SMA,PRICE_TYPICAL,2); MA13_cur = iMA(NULL,PERIOD_M1,13,0,MODE_SMA,PRICE_TYPICAL,0); MA13_prev = iMA(NULL,PERIOD_M1,13,0,MODE_SMA,PRICE_TYPICAL,1); MA13_2p = iMA(NULL,PERIOD_M1,13,0,MODE_SMA,PRICE_TYPICAL,2); MA60_cur = iMA(NULL,PERIOD_M1,60,0,MODE_SMA,PRICE_TYPICAL,0); MA60_prev = iMA(NULL,PERIOD_M1,60,0,MODE_SMA,PRICE_TYPICAL,1); MA60_2p = iMA(NULL,PERIOD_M1,60,0,MODE_SMA,PRICE_TYPICAL,2); MA24_cur_h1 = iMA(NULL,PERIOD_H1,24,0,MODE_SMA,PRICE_TYPICAL,0); kol = OrdersTotal(); Time_cur = TimeCurrent(); if(kol < 1) //continue if there are no open orders { //---MARKET ENTRY ALGORITHM - BUY------------------------------------------------------------------------------------------- if( //----REDUCE RISKS RELATED TO THE PRESENCE OF A STRONG VOLATILITY AT THE TIME OF MARKET ENTRY ---- //Simulate the absence of a strong volatility in the recent history: ( High[1] - Low[1]) <= 200*Point && //limit the amplitude of a lower timeframe (М1) ( High[2] - Low[2]) <= 200*Point && ( High[3] - Low[3]) <= 200*Point && (H_prev_m15 - L_prev_m15) <= 300*Point && //limit the amplitude of a higher timeframe (М15) (H_2p_m15 - L_2p_m15) <= 300*Point && (H_3p_m15 - L_3p_m15) <= 300*Point && (H_prev_m15 - L_3p_m15) <= 300*Point && //limit the amplitude of the channel made of the higher timeframe candles (М15) (High[1] - Low[1]) >= (1.1*(High[2] - Low[2])) && //limit activity on the previous bar relative to the 2 nd bar in the quote history (High[1] - Low[1]) < (3.0*(High[2] - Low[2])) && //same //----REDUCE RISKS RELATED TO RESISTANCE LEVELS AT THE TIME OF MARKET ENTRY----- //Simulate the case when local resistance levels are broken by the current price: Bid > High[1] && //on М1 (smaller scale) Bid > H_prev_m15 && //on М15 (larger scale) //---REDUCE RISKS RELATED TO ENTERING THE OVERBOUGHT AREA AT THE TIME OF MARKET ENTRY----- //Simulate binding to the start of the wave to decrease the entry probability in the overbought area: ((MA8_prev > Low[1] && MA8_prev < High[1]) || (MA8_2p > Low[2] && MA8_2p < High[2]) || //start of the wave - not farther than three bars in data history (М1) (MA8_3p > Low[3] && MA8_3p < High[3])) && //same MA5_prev_m15 > L_prev_m15 && MA5_prev_m15 < H_prev_m15 && //start of the wave - on the previous bar of the higher timeframe (М15) //---REDUCE RISKS RELATED TO THE ABSENCE OF A CLEARLY DEFINED TREND AT THE TIME OF MARKET ENTRY------- //Simulate the candles direction on the lower timeframe: Close[2] > Open[2] && //upward candle direction on the 2 nd bar in history (М1) Close[1] > Open[1] && //previous candle's upward direction (М1) //Simulate direction of moving averages on the higher timeframe: MA5_cur > MA5_2p && MA60_cur > MA60_2p && //upward МАs: use moving averages with the periods of 5 and 60 (М1) //Simulate the hierarchy of moving averages on the lower timeframe: MA5_cur > MA8_cur && MA8_cur > MA13_cur && //form the "hierarchy" of three МАs on М1 (Fibo periods:5,8,13), this is the indirect sign of the upward movement //Simulate the location of the current price relative to the lower timeframes' moving averages: Bid > MA5_cur && Bid > MA8_cur && Bid > MA13_cur && Bid > MA60_cur && //current price exceeds МА (5,8,13,60) on М1, this is an indirect sign of the upward movement //Simulate the candle direction on the higher timeframe: C_prev_m15 > O_prev_m15 && //previous candle's upward direction (М15) //Simulate the MA direction on the higher timeframe: MA4_cur_m15 > MA4_2p_m15 && //upward МА with the period of 4 (М15) //Simulate the hierarchy of moving averages on the higher timeframe: MA4_prev_m15 > MA8_prev_m15 && //form the "hierarchy" of two МАs on М15 (periods 4 and 8), this is the indirect sign of the upward movement //Simulate the location of the current price relative to the higher timeframes' moving averages: Bid > MA4_cur_m15 && //current price exceeds МА4 (М15), this is an indirect sign of the upward movement Bid > MA24_cur_h1 && //current price exceeds МА24 (МН1), this is an indirect sign of the upward movement //Simulate a micro-trend inside the current candle of the lower timeframe, as well as the entry point: Bid > Open[0] && //presence of the upward movement inside the current candle (М1) //Simulate sufficient activity of the previous process at the higher timeframe: (C_prev_m15 - O_prev_m15) > (0.5*(H_prev_m15 - L_prev_m15)) && //share of the candle "body" exceeds 50% of the candle amplitude value (previous М15 candle) (H_prev_m15 - C_prev_m15) < (0.25*(H_prev_m15 - L_prev_m15)) && //correction depth limitation is less than 25% of the candle amplitude (previous М15 candle) H_prev_m15 > H_2p_m15 && //upward trend by local resistance levels (two М15 candles) O_prev_m15 < H_prev_m15 && O_prev_m15 > L_prev_m15 && //presence of a wick (previous М15 candle) relative to the current candle's Open price //Simulate sufficient activity of the previous process on the lower timeframe: (Close[1] - Open[1]) > (0.5*(High[1] - Low[1])) && //share of the candle "body" exceeds 50% of the candle amplitude value (previous М1 candle) (High[1] - Low[1]) > 70*Point && //previous candle has an amplitude exceeding the threshold one (excluding an evident flat) (High[2] - Close[2]) < (0.25*(High[2] - Low[2])) && //correction depth limitation is less than 20% of the candle amplitude (the second candle in the М1 data history) High[1] > High[2] && //upward trend by local resistance levels (two М1 candles) Open[1] < High[1] && Open[1] > Low[1] ) //presence of the wick (previous tfМ1 candle) relative to the current candle's Open price { //if the Buy entry algorithm conditions specified above are met, generate the Buy entry order: sl_buy = NormalizeDouble((Bid-StopLoss*Point),Digits); tp_buy = NormalizeDouble((Ask+TakeProfit*Point),Digits); numb = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,sl_buy,tp_buy,"Reduce_risks",16384,0,Green); if(numb > 0) { if(OrderSelect(numb,SELECT_BY_TICKET,MODE_TRADES)) { Print("Buy entry : ",OrderOpenPrice()); } } else Print("Error when opening Buy order : ",GetLastError()); return; } //--- MARKET ENTRY ALGORITHM - SELL-------------------------------------------------------------------------------------------------- if( //----REDUCE RISKS RELATED TO THE PRESENCE OF A STRONG VOLATILITY AT THE TIME OF MARKET ENTRY ---- //Simulate the absence of a strong volatility in the recent history: ( High[1] - Low[1]) <= 200*Point && //limit the amplitude of a lower timeframe (М1) ( High[2] - Low[2]) <= 200*Point && ( High[3] - Low[3]) <= 200*Point && (H_prev_m15 - L_prev_m15) <= 300*Point && //limit the amplitude of a higher timeframe (М15) (H_2p_m15 - L_2p_m15) <= 300*Point && (H_3p_m15 - L_3p_m15) <= 300*Point && (H_prev_m15 - L_3p_m15) <= 300*Point && //limit the amplitude of the channel made of the higher timeframe candles (М15) (High[1] - Low[1]) >= (1.1*(High[2] - Low[2])) && //limit activity on the previous bar relative to the 2 nd bar in the quote history (High[1] - Low[1]) < (3.0*(High[2] - Low[2])) && //same //----REDUCE RISKS RELATED TO RESISTANCE LEVELS AT THE TIME OF MARKET ENTRY----- //Simulate the case when local resistance levels are broken by the current price: Bid < Low[1] && //on М1 (smaller scale) Bid < L_prev_m15 && //on М15 (larger scale) //---REDUCE RISKS RELATED TO ENTERING IN THE OVERSOLD AREA AT THE TIME OF MARKET ENTRY----- //Simulate binding to the start of the wave to decrease the entry probability in the oversold area: ((MA8_prev > Low[1] && MA8_prev < High[1]) || (MA8_2p > Low[2] && MA8_2p < High[2]) || //start of the wave - not farther than three bars in data history (М1) (MA8_3p > Low[3] && MA8_3p < High[3])) && //same MA5_prev_m15 > L_prev_m15 && MA5_prev_m15 < H_prev_m15 && //start of the wave - on the previous bar of the higher timeframe (М15) //---REDUCE RISKS RELATED TO THE ABSENCE OF A CLEARLY DEFINED TREND AT THE TIME OF MARKET ENTRY------- //Simulate the candles direction on the lower timeframe: Close[2] < Open[2] && //downward candle direction on the 2 nd bar in history (М1) Close[1] < Open[1] && //previous candle's downward direction (М1) //Simulate direction of moving averages on the lower timeframe: MA5_cur < MA5_2p && MA60_cur < MA60_2p && //downward МАs: use moving averages with the periods of 5 and 60 (М1) //Simulate the hierarchy of moving averages on the lower timeframe: MA5_cur < MA8_cur && MA8_cur < MA13_cur && //form the "hierarchy" of three МАs on М1 (Fibo periods:5,8,13), this is the indirect sign of the downward movement //Simulate the location of the current price relative to the lower timeframes' moving averages: Bid < MA5_cur && Bid < MA8_cur && Bid < MA13_cur && Bid < MA60_cur && //current price exceeds МА (5,8,13,60) on М1, this is an indirect sign of the downward movement //Simulate the candle direction on the higher timeframe: C_prev_m15 < O_prev_m15 && //previous candle's downward direction (М15) //Simulate the candle direction on the higher timeframe: MA4_cur_m15 < MA4_2p_m15 && //previous candle's downward direction 4 (М15) //Simulate the MA direction on the higher timeframe: MA4_prev_m15 < MA8_prev_m15 && //form the "hierarchy" of two МАs on М1 (periods 4 and 8), this is the indirect sign of the downward movement //Simulate the location of the current price relative to the higher timeframes' moving averages: Bid < MA4_cur_m15 && //current price is lower than МА4 (М15), this is an indirect sign of the downward movement Bid < MA24_cur_h1 && //current price is lower than МА24 (МН1), this is an indirect sign of the downward movement //Simulate a micro-trend inside the current candle of the lower timeframe, as well as the entry point: Bid < Open[0] && //presence of the downward movement inside the current candle (М1) //Simulate sufficient activity of the previous process at the higher timeframe: (O_prev_m15 - C_prev_m15) > (0.5*(H_prev_m15 - L_prev_m15)) && //share of the candle "body" exceeds 50% of the candle amplitude value (previous М15 candle) (C_prev_m15 - L_prev_m15) < (0.25*(H_prev_m15 - L_prev_m15)) && //correction depth limitation is less than 25% of the candle amplitude (previous М15 candle) L_prev_m15 < L_2p_m15 && //upward trend by local resistance levels (two М15 candles) O_prev_m15 < H_prev_m15 && O_prev_m15 > L_prev_m15 && //presence of a wick (previous М15 candle) relative to the current candle's Open price //Simulate sufficient activity of the previous process on the lower timeframe: (Open[1] - Close[1]) > (0.5*(High[1] - Low[1])) && //share of the candle "body" exceeds 50% of the candle amplitude value (previous М1 candle) (High[1] - Low[1]) > 70*Point && //previous candle has an amplitude exceeding the threshold one (excluding an evident flat) (Close[2] - Low[2]) < (0.25*(High[2] - Low[2])) && //correction depth limitation is less than 20% of the candle amplitude (the second candle in the М1 data history) Low[1] < Low[2] && //downward trend by local resistance levels (two М1 candles) Open[1] < High[1] && Open[1] > Low[1] ) //presence of the wick (previous М1 candle) relative to the current candle's Open price { //if the Sell entry algorithm conditions specified above are met, generate the Sell entry order: sl_sell = NormalizeDouble((Ask+StopLoss*Point),Digits); tp_sell = NormalizeDouble((Bid-TakeProfit*Point),Digits); numb = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,sl_sell,tp_sell,"Reduce_risks",16384,0,Red); if(numb > 0) { if(OrderSelect(numb,SELECT_BY_TICKET,MODE_TRADES)) { Print("Sell entry : ",OrderOpenPrice()); } } else Print("Error when opening Sell order : ",GetLastError()); } //--- the market entry algorithm (Buy, Sell) ends here return; } //--- Check open orders and financial instrument symbol to prepare position closing: for(f=0; f < kol; f++) { if(!OrderSelect(f,SELECT_BY_POS,MODE_TRADES)) continue; if(OrderType()<=OP_SELL && //check the order type OrderSymbol()==Symbol()) //check the symbol { if(OrderType()==OP_BUY) //if the order is "Buy", move to closing Buy position: { //------BUY POSITION CLOSING ALGORITHM------------------------------------------------------------------------------------------------- //The module to search for the price maximum inside the open position-------------- //first, define the distance from the current point inside the open position up to the market entry point: shift_buy = 0; if(Time_cur > OrderOpenTime() && OrderOpenTime() > 0) //if the current time is farther than the entry point... { shift_buy = NormalizeDouble( ((Time_cur - OrderOpenTime() ) /60), 0 ); } //define the distance in tfM1 bars up to the entry point //now, define the price maximum after the market entry: Max_pos = 0; if(Time_cur > OrderOpenTime() && shift_buy > 0) { Max_pos = NormalizeDouble((High[iHighest(NULL,PERIOD_M1, MODE_HIGH ,(shift_buy + 1), 0)]), Digits);} //the module to search for the price maximum inside the open position ends here-- //Pass to closing the Buy position (OR logic options): if( //REDUCE RISKS RELATED TO PRICE COLLAPSES AFTER MARKET ENTRY---------------------- (Bid < Open[0] && (Open[0] - Bid) >= 100*Point && (Time_cur - Time[0]) <= 20) //entry conditions (in any area) in case of a price collapse (reference point - current M1 candle Open price) || (Bid < O_cur_m15 && (O_cur_m15 - Bid) >= 200*Point && (Time_cur - Time_cur_m15) <= 120) //exit conditions (in any area) in case of a price collapse (reference point - current M15 candle Open price) || ((Time_cur - OrderOpenTime()) > 60 && Close[1] < Open[1] && (Open[1] - Close[1]) >= 200*Point) //exit conditions (in any area) in case of a price collapse (reference parameter - previous M1 candle amplitude) || //REDUCE RISKS RELATED TO UNCERTAIN PRICE MOVEMENT AMPLITUDE AFTER MARKET ENTRY-- //Manage fixed profit (per position): (Bid > OrderOpenPrice() && (Bid - OrderOpenPrice()) >= 100*Point) //exit conditions in profit area (shadow take profit) || //Manage the maximum acceptable price deviation //from the current High after market entry: (shift_buy >= 1 && //shift no less than 1 bar from the entry point Time_cur > OrderOpenTime() && Max_pos > 0 && OrderOpenTime() > 0 && OrderOpenPrice() > 0 && //there is the current maximum after the entry Max_pos > OrderOpenPrice() && //current maximum is in the profit area Bid < Max_pos && //there is the reversal price movement (Max_pos - Bid) >= 200*Point) //reverse deviation from the current maximum for market entry || //Manage pre-defined risk limit (per position): (Bid < OrderOpenPrice() && (OrderOpenPrice() - Bid) >= 200*Point) //exit conditions in the loss area (shadow stop loss) || //Manage pre-defined risk limit (for the entire deposit): (AccountBalance() <= NormalizeDouble( (Depo_first*((100 - Percent_risk_depo)/100)), 0)) ) //if the risk limit is exceeded for the entire deposit during the current trading { if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet)) //if the closing algorithm is processed, form the order for closing the Buy position Print("Error closing Buy position ",GetLastError()); //otherwise print Buy position closing error return; } } else //otherwise, move to closing the Sell position: { //------SELL POSITION CLOSING ALGORITHM------------------------------------------------------------------------------------ //The module to search for the price maximum inside the open position-------------- //first, define the distance from the current point inside the open position up to the market entry point: shift_sell = 0; if(Time_cur > OrderOpenTime() && OrderOpenTime() > 0) //if the current time is farther than the entry point... { shift_sell = NormalizeDouble( ((Time_cur - OrderOpenTime() ) /60), 0 ); } //define the distance in M1 bars up to the entry point //now, define the price minimum after entering the market: Min_pos = 0; if(Time_cur > OrderOpenTime() && shift_sell > 0) { Min_pos = NormalizeDouble( (Low[iLowest(NULL,PERIOD_M1, MODE_LOW ,(shift_sell + 1), 0)]), Digits); } //the module to search for the price maximum inside the open position ends here-- //Pass to closing the open Sell position (OR logic options): if( //REDUCE RISKS RELATED TO PRICE COLLAPSES AFTER MARKET ENTRY----------------- (Bid > Open[0] && (Bid - Open[0]) >= 100*Point && (Time_cur - Time[0]) <= 20) //exit conditions (in any area) during a price collapse (reference point - current M1 candle Open price) || (Bid > O_cur_m15 && (Bid - O_cur_m15) >= 200*Point && (Time_cur - Time_cur_m15) <= 120) //exit conditions (in any area) during a price collapse (reference point - current M15 candle Open price) || ((Time_cur - OrderOpenTime()) > 60 && Close[1] > Open[1] && (Close[1] - Open[1]) >= 200*Point) //exit conditions in any zone during a price collapse (reference parameter - previous M1 candle amplitude) || //REDUCE RISKS RELATED TO UNCERTAIN PRICE MOVEMENT AMPLITUDE AFTER MARKET ENTRY-- //Manage fixed profit (per position): (Bid < OrderOpenPrice() && (OrderOpenPrice()- Bid) >= 100*Point) //exit conditions in profit area (shadow take profit) || //Manage the maximum acceptable price deviation //from the current minimum after market entry: (shift_sell >= 1 && //shift no less than 1 bar from the entry point Time_cur > OrderOpenTime() && Min_pos > 0 && OrderOpenTime() > 0 && OrderOpenPrice() > 0 && //there is the current minimum after entry Min_pos < OrderOpenPrice() && //current minimum is in the profit area Bid > Min_pos && //there is a reverse price movement (Bid - Min_pos) >= 200*Point) //reverse deviation from the current minimum to exit the market || //Manage pre-defined risk limit (per position): (Bid > OrderOpenPrice() && (Bid - OrderOpenPrice()) >= 200*Point) //exit conditions in the loss area (shadow stop loss) || //Manage pre-defined risk limit (for the entire deposit): (AccountBalance() <= NormalizeDouble( (Depo_first*((100 - Percent_risk_depo)/100)), 0)) ) //if the risk limit exceeded for the entire deposit during the current trading { if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet)) //if the close algorithm is executed, generate the Sell position close order Print("Error closing Sell position ",GetLastError()); //otherwise, print the Sell position closing error return; } } } } //--- } //-----------------------------------------------------------------------------------------------------------