SWB Grid 4.1 Candle

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

//+------------------------------------------------------------------+

//| swb grid 4 .mq4 |


//| totom sukopratomo |
//| forexengine@gmail.com |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//+----- belum punya account fxopen? --------------------------------+
//+----- buka di http://fxind.com?agent=123621 ----------------------+
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//+----- ingin bisa scalping dengan real tp 3 pips? -----------------+
//+----- ingin dapat bonus $30 dengan deposit awal $100? ------------+
//+----- buka account di http://instaforex.com/index.php?x=NQW ------+
//+------------------------------------------------------------------+

#property copyright "totom sukopratomo"


#property link "forexengine@gmail.com"
#define buy 10
#define sell -10

#define SIGNAL_NONE 0
#define SIGNAL_BUY 1
#define SIGNAL_SELL 2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
extern bool SignalMail = False;
extern int Slippage = 3;

//---- input parameters


extern bool use_daily_target=false;
extern double daily_target=100;
extern bool trade_in_fri=true;
extern int magic=123;
extern double start_lot=0.1;
// 1000$ use MaxLot = 0.1 and multiplier = 2.0
extern double range=50;
extern int level=10;
extern bool lot_multiplier=false;
extern double multiplier=2.0;
extern double increament=0.1;
extern bool use_sl_and_tp=false;
extern double sl=60;
extern double tp=30;
extern double tp_in_money=50.0;
extern bool stealth_mode=true;

extern bool Use_Exit=true;


extern bool use_ema=true;
extern bool use_martigal = false;
extern bool use_closeall = true;
extern bool Use_EMATrailingStop = true;
extern bool Use_TrailingStop = true;
extern double TrailStart=100;
extern double TrailStop=100;

double pt;
double minlot;
double stoplevel;
int prec=0;
int a=0;

Page 1/14
int ticket=0;

