//+------------------------------------------------------------------+
//|                                          CoinflipwithMM          |
//|                                                                  |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|              External Variables                                  |
//+------------------------------------------------------------------+

extern int ExpertID = 3224888;   
extern int StopLoss             =   40;
extern int TakeProfit           =   43;
extern int    Slippage          =   3;
extern double Lots              =   0.1;
extern double IncrementSize = 0.1;
extern double DecrementSize = 0.1;




double MinLots, MaxLots, BuyLots, SellLots;

int    NumBuys, NumSells;

int start()
{
   int MagicNumber = MakeMagicNumber(true);
  NumBuys  = 0;
  NumSells = 0;
  
  BuyLots  = 0.00;
  SellLots = 0.00;


  {
    OrderSelect(SELECT_BY_POS,MODE_TRADES);

    if (OrderSymbol() == Symbol()) 
    
    {
      if (OrderType() == OP_BUY)  NumBuys++;    
      if (OrderType() == OP_SELL) NumSells++;     
    }   
  }
   
   int last_trade=HistoryTotal();
   if(last_trade>0) {
      if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true) {
         if(OrderSymbol()==Symbol() && OrderClosePrice()==OrderStopLoss() && OrderMagicNumber() == MagicNumber) {
             //Print("Last closed order was OrderType = ",OrderType(),"  Time = ",TimeToStr(OrderCloseTime()),"  close at StopLoss  ",OrderClosePrice());
             Lots = OrderLots() + IncrementSize;
             if (Lots > MarketInfo(Symbol(), MODE_MAXLOT)) {
               Lots = MarketInfo(Symbol(), MODE_MAXLOT);
             }
         }
         if(OrderSymbol()==Symbol() && OrderClosePrice()==OrderTakeProfit() && OrderMagicNumber() == MagicNumber) {
             //Print("Last closed order was OrderType = ",OrderType(),"  Time = ",TimeToStr(OrderCloseTime()),"  close at TakeProfit  ",OrderClosePrice());
             Lots = OrderLots() - DecrementSize;
             if (Lots < 0.1) {
               Lots = 0.1;
             }
         }
      }
   }
     
   if(MathMod(TimeCurrent()/60, 2)==0 && NumSells <1 && NumBuys <1) 
    {
      OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"CoinflipwithMM ",MagicNumber,0,White);
      Print ("Errors opening BUY order = ",GetLastError());
      return(0);
    }
 
 
if ( NumBuys <1 && NumSells <1)
 
    {
      OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"CoinflipwithMM ",MagicNumber,0,Red);
      Print ("Errors opening SELL order = ",GetLastError());
      return(0);
    }
   
 
//Screen comments         
  Comment("Balance: ", AccountBalance(), ", Equity: ", AccountEquity(), ", Lots: ",Lots,
        
        "\nNum Buys: ", NumBuys, ", Num Sells: ", NumSells, ", BuyLots:", BuyLots, ", SellLots:", SellLots);
        

  return(0);
}

//+------------------------------------------------------------------+
//| Make Magic Number                                                |
//+------------------------------------------------------------------+
int MakeMagicNumber(bool TimeSpecific )
{
   int SymbolCode  = 0;
   int PeriodCode  = 0;
   int MagicNumber = 0; 
   
   //---- Symbol Code
        if( Symbol() == "AUDCAD" || Symbol() == "AUDCADm" ) { SymbolCode = 1000; }
   else if( Symbol() == "AUDJPY" || Symbol() == "AUDJPYm" ) { SymbolCode = 2000; }
   else if( Symbol() == "AUDNZD" || Symbol() == "AUDNZDm" ) { SymbolCode = 3000; }
   else if( Symbol() == "AUDUSD" || Symbol() == "AUDUSDm" ) { SymbolCode = 4000; }
   else if( Symbol() == "CHFJPY" || Symbol() == "CHFJPYm" ) { SymbolCode = 5000; }
   else if( Symbol() == "EURAUD" || Symbol() == "EURAUDm" ) { SymbolCode = 6000; }
   else if( Symbol() == "EURCAD" || Symbol() == "EURCADm" ) { SymbolCode = 7000; }
   else if( Symbol() == "EURCHF" || Symbol() == "EURCHFm" ) { SymbolCode = 8000; }
   else if( Symbol() == "EURGBP" || Symbol() == "EURGBPm" ) { SymbolCode = 9000; }
   else if( Symbol() == "EURJPY" || Symbol() == "EURJPYm" ) { SymbolCode = 1000; }
   else if( Symbol() == "EURUSD" || Symbol() == "EURUSDm" ) { SymbolCode = 1100; }
   else if( Symbol() == "GBPCHF" || Symbol() == "GBPCHFm" ) { SymbolCode = 1200; }
   else if( Symbol() == "GBPJPY" || Symbol() == "GBPJPYm" ) { SymbolCode = 1300; }
   else if( Symbol() == "GBPUSD" || Symbol() == "GBPUSDm" ) { SymbolCode = 1400; }
   else if( Symbol() == "NZDJPY" || Symbol() == "NZDJPYm" ) { SymbolCode = 1500; }
   else if( Symbol() == "NZDUSD" || Symbol() == "NZDUSDm" ) { SymbolCode = 1600; }
   else if( Symbol() == "USDCAD" || Symbol() == "USDCADm" ) { SymbolCode = 1700; }
   else if( Symbol() == "USDCHF" || Symbol() == "USDCHFm" ) { SymbolCode = 1800; }
   else if( Symbol() == "USDJPY" || Symbol() == "USDJPYm" ) { SymbolCode = 1900; }
                     
   
   //---- Period Code
   if( TimeSpecific )
   {
              if( Period() == 1 )    { PeriodCode = 10; }
         else if( Period() == 5 )    { PeriodCode = 20; }
         else if( Period() == 15 )   { PeriodCode = 30; }
         else if( Period() == 30 )   { PeriodCode = 40; }
         else if( Period() == 60 )   { PeriodCode = 50; }
         else if( Period() == 240 )  { PeriodCode = 60; }
         else if( Period() == 1440 ) { PeriodCode = 70; }
         else if( Period() == 10080 ){ PeriodCode = 80; }
   }
   else
   {
      PeriodCode = 0;
   }
   //---- Calculate MagicNumber
   MagicNumber = ExpertID+SymbolCode+PeriodCode;
   return(MagicNumber);
}