//+------------------------------------------------------------------+
//|                                                  C2TestingEA     |
//|                      Copyright © 2007, Infinite Idea             |
//|                                    http://www.infinite-idea.com/ |
//+------------------------------------------------------------------+

#property copyright "Duncan McQueen"
#property link      "http://www.infinite-idea.com"

#import "Collective2Library.ex4"
   bool InitCollective2(string systemid, string password, string email, double lotmultiplier);
   int C2OrderSend(string symbol, int cmd, double volume, double price, int slippage,
                double stoploss, double takeprofit, string comment, int magic,
                datetime expiration=0, color arrow_color=CLR_NONE);
   bool C2OrderClose(int ticket, double lots, double price, int slippage,
                 color Color=CLR_NONE);
   bool C2OrderModify(int ticket, double price, double stoploss, double takeprofit, datetime expiration, color arrow_color=CLR_NONE);
#import
extern int  MaxOpenTrade      = 1;  
extern string Collective2Password = "";
extern string Collective2SystemID = "";
extern string Collective2Email = "";
extern double Collective2LotMultiplier = 10;

extern double TakeProfit = 50;
extern double Lots = 0.1;
extern double TrailingStop = 30;

datetime LastDateSnap;
bool OpenCloseFlag;
int ticket;
int NextOrderType;

//Open is true, close is false                            
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

   LastDateSnap = CurTime();
   OpenCloseFlag = false;
   NextOrderType = 0;
   InitCollective2(Collective2SystemID, Collective2Password, Collective2Email, Collective2LotMultiplier);
   Print("Starting at", LastDateSnap);             
  }

//----------------------- MaxTrade ALWAYS >= 1
   if(MaxOpenTrade<=0) MaxOpenTrade = 1;
   
   return(0);
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   Print("Starting");
   // If one minute from LastDateSnap - open trade
   if (CurTime() - LastDateSnap > 5)
   {
      switch (NextOrderType)
      {
         case 0:
            ticket=C2OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"C2TestingEA",0,0,Red);
            NextOrderType = 1; 
            if(ticket>0)
            {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
            }
            else Print("Error opening SELL order : ",GetLastError());
            
            break;
         case 1:
            Print("Trying OrderModify");
            C2OrderModify(ticket, Bid, Bid+TakeProfit*Point, Bid-(TakeProfit*2)*Point, "C2TestingEA", Blue);
            NextOrderType = 2;
            break;
         case 2:
            Print("Trying OrderClose");
            C2OrderClose(ticket,Lots,Ask,3,Violet);
            NextOrderType = 0;
            break;
         
      }
      
      LastDateSnap = CurTime();
   }

   return(0);
  }
// the end.