int BarCount;
int Current;
bool TickCheck = False;
extern bool EachTickMode = True;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
if(Digits==3 || Digits==5) pt=10*Point;
else pt=Point;
minlot = MarketInfo(Symbol(),MODE_MINLOT);
stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
if(start_lot<minlot) Print("lotsize is to small.");
if(sl<stoplevel) Print("stoploss is to tight.");
if(tp<stoplevel) Print("takeprofit is to tight.");
if(minlot==0.01) prec=2;
if(minlot==0.1) prec=1;
//----
BarCount = Bars;
if (EachTickMode) Current = 0; else Current = 1;
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if(use_daily_target && dailyprofit()>=daily_target)
{
Comment("\ndaily target achieved.");
return(0);
}
if(!trade_in_fri && DayOfWeek()==5 && total()==0)
{
Comment("\nstop trading in Friday.");
return(0);
}
if(total()==0 && a==0)
{
if(signal()==buy)
{
if(stealth_mode)
{
if(use_sl_and_tp) ticket=OrderSend(Symbol(),0,start_lot,Ask,3,Ask
-sl*pt,Ask+tp*pt,"",magic,0,Blue);
else ticket=OrderSend(Symbol(),0,start_lot,Ask,3,
0, 0,"",magic,0,Blue);
}

Page 2/14
else
{
if(use_sl_and_tp)
{
if(OrderSend(Symbol(),0,start_lot,Ask,3,Ask-sl*pt,Ask+tp*pt,""
,magic,0,Blue)>0)
{
for(int i=1; i<level; i++)
{
if(lot_multiplier) ticket=OrderSend(Symbol(),2,
NormalizeDouble(start_lot*MathPow(multiplier,i),prec),Ask-(range*i)*pt,3,(
Ask-(range*i)*pt)-sl*pt,(Ask-(range*i)*pt)+tp*pt,"",magic,0,Blue);
else ticket=OrderSend(Symbol(),2,
NormalizeDouble(start_lot+increament*i,prec) ,Ask-(range*i)*pt,3,(
Ask-(range*i)*pt)-sl*pt,(Ask-(range*i)*pt)+tp*pt,"",magic,0,Blue);
}
}
}
else
{
if(OrderSend(Symbol(),0,start_lot,Ask,3,0,0,"",magic,0,Blue)>0
)
{
for(i=1; i<level; i++)
{
if(lot_multiplier) ticket=OrderSend(Symbol(),2,
NormalizeDouble(start_lot*MathPow(multiplier,i),prec),Ask-(range*i)*pt,3,0,
0,"",magic,0,Blue);
else ticket=OrderSend(Symbol(),2,
NormalizeDouble(start_lot+increament*i,prec) ,Ask-(range*i)*pt,3,0,
0,"",magic,0,Blue);
}
}
}
}
}
if(signal()==sell)
{
if(stealth_mode)
{
if(use_sl_and_tp) ticket=OrderSend(Symbol(),1,start_lot,Bid,3,Bid
+sl*pt,Bid-tp*pt,"",magic,0,Red);
else ticket=OrderSend(Symbol(),1,start_lot,Bid,3,
0, 0,"",magic,0,Red);
}
else
{
if(use_sl_and_tp)
{
if(OrderSend(Symbol(),1,start_lot,Bid,3,Bid+sl*pt,Bid-tp*pt,""
,magic,0,Red)>0)
{
for(i=1; i<level; i++)
{
if(lot_multiplier) ticket=OrderSend(Symbol(),3,
NormalizeDouble(start_lot*MathPow(multiplier,i),prec),Bid+(range*i)*pt,3,(
Bid+(range*i)*pt)+sl*pt,(Bid+(range*i)*pt)-tp*pt,"",magic,0,Red);
else ticket=OrderSend(Symbol(),3,
NormalizeDouble(start_lot+increament*i,prec) ,Bid+(range*i)*pt,3,(
Bid+(range*i)*pt)+sl*pt,(Bid+(range*i)*pt)-tp*pt,"",magic,0,Red);
}
}

Page 3/14
}
else
{
if(OrderSend(Symbol(),1,start_lot,Bid,3,0,0,"",magic,0,Red)>0)
{
for(i=1; i<level; i++)
{
if(lot_multiplier) ticket=OrderSend(Symbol(),3,
NormalizeDouble(start_lot*MathPow(multiplier,i),prec),Bid+(range*i)*pt,3,0,
0,"",magic,0,Red);
else ticket=OrderSend(Symbol(),3,
NormalizeDouble(start_lot+increament*i,prec) ,Bid+(range*i)*pt,3,0,
0,"",magic,0,Red);
}
}
}
}
}
}
if(stealth_mode && total()>0 && total()<level)
{
int type; double op, lastlot;
for(i=0; i<OrdersTotal(); i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
type=OrderType();
op=OrderOpenPrice();
lastlot=OrderLots();
}
if(type==0 && Ask<=op-range*pt)
{
if(use_sl_and_tp)
{
if(lot_multiplier) ticket=OrderSend(Symbol(),0,NormalizeDouble(
lastlot*multiplier,prec),Ask,3,Ask-sl*pt,Ask+tp*pt,"",magic,0,Blue);
else ticket=OrderSend(Symbol(),0,NormalizeDouble(
lastlot+increament,prec),Ask,3,Ask-sl*pt,Ask+tp*pt,"",magic,0,Blue);
}
else
{
if(lot_multiplier) ticket=OrderSend(Symbol(),0,NormalizeDouble(
lastlot*multiplier,prec),Ask,3,0,0,"",magic,0,Blue);
else ticket=OrderSend(Symbol(),0,NormalizeDouble(
lastlot+increament,prec),Ask,3,0,0,"",magic,0,Blue);
}
}
if(type==1 && Bid>=op+range*pt)
{
if(use_sl_and_tp)
{
if(lot_multiplier) ticket=OrderSend(Symbol(),1,NormalizeDouble(
lastlot*multiplier,prec),Bid,3,Bid+sl*pt,Bid-tp*pt,"",magic,0,Red);
else ticket=OrderSend(Symbol(),1,NormalizeDouble(
lastlot+increament,prec),Bid,3,Bid+sl*pt,Bid-tp*pt,"",magic,0,Red);
}
else
{
if(lot_multiplier) ticket=OrderSend(Symbol(),1,NormalizeDouble(
lastlot*multiplier,prec),Bid,3,0,0,"",magic,0,Red);
else ticket=OrderSend(Symbol(),1,NormalizeDouble(
lastlot+increament,prec),Bid,3,0,0,"",magic,0,Red);

Page 4/14
}
}
}
if(use_sl_and_tp && total()>1)
{
double s_l, t_p;
for(i=0; i<OrdersTotal(); i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic ||
OrderType()>1) continue;
type=OrderType();
s_l=OrderStopLoss();
t_p=OrderTakeProfit();
}
for(i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic || OrderType
()>1) continue;
if(OrderType()==type)
{
if(OrderStopLoss()!=s_l || OrderTakeProfit()!=t_p)
{
OrderModify(OrderTicket(),OrderOpenPrice(),s_l,t_p,0,CLR_NONE
);
}
}
}
}
double profit=0;
for(i=0; i<OrdersTotal(); i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic || OrderType
()>1) continue;
profit+=OrderProfit();
}
if(use_closeall && profit>=tp_in_money || a>0)
{
closeall();
closeall();
closeall();
a++;
if(total()==0) a=0;
}
if(!stealth_mode && use_sl_and_tp && total()<level) closeall();
//----
if (Use_TrailingStop)
{
TrailingAlls(TrailStart, TrailStop);
}
//----
if (use_martigal && total()>0)
{
martingel();
}

