cm RSI martingel

Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reachedChecks for the total of closed orders
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
cm RSI martingel
ÿþ//+------------------------------------------------------------------+

#property copyright "Copyright © 2018, http://cmillion.ru"

#property link      "cmillion@narod.ru"

#property strict

#property description "!>25B=8: >B:@K205B ?>78F88 ?> 8=48:0B>@C RSI ?>A;5 SL C25;8G8205B ;>B"

#property description "!;54CNI0O A45;:0 =5 >B:@K205BAO, ?>:0 =5 70:@KB0 ?@54K4CI0O"

//-------------------------------------------------------------------

extern ENUM_TIMEFRAMES timeframe_RSI = 60;

extern int    period_RSI   = 14;

extern int    level_buy    = 30;

extern int    level_sell   = 70;



extern double Lot          = 0.1;

extern double K_Martin     = 2.0;



extern int    Stoploss     = 10,

              Takeprofit   = 50;

extern int    OrdersClose  = 5;



extern int    Magic        = 0;

extern int    DigitsLot    = 2;

extern int    slippage     = 3;

//-------------------------------------------------------------------

string AC;

datetime OpenTime;

double MINLOT,MAXLOT;

//-------------------------------------------------------------------

void OnTick()

{

   if (!IsTradeAllowed()) 

   {

      DrawLABEL("">@3>2;O",0,0,0,Red,"">@3>2;O 70?@5I5=0");

      return;

   } 

   else DrawLABEL("">@3>2;O",0,0,0,Lime,"">@3>2;O @07@5H5=0");



   //---



   double STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);

   //---

   double OSL,OTP,OOP,SL,TP,Profit=0;

   int b=0,s=0,tip;

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

   {    

      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

      { 

         if (OrderSymbol()==Symbol() && Magic==OrderMagicNumber())

         { 

            tip = OrderType(); 

            OSL = NormalizeDouble(OrderStopLoss(),Digits);

            OTP = NormalizeDouble(OrderTakeProfit(),Digits);

            OOP = NormalizeDouble(OrderOpenPrice(),Digits);

            SL=OSL;TP=OTP;

            Profit+=OrderProfit()+OrderCommission()+OrderSwap();

            if (tip==OP_BUY)             

            {  

               b++; 

               if (OSL==0 && Stoploss!=0)

               {

                  if ((Bid-NormalizeDouble(OOP - Stoploss * Point,Digits)) / Point>=STOPLEVEL) SL = NormalizeDouble(OOP - Stoploss * Point,Digits);

               } 

               if (OTP==0 && Takeprofit!=0)

               {

                  if ((NormalizeDouble(OOP + Takeprofit * Point,Digits)-Ask) / Point>=STOPLEVEL) TP = NormalizeDouble(OOP + Takeprofit * Point,Digits);

               } 

               if (SL != OSL || TP != OTP)

               {  

                  if (!OrderModify(OrderTicket(),OOP,SL,TP,0,White)) Print("Error OrderModify ",GetLastError());

               }

            }                                         

            if (tip==OP_SELL)        

            {

               s++;

               if (OSL==0 && Stoploss!=0)

               {

                  if ((NormalizeDouble(OOP + Stoploss * Point,Digits)-Ask) / Point>=STOPLEVEL) SL = NormalizeDouble(OOP + Stoploss   * Point,Digits);

               }

               if (OTP==0 && Takeprofit!=0)

               {

                  if ((Bid-NormalizeDouble(OOP - Takeprofit * Point,Digits)) / Point>=STOPLEVEL) TP = NormalizeDouble(OOP - Takeprofit * Point,Digits);

               }

               if (SL != OSL || TP != OTP)

               {  

                  if (!OrderModify(OrderTicket(),OOP,SL,TP,0,White)) Print("Error OrderModify ",GetLastError());

               }

            } 

         }

      }

   }

   

   

   //---

   

   double AB = AccountBalance();

   

   //---

   

   DrawLABEL("Balance"        ,1,5,0,clrGreen,StringConcatenate("Balance ",DoubleToStr(AB,2),AC));

   DrawLABEL("Equity"         ,1,5,0,clrGreen,StringConcatenate("Equity ",DoubleToStr(AccountEquity(),2),AC));

   DrawLABEL("FreeMargin"     ,1,5,0,clrGreen,StringConcatenate("FreeMargin ",DoubleToStr(AccountFreeMargin(),2),AC));

   DrawLABEL("Take"           ,1,5,0,Color(Profit>0,Lime,Red),StringConcatenate("Profit ",DoubleToStr(Profit,2),AC));

   

   double Lots=0,RSI1=0,RSI0=0;

   if (b+s==0)

   {

      RSI0= iRSI (NULL,timeframe_RSI,period_RSI,PRICE_CLOSE,0);

      RSI1= iRSI (NULL,timeframe_RSI,period_RSI,PRICE_CLOSE,1);

   }

   else return;



   //---

   if (RSI0>=level_buy && RSI1<=level_buy)

   {

      Lots=LOT();



      if (Lots>MAXLOT) Lots = MAXLOT;

      if (Lots<MINLOT) Lots = MINLOT;

      

      if(SendOrder(OP_BUY, Lots, NormalizeDouble(Ask,Digits)))OpenTime=iTime(NULL,timeframe_RSI,1); 

   }



   //---

   

   if (RSI0<=level_sell && RSI1>=level_sell)

   {

      Lots=LOT();



      if (Lots>MAXLOT) Lots = MAXLOT;

      if (Lots<MINLOT) Lots = MINLOT;



      if(SendOrder(OP_SELL, Lots, NormalizeDouble(Bid,Digits))) OpenTime=iTime(NULL,timeframe_RSI,1); 

   }

}

