//+------------------------------------------------------------------+
//|                                                    InsideBuy.mq4 |
//|                                   Copyright © 2009, Mats G Myhre |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Mats G Myhre"
#property link      ""
#property show_inputs

extern double RiskPercent = 5;
extern double TakeProfitATR = 20;
extern bool buy = false;
extern int MaxLotDecimal = 2;
extern int shift = 2;
extern int magicnumber = 1337;

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
double accountrisk = AccountFreeMargin()*RiskPercent/100.0;
double hi = iHigh(Symbol(),0,shift);
double lo = iLow(Symbol(),0,shift);
double size = hi - lo + 6*Point;

if (StringFind(Symbol(),"JPY",0) != -1)
{
double size2 = size*100;
}
else
{
size2 = size*10000;
}
double tick = MarketInfo(Symbol(), MODE_TICKVALUE);
double value = size2 * MarketInfo(Symbol(), MODE_TICKVALUE);

double lotx = accountrisk / value;
double lot = NormalizeDouble(lotx,MaxLotDecimal);

if (MaxLotDecimal == 1)
   if ( lot < 0.1) lot = 0.1;

if (MaxLotDecimal == 2)
if(lot < 0.01)  lot = 0.01;


if(buy == true)
{
OrderSend(Symbol(),OP_BUYSTOP,lot,hi+3*Point,5,lo-3*Point,hi+iATR(Symbol(),0,TakeProfitATR,0),"Buy Order",magicnumber,0,Green);
   int error = GetLastError();
   if(error != 0)
      {
      Alert("Error: "+error+". Find solution here: http://docs.mql4.com/constants/errors");
      Alert("Debug: Lots: "+lot+" Hi: "+hi+" Lo: "+lo+" ATR: "+iATR(Symbol(),0,TakeProfitATR,0));
      }
}
if(buy == false)
{
OrderSend(Symbol(),OP_SELLSTOP,lot,lo-3*Point,5,hi+3*Point,lo-iATR(Symbol(),0,TakeProfitATR,0),"Sell Order",magicnumber,0,Red);
   error = GetLastError();
   if(error != 0)
      {
      Alert("Error: "+error+". Find solution here: http://docs.mql4.com/constants/errors");
      Alert("Debug: Lots: "+lot+" Hi: "+hi+" Lo: "+lo+" ATR: "+iATR(Symbol(),0,TakeProfitATR,0));
      }
}

//----
   return(0);
  }
//+------------------------------------------------------------------+