ReverseSystem





#define MAINSEEK 148
#define BARSIZE 44  // LONG_VALUE + 5 * DOUBLE_VALUE

extern int Pips = 50;
extern double Lots = 0.1;

int handle;
bool MainError;

int GetTime( int Pos )
{
  int PosTime;
  
  FileSeek(handle, MAINSEEK + Pos, SEEK_SET);
  PosTime = FileReadInteger(handle);

  return(PosTime);
}

bool FindTimePlace( int SearchTime )
{
  int LeftTime, RightTime, PosTime;
  int Left, Right, Pos;
  
  Left = 0;
  Right = FileSize(handle) - MAINSEEK - BARSIZE;
  
  LeftTime = GetTime(Left);
  RightTime = GetTime(Right);
  
  while ((LeftTime < SearchTime) && (SearchTime < RightTime))
  {    
    Pos = (Left + Right) / 2;
    Pos -= Pos % BARSIZE;
    
    if (Pos == Left)
      break;
    
    PosTime = GetTime(Pos);
    
    if (SearchTime >= PosTime)
    {
      Left = Pos;
      LeftTime = GetTime(Left);
    }
    else // if (SearchTime < PosTime)
    {
      Right = Pos;
      RightTime = GetTime(Right);
    }
  }
  
  if (SearchTime <= RightTime)
  {
    FileSeek(handle, Left + MAINSEEK, SEEK_SET);
    return(TRUE);
  }
  else
    return(FALSE);
}

void init()
{
  handle = FileOpenHistory(Symbol() + Period() + ".hst", FILE_BIN|FILE_READ);
  
  if (handle > 0)
    MainError = TRUE;
  else
  {
    MainError = FALSE;
    
    return;
  }

  MainError = FindTimePlace(Time[0]);
  
  if (!MainError)
    FileClose(handle);
    
  return;
}

void deinit()
{
  if (MainError)
    FileClose(handle);
  
  return;
}

bool GetPrices( int& PriceTime, int& PriceLow, int& PriceHigh)
{
  PriceTime = FileReadInteger(handle);
  FileSeek(handle, DOUBLE_VALUE, SEEK_CUR);
  PriceLow = FileReadDouble(handle) / Point + 0.1;
  PriceHigh = FileReadDouble(handle) / Point + 0.1;
  FileSeek(handle, 2 * DOUBLE_VALUE, SEEK_CUR);

  if (FileTell(handle) + BARSIZE <= FileSize(handle))
    return(TRUE);
  else
    return(FALSE);
}

int GetTimeTrade()
{
  static bool FlagUP = TRUE;
  static int Min = 999999;
  static int Max = 0;
  static int NTime;
  int ResTime;
  
  int PriceTime, PriceLow, PriceHigh;
    
  while (TRUE)
  {
    if (!GetPrices(PriceTime, PriceLow, PriceHigh))
      return(-1);

    if (FlagUP)
    {
      if (PriceHigh > Max)
      {
        Max = PriceHigh;
        NTime = PriceTime;
      }
      else if (Max - PriceLow >= Pips)
      {
        FlagUP = FALSE;
        Min = PriceLow;
        
        break;
      }
    }
    else // (FlagUP == FALSE)
    {
      if (PriceLow < Min)
      {
        Min = PriceLow;
        NTime = PriceTime;
      }
      else if (PriceHigh - Min >= Pips)
      {
        FlagUP = TRUE;
        Max = PriceHigh;
        
        break;
      }
    }
  }
  
  ResTime = NTime;
  NTime = PriceTime;

  return(ResTime);
}

void CloseOrder( int Ticket )
{
  OrderSelect(Ticket, SELECT_BY_TICKET);
  
  if (OrderType() == OP_BUY)
    OrderClose(Ticket, OrderLots(), Bid, 0);
  else  // (OrderType() == OP_SELL)
    OrderClose(Ticket, OrderLots(), Ask, 0);

  return;  
}

int ReverseOrder( int Ticket)
{
  if (Ticket == 0)
    Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 0, 0, 0);
  else
  {
    OrderSelect(Ticket, SELECT_BY_TICKET);
  
    if (OrderType() == OP_BUY)
    {
      OrderClose(Ticket, OrderLots(), Bid, 0);
      Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 0, 0, 0);
    }
    else  // (OrderType() == OP_SELL)
    {
      OrderClose(Ticket, OrderLots(), Ask, 0);
      Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 0, 0, 0);
    }
  }
  
  return(Ticket);
}

void System()
{
  static int Ticket = 0;
  static int NewTime = 0;
  
  if (NewTime < 0)
    return;
    
  if (Time[0] < NewTime)
    return;

  Ticket = ReverseOrder(Ticket);
  
  NewTime = GetTimeTrade();
  
  if (NewTime < 0)
    CloseOrder(Ticket);
}

void start()
{
  if (!MainError)
    return;

  System();
    
  return;
}



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar


Indicator Curves created:


Indicators Used:



Custom Indicators Used:

Order Management characteristics:
It Closes Orders by itself
It automatically opens orders when conditions are reached

Other Features:


It reads information from a file

BackTest : EURUSD on H1

From 2009-08-01 to 2009-10-01 Profit Factor:2.59 Total Net Profit:3721.16

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:3.13 Total Net Profit:3341.26

BackTest : EURUSD on H1

From 2010-03-01 to 2010-03-27 Profit Factor:2.22 Total Net Profit:1352.71

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-30 Profit Factor:3.26 Total Net Profit:2306.66

BackTest : EURUSD on H1

From 2010-05-01 to 2010-05-31 Profit Factor:2.98 Total Net Profit:3496.59

BackTest : EURUSD on H1

From 2010-06-01 to 2010-06-30 Profit Factor:3.89 Total Net Profit:3138.00

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-02-27 Profit Factor:1.54 Total Net Profit:2017.59

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-04-16 Profit Factor:0.24 Total Net Profit:-9995.75

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:1.26 Total Net Profit:6509.20

BackTest : USDCAD on H1

From 2009-12-01 to 2010-01-01 Profit Factor:2.48 Total Net Profit:1628.97

BackTest : USDCHF on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.45 Total Net Profit:-1417.61

BackTest : USDJPY on H1

From 2009-11-01 to 2009-11-30 Profit Factor:1.24 Total Net Profit:319.85

Request Backtest for ReverseSystem


From : (yyyy/mm/dd) To: (yyyy/mm/dd)

Pair: Period: