Advertisement
ProzacR

RSI strat

Jan 28th, 2016
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.42 KB | None | 0 0
  1. //+------------------------------------------------------------------+
  2. //|                                               Moving Average.mq4 |
  3. //|                   Copyright 2005-2014, MetaQuotes Software Corp. |
  4. //|                                              http://www.mql4.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright   "2005-2014, MetaQuotes Software Corp."
  7. #property link        "http://www.mql4.com"
  8. #property description "Moving Average sample expert advisor"
  9.  
  10. #define MAGICMA  20131111
  11. //--- Inputs
  12. input double Lots          =0.1;
  13. input double MaximumRisk   =0.02;
  14. input double DecreaseFactor=3;
  15. input int    MovingPeriod1  =5;
  16. input int    MovingPeriod2  =200;
  17. input int    MovingShift   =6;
  18. input int    StopLoss      =10;
  19. input int    RsiPeriod     =2;
  20. //+------------------------------------------------------------------+
  21. //| Calculate open positions                                         |
  22. //+------------------------------------------------------------------+
  23. int CalculateCurrentOrders(string symbol)
  24.   {
  25.    int buys=0,sells=0;
  26. //---
  27.    for(int i=0;i<OrdersTotal();i++)
  28.      {
  29.       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
  30.       if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
  31.         {
  32.          if(OrderType()==OP_BUY)  buys++;
  33.          if(OrderType()==OP_SELL) sells++;
  34.         }
  35.      }
  36. //--- return orders volume
  37.    if(buys>0) return(buys);
  38.    else       return(-sells);
  39.   }
  40. //+------------------------------------------------------------------+
  41. //| Calculate optimal lot size                                       |
  42. //+------------------------------------------------------------------+
  43. /*
  44. double LotsOptimized()
  45.   {
  46.    double lot=Lots;
  47.    int    orders=HistoryTotal();     // history orders total
  48.    int    losses=0;                  // number of losses orders without a break
  49. //--- select lot size
  50.    lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
  51. //--- calcuulate number of losses orders without a break
  52.    if(DecreaseFactor>0)
  53.      {
  54.       for(int i=orders-1;i>=0;i--)
  55.         {
  56.          if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
  57.            {
  58.             Print("Error in history!");
  59.             break;
  60.            }
  61.          if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
  62.             continue;
  63.          //---
  64.          if(OrderProfit()>0) break;
  65.          if(OrderProfit()<0) losses++;
  66.         }
  67.       if(losses>1)
  68.          lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
  69.      }
  70. //--- return lot size
  71.    if(lot<0.1) lot=0.1;
  72.    return(lot);
  73.   }
  74.   */
  75.  
  76. //+------------------------------------------------------------------+
  77. //| Check for open order conditions                                  |
  78. //+------------------------------------------------------------------+
  79. void CheckForOpen()
  80.   {
  81.    double ma1, ma2, rsi;
  82.    int    res;
  83. //--- go trading only for first tiks of new bar
  84.    if(Volume[0]>1) return;
  85. //--- get Moving Averages
  86.    ma1=iMA(NULL,0,MovingPeriod1,MovingShift,MODE_SMA,PRICE_CLOSE,0);
  87.    ma2=iMA(NULL,0,MovingPeriod2,MovingShift,MODE_SMA,PRICE_CLOSE,0);
  88.    rsi=iRSI(NULL,0,RsiPeriod,PRICE_CLOSE,0);  
  89. //--- sell conditions
  90.    if(rsi>90)
  91.      {
  92.       res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point(),0,"",MAGICMA,0,Red);
  93.       return;
  94.      }
  95. //--- buy conditions
  96.    if(rsi<10)
  97.      {
  98.       res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point(),0,"",MAGICMA,0,Blue);
  99.       return;
  100.      }
  101. //---
  102.   }
  103. //+------------------------------------------------------------------+
  104. //| Check for close order conditions                                 |
  105. //+------------------------------------------------------------------+
  106. void CheckForClose()
  107.   {
  108.    double ma1, ma2, rsi;
  109. //--- go trading only for first tiks of new bar
  110.    if(Volume[0]>1) return;
  111. //--- get Moving Averages
  112.    ma1=iMA(NULL,0,MovingPeriod1,MovingShift,MODE_SMA,PRICE_CLOSE,0);
  113.    ma2=iMA(NULL,0,MovingPeriod2,MovingShift,MODE_SMA,PRICE_CLOSE,0);
  114.    rsi=iRSI(NULL,0,RsiPeriod,PRICE_CLOSE,0);
  115. //---
  116.    for(int i=0;i<OrdersTotal();i++)
  117.      {
  118.       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
  119.       if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
  120.       //--- check order type
  121.       if(OrderType()==OP_BUY)
  122.         {
  123.          //if(Open[1]>ma1 && Close[1]<ma1)
  124.          if(rsi>90)
  125.            {
  126.             if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
  127.                Print("OrderClose error ",GetLastError());
  128.            }
  129.          break;
  130.         }
  131.       if(OrderType()==OP_SELL)
  132.         {
  133.          //if(Open[1]<ma1 && Close[1]>ma1)
  134.          if(rsi<10)
  135.            {
  136.             if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
  137.                Print("OrderClose error ",GetLastError());
  138.            }
  139.          break;
  140.         }
  141.      }
  142. //---
  143.   }
  144.  
  145. //+------------------------------------------------------------------+
  146. //| OnTick function                                                  |
  147. //+------------------------------------------------------------------+
  148. void OnTick()
  149.   {
  150. //--- check for history and trading
  151.    if(Bars<100 || IsTradeAllowed()==false)
  152.       return;
  153. //--- calculate open orders by current symbol
  154.    if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
  155.    else                                    CheckForClose();
  156. //---
  157.   }
  158. //+------------------------------------------------------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement