//+------------------------------------------------------------------+
//|                                         Risk vs Reward Ratio.mq4 |
//|                                                  Scott Johnstone |
//|                                         scottstone_2@hotmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Scott Johnstone"
#property link      "scottstone_2@hotmail.com"

#property indicator_chart_window
//---- input parameters
extern bool   ShowIndicator = true;
bool      BidValue=False; 
string    SLLevel="StopLoss";
string    TPLevel="TakeProfit";
string    OpenPositionLevel="Entry";
extern string note2 = "Default Font Color";
extern color  FontColor = Yellow;
extern string note3 = "Font Size";
extern int    FontSize=8;
extern string note4 = "Font Type";
extern string FontType="Comic Sans MS";
extern string note5 = "Display the price in what corner?";
extern string note6 = "Upper left=0; Upper right=1";
extern string note7 = "Lower left=2; Lower right=3";
extern int    WhatCorner=2;
extern string AccountType = "Micro=0.01, Mini=0.1, Standard=1.0";
extern double AccType =0.01;
extern double RiskPercent = 3.0;
int nDigits;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                       
//+------------------------------------------------------------------+
int init()

{
     
   
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
  
      //ObjectDelete(SLLevel);
      //ObjectDelete(TPLevel);
      //ObjectDelete(OpenPositionLevel);
      ObjectDelete("RiskReward_ratio");
      ObjectDelete("TickValue");
      ObjectDelete("Risk");
      ObjectDelete("Gain");
      ObjectDelete("SL_price");
      ObjectDelete("TP_price");
      ObjectDelete("Open_Price");
      ObjectDelete("Lots");
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  
  ObjectDelete("RiskReward_ratio");
      ObjectDelete("TickValue");
      ObjectDelete("Risk");
      ObjectDelete("Gain");
      ObjectDelete("SL_price");
      ObjectDelete("TP_price");
      ObjectDelete("Open_Price");
      ObjectDelete("Lots");

   if (ShowIndicator == true){

   if (ObjectFind(SLLevel)==-1)
     {
      ObjectCreate(SLLevel,OBJ_HLINE,0,0,Close[0]-20*Point,Digits); 
      ObjectSet(SLLevel,OBJPROP_COLOR,Crimson);
      ObjectSet(SLLevel,OBJPROP_STYLE,STYLE_DASHDOTDOT);
      ObjectSetText(SLLevel,SLLevel,8,"Courier",LightSteelBlue);
     }
   if (ObjectFind(TPLevel)==-1)
     {
      ObjectCreate(TPLevel,OBJ_HLINE,0,0,Close[0]+30*Point,Digits);
      ObjectSet(TPLevel,OBJPROP_COLOR,Yellow);
      ObjectSet(TPLevel,OBJPROP_STYLE,STYLE_DASHDOTDOT);
      ObjectSetText(TPLevel,TPLevel,8,"Courier",LightSteelBlue);
     }
   if(BidValue == false)
     {
   if (ObjectFind(OpenPositionLevel)==-1)
     {
      ObjectCreate(OpenPositionLevel,OBJ_HLINE,0,0,Close[0]);
      ObjectSet(OpenPositionLevel,OBJPROP_COLOR,LimeGreen);
      ObjectSet(OpenPositionLevel,OBJPROP_STYLE,STYLE_DASHDOTDOT);
      ObjectSetText(OpenPositionLevel,OpenPositionLevel,8,"Courier",LightSteelBlue);
     }
     }
     }
   
   
   //----

   double   RiskReward_ratio=0, SL_price=0, TP_price=0, Open_Price=0; 
   string   Text="";
   int i=0;

   if(BidValue == true) {Open_Price = SL_price;}
   if(BidValue == false) {if (ObjectFind(OpenPositionLevel)==-1) return(0);
   Open_Price = ObjectGet(OpenPositionLevel, OBJPROP_PRICE1);}

   if (ObjectFind(SLLevel)==-1) return(0);
   SL_price = ObjectGet(SLLevel, OBJPROP_PRICE1);
   if (ObjectFind(TPLevel)==-1) return(0);
   TP_price = ObjectGet(TPLevel, OBJPROP_PRICE1);
   Open_Price = ObjectGet(OpenPositionLevel, OBJPROP_PRICE1);
   
   

   RiskReward_ratio = (TP_price - Open_Price) / (Open_Price - SL_price); 
   
   
   //Risk Reward Ratio
   string RiskReward_ratio2 = DoubleToStr(RiskReward_ratio,1);
   ObjectCreate("RiskReward_ratio", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("RiskReward_ratio",  "R/R Ratio  1 / " + RiskReward_ratio2, FontSize + 4, FontType, White);
   ObjectSet("RiskReward_ratio", OBJPROP_CORNER, WhatCorner);
   ObjectSet("RiskReward_ratio", OBJPROP_XDISTANCE, 10);
   ObjectSet("RiskReward_ratio", OBJPROP_YDISTANCE, 180);
   
   //PipValue
   double TickValue = MarketInfo(Symbol(),MODE_TICKVALUE)*AccType;
   ObjectCreate("TickValue",OBJ_LABEL, 0, 0, 0); 
   ObjectSetText("TickValue", "PipValue = $" + DoubleToStr(TickValue,3) ,FontSize, FontType, FontColor);
   ObjectSet("TickValue", OBJPROP_CORNER, WhatCorner);
   ObjectSet("TickValue", OBJPROP_XDISTANCE, 10);
   ObjectSet("TickValue", OBJPROP_YDISTANCE, 10);
   
   //Percent of account at risk
   double PipsAtRisk = (Open_Price - SL_price)/Point; 
   double AccDollarToRisk = (AccountBalance()*RiskPercent) /100; 
   double SLDollarAmount= PipsAtRisk * TickValue; 
   double Lots = AccDollarToRisk / SLDollarAmount; 
   double LotValue =  Lots * TickValue; 
   double StopPerAtRisk = LotValue * PipsAtRisk; 
   ObjectCreate("Risk",OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Risk", DoubleToStr(MathAbs(RiskPercent),1) + "% At Risk =  $" + DoubleToStr(MathAbs(AccDollarToRisk),2),FontSize, FontType, FontColor);
   ObjectSet("Risk", OBJPROP_CORNER, WhatCorner); 
   ObjectSet("Risk", OBJPROP_XDISTANCE, 10);
   ObjectSet("Risk", OBJPROP_YDISTANCE, 150);
   
   //Percent of acount to gain
   double TPGainPips = (Open_Price - TP_price)/Point;
   double TPDollarAmount =  TPGainPips * LotValue; 
   double TPPercentRisk = (TPDollarAmount/AccountBalance()*100);
   ObjectCreate("Gain",OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Gain", DoubleToStr(MathAbs(TPPercentRisk),1) + "% To Gain =  $" + DoubleToStr(MathAbs(TPDollarAmount),2),FontSize, FontType, FontColor);
   ObjectSet("Gain", OBJPROP_CORNER, WhatCorner); 
   ObjectSet("Gain", OBJPROP_XDISTANCE, 10);
   ObjectSet("Gain", OBJPROP_YDISTANCE, 130);
   
   //Lots To Trade
   ObjectCreate("Lots",OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Lots", "LotsToTrade = " + DoubleToStr(MathAbs(Lots),2),FontSize, FontType, FontColor);
   ObjectSet("Lots", OBJPROP_CORNER, WhatCorner); 
   ObjectSet("Lots", OBJPROP_XDISTANCE, 10);
   ObjectSet("Lots", OBJPROP_YDISTANCE, 110);
   
    //Stop Price
   double Differ = (Open_Price - SL_price)/Point;
   ObjectCreate("SL_price",OBJ_LABEL, 0, 0, 0);
   ObjectSetText("SL_price", "StopLossPrice = " + DoubleToStr(SL_price,Digits) + " /-" + DoubleToStr(MathAbs(Differ),0) + " pips",FontSize, FontType, Crimson); 
   ObjectSet("SL_price", OBJPROP_CORNER, WhatCorner);
   ObjectSet("SL_price", OBJPROP_XDISTANCE, 10);
   ObjectSet("SL_price", OBJPROP_YDISTANCE, 40);
   
   //Target Price
   double Differ2 = (Open_Price - TP_price)/Point; 
   ObjectCreate("TP_price",OBJ_LABEL, 0, 0, 0);
   ObjectSetText("TP_price", "TargetPrice = " + DoubleToStr(TP_price,Digits) + " /+" + DoubleToStr(MathAbs(Differ2),0) + " pips",FontSize, FontType, Yellow);
   ObjectSet("TP_price", OBJPROP_CORNER, WhatCorner);
   ObjectSet("TP_price", OBJPROP_XDISTANCE, 10);
   ObjectSet("TP_price", OBJPROP_YDISTANCE, 80);
   
   //Entry Price
   ObjectCreate("Open_Price",OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Open_Price", "EntryPrice = " + DoubleToStr(Open_Price,Digits),FontSize, FontType, LimeGreen);
   ObjectSet("Open_Price", OBJPROP_CORNER, WhatCorner);
   ObjectSet("Open_Price", OBJPROP_XDISTANCE, 10);
   ObjectSet("Open_Price", OBJPROP_YDISTANCE, 60);
   

  
   return(0);
  }
//+------------------------------------------------------------------+//+------------------------------------------------------------------+