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
#include <Trade/Trade.mqh> CTrade trade; ulong posTicket; int OnInit(){ return(INIT_SUCCEEDED); } void OnDeinit(const int reason){ } void OnTick(){ static datetime timestamp; datetime time = iTime(_Symbol,PERIOD_CURRENT,0); if (timestamp!= time){ timestamp = time; static int handleSlowMa = iMA(_Symbol,PERIOD_CURRENT,35,0,MODE_SMA,PRICE_CLOSE); double slowMaArray[]; CopyBuffer(handleSlowMa,0,1,2,slowMaArray); ArraySetAsSeries(slowMaArray,true); static int handleFastMa = iMA(_Symbol,PERIOD_CURRENT,10,0,MODE_SMA,PRICE_CLOSE); double fastMaArray[]; CopyBuffer(handleFastMa,0,1,2,fastMaArray); ArraySetAsSeries(fastMaArray,true); if(fastMaArray[0] > slowMaArray[0] && fastMaArray[1] < slowMaArray[1]){ Print("fast ma is now > than slow ma"); double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); trade.Buy(0.2,_Symbol,ask); if(posTicket>0 && PositionSelectByTicket(posTicket)){ int posType = (int) PositionGetInteger(POSITION_TYPE); int(posType == POSITION_TYPE_BUY){ trade.PositionClose(posTicket); posTicket = 0 } if(fastMaArray[0] < slowMaArray[0] && fastMaArray[1] > slowMaArray[1]){ Print("fast ma is now < than slow ma"); double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); trade.Sell(0.2,_Symbol,bid); if(posTicket>0 && PositionSelectByTicket(posTicket)){ int posType = (int) PositionGetInteger(POSITION_TYPE); int(posType == POSITION_TYPE_SELL){ trade.PositionClose(posTicket); posTicket = 0 } Comment("\nslowMaArray[0]: ",slowMaArray[0], "\nslowMaArray[1]: ",slowMaArray[1], "\nfastMaArray[0]: ",fastMaArray[0], "\nfastMaArray[1]: ",fastMaArray[1]); }
So I changed my attempt at closing the trades but cant test it as it gives me a error saing unbalanced paretheses for the parentheses after OnTick
Does this code look like it can work and if so can someone help to resolve the error?

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
Hi everyone.
I am new to MQL5 but have managed to create some code that buy and sell as I want but I am having trouble with the closing of the trades.
I want the trade to close as soon as the next and oppisite trade opens, can anyone help?
TIA