Channeal break out code New comment Mathieu76 2020.10.25 18:32 Hi, I begin to MQL4 and I wrote this code. This code is "if price is > x, close sell position and buy", and "if price is < y, close buy position and sell". Only one open trade each time. Compilaton is ok, but is there easy to fix or obvious mistakes ? #property copyright "" #property link "" extern double LevelExposure1 = 11400; extern double LevelExposure2 = 11300; extern double Lots = 1.0; extern int StopLoss = 0; extern int TakeProfit = 0; extern int Slippage = 2000; int ID=234; int init() { return(0); } int deinit() { return(0); } int getCurrentOrderType() { int semua=OrdersTotal(); for(int a=semua-1;semua>=0;a--) { if(OrderSelect(a,SELECT_BY_POS)) { if(OrderSymbol()!=Symbol()) continue; if(OrderType()>1) continue; //tipe=OrderType(); return OrderType(); break; } } return(-1); } int start() { int tiketnya; int tipe; tipe = getCurrentOrderType(); // currently one selling trade, but now price > target => close + open a buy if(tipe==OP_SELL && Ask>=LevelExposure1) { if(close_positions(OP_SELL)==true) { tiketnya=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"TheGuardian",ID,0,DeepSkyBlue); if(tiketnya<0) { Alert("FAIL BUY Order: "+Symbol()+" @"+DoubleToStr(Ask,Digits)+" #"+GetLastError()); return(0); } else { return(0); } } } // currently one buying trade, but now price < target 2 => close + open a sell if(tipe==OP_BUY && Bid<=LevelExposure2) { if(close_positions(OP_BUY)==true) { tiketnya=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"TheGuardian",ID,0,Red); if(tiketnya<0) { Alert("FAIL SELL Order: "+Symbol()+" @"+DoubleToStr(Ask,Digits)+" #"+GetLastError()); return(0); } else { return(0); } } } if(StopLoss!=0||TakeProfit!=0) { //setSL_and_TP(); } return(0); } int close_positions(int t) { int hasil=false; int semua=OrdersTotal(); for(int a=semua-1;semua>=0;a--) { if(OrderSelect(a,SELECT_BY_POS)) { if(OrderSymbol()!=Symbol()) continue; if(OrderType()==t&&OrderType()==OP_SELL) { hasil=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,CLR_NONE); } if(OrderType()==t&&OrderType()==OP_BUY) { hasil=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,CLR_NONE); } break; } } return(hasil); } Buy if current price > x Traders Forum How to code? New comment
Hi,
I begin to MQL4 and I wrote this code.
This code is "if price is > x, close sell position and buy", and "if price is < y, close buy position and sell".
Only one open trade each time.
Compilaton is ok, but is there easy to fix or obvious mistakes ?