- Edit your post and post your code as code:
- if you program does not behave as it should use the debugger to control and follow every single program step:
https://www.metatrader5.com/en/metaeditor/help/development/debug
Code debugging - Developing programs - MetaEditor Help
- www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
#include <Trade\Trade.mqh> #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //| Input Variables | //+------------------------------------------------------------------+ input int InpFastEMA = 5 ; //FastEMA input int InpSlowSMA = 40 ; //SlowSMA input int InpTakeprofit = 150 ; //Take profit in points input int InpStoploss = 400 ; //Stoploss in points //+------------------------------------------------------------------+ //| Global Variables | //+------------------------------------------------------------------+ int fastHandle; int slowHandle; double fastbuffer[]; double slowbuffer[]; datetime openTimeBuy = 0; datetime openTimeSell = 0; datetime iClose(); bool TradeOpen = false; double open = iOpen(NULL,PERIOD_CURRENT,0); double high = iHigh(NULL,PERIOD_CURRENT,0); double low = iLow(NULL,PERIOD_CURRENT,0); //double close = iClose(NULL,PERIOD_CURRENT,0); //long volume= iVolume(NULL,0,0); int bars = iBars(NULL,0); CTrade Trade; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // check user input if(InpFastEMA <= 0){ Print("Fast EMA <=0"); return INIT_PARAMETERS_INCORRECT; } if(InpSlowSMA <= 0){ Print("Slow SMA <=0"); return INIT_PARAMETERS_INCORRECT; } if(InpFastEMA >= InpSlowSMA ){ Print("Fast Ema >= Slow SMA"); return INIT_PARAMETERS_INCORRECT; } if(InpStoploss <= 0){ Print("Stoploss <=0"); return INIT_PARAMETERS_INCORRECT; } if(InpTakeprofit <= 0){ Print("Take profit <=0"); return INIT_PARAMETERS_INCORRECT; } // Create handle fastHandle = iMA(_Symbol,PERIOD_CURRENT,InpFastEMA,0,MODE_EMA,PRICE_CLOSE); if(fastHandle == INVALID_HANDLE){ Print("Failed to create fast handle"); return INIT_FAILED; } slowHandle = iMA(_Symbol,PERIOD_CURRENT,InpSlowSMA,0,MODE_SMA,PRICE_CLOSE); if(slowHandle == INVALID_HANDLE){ Print("Failed to create slow handle"); return INIT_FAILED; } ArraySetAsSeries(fastbuffer,true); ArraySetAsSeries(slowbuffer,true); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason){ if(fastHandle != INVALID_HANDLE){IndicatorRelease(fastHandle);} if(slowHandle != INVALID_HANDLE){IndicatorRelease(slowHandle);} } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // get indicator value int values = CopyBuffer(fastHandle,MAIN_LINE,0,3,fastbuffer); if(values != 3){ Print("not enough data for EMA"); return; } values = CopyBuffer(slowHandle,MAIN_LINE,0,3,slowbuffer); if(values != 3){ Print("not enough data for SMA"); return; } //Print ( "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[2] < slowbuffer[2] && !TradeOpen){ iClose(_Symbol,PERIOD_CURRENT,0); double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); double tp = ask + InpTakeprofit * SymbolInfoDouble(_Symbol,SYMBOL_POINT); double sl = ask - InpStoploss * SymbolInfoDouble(_Symbol,SYMBOL_POINT); Trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.1,ask,sl,tp,"Long"); TradeOpen= true; } return; if(fastbuffer[1] < slowbuffer[1] && fastbuffer[2] > slowbuffer[2] && !TradeOpen){ iClose(_Symbol,PERIOD_CURRENT,0); double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); double tp = bid - InpTakeprofit * SymbolInfoDouble(_Symbol,SYMBOL_POINT); double sl = bid + InpStoploss * SymbolInfoDouble(_Symbol,SYMBOL_POINT); Trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,0.1,bid,sl,tp,"Short"); return; } } } //+------------------------------------------------------------------+
Carl Schreiber #:
Thanks think I have added it right above.
- Edit your post and post your code as code:
- if you program does not behave as it should use the debugger to control and follow every single program step:
https://www.metatrader5.com/en/metaeditor/help/development/debug
Comments that do not relate to this topic, have been moved to "Off-topic posts".
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
Hey all, pulling my hair out trying to work out why my EA will only perform one trade on backtest and wont open another afterwards even though I can see conditions are met. Good someone kindly point me in the right direction.