//+------------------------------------------------------------------+ //+ Code generated using FxPro Quant 2.1.4 | //+------------------------------------------------------------------+ #property strict #define __STRATEGY_MAGIC 1001000000 #define __SLEEP_AFTER_EXECUTION_FAIL 400 //Input variables input int _Whole_Number_Input = 20; // Start Hour input int _Whole_Number_Input_6 = 50; // Trailing points input double _RSI_Oversold_Level = 30; // RSI Oversold Level input double _RSI_Exit_Long_Level = 60; // RSI Exit Long Level input double _Number_Input = 0.2; // Number Input input double _RSI_Exit_Short_Level = 40; // RSI Exit Short Level input int _Whole_Number_Input_3 = 1000; // Multiplier input int _Whole_Number_Input_5 = 500; // Take Profit input int _Whole_Number_Input_4 = 250; // Stop Loss input int _Whole_Number_Input_2 = 23; // End Hour //Global declaration double _Relative_Strength_Index; double _MACD; double _Stochastic; bool _IsTime; bool _RSI_Exit_Long; bool _RSI_Exit_Short; bool _Sell_Signal; bool _Buy_Signal; int init() { return(0); } int start() { //Local declaration bool _Simple_Trailing_Stop = false; bool _Close_All_Long_Trades = false; bool _Close_All_Short_Trades = false; bool _Buy = false; bool _Sell = false; _Relative_Strength_Index = iRSI(Symbol(), 0, 14, 0, 0); _MACD = iMACD(Symbol(), 0, 10, 42, 8, 0, 0, 0); _Stochastic = iStochastic(Symbol(), 0, 5, 3, 3, 0, 0, 0, 0); _IsTime = IsTime(_Whole_Number_Input, _Whole_Number_Input_2, 0, 0); _Simple_Trailing_Stop = Simple_Trailing_Stop(0, 0, _Whole_Number_Input_6, 10); _RSI_Exit_Long = (_Relative_Strength_Index > _RSI_Exit_Long_Level); _RSI_Exit_Short = (_Relative_Strength_Index < _RSI_Exit_Short_Level); if( _RSI_Exit_Long ) _Close_All_Long_Trades = Close_All_Long_Trades(0); if( _RSI_Exit_Short ) _Close_All_Short_Trades = Close_All_Short_Trades(0); _Sell_Signal = ((_Relative_Strength_Index > (100 - (_RSI_Oversold_Level))) && (_MACD > 0) && (_Stochastic >= 80) && _IsTime && _Simple_Trailing_Stop); _Buy_Signal = ((_Relative_Strength_Index < _RSI_Oversold_Level) && (_MACD < 0) && (_Stochastic <= 20) && _IsTime && _Simple_Trailing_Stop); if( _Buy_Signal && !__isExist( 0, Symbol() ) ) { _Buy = ((AccountBalance() / (_Whole_Number_Input_3)) * (_Number_Input)) >= MarketInfo( Symbol() ,MODE_MINLOT) && AccountFreeMarginCheck( Symbol(), 0, ((AccountBalance() / (_Whole_Number_Input_3)) * (_Number_Input))) >= 0; if( _Buy == true ) _Buy = OrderSend( Symbol(), 0, ((AccountBalance() / (_Whole_Number_Input_3)) * (_Number_Input)), MarketInfo( Symbol(), MODE_ASK ), 0, __stopLossValue( Symbol(), 0, MarketInfo( Symbol(), MODE_ASK ), _Whole_Number_Input_4 ), __takeProfitValue( Symbol(), 0, MarketInfo( Symbol(), MODE_ASK ), _Whole_Number_Input_5 ), "FxProQuant" + "(" + WindowExpertName() + ") " + "", __STRATEGY_MAGIC + 0 ) >= 0; if( _Buy == false ) Sleep(__SLEEP_AFTER_EXECUTION_FAIL); } if( _Sell_Signal && !__isExist( 0, Symbol() ) ) { _Sell = ((AccountBalance() / (_Whole_Number_Input_3)) * (_Number_Input)) >= MarketInfo( Symbol() ,MODE_MINLOT) && AccountFreeMarginCheck( Symbol(), 1, ((AccountBalance() / (_Whole_Number_Input_3)) * (_Number_Input))) >= 0; if( _Sell == true ) _Sell = OrderSend( Symbol(), 1, ((AccountBalance() / (_Whole_Number_Input_3)) * (_Number_Input)), MarketInfo( Symbol(), MODE_BID ), 1, __stopLossValue( Symbol(), 1, MarketInfo( Symbol(), MODE_BID ), _Whole_Number_Input_4 ), __takeProfitValue( Symbol(), 1, MarketInfo( Symbol(), MODE_BID ), _Whole_Number_Input_5 ), "FxProQuant" + "(" + WindowExpertName() + ") " + "", __STRATEGY_MAGIC + 0 ) >= 0; if( _Sell == false ) Sleep(__SLEEP_AFTER_EXECUTION_FAIL); } return(0); } bool __selectOrderByMagic(int magic, string symbol) { for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == __STRATEGY_MAGIC + magic && OrderSymbol() == symbol) return(true); } return(false); } bool __isExist(int magic, string symbol) { return(__selectOrderByMagic(magic, symbol)); } double __takeProfitValue(string symbol,int orderType, double price, double points) { if (points == 0) return (0); else if (orderType == 0 || orderType == 2 || orderType == 4) return ( NormalizeDouble( price + MarketInfo( symbol, MODE_POINT ) * points, (int)MarketInfo( symbol, MODE_DIGITS ) ) ); else return ( NormalizeDouble( price - MarketInfo( symbol, MODE_POINT ) * points, (int)MarketInfo( symbol, MODE_DIGITS ) ) ); } double __stopLossValue(string symbol, int orderType, double price, double points) { if (points == 0) return (0); else if (orderType == 0 || orderType == 2 || orderType == 4) return ( NormalizeDouble( price - MarketInfo( symbol, MODE_POINT ) * points, (int)MarketInfo( symbol, MODE_DIGITS ) ) ); else return ( NormalizeDouble( price + MarketInfo( symbol, MODE_POINT ) * points, (int)MarketInfo( symbol, MODE_DIGITS ) ) ); } bool Close_All_Long_Trades(int MagicIndex) { int total = OrdersTotal(); bool result = true; for(int i=total-1;i>=0;i--) { if(!OrderSelect(i, SELECT_BY_POS)) continue; if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue; if(OrderType() != OP_BUY) continue; if(!OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red )){ result = false; Sleep(__SLEEP_AFTER_EXECUTION_FAIL); } } return(result); } bool Close_All_Short_Trades(int MagicIndex) { int total = OrdersTotal(); bool result = true; for(int i=total-1;i>=0;i--) { if(!OrderSelect(i, SELECT_BY_POS)) continue; if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue; if(OrderType() != OP_SELL) continue; if(!OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red )){ result = false; Sleep(__SLEEP_AFTER_EXECUTION_FAIL); } } return(result); } bool IsTime (int startHour, int endHour, int startMinute, int endMinute) { if (startHour < 0 || startHour > 23 || endHour < 0 || endHour > 23 || startMinute < 0 || startMinute > 59 || endMinute < 0 || endMinute > 59) return false; int startTime = startHour*60 + startMinute; int endTime = endHour*60 + endMinute; int time = Hour()*60 + Minute(); if (startTime < endTime) return (time >= startTime && time <= endTime); else if (startTime > endTime) return (time >= startTime || time <= endTime); else return (time == startTime); } bool Simple_Trailing_Stop(int MagicIndex, int WaitForProfit, int TrailingStopPoints, int MinAdjustmentPoints) { double pnlPoints=0; double price, sl, tp; double point = MarketInfo(Symbol(),MODE_POINT); int stopLevel = int(MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD)); int cmd; bool result = true; double newSl; int total = OrdersTotal(); for(int i=total-1;i>=0;i--){ if (!OrderSelect(i, SELECT_BY_POS)) continue; if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue; cmd = OrderType(); sl = NormalizeDouble(OrderStopLoss(),Digits); tp = OrderTakeProfit(); if (OrderType() == OP_BUY) { price = MarketInfo(Symbol(),MODE_BID); newSl = NormalizeDouble(price - TrailingStopPoints * point, Digits); if(((tp - price)/point) < stopLevel && tp != 0) continue; if(((price - newSl)/point) < stopLevel)continue; if(WaitForProfit == 0) { pnlPoints = (price - OrderOpenPrice())/point; if (pnlPoints < TrailingStopPoints ) continue; } if (sl + MinAdjustmentPoints*point>= newSl) continue; if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSl, tp, 0)) { printf("Error: Failed to modify trade. Ticket #%i, error code: %i", OrderTicket(), GetLastError()); result = false; Sleep(__SLEEP_AFTER_EXECUTION_FAIL); } } else if (OrderType() == OP_SELL) { price = MarketInfo(Symbol(),MODE_ASK); newSl = NormalizeDouble(price+ TrailingStopPoints * point, Digits); if(((price - tp)/point) < stopLevel) continue; if(((newSl - price)/point) < stopLevel) continue; if(WaitForProfit == 0) { pnlPoints = (OrderOpenPrice() - price)/point; if (pnlPoints < TrailingStopPoints) continue; } if (sl - MinAdjustmentPoints*point <= newSl && sl != 0) continue; if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSl, tp, 0)) { printf("Error: Failed to modify trade. Ticket #%i, error code: %i", OrderTicket(), GetLastError()); result = false; Sleep(__SLEEP_AFTER_EXECUTION_FAIL); } } } return(result); }