//------------------------------------------------------------------

bool SendOrder(int tip, double lots, double price, double sl=0, double tp=0)

{

   if (tip<2)

   {

      if (AccountFreeMarginCheck(Symbol(),tip,lots)<0)

      {

         Alert("54>AB0B>G=> A@54AB2");

         return(0);

      }

   }

   for (int i=0; i<10; i++)

   {    

      if (OrderSend(Symbol(),tip, lots,price,slippage,sl,tp,NULL,Magic,0,clrNONE)!=-1) return(1);

         Alert(" ?>?KB:0 ",i," H81:0 >B:@KB8O >@45@0 ",Strtip(tip)," <<",(GetLastError()),">>  lot=",lots,"  pr=",price," sl=",sl," tp=",tp);

      Sleep(500);

      RefreshRates();

      if (IsStopped()) return(0);

   }

   return(0);

}

//------------------------------------------------------------------

string Strtip(int tip)

{

   switch(tip) 

   { 

   case OP_BUY:

      return("BUY"); 

   case OP_SELL:

      return("SELL"); 

   case OP_BUYSTOP:

      return("BUYSTOP"); 

   case OP_SELLSTOP:

      return("SELLSTOP"); 

   case OP_BUYLIMIT:

      return("BUYLIMIT"); 

   case OP_SELLLIMIT:

      return("SELLLIMIT"); 

   }

   return("error"); 

}

//------------------------------------------------------------------

void OnDeinit(const int reason)

{

   if (!IsTesting()) 

   {

      ObjectsDeleteAll(0);

   }

}

//-------------------------------------------------------------------

void DrawLABEL(string name, int CORNER, int X, int Y, color clr, string Name)

{

   if (ObjectFind(name)==-1)

   {

      ObjectCreate(name, OBJ_LABEL, 0, 0, 0);

      ObjectSet(name, OBJPROP_CORNER, CORNER);

      ObjectSet(name, OBJPROP_XDISTANCE, X);

      ObjectSet(name, OBJPROP_YDISTANCE, Y);

   }

   ObjectSetText(name,Name,10,"Arial",clr);

}

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

int OnInit()

{

   MINLOT = MarketInfo(Symbol(),MODE_MINLOT);

   MAXLOT = MarketInfo(Symbol(),MODE_MAXLOT);

   Comment("Start ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));

   

   AC = StringConcatenate(" ", AccountCurrency());

   

   int Y=15;

   DrawLABEL("">@3>2;O"  ,1,5,Y,Red,"">@3>2;O ");Y += 20;

   DrawLABEL("Balance"   ,1,5,Y,clrGreen,StringConcatenate("Balance ",DoubleToStr(AccountBalance(),2),AC));Y += 15;

   DrawLABEL("Equity"    ,1,5,Y,clrGreen,StringConcatenate("Equity ",DoubleToStr(AccountEquity(),2),AC));Y += 15;

   DrawLABEL("FreeMargin",1,5,Y,clrGreen,StringConcatenate("FreeMargin ",DoubleToStr(AccountFreeMargin(),2),AC));Y += 30;



   DrawLABEL("Take"           ,1,5,Y,Lime,"Profit ");Y += 20;

   

   return(INIT_SUCCEEDED);

}

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

color Color(bool P,color a,color b)

{

   if (P) return(a);

   else return(b);

}

//------------------------------------------------------------------

double LOT()

{

   int n=0;

   double OL=Lot;

   for (int j = OrdersHistoryTotal()-1; j >= 0; j--)

   {

      if (OrderSelect(j, SELECT_BY_POS,MODE_HISTORY))

      {

         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)

         {

            if (OrderProfit()<0) 

            {

               if (n==0) OL=NormalizeDouble(OrderLots()*K_Martin,DigitsLot);

               n++;

               if (n>=OrdersClose) {Comment("1");return(Lot);}

            }

            else

            {

               if (n==0) {Comment("2");return(Lot);}

               else {Comment("3");return(OL);}

            }

         }

      }

   }

   return(OL);

}

//------------------------------------------------------------------





Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---