//+------------------------------------------------------------------+ //| ea_swst.mq5 | //| ea_swst v1.00 Copyright 2015, fxborg | //| http://fxborg-labo.hateblo.jp/ | //+------------------------------------------------------------------+ #property copyright "Copyright 2015, fxborg" #property link "http://fxborg-labo.hateblo.jp/" #property version "1.00" #include input string description1="1.-------------------------------"; input double Risk=0.1; // Risk input int SL = 1000; // Stop Loss distance input int TP = 4000; // Take Profit distance input int HourStart = 7; // Hour of trade start input int HourEnd = 20; // Hour of trade end input string description2="2.-------------------------------"; input int RSI_Period=8; // XRSI Period input int RSI_Smoothing=100; // XRSI Smoothing input int RSI_Threshold=5; // XRSI Threshold input double RSI_LongLevel=20;// Long Entry Level input double RSI_ShortLevel=80;// Short Entry Level input string description3="3.-------------------------------";// input ENUM_TIMEFRAMES Trend_TimeFrame=PERIOD_H1; // Trend Time Frame input int Trend_Period=43; // Trend period input int Trend_Smoothing=120;// Trend Smoothing input double Trend_Threshold=0.05;// Trend input string description4="4.-------------------------------";// input int Trail_Period=2; // Trailing Stop Period input int Trail_Minimum=150; // Trailing Stop Minimum Size input int Trail_Size=220; // Trailing Stop Size input int Trail_Maximum=700; // Trailing Stop Maximum Size //--- class CMyEA : public CExpertAdvisor { protected: double m_risk; // size of risk int m_sl; // Stop Loss int m_tp; // Take Profit int m_ts; // Trailing Stop int m_hourStart; // Hour of trade start int m_hourEnd; // Hour of trade end int m_trend_handle; // Trend Handle int m_rsi_handle; // Swing Strength Handle ENUM_TIMEFRAMES m_trend_tf; // Trend Timeframe int m_trend_period; // Trend period int m_trend_smoothing; // Trend Smoothing double m_trend_threshold; // Trend threshold int m_rsi_period; // RSI period int m_rsi_threshold; // RSI Threshold int m_rsi_smoothing; // RSI Smoothing double m_rsi_long_level; double m_rsi_short_level; int m_trail_handle; int m_trail_size; int m_trail_period; int m_trail_minimum; int m_trail_maximum; public: void CMyEA(); void ~CMyEA(); virtual bool Init(string smb,ENUM_TIMEFRAMES tf); // initialization virtual bool Main(); // main function virtual void OpenPosition(long dir); // open position on signal virtual void ClosePosition(long dir); // close position on signal }; //------------------------------------------------------------------ CMyEA void CMyEA::CMyEA(void) { } //------------------------------------------------------------------ ~CMyEA void CMyEA::~CMyEA(void) { IndicatorRelease(m_trend_handle); IndicatorRelease(m_trail_handle); IndicatorRelease(m_rsi_handle); } //------------------------------------------------------------------ Init bool CMyEA::Init(string smb,ENUM_TIMEFRAMES tf) { if(!CExpertAdvisor::Init(0,smb,tf)) return(false); // initialize parent class // copy parameters if(Trail_Minimum>=Trail_Size)return (false); if(Trail_Size>=Trail_Maximum)return (false); m_risk=Risk; m_tp=TP; m_sl=SL; m_hourStart=HourStart; m_hourEnd=HourEnd; //--- m_rsi_period=RSI_Period; m_rsi_smoothing=RSI_Smoothing; m_rsi_long_level=RSI_LongLevel; m_rsi_short_level=RSI_ShortLevel; m_rsi_threshold=RSI_Threshold; //--- m_trend_tf=Trend_TimeFrame; m_trend_period=Trend_Period; m_trend_smoothing=Trend_Smoothing; m_trend_threshold=Trend_Threshold; //--- m_trail_size=Trail_Size; m_trail_period=Trail_Period; m_trail_minimum=Trail_Minimum; m_trail_maximum=Trail_Maximum; //--- m_trend_handle=iCustom(NULL,m_trend_tf,"snma",m_trend_period,m_trend_smoothing,m_trend_threshold); if(m_trend_handle==INVALID_HANDLE) return(false); // if there is an error, then exit m_rsi_handle=iCustom(NULL,m_tf,"XRSI_v1_00",m_rsi_period,m_rsi_smoothing,m_rsi_threshold); if(m_rsi_handle==INVALID_HANDLE) return(false); // if there is an error, then exit m_trail_handle=iCustom(NULL,m_tf,"LogicalStops",m_trail_size,m_trail_period); if(m_trail_handle==INVALID_HANDLE) return(false); // if there is an error, then exit m_bInit=true; return(true); // "trade allowed" } //------------------------------------------------------------------ Main bool CMyEA::Main() // main function { if(!CExpertAdvisor::Main()) return(false); // call function of parent class if(Bars(m_smb,m_trend_tf)<=m_trend_period*2) return(false); // if there are insufficient number of bars static CIsNewBar NB; if(!NB.IsNewBar(m_smb,m_tf))return (true); // check each direction MqlRates rt[2]; if(CopyRates(m_smb,m_tf,1,2,rt)!=2) { Print("CopyRates ",m_smb," history is not loaded"); return(WRONG_VALUE); } double TREND[2]; double OSC[3]; double OSC_SIG[2]; double TRAIL_UP[2]; double TRAIL_DN[2]; if(CopyBuffer(m_trend_handle,1,1,2,TREND)!=2) { Print("CopyBuffer Trend - no data"); return(WRONG_VALUE); } if(CopyBuffer(m_rsi_handle,0,1,3,OSC)!=3) { Print("CopyBuffer Signal - no data"); return(WRONG_VALUE); } if(CopyBuffer(m_rsi_handle,1,1,2,OSC_SIG)!=2) { Print("CopyBuffer Signal - no data"); return(WRONG_VALUE); } if(CopyBuffer(m_trail_handle,0,1,2,TRAIL_UP)!=2) { Print("CopyBuffer LogicalStops - no data"); return(WRONG_VALUE); } if(CopyBuffer(m_trail_handle,1,1,2,TRAIL_DN)!=2) { Print("CopyBuffer LogicalStops - no data"); return(WRONG_VALUE); } double long_level=m_rsi_long_level; double short_level=m_rsi_short_level; // OPEN BUY if(TREND[1]!=0 && OSC_SIG[1]==2 && OSC[2]long_level-5) OpenPosition(ORDER_TYPE_BUY); // CLOSE BUY if(TREND[1]==0) ClosePosition(ORDER_TYPE_BUY); CheckTrailingStopLong(TRAIL_DN[1],rt[1].low,m_trail_minimum,m_trail_maximum); // OPEN SELL if(TREND[1]!=2 && OSC_SIG[1]==0 && OSC[2]>short_level && OSC[2]