You need to check for the stops level of the broker too , the code is on top of the onInit function but ideally it should be checked after init (if not testing)
#include <Trade/Trade.mqh> CTrade trade; input int InpFastPeriod = 14; // Fast period input int InpSlowPeriod = 21; // Slow period input int InpStopLoss = 100; // Stop Loss input int InpTakeProfit = 200; // Take Profit input double EnforceLimitX = 1.5; // Enforced multiple of stop level int fastHandle; int slowHandle; double fastBuffer[]; double slowBuffer[]; datetime openTimeBuy = 0; datetime openTimeSell =0; double actualSL=0.0,actualTP=0.0; int OnInit(){ actualSL=InpStopLoss*_Point; actualTP=InpTakeProfit*_Point; //get the stop level for the symbol double stop_level=((double)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL))*_Point; if(stop_level>0.0&&actualSL<=stop_level){ actualSL=NormalizeDouble(stop_level*EnforceLimitX,_Digits); } if(stop_level>0.0&&actualTP<=stop_level){ actualTP=NormalizeDouble(stop_level*EnforceLimitX,_Digits); } //create handles fastHandle = iMA(_Symbol,PERIOD_CURRENT,InpFastPeriod,0,MODE_SMA,PRICE_CLOSE); if(fastHandle == INVALID_HANDLE){ Alert("Failed to create fast handle"); return INIT_FAILED; } slowHandle = iMA (_Symbol,PERIOD_CURRENT,InpSlowPeriod,0,MODE_SMA,PRICE_CLOSE); if (slowHandle == INVALID_HANDLE){ Alert("faild to create slow handle"); return INIT_FAILED; } ArraySetAsSeries(fastBuffer, true); ArraySetAsSeries(slowBuffer, true); return(INIT_SUCCEEDED); } void OnDeinit(const int reason){ if (fastHandle != INVALID_HANDLE) {IndicatorRelease(fastHandle);} if (slowHandle != INVALID_HANDLE) {IndicatorRelease(slowHandle);} } void OnTick() { //get indicator values int values = CopyBuffer(fastHandle,0,0,2,fastBuffer); if (values != 2) { Print("Not enough data for fast moving average"); return; } values = CopyBuffer(slowHandle,0,0,2,slowBuffer); if (values != 2) { Print("Not enough data for slow moving average"); return; } //Comment("fast[0]:",fastBuffer[0],"\n", //"fast[1]:",fastBuffer[1],"\n", //"slow[0]:",slowBuffer[0],"\n", //"slow[1]:",slowBuffer[1]); //check for cross buy if (fastBuffer[1] <= slowBuffer[1] && fastBuffer[0] > slowBuffer[0] && openTimeBuy != iTime(_Symbol,PERIOD_CURRENT,0)){ openTimeBuy = iTime(_Symbol,PERIOD_CURRENT,0); double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); double sl =ask - actualSL; double tp =ask + actualTP;//InpStopLoss * SymbolInfoDouble(_Symbol,SYMBOL_POINT); trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.01,ask,sl,tp); } //check for cross sell if (fastBuffer[1] >= slowBuffer[1] && fastBuffer[0] < slowBuffer[0] && openTimeSell != iTime(_Symbol,PERIOD_CURRENT,0)){ openTimeSell = iTime(_Symbol,PERIOD_CURRENT,0); double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); double sl = bid + actualSL; double tp = bid - actualTP;//InpTakeProfit * SymbolInfoDouble(_Symbol,SYMBOL_POINT); trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,0.1,bid,sl,tp); } }
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello,
please can help code now work with stop loss and take profit
Thanks