hi, i need the attached code updated so that it will work on the latest mt4 build.
i will gladly pay for any help.
thank you
i will gladly pay for any help.
thank you
Inserted Code
#include <stdlib.mqh> extern int myMagic = 123; extern double TakeProfit =500; extern bool PartialClosing = True; extern int TPDivide = 1; extern double StopLoss =500; extern int TrailingStop =0; extern double BreakEvenStop =0; extern int slippage=2; extern int shift=0; extern double Lots = 0.10; extern double MaximumRisk = 1.0; // extern bool FixedLot = true; extern int TimeStart1=000; extern int TimeStop1=2359; extern int TimeStart2=0000; extern int TimeStop2=0000; extern int TimeStart3=0000; extern int TimeStop3=0000; extern int TimeStart4=0000; extern int TimeStop4=0000; extern int OrderTriesNumber=25; bool buysig,sellsig,closebuy,closesell; int lastsig,tries; double LotsRisk(int StopLoss) { double lot=Lots; //---- select lot size if (!FixedLot && (StopLoss > 0)) lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk*0.001/StopLoss,2); else lot=Lots; //---- return lot size if(lot<0.01) lot=0.01; return(lot); } //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; //---- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue; if(OrderSymbol()==Symbol() && OrderMagicNumber()==myMagic) { if(OrderType()==OP_BUY) buys++; if(OrderType()==OP_SELL) sells++; } } //---- return orders volume if(buys>0) return(buys); else return(-sells); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForSignals(datetime tradeopenbar) { sellsig=false; buysig=false; if(tradeopenbar<Time[0]){ if (Close[0]>Close[50]) { buysig=true; } if (Close[0]<Close[50]) { sellsig=true; } } closebuy=false; closesell=false; if (High[0]<Low[0]) { closebuy=true; } if (High[0]<Low[0]) { closesell=true; } } datetime CheckForOpen(datetime orderopenbar) { int res,ct; datetime result = orderopenbar; ct=Hour()*100+Minute(); if ( !((ct>=TimeStart1 && ct<TimeStop1) || (ct>=TimeStart2 && ct<TimeStop2) || (ct>=TimeStart3 && ct<TimeStop3) || (ct>=TimeStart4 && ct<TimeStop4)) ) return(result); double takeProfit; double SetLots=0; string EAComment=''; //---- sell conditions if(sellsig && lastsig!=-1) { res=0; tries=0; while (res<=0 && tries<OrderTriesNumber) { while(!IsTradeAllowed()) Sleep(5000); RefreshRates(); if(TakeProfit > MarketInfo(Symbol(), MODE_STOPLEVEL)) takeProfit = Bid-TakeProfit*Point; else takeProfit = 0; SetLots=LotsRisk(StopLoss); EAComment="TimeWave EURUSD M15"; res=OrderSend(Symbol(),OP_SELL,SetLots,Bid,slippage,Bid+StopLoss*Point, takeProfit,EAComment,myMagic,0,DeepPink); if (res<0) Print("Error opening SELL order : ",ErrorDescription(GetLastError())); else result = Time[0]; // return this bar open time. tries++; } lastsig= 0; return(result); } //---- buy conditions if(buysig && lastsig!=1) { res=0; tries=0; while (res<=0 && tries<OrderTriesNumber) { while(!IsTradeAllowed()) Sleep(5000); RefreshRates(); if(TakeProfit > MarketInfo(Symbol(), MODE_STOPLEVEL)) takeProfit = Ask+TakeProfit*Point; else takeProfit = 0; SetLots=LotsRisk(StopLoss); EAComment="TimeWave EURUSD M15"; res=OrderSend(Symbol(),OP_BUY,LotsRisk(StopLoss),Ask,slippage,Ask-StopLoss*Point, takeProfit,EAComment,myMagic,0,Yellow); if (res<0) Print("Error opening BUY order : ",ErrorDescription(GetLastError())); else result = Time[0]; // return this bar open Time tries++; } lastsig = 0; return(result); } return(result); } //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { bool bres; int tr; for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=myMagic || OrderSymbol()!=Symbol()) continue; //---- check order type if(OrderType()==OP_BUY) { if (closebuy) { bres=false; tries=0; while (!bres && tries<OrderTriesNumber) { while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); } RefreshRates(); bres=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,White); Sleep(3000); if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError())); tries++; } } else if(PartialClosing) PartialClosingExe(); break; } if(OrderType()==OP_SELL) { if (closesell) { bres=false; tries=0; while (!bres && tries<OrderTriesNumber) { while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); } RefreshRates(); bres=OrderClose(OrderTicket(),OrderLots(),Ask,slippage,White); Sleep(3000); if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError())); tries++; } } else if(PartialClosing) PartialClosingExe(); break; } } } void BreakEvenStop() { bool bres; double StopLoss; if ( BreakEvenStop > 2 ) { for (int i = 0; i < OrdersTotal(); i++) { if ( OrderSelect (i, SELECT_BY_POS) == false ) continue; if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic ) continue; if ( OrderType() == OP_BUY ) { if ( (Bid < OrderOpenPrice()+BreakEvenStop*Point) ) return; StopLoss = OrderOpenPrice(); if ( StopLoss > OrderStopLoss() ) { bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss+4*Point, OrderTakeProfit(), 0, White); Sleep(3000); if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError())); } } if ( OrderType() == OP_SELL ) { if ( (Ask > OrderOpenPrice()-BreakEvenStop*Point) ) return; StopLoss = OrderOpenPrice(); if ( StopLoss < OrderStopLoss() ) { bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss-4*Point, OrderTakeProfit(), 0, White); Sleep(3000); if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError())); } } } } return; } void TrailingStop() { bool bres; double StopLoss; if ( TrailingStop > MarketInfo(Symbol(), MODE_STOPLEVEL)) { for (int i = 0; i < OrdersTotal(); i++) { if ( OrderSelect (i, SELECT_BY_POS) == false ) continue; if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic ) continue; if ( OrderType() == OP_BUY ) { if ( (Bid < OrderStopLoss()+TrailingStop*Point) ) return; StopLoss = Bid-TrailingStop*Point; if ( StopLoss > OrderStopLoss() ) { bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White); Sleep(3000); if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError())); } } if ( OrderType() == OP_SELL ) { if ( (Ask > OrderStopLoss()-TrailingStop*Point) ) return; StopLoss = Ask+TrailingStop*Point; if ( StopLoss < OrderStopLoss() ) { bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White); Sleep(3000); if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError())); } } } } return; } double RoundLots(string S, double P) { double value=P; double MinimalLot=0; MinimalLot=MarketInfo(S,MODE_MINLOT); if(MinimalLot<=0) { RefreshRates(); return(-1); } value=MathFloor(P/MinimalLot)*MinimalLot; return(value); } //+------------------------------------------------------------------+ //| Linear closing of position | //+------------------------------------------------------------------+ void PartialClosingExe() { int Profit=0; if(OrderType()==OP_BUY) Profit=MathRound((Bid-OrderOpenPrice())/Point); if(OrderType()==OP_SELL) Profit=MathRound((OrderOpenPrice()-Ask)/Point); if(Profit<=0) return; int PipStep=MathFloor(TakeProfit/TPDivide); if(PipStep<1) PipStep=1; if(PipStep>TakeProfit) PipStep=TakeProfit; double LotPercent=1.00/TPDivide; if(LotPercent>1.00) LotPercent=1.00; if(TPDivide>TakeProfit && TakeProfit>0) LotPercent=1.00/TakeProfit; double FirstPosition=LotsRisk(StopLoss); double CutLots=FirstPosition*LotPercent*MathFloor(Profit/PipStep); CutLots=RoundLots(Symbol(),CutLots); if(CutLots<=0) return; CutLots=CutLots-(FirstPosition-OrderLots()); if(CutLots<=0) return; if(OrderLots()<CutLots) CutLots=OrderLots(); if(OrderType()==OP_BUY) OrderClose(OrderTicket(),CutLots,Bid,slippage,White); if(OrderType()==OP_SELL) OrderClose(OrderTicket(),CutLots,Ask,slippage,White); return; } void start() { static datetime TradeOpenBar; //---- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; //---- check for signals CheckForSignals(TradeOpenBar); //---- calculate open orders by current symbol if (CalculateCurrentOrders(Symbol())==0) TradeOpenBar = CheckForOpen(TradeOpenBar); else CheckForClose(); BreakEvenStop(); TrailingStop(); } //+------------------------------------------------------------------+