//+------------------------------------------------------------------+ //| Stoh_test.mq4 | //| Copyright © 2008, ZerkMax | //| zma@mail.ru | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, ZerkMax" #property link "zma@mail.ru" extern double RiskPercentage = 10; extern double Lots = 0; // лот, если 0, то динамический extern int TrailingStop = 30; extern int TakeProfit = 50; extern int StopLoss = 0; //extern double Lots = 0.01; extern int magicnumber = 777; extern bool PolLots = true; extern int MaxOrders = 1; extern bool Prov = false; int prevtime; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- int LotsCount=0; double Lot = Lots; if (Lots==0) { double margin = MarketInfo(Symbol(), MODE_MARGINREQUIRED); double minLot = MarketInfo(Symbol(), MODE_MINLOT); double maxLot = MarketInfo(Symbol(), MODE_MAXLOT); double step = MarketInfo(Symbol(), MODE_LOTSTEP); double account = AccountFreeMargin(); double percentage = account*RiskPercentage/100; Lot = MathRound(percentage/margin/step)*step; if(Lot < minLot) { Lot = minLot; } if(Lot > maxLot) { Lot = maxLot; } } if(Time[0] == prevtime) return(0); prevtime = Time[0]; if(!IsTradeAllowed()) { prevtime = Time[1]; return(0); } double Lots = MathRound(percentage/margin/step)*step; if(Lots < minLot) { Lots = minLot; } if(Lots > maxLot) { Lots = maxLot; } bool BuyOp1=false; bool SellOp1=false; int ticket=0; double orderl=0; int i=0; int total = OrdersTotal(); for(i = 0; i <= total; i++) { if(TrailingStop>0) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderMagicNumber() == magicnumber) { LotsCount ++; TrailingStairs(OrderTicket(),TrailingStop); if (OrderType()==0) { BuyOp1=true;ticket=OrderTicket();orderl=OrderLots(); } if (OrderType()==1) { SellOp1=true;ticket=OrderTicket();orderl=OrderLots(); } } } } bool BuyOp=false; bool SellOp=false; bool BuyOp2=false; bool SellOp2=false; if((NormalizeDouble(iStochastic(NULL,60,5,3,3,MODE_SMA,1,MODE_MAIN,0),0)==NormalizeDouble(iStochastic(NULL,60,5,3,3,MODE_SMA,1,MODE_SIGNAL,0),0)) &&(iStochastic(NULL,60,5,3,3,MODE_SMA,1,MODE_MAIN,0)>iStochastic(NULL,60,5,3,3,MODE_SMA,1,MODE_MAIN,1))) BuyOp=true; if((NormalizeDouble(iStochastic(NULL,60,5,3,3,MODE_SMA,1,MODE_MAIN,0),0)==NormalizeDouble(iStochastic(NULL,60,5,3,3,MODE_SMA,1,MODE_SIGNAL,0),0)) &&(iStochastic(NULL,60,5,3,3,MODE_SMA,1,MODE_MAIN,0)iStochastic(NULL,240,5,3,3,MODE_SMA,1,MODE_MAIN,1))) BuyOp2=true; if((NormalizeDouble(iStochastic(NULL,240,5,3,3,MODE_SMA,1,MODE_MAIN,0),0)==NormalizeDouble(iStochastic(NULL,240,5,3,3,MODE_SMA,1,MODE_SIGNAL,0),0)) &&(iStochastic(NULL,240,5,3,3,MODE_SMA,1,MODE_MAIN,0)0){ OrderClose(ticket,orderl,Ask,0,Red);LotsCount--;} // if(SellOp&&SellOp2) if (BuyOp1 && ticket>0){ OrderClose(ticket,orderl,Bid,0,Red);LotsCount--;} } if (LotsCount < MaxOrders || MaxOrders == 0) { if(BuyOp&&BuyOp2) { if (StopLoss!=0) { OrderSend(Symbol(),OP_BUY,Lot,Ask,3,Bid-(StopLoss*Point),Ask+(TakeProfit*Point),"OpenTiks_Buy",magicnumber,0,Green); } else { OrderSend(Symbol(),OP_BUY,Lot,Ask,3,0,Ask+(TakeProfit*Point),"OpenTiks_Buy",magicnumber,0,Green); } } if(SellOp&&SellOp2) { if (StopLoss!=0) { OrderSend(Symbol(),OP_SELL,Lot,Bid,3,Ask+(StopLoss*Point),Bid-(TakeProfit*Point),"OpenTiks_Sell",magicnumber,0,Red); } else { OrderSend(Symbol(),OP_SELL,Lot,Bid,3,0,Bid-(TakeProfit*Point),"OpenTiks_Sell",magicnumber,0,Red); } } } //---- return(0); } //+------------------------------------------------------------------+ void TrailingStairs(int ticket,int trldistance) { if (OrderType()==OP_BUY) { if((Bid-OrderOpenPrice())>(Point*trldistance)) { if(OrderStopLoss()MarketInfo(Symbol(), MODE_MINLOT)) { OrderClose(ticket,NormalizeDouble(OrderLots()/2,2),Bid,3,Green); } else { OrderClose(ticket,OrderLots(),Bid,3,Green); } } } } else { if((OrderOpenPrice()-Ask)>(Point*trldistance)) { if((OrderStopLoss()>(Ask+Point*trldistance)) || (OrderStopLoss()==0)) { OrderModify(ticket,OrderOpenPrice(),Ask+Point*trldistance,OrderTakeProfit(),0,Red); if (PolLots) if (NormalizeDouble(OrderLots()/2,2)>MarketInfo(Symbol(), MODE_MINLOT)) { OrderClose(ticket,NormalizeDouble(OrderLots()/2,2),Ask,3,Green); } else { OrderClose(ticket,OrderLots(),Ask,3,Green); } } } } }