#include extern int myMagic = 10; extern double TakeProfit =100; extern bool PartialClosing = True; extern int TPDivide = 2; extern double StopLoss =20; extern double BreakEvenStop =20; extern int slippage=2; extern int shift=0; extern double Lots = 0.01; extern double MaximumRisk = 1.0 extern bool FixedLot = false; 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); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForSignals(datetime tradeopenbar) { int i,hstTotal=OrdersHistoryTotal(); for(i=0;iOpen[1]) if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==1) && Profit>0); //selecting the last trade OrderSend(Symbol(),OP_BUY,SetLots,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,Yellow); { if (Close[1]0); //selecting the last trade OrderSend(Symbol(),OP_SELL,SetLots,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,DeepPink); { //SECOND LEVEL TRADE 2 X MULTIPLIER if (Close[1]>Open[1]) if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==1) && Profit<0); //selecting the last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==2) && Profit>0); //selecting the second to last trade OrderSend(Symbol(),OP_BUY,SetLots*2,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,Yellow); { if (Close[1]0); //selecting the second to last trade OrderSend(Symbol(),OP_SELL,SetLots*2,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,DeepPink); { //THIRD LEVEL TRADE 4 X MULTIPLIER if (Close[1]>Open[1]) if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==1) && Profit<0); //selecting the last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==2) && Profit<0); //selecting the second to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==3) && Profit>0); //selecting the second to last trade OrderSend(Symbol(),OP_BUY,SetLots*4,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,Yellow); { if (Close[1]0); //selecting the third to last trade OrderSend(Symbol(),OP_SELL,SetLots*4,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,DeepPink); { //FOURTH LEVEL TRADE 8 X MULTIPLIER if (Close[1]>Open[1]) if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==1) && Profit<0); //selecting the last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==2) && Profit<0); //selecting the second to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==3) && Profit<0); //selecting the third to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==4) && Profit>0); //selecting the fourth to last trade OrderSend(Symbol(),OP_BUY,SetLots*8,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,Yellow); { if (Close[1]0); //selecting the fourth to last trade OrderSend(Symbol(),OP_SELL,SetLots*8,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,DeepPink); { //FIFTH LEVEL TRADE 16 X MULTIPLIER if (Close[1]>Open[1]) if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==1) && Profit<0); //selecting the last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==2) && Profit<0); //selecting the second to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==3) && Profit<0); //selecting the third to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==4) && Profit>0); //selecting the fourth to last trade OrderSend(Symbol(),OP_BUY,SetLots*16,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,Yellow); { if (Close[1]0); //selecting the fourth to last trade OrderSend(Symbol(),OP_SELL,SetLots*16,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,DeepPink); { //SIXTH LEVEL TRADE 32 X MULTIPLIER if (Close[1]>Open[1]) if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==1) && Profit<0); //selecting the last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==2) && Profit<0); //selecting the second to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==3) && Profit<0); //selecting the third to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==4) && Profit<0); //selecting the fourth to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==5) && Profit>0); //selecting the fifth to last trade OrderSend(Symbol(),OP_BUY,SetLots*32,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,Yellow); { if (Close[1]0); //selecting the fifth to last trade OrderSend(Symbol(),OP_SELL,SetLots*32,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,DeepPink); { //SEVENTH LEVEL TRADE 64 X MULTIPLIER if (Close[1]>Open[1]) if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==1) && Profit<0); //selecting the last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==2) && Profit<0); //selecting the second to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==3) && Profit<0); //selecting the third to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==4) && Profit<0); //selecting the fourth to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==5) && Profit<0); //selecting the fifth to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==6) && Profit>0); //selecting the sixth to last trade OrderSend(Symbol(),OP_BUY,SetLots*64,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,Yellow); { if (Close[1]0); //selecting the sixth to last trade OrderSend(Symbol(),OP_SELL,SetLots*64,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,DeepPink); { //EIGHTH LEVEL TRADE if (Close[1]>Open[1]) if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==1) && Profit<0); //selecting the last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==2) && Profit<0); //selecting the second to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==3) && Profit<0); //selecting the third to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==4) && Profit<0); //selecting the fourth to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==5) && Profit<0); //selecting the fifth to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==6) && Profit<0); //selecting the sixth to last trade if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==7) && Profit>0); //selecting the seventh to last trade OrderSend(Symbol(),OP_BUY,SetLots*128,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,Yellow); { if (Close[1]0); //selecting the seventh to last trade OrderSend(Symbol(),OP_SELL,SetLots*128,Bid,slippage,Bid+StopLoss*Point,takeProfit,EAComment,myMagic,0,DeepPink); { //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=myMagic || OrderSymbol()!=Symbol()) continue; if TakeProfit=true OrderClose(OrderTicket(),OrderLots(),Bid,3,White); // if take profit or stop loss is reached, close order break; else } //+------------------------------------------------------------------+ //| Execute BreakEvenStop | //+------------------------------------------------------------------+ 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+1*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-1*Point, 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()