//---Use Ema trailing


if (Use_EMATrailingStop)
{

Page 5/14
int cnt;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol() != Symbol()) continue;
if ( OrderMagicNumber() != magic ) continue;
if(OrderType() == OP_BUY)
{
EMA_TrailingStop(OP_BUY, OrderTicket(),OrderOpenPrice(),
OrderStopLoss());
}
if(OrderType() == OP_SELL)
{
EMA_TrailingStop(OP_SELL, OrderTicket(),OrderOpenPrice(),
OrderStopLoss());
}
}
}
//----
if (Use_Exit)
{
int Order = SIGNAL_NONE;
int Total, Ticket;
double StopLossLevel, TakeProfitLevel;
if (EachTickMode && Bars != BarCount) TickCheck = False;
Total = OrdersTotal();
Order = SIGNAL_NONE;
double C4= iClose(Symbol(),0,4);
double EMA200 = iMA(NULL,0,200,0,MODE_EMA,PRICE_CLOSE,3);
//+------------------------------------------------------------------+
//| Variable End |
//+------------------------------------------------------------------+

//Check position
bool IsTrade = False;
for (int l = 0; l < Total; l ++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {
IsTrade = True;
if(OrderType() == OP_BUY) {
//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy) |

//+------------------------------------------------------------------+
if (C4<EMA200) Order = SIGNAL_CLOSEBUY;

//+------------------------------------------------------------------+

//| Signal End(Exit Buy) |

//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck)
|| (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage,
MediumSeaGreen);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() +
"] " + DoubleToStr(Bid, Digits) + " Close Buy");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;

Page 6/14
continue;
}
//Trailing stop
} else {
//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Sell) |

//+------------------------------------------------------------------+
if (C4>EMA200) Order = SIGNAL_CLOSESELL;

//+------------------------------------------------------------------+

//| Signal End(Exit Sell) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck)


|| (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage,
DarkOrange);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() +
"] " + DoubleToStr(Ask, Digits) + " Close Sell");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
}
}
}
}
//TrailStop();
return(0);
}
//+------------------------------------------------------------------+
double dailyprofit()
{
int day=Day(); double res=0;
for(int i=0; i<OrdersHistoryTotal(); i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
if(TimeDay(OrderOpenTime())==day) res+=OrderProfit();
}
return(res);
}
//+------------------------------------------------------------------+
int total()
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
total++;
}
return(total);
}
//+------------------------------------------------------------------+
int signal()

Page 7/14
{

double O1= iOpen(Symbol(),0,1);


double C1= iClose(Symbol(),0,1);
double O2= iOpen(Symbol(),0,2);
double C2= iClose(Symbol(),0,2);
double O3= iOpen(Symbol(),0,3);
double C3= iClose(Symbol(),0,3);
double O4= iOpen(Symbol(),0,4);
double C4= iClose(Symbol(),0,4);
double ema = iMA(NULL,0,200,0,MODE_EMA,PRICE_CLOSE,1);

if(use_ema)
{
if( O1 > ema && C1>O1 && C2<O2 && (C1>(O2-C2)*0.5+C2) && C2<C3 && C3<
C4) return(buy);
if( O1 < ema && C1<O1 && C2>O2 && (C1<(C2-O2)*0.5+O2) && C2>C3 && C3>
C4) return(sell);
}
return(0);
}
//+------------------------------------------------------------------+
void closeall()
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
if(OrderType()>1) OrderDelete(OrderTicket());
else
{
if(OrderType()==0) OrderClose(OrderTicket(),OrderLots(),Bid,3,
CLR_NONE);
else OrderClose(OrderTicket(),OrderLots(),Ask,3,
CLR_NONE);
}
}
}
//----------------------- MARTINGEL
double total;
int cnt;
double PriceTarget;
double AveragePrice;
int flag;
//----------------------- CALCULATE AVERAGE OPENING PRICE
void martingel()
{
total=CountTrades();
AveragePrice=0;
double Count=0;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=magic)
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==magic)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
AveragePrice += OrderOpenPrice()*OrderLots();
Count += OrderLots();
}
}

Page 8/14
if(total > 0)
AveragePrice=NormalizeDouble(AveragePrice/Count, Digits);

//----------------------- RECALCULATE STOPLOSS & PROFIT TARGET BASED ON AVERAGE


for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=magic)
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==magic)
if(OrderType()==OP_BUY)
// Calculate profit/stop target for long
{
PriceTarget=AveragePrice+(50*Point);
flag=1;
}
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==magic)
if(OrderType()==OP_SELL)
// Calculate profit/stop target for short
{
PriceTarget=AveragePrice-(50*Point);
flag=1;
}
}

//----------------------- IF NEEDED CHANGE ALL OPEN ORDERS TO NEWLY CALCULATED P


if(flag==1)// check if average has really changed
{
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
// PriceTarget=total;
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=magic)
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==magic)
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),
PriceTarget,0,Yellow);// set all positions to averaged levels
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CountTrades()
{
int count=0;
int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=magic)
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==magic)
if(OrderType()==OP_SELL || OrderType()==OP_BUY)
count++;
}//for
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

Page 9/14
void TrailingAlls(int start,int stop)
{
int profit;
double stoptrade;
double stopcal;
if(stop==0)
return;
int trade;
int cnt;
//----------------------- CALCULATE AVERAGE OPENING PRICE
double total=CountTrades();
double AvgPrice=0;
double Count=0;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!= magic)
continue;
if(OrderSymbol()==Symbol()&& OrderMagicNumber()== magic)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
AvgPrice += OrderOpenPrice()*OrderLots();
Count += OrderLots();
}
}
if(total > 0)
{
AvgPrice=NormalizeDouble(AvgPrice/Count, Digits);
}

for(trade=OrdersTotal()-1;trade>=0;trade--)
{
if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=magic)
continue;
if(OrderSymbol()==Symbol()||OrderMagicNumber()==magic)
{
if(OrderType()==OP_BUY)
{
profit=NormalizeDouble((Bid-AvgPrice)/Point,0);
if(profit<start)
continue;
stoptrade=OrderStopLoss();
stopcal=Bid-(stop*Point);
if(stoptrade==0||(stoptrade!=0&&stopcal>stoptrade))
OrderModify(OrderTicket(),AvgPrice,stopcal,
OrderTakeProfit(),0,Aqua);
}//Long
if(OrderType()==OP_SELL)
{
profit=NormalizeDouble((AvgPrice-Ask)/Point,0);
if(profit<start)
continue;
stoptrade=OrderStopLoss();
stopcal=Ask+(stop*Point);
if(stoptrade==0||(stoptrade!=0&&stopcal<stoptrade))
OrderModify(OrderTicket(),AvgPrice,stopcal,
OrderTakeProfit(),0,Red);
}//Short
}
Sleep(1000);

Page 10/14
}//for
}
//+------------------------------------------------------------------+
void CloseBuyOrder()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;

switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(),
MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close pending orders
case OP_BUYLIMIT :
case OP_BUYSTOP : result = OrderDelete( OrderTicket() );
}

if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" ,
GetLastError() );
Sleep(3000);
}
}
}

//------------------------------------------------------------------------------
void CloseSellOrder()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;

switch(type)
{
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(),
MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
break;
//Close pending orders
case OP_SELLLIMIT :
case OP_SELLSTOP : result = OrderDelete( OrderTicket() );
}

if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" ,
GetLastError() );
Sleep(3000);
}
}
}

Page 11/14
extern int Price = 0;
extern int InitialStop = 0;
//+------------------------------------------------------------------+
//| EMATrailingStop_v1.mq4 |
//| Copyright © 2006, Forex-TSD.com |
//| Written by IgorAD,igorad2003@yahoo.co.uk |

//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//+------------------------------------------------------------------+
void EMA_TrailingStop(int type, int ticket, double op, double os)
{
int digits;
double pBid, pAsk, pp, BuyStop, SellStop, ema;

pp = MarketInfo(Symbol(), MODE_POINT);
digits = MarketInfo(Symbol(), MODE_DIGITS) ;
ema = iMA(Symbol(),0,34,0,MODE_EMA,PRICE_CLOSE,1);

if (type==OP_BUY)
{
BuyStop = ema;
pBid = MarketInfo(Symbol(),MODE_BID);
if(os == 0 && InitialStop>0 ) BuyStop = pBid-InitialStop*pp;
if (digits > 0) BuyStop = NormalizeDouble(SellStop, digits);
BuyStop = ValidStopLoss(OP_BUY, pBid, BuyStop);
if (digits > 0) BuyStop = NormalizeDouble(BuyStop, digits);
Print("MA=",ema," BuyStop=",BuyStop);
if ((op <= BuyStop && BuyStop > os) || os==0)
{
ModifyOrder(ticket,op,BuyStop,LightGreen);
return;
}
}

if (type==OP_SELL)
{
SellStop = ema;
pAsk = MarketInfo(Symbol(),MODE_ASK);
if (os==0 && InitialStop > 0) SellStop = pAsk+InitialStop*pp;
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);
if (digits > 0) SellStop = NormalizeDouble(SellStop, digits);
Print("MA=",ema," SellStop=",SellStop);
if( (op >= SellStop && os > SellStop) || os==0)
{
ModifyOrder(ticket,op,SellStop,DarkOrange);
return;
}
}
}
double ValidStopLoss(int type, double price, double SL)
{

double minstop, pp;


double newSL;

pp = MarketInfo(Symbol(), MODE_POINT);
minstop = MarketInfo(Symbol(),MODE_STOPLEVEL);
newSL = SL;
if (type == OP_BUY)
{
if((price - SL) < minstop*pp) newSL = price - minstop*pp;

Page 12/14
}
if (type == OP_SELL)
{
if((SL-price) < minstop*pp) newSL = price + minstop*pp;
}

return(newSL);
}
int ModifyOrder(int ord_ticket,double op, double price, color mColor)
{
int CloseCnt, err;

CloseCnt=0;
while (CloseCnt < 3)
{
if (OrderModify(ord_ticket,op,price,0,mColor))
{
CloseCnt = 3;
}
else
{
err=GetLastError();
if (err>0) CloseCnt++;
}
}
}

//---- input parameters


extern int InitialStop_1 = 0;

// ---- Stepped Stops


void TrailStop()
{
double BuyStop, SellStop;
double minstop = MarketInfo(Symbol(),MODE_STOPLEVEL);
double ask = MarketInfo(Symbol(),MODE_ASK);
double bid = MarketInfo(Symbol(),MODE_BID);
double ema = iMA(Symbol(),0,200,0,MODE_EMA,0,3);

for (int cnt=0;cnt<OrdersTotal();cnt++)


{
OrderSelect(cnt, SELECT_BY_POS);
int mode=OrderType();
if ( OrderSymbol()==Symbol())
{
if (mode==OP_BUY)
{
if(OrderStopLoss() == 0)
{
if( InitialStop_1>0 ) BuyStop = Bid-InitialStop_1*Point; else
BuyStop = ema;
}
else
BuyStop = ema;
if((bid - BuyStop) < minstop*Point) BuyStop = bid - minstop*Point;

Print("MA=",ema," BuyStop=",BuyStop);
if (BuyStop > OrderStopLoss() || OrderStopLoss()==0)
{
bool result = OrderModify(OrderTicket(),OrderOpenPrice(),
NormalizeDouble(BuyStop, Digits),
OrderTakeProfit(),0,LightGreen);

Page 13/14
if( !result )
{
Print("BUY: OrderModify failed with error");
GetLastError();
}
return(0);

}
}
// - SELL Orders
if (mode==OP_SELL)
{
if (OrderStopLoss()==0)
{
if (InitialStop_1 > 0) SellStop = ask+InitialStop_1*Point; else
SellStop = ema;
}
else
SellStop = ema;
if((SellStop-ask) < minstop*Point) SellStop = ask + minstop*Point
;
Print("MA=",ema," SellStop=",SellStop);
if( OrderStopLoss() > SellStop || OrderStopLoss()==0)
{
OrderModify(OrderTicket(), OrderOpenPrice(),
NormalizeDouble(SellStop, Digits),
OrderTakeProfit(),0,DarkOrange);
if( !result )
{
Print("SELL: OrderModify failed with error");
GetLastError();
}
return(0);

}
}
}
}
}

Page 14/14

You might also like