Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.26 KB | None | 0 0
  1. //+------------------------------------------------------------------+
  2. //|                                                   Copyright 2010 |
  3. //|                                        steadywinner.mipropia.com |
  4. //|                                           steadywinner@gmail.com |
  5. //|                                      SteadyWinner    Version 4.2 |
  6. //|                                        Pair:EURUSD Any timeframe |
  7. //+------------------------------------------------------------------+
  8.  
  9. #property copyright "SteadyWinner"
  10. #property link      "steadywinner@gmail.com"
  11. #define MIN_STOPLOSS_POINT 5
  12. #define MIN_TAKEPROFIT_POINT 5
  13.  
  14. string sNameExpert = "SteadyWinner V4.2";
  15. int nAccount =0;
  16.  
  17. // 2010/9/18 Change (4) - Set sane value for StopLoss, TakeProfit and TrailingStop
  18. //    in both Buy / Sell orders
  19. double dOneStopLossPoint = 50;
  20. double dOneTakeProfitPoint = 12;
  21. double dOneTrailingStopPoint = 10;
  22.  
  23. double dBuyStopLossPoint;
  24. double dSellStopLossPoint;
  25. double dBuyTakeProfitPoint;
  26. double dSellTakeProfitPoint;
  27. double dBuyTrailingStopPoint;
  28. double dSellTrailingStopPoint;
  29. extern int MagicNumber = 338833;
  30. extern int nPctToRisk = 2;
  31. double dLots=0.01;
  32. double MinLots = 0;
  33. double MaxLots = 0;
  34. double point = 0;
  35. int nSlippage = 3;
  36. bool lFlagUseHourTrade = True;
  37. int nFromHourTrade = 0;
  38. int nToHourTrade = 23;
  39. bool lFlagUseSound = True;
  40. string sSoundFileName = "alert.wav";
  41. color colorOpenBuy = Blue;
  42. color colorCloseBuy = Aqua;
  43. color colorOpenSell = Red;
  44. color colorCloseSell = Aqua;
  45. string Please_Enter_Password = "0";
  46. string password;
  47.  
  48. //--------------------------------------------------------------------------------
  49.  
  50. void init() {
  51.    // for 4 or 5 digit brokers
  52.    if (Digits == 3 || Digits == 5) {
  53.       point = Point*10;
  54.    }
  55.    else {
  56.       point = Point;
  57.    }
  58.  
  59.    // 2010/9/18 Change (4) - Use single variable for StopLoss / TakeProfit / TrailingStop
  60.    //    But keep original variable intact so can be fine tuned for buy / sell separately later
  61.    dBuyStopLossPoint = dOneStopLossPoint;
  62.    dSellStopLossPoint = dOneStopLossPoint;
  63.    dBuyTakeProfitPoint = dOneTakeProfitPoint;
  64.    dSellTakeProfitPoint = dOneTakeProfitPoint;
  65.    dBuyTrailingStopPoint = dOneTrailingStopPoint;
  66.    dSellTrailingStopPoint = dOneTrailingStopPoint;
  67.  
  68.    ObjectCreate("ObjName1", OBJ_LABEL, 0, 0, 0);
  69.    ObjectSetText("ObjName1", "http://steadywinner.mipropia.com  steadywinner@gmail.com", 9, "Verdana", White);
  70.    ObjectSet("ObjName1", OBJPROP_CORNER, 0);
  71.    ObjectSet("ObjName1", OBJPROP_XDISTANCE, 20);
  72.    ObjectSet("ObjName1", OBJPROP_YDISTANCE, 20);
  73.    
  74.    ObjectCreate("ObjName2", OBJ_LABEL, 0, 0, 0);
  75.    ObjectSetText("ObjName2", "SteadyWinner (V4), risking "+nPctToRisk+"% of your account", 9, "Verdana", White);
  76.    ObjectSet("ObjName2", OBJPROP_CORNER, 0);
  77.    ObjectSet("ObjName2", OBJPROP_XDISTANCE, 20);
  78.    ObjectSet("ObjName2", OBJPROP_YDISTANCE, 40);
  79.  
  80.    ObjectCreate("ObjName3", OBJ_LABEL, 0, 0, 0);
  81.  
  82.    // 2010/9/18 Change (2) - Unified and Simplified Lot Size Display
  83.    MinLots = MarketInfo(Symbol(),MODE_MINLOT);
  84.     dLots = (MathCeil(AccountEquity ()/((dOneStopLossPoint*10)/nPctToRisk)))/100;
  85.    dLots = NormalizeDouble(dLots/MinLots,0) * MinLots;
  86.    ObjectSetText("ObjName3", "You are trading " + DoubleToStr(dLots,2) + " Lots. Account Minimum Lot Size=" + DoubleToStr(MinLots,2), 9, "Verdana", White);
  87.  
  88.    ObjectSet("ObjName3", OBJPROP_CORNER, 0);
  89.    ObjectSet("ObjName3", OBJPROP_XDISTANCE, 20);
  90.    ObjectSet("ObjName3", OBJPROP_YDISTANCE, 60);
  91.    
  92.    ObjectCreate("ObjName4", OBJ_LABEL, 0, 0, 0);
  93.    // 2010/9/18 Change (5) - Screen display now show values from variables instead of hard coded values
  94.    ObjectSetText("ObjName4", "Stoploss=" + DoubleToStr(dOneStopLossPoint,0)
  95.    + "  Take profit=" + DoubleToStr(dOneTakeProfitPoint,0)
  96.    + "  Trailing stop=" + DoubleToStr(dOneTrailingStopPoint,0), 9, "Verdana", White);
  97.    ObjectSet("ObjName4", OBJPROP_CORNER, 0);
  98.    ObjectSet("ObjName4", OBJPROP_XDISTANCE, 20);
  99.    ObjectSet("ObjName4", OBJPROP_YDISTANCE, 80);
  100.    
  101.    ObjectCreate("ObjName5", OBJ_LABEL, 0, 0, 0);
  102.    ObjectSetText("ObjName5", "Put this EA on EURUSD H1 ", 9, "Verdana", White);
  103.    ObjectSet("ObjName5", OBJPROP_CORNER, 0);
  104.    ObjectSet("ObjName5", OBJPROP_XDISTANCE, 20);
  105.    ObjectSet("ObjName5", OBJPROP_YDISTANCE, 100);
  106.    if (StringFind(Symbol(),"EURUSD",0) < 0) Alert("Put this EA on EURUSD chart. H1.");
  107. }
  108.  
  109. //--------------------------------------------------------------------------------
  110.  
  111. void deinit() {
  112.    // 2010/9/18 Change (3) - Delete only objects from this EA
  113.    ObjectDelete("ObjName1");
  114.    ObjectDelete("ObjName2");
  115.    ObjectDelete("ObjName3");
  116.    ObjectDelete("ObjName4");
  117.    ObjectDelete("ObjName5");        
  118. }
  119.  
  120. //--------------------------------------------------------------------------------
  121.  
  122. int start(){
  123.    // 2010/9/18 Change (2) - Unified and Simplified Lot Size Display
  124.    // 2010/9/18 Change (5) - Screen display now show values from variables instead of hard coded values
  125.    MinLots = MarketInfo(Symbol(),MODE_MINLOT);
  126.     dLots = (MathCeil(AccountEquity ()/((dOneStopLossPoint*10)/nPctToRisk)))/100;
  127.    dLots = NormalizeDouble(dLots/MinLots,0) * MinLots;
  128.    ObjectSetText("ObjName3", "You are trading " + DoubleToStr(dLots,2) + " Lots. Account Minimum Lot Size=" + DoubleToStr(MinLots,2), 9, "Verdana", White);
  129.    
  130.    if (lFlagUseHourTrade){
  131.       if (!(Hour()>=nFromHourTrade && Hour()<=nToHourTrade)) {
  132.          Comment("Time for trade has not come else!");
  133.          return(0);
  134.       }
  135.    }
  136.    
  137.    if(Bars < 100){
  138.       Print("bars less than 100");
  139.       return(0);
  140.    }
  141.    
  142.    if (nAccount > 0 && nAccount != AccountNumber()){
  143.       Comment("Trade on account :"+AccountNumber()+" FORBIDDEN!");
  144.       return(0);
  145.    }
  146.    
  147.    if((dBuyStopLossPoint > 0 && dBuyStopLossPoint < MIN_STOPLOSS_POINT) ||
  148.       (dSellStopLossPoint > 0 && dSellStopLossPoint < MIN_STOPLOSS_POINT)){
  149.       Print("StopLoss less than " + MIN_STOPLOSS_POINT);
  150.       return(0);
  151.    }
  152.    if((dBuyTakeProfitPoint > 0 && dBuyTakeProfitPoint < MIN_TAKEPROFIT_POINT) ||
  153.       (dSellTakeProfitPoint > 0 && dSellTakeProfitPoint < MIN_TAKEPROFIT_POINT)){
  154.       Print("TakeProfit less than " + MIN_TAKEPROFIT_POINT);
  155.       return(0);
  156.    }
  157.  
  158. // indicators
  159. double diWPR1=iWPR(NULL,1,155,0);
  160. double diWPR2=iWPR(NULL,5,155,0);
  161. double diWPR3=iWPR(NULL,15,155,0);
  162. double diWPR4=iWPR(NULL,1,155,0);
  163. double diWPR5=iWPR(NULL,1,250,0);
  164. double diWPR6=iWPR(NULL,1,500,0);
  165. double diClose25=iClose(NULL,1,0);
  166. double diMA26=iMA(NULL,1,605,0,MODE_EMA,PRICE_CLOSE,0);
  167. double diMA27=iMA(NULL,1,700,0,MODE_EMA,PRICE_CLOSE,0);
  168. double diMA28=iMA(NULL,5,700,0,MODE_EMA,PRICE_CLOSE,0);
  169. double diMA29=iMA(NULL,15,700,0,MODE_EMA,PRICE_CLOSE,0);
  170. double diATR64=iATR(NULL,1,600,0);
  171. double diStochastic81=iStochastic(NULL,5,55,15,3,MODE_EMA,PRICE_CLOSE,MODE_MAIN,0);
  172. double diStochastic82=iStochastic(NULL,5,55,15,3,MODE_EMA,PRICE_CLOSE,MODE_SIGNAL,0);
  173. double diStochastic83=iStochastic(NULL,5,100,35,3,MODE_EMA,PRICE_CLOSE,MODE_MAIN,0);
  174. double diStochastic84=iStochastic(NULL,5,100,35,3,MODE_EMA,PRICE_CLOSE,MODE_SIGNAL,0);
  175. double diRSI93=iRSI(NULL,1,3,PRICE_CLOSE,0);
  176.  
  177.  
  178.  
  179.    // MoneyManagement
  180.  
  181.    MinLots = MarketInfo(Symbol(),MODE_MINLOT);
  182.    MaxLots = MarketInfo(Symbol(),MODE_MAXLOT);
  183.  
  184.    for (int ioht=OrdersHistoryTotal() -1; ioht >= 0; ioht--) {
  185.        OrderSelect(ioht, SELECT_BY_POS, MODE_HISTORY);
  186.        if (OrderMagicNumber() == MagicNumber)
  187.           ioht = 0;
  188.    }
  189.  
  190.    if (OrderProfit() == 0)
  191.       dLots = MinLots;  
  192.  
  193.    if (OrderProfit() > 0 && OrderLots() >= MinLots && OrderMagicNumber() == MagicNumber) {
  194.       // 2010/9/18 Change (5) - Pip value from variables dStopLossPoint are used instead of hard coded values 500 (50x10)
  195.        dLots = (MathCeil(AccountEquity ()/((dOneStopLossPoint*10)/nPctToRisk)))/100;
  196.  
  197.       // 2010/9/18 Change (1) - Tranform dLots to Integer Multiple of MinLots
  198.       dLots = NormalizeDouble(dLots/MinLots,0) * MinLots;
  199.       if (dLots == 0) {
  200.            dLots = MinLots;
  201.            Print("Balance too low: Trading minimum lot size!");
  202.       }
  203.    }
  204.  
  205.    // if lose, use smallest lot to test the market before resume full lots
  206.    if (OrderProfit() < 0 && OrderMagicNumber() == MagicNumber)
  207.       dLots = MinLots;
  208.  
  209.    // ********** special days - trade smallest lot to avoid risk *******
  210.    if ( Month() == 12 && Day()>15)
  211.       dLots = MinLots;
  212.  
  213.    if (DayOfWeek()==5 && Hour()>18)
  214.       dLots = MinLots;
  215.  
  216.    // limits
  217.    if (dLots < MinLots)
  218.       dLots = MinLots;
  219.    if (dLots > MaxLots)
  220.       dLots = MaxLots;
  221.    // *******************************************************************
  222.  
  223.    // Stop SteadyWinner from trading if capital is danger low, less than 100
  224.    if(AccountFreeMargin() < (100)){
  225.       Print("We have no money. Free Margin = " + AccountFreeMargin());
  226.       return(0);
  227.    }
  228.    
  229.    bool lFlagBuyOpen = false, lFlagSellOpen = false, lFlagBuyClose = false, lFlagSellClose = false;
  230.  
  231.   lFlagBuyOpen =  ((diATR64>0.0001)) &&  diRSI93<35 &&
  232.                    (diStochastic81<diStochastic82) && (diStochastic83<diStochastic84)  
  233.                    && (diWPR1<-99.99) &&  
  234.                    (diMA27 < diClose25) && (diMA28 < diClose25) && (diMA29 < diClose25)
  235.                    ;
  236.  
  237.    
  238.    lFlagSellOpen = ((diATR64>0.0001)) &&  diRSI93>65 &&
  239.                    (diStochastic81>diStochastic82) && (diStochastic83>diStochastic84)  
  240.                    && (diWPR1>-0.01) &&  
  241.                    (diMA27 > diClose25) && (diMA28 > diClose25) && (diMA29 > diClose25)
  242.                    ;
  243.  
  244.  
  245.    lFlagBuyClose =   (OrderMagicNumber() == MagicNumber && TimeCurrent()-OrderOpenTime()>360) &&
  246.                      (diStochastic81>diStochastic82) && (diStochastic83>diStochastic84)
  247.                      && (diWPR1>-50)  
  248.                      ;
  249.  
  250.  
  251.  
  252.     lFlagSellClose = (OrderMagicNumber() == MagicNumber && TimeCurrent()-OrderOpenTime()>360) &&
  253.                      (diStochastic81<diStochastic82) && (diStochastic83<diStochastic84)
  254.                      && (diWPR1<-50)    
  255.                      ;
  256.    
  257.    
  258.    
  259.    // operaton codes
  260.    if (!ExistPositions()){
  261.  
  262.       if (lFlagBuyOpen){
  263.          OpenBuy();
  264.          return(0);
  265.       }
  266.  
  267.       if (lFlagSellOpen){
  268.          OpenSell();
  269.          return(0);
  270.       }
  271.    }
  272.  
  273.    if (ExistPositions()){
  274.       if(OrderType()==OP_BUY){
  275.          if (lFlagBuyClose){
  276.             bool flagCloseBuy = OrderClose(OrderTicket(), OrderLots(), Bid, nSlippage, colorCloseBuy);
  277.             if (flagCloseBuy && lFlagUseSound)
  278.                PlaySound(sSoundFileName);
  279.             return(0);
  280.          }
  281.       }
  282.       if(OrderType()==OP_SELL){
  283.          if (lFlagSellClose){
  284.             bool flagCloseSell = OrderClose(OrderTicket(), OrderLots(), Ask, nSlippage, colorCloseSell);
  285.             if (flagCloseSell && lFlagUseSound)
  286.                PlaySound(sSoundFileName);
  287.             return(0);
  288.          }
  289.       }
  290.    }
  291.    
  292.    if (dBuyTrailingStopPoint > 0 || dSellTrailingStopPoint > 0){
  293.      
  294.       for (int i=0; i<OrdersTotal(); i++) {
  295.          if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
  296.             bool lMagic = true;
  297.             if (MagicNumber > 0 && OrderMagicNumber() != MagicNumber)
  298.                lMagic = false;
  299.            
  300.             if (OrderSymbol()==Symbol() && lMagic) {
  301.                if (OrderType()==OP_BUY && dBuyTrailingStopPoint > 0) {
  302.                   if (Bid-OrderOpenPrice() > dBuyTrailingStopPoint*point) {
  303.                      if (OrderStopLoss()<Bid-dBuyTrailingStopPoint*point)
  304.                         ModifyStopLoss(Bid-NormalizeDouble(dBuyTrailingStopPoint*point,5));
  305.                   }
  306.                }
  307.                if (OrderType()==OP_SELL) {
  308.                   if (OrderOpenPrice()-Ask>dSellTrailingStopPoint*point) {
  309.                      if (OrderStopLoss()>Ask+dSellTrailingStopPoint*point || OrderStopLoss()==0)  
  310.                         ModifyStopLoss(Ask+NormalizeDouble(dSellTrailingStopPoint*point,5));
  311.                   }
  312.                }
  313.             }
  314.          }
  315.       }
  316.    }
  317.    return (0);
  318. }
  319.  
  320. //--------------------------------------------------------------------------------
  321.  
  322. bool ExistPositions() {
  323.     for (int i=0; i<OrdersTotal(); i++) {
  324.         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
  325.          bool lMagic = true;
  326.          
  327.          if (MagicNumber > 0 && OrderMagicNumber() != MagicNumber)
  328.             lMagic = false;
  329.  
  330.             if (OrderSymbol()==Symbol() && lMagic) {
  331.                 return(True);
  332.             }
  333.         }
  334.     }
  335.     return(false);
  336. }
  337.  
  338. //--------------------------------------------------------------------------------
  339.  
  340. void ModifyStopLoss(double ldStopLoss) {
  341.    bool lFlagModify = OrderModify(OrderTicket(), NormalizeDouble(OrderOpenPrice(),5), NormalizeDouble(ldStopLoss,5), NormalizeDouble(OrderTakeProfit(),5), 0, CLR_NONE);
  342.    if (lFlagModify && lFlagUseSound)
  343.       PlaySound(sSoundFileName);
  344. }
  345.  
  346. //--------------------------------------------------------------------------------
  347.  
  348. void OpenBuy() {
  349.    double dStopLoss = 0, dTakeProfit = 0;
  350.  
  351.    if (dBuyStopLossPoint > 0)
  352.       dStopLoss = NormalizeDouble(Bid-dBuyStopLossPoint*point,5);
  353.    
  354.    if (dBuyTakeProfitPoint > 0)
  355.       dTakeProfit = NormalizeDouble(Bid + dBuyTakeProfitPoint * point,5);
  356.    
  357.    int numorder = OrderSend(Symbol(), OP_BUY, dLots, Ask, nSlippage, 0, 0, "SteadyWinner4", MagicNumber, 0, colorOpenBuy);
  358.    
  359.    if (numorder > -1) {
  360.       OrderSelect(numorder,SELECT_BY_TICKET);  
  361.       OrderModify(OrderTicket(),OrderOpenPrice(),dStopLoss,dTakeProfit,0,Green);
  362.    }
  363.    
  364.    if (numorder > -1 && lFlagUseSound)
  365.       PlaySound(sSoundFileName);
  366. }
  367.  
  368. //--------------------------------------------------------------------------------
  369.  
  370. void OpenSell() {
  371.    double dStopLoss = 0, dTakeProfit = 0;
  372.    
  373.    if (dSellStopLossPoint > 0)
  374.       dStopLoss = NormalizeDouble(Ask+dSellStopLossPoint*point,5);
  375.    
  376.    if (dSellTakeProfitPoint > 0)
  377.       dTakeProfit = NormalizeDouble(Ask-dSellTakeProfitPoint*point,5);
  378.    
  379.    int numorder = OrderSend(Symbol(),OP_SELL, dLots, Bid, nSlippage, 0, 0, "SteadyWinner4", MagicNumber, 0, colorOpenSell);
  380.    
  381.    if (numorder > -1) {
  382.       OrderSelect(numorder,SELECT_BY_TICKET);  
  383.       OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(dStopLoss,5), NormalizeDouble(dTakeProfit,5),0,Green);
  384.    }
  385.    
  386.    if (numorder > -1 && lFlagUseSound)
  387.       PlaySound(sSoundFileName);
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement