Help! Where are my EA code mistake?

 
Help! Where are my EA code mistake?

Dual Thrust. Basic principle is simple, describe below
1. In today's closing, calculated two values​​: the highest price - closing price, and closing price - the lowest. Then take the larger of two values​​, multiplied by the k value 0.7. The result is called the trigger value.

2 in tomorrow's opening, record opening, then the price over (opening + trigger value) immediately buy or price lower than the (opening - the trigger value) as soon as short selling.

3. There is no clear stop. This system is a reverse system, that is, if the price exceeds (opening + trigger value) on hand an empty one, then buy two. Similarly, if the price is lower than (opening - the trigger value) do have more than one single, has sold two.

The question now is in testing, at a time repeating the closing / opening until all the funds run out. Who can help me find out where the problem code?
Thank you



My EA code:
//+------------------------------------------------------------------+
//| WJ_DualThrust_For_ATC2011.mq5 |
//| Copyright 2010, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"

#include "Trade\AccountInfo.mqh"
#include "Trade\PositionInfo.mqh"
#include "Trade\Trade.mqh"

//--- input parameters
input int Input1=0;
input double Input2=0.1;
input int Input3;
input int Input4;

input double Lot=0.1; // Lot size

#define BAR_COUNT 3

double Trigger_value;
int Position_Status = 0; //1 for Buy, -1 for Sell;
int Position_Lot = 0; //1 for Long, -1 for Short;


//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//--- Minimum allowed volume for trade operations
double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
//--- Maximum allowed volume for trade operations
double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
//--- Obtain minimum volume gradation
double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

//--- Get the number of pending orders allowed on the account
int max_allowed_orders=(int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS );
double max_positions_volume=15;


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


//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---

//---
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---

}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{

//---
// double Open[BAR_COUNT],High[BAR_COUNT],Low[BAR_COUNT],Close[BAR_COUNT];
double Open[],High[],Low[],Close[];

if(CopyOpen(_Symbol,PERIOD_CURRENT,0,BAR_COUNT,Open)!=BAR_COUNT
|| CopyHigh(_Symbol,PERIOD_CURRENT,0,BAR_COUNT,High)!=BAR_COUNT
|| CopyLow(_Symbol,PERIOD_CURRENT,0,BAR_COUNT,Low)!=BAR_COUNT
|| CopyClose(_Symbol,PERIOD_CURRENT,0,BAR_COUNT,Close)!=BAR_COUNT)
{
Print("CopyData failed, no data");
}

Trigger_value = 0.7*MathMax((High[BAR_COUNT-2]-Close[BAR_COUNT-2]), (Close[BAR_COUNT-2]-Low[BAR_COUNT-2]));
/*
Print("Trigger_value =", Trigger_value);
Print("High[BAR_COUNT-2] =", High[BAR_COUNT-2]);
Print("Close[BAR_COUNT-2] =", Close[BAR_COUNT-2]);
Print("Low[BAR_COUNT-2] =", Low[BAR_COUNT-2]);
Print("Open[BAR_COUNT-2] =", Open[BAR_COUNT-2]);
Print("Open[BAR_COUNT-1] =", Open[BAR_COUNT-1]);
Print("High[BAR_COUNT-1] =", High[BAR_COUNT-1]);
Print("Close[BAR_COUNT-1] =", Close[BAR_COUNT-1]);
Print("Low[BAR_COUNT-1] =", Low[BAR_COUNT-1]);
Print("SymbolInfoDouble(_Symbol,SYMBOL_BID) =", SymbolInfoDouble(_Symbol,SYMBOL_BID));
Print("SymbolInfoDouble(_Symbol,SYMBOL_ASk) =", SymbolInfoDouble(_Symbol,SYMBOL_ASK));
*/

if(SymbolInfoDouble(_Symbol,SYMBOL_ASK) > (Open[BAR_COUNT-1]+Trigger_value)) {Position_Status =1;} //1 for buy
if(SymbolInfoDouble(_Symbol,SYMBOL_BID) < (Open[BAR_COUNT-1]-Trigger_value)) {Position_Status =-1;} //-1 for sell

/*
Print("Position_Status =", Position_Status);
*/
if(Position_Status==1) //1 for buy and long
{
CPositionInfo posinf;
CTrade trade;
double lot=Lot;
Print("posinf.Select(_Symbol)= ",posinf.Select(_Symbol));
if(posinf.Select(_Symbol))
{
if(posinf.Type()==POSITION_TYPE_SELL)
{
// lot=posinf.Volume();
trade.PositionClose(_Symbol,3);
// Print(trade.ResultRetcodeDescription());
}
}
else
{
trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,lot,SymbolInfoDouble(_Symbol,SYMBOL_ASK),0,0);
// Print(trade.ResultRetcodeDescription());
}

}


if(Position_Status==-1) //-1 for sell
{
CPositionInfo posinf;
CTrade trade;
double lot=Lot;

if(posinf.Select(_Symbol))
{
if(posinf.Type()==POSITION_TYPE_BUY)
{
// lot=posinf.Volume();
trade.PositionClose(_Symbol,3);
// Print(trade.ResultRetcodeDescription());
}
}
else
{
trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,lot,SymbolInfoDouble(_Symbol,SYMBOL_BID),0,0);
// Print(trade.ResultRetcodeDescription());
}

}

}
//+------------------------------------------------------------------+
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
wenjin:
Help! Where are my EA code mistake?

Dual Thrust. Basic principle is simple, describe below
1. In today's closing, calculated two values​​: the highest price - closing price, and closing price - the lowest. Then take the larger of two values​​, multiplied by the k value 0.7. The result is called the trigger value.

2 in tomorrow's opening, record opening, then the price over (opening + trigger value) immediately buy or price lower than the (opening - the trigger value) as soon as short selling.

3. There is no clear stop. This system is a reverse system, that is, if the price exceeds (opening + trigger value) on hand an empty one, then buy two. Similarly, if the price is lower than (opening - the trigger value) do have more than one single, has sold two.

The question now is in testing, at a time repeating the closing / opening until all the funds run out. Who can help me find out where the problem code?
Thank you

Well that's because you put the logic in the OnTick() event handler. How many times do you think the algorithm will evaluate the same result? There will be a lot because a tick price change won't change much the result of your algorithm.

If the algorithm should be run only once a day, why don't you use OnTimer() event handler, and call EventSetTimer to trigger once a day.

Documentation on MQL5: Working with Events / EventSetTimer
  • www.mql5.com
Working with Events / EventSetTimer - Documentation on MQL5
 
If I think the the algorithm once 1 hours, How to modify the My EA code?

Thank you!
 
wenjin:
If I think the the algorithm once 1 hours, How to modify the My EA code?

Thank you!

In OnInit() call EventSetTimer(3600) (1hour = 3600 seconds)

Then, just put your code inside OnTimer() and it will be automatically called every hour

Documentation on MQL5: Working with Events / EventSetTimer
  • www.mql5.com
Working with Events / EventSetTimer - Documentation on MQL5
 

Modified to:

if(posinf.PositionType()==POSITION_TYPE_SELL)
{
// lot=posinf.Volume();
trade.PositionClose(_Symbol,3);
// Print(trade.ResultRetcodeDescription());
}
}
else
{
trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,lot,SymbolInfoDouble(_Symbol,SYMBOL_ASK),0,0);
// Print(trade.ResultRetcodeDescription());
}

}


if(Position_Status==-1) //-1 for sell
{
CPositionInfo posinf;
CTrade trade;
double lot=Lot;

if(posinf.Select(_Symbol))
{
if(posinf.PositionType()==POSITION_TYPE_BUY)  

Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Symbol Properties - Documentation on MQL5
 
Thank you for yyy999.
You come from China? 以后有问题向你请教。谢谢
 
yyy999:
如果我上面代码中开仓指令中包含了sl,tp参数而且在执行过程中因为sl,tp的关系平仓了,如果修改上面的代码来保证在选定的时间框架内(如选定的时间框架周期为日)不会出现同方向的多次开仓?现在我这个代码在因为sl,tp平仓后会立即同方向再次开仓!
谢谢
If I am opening the code above instructions included sl, tp parameters and in the implementation process because sl, tp open relationship, and if you modify the above code to ensure that the selected time frame (such as the selected time framework of the cycle date) does not appear in the same direction of the opening times? Now my code because sl, tp open immediately after the re-opening the same direction!
Thank you.
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Position Properties - Documentation on MQL5
 

这段代码你可以参考下,可以保证一柱只开一单

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool FindLastTime(string symbol,datetime startTime)
{ 
  MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied=CopyRates(symbol,timeframe1,0,10,rates);
   if(copied<0)
     {
     Print("Failed to get history data for the symbol ",symbol);
      return(0);
     }/**/
   if(startTime==0) startTime= rates[0].time;
   datetime timeNowBar=startTime; 
   if((TimeCurrent()- timeNowBar) >=runTime*60)return(0);
   
   double oldorderopenprice=0,orderprice=0;
   int cnt;
   ulong oldticketnumber=0,ticketnumber;
   datetime from=0;
   datetime to=TimeCurrent();
//--- request the entire history
   HistorySelect(from,to);
//--- variables for returning values from order properties
   ulong    ticket;
   double   open_price;
   double   initial_volume;
   datetime time_open;
   string   symbol_order;
// ENUM_ORDER_TYPE order_type;
   long order_type;
   long     order_magic;
   long  deal_order_entry;
//--- number of current pending orders
   uint     total=HistoryDealsTotal();

//--- go through orders in a loop
   for(int i=total-1;i>=0;i--)
     {
      //--- return order ticket by its position in the list
      if((ticket=HistoryDealGetTicket(i))>0)
        {
         //--- return order properties
        
          time_open= (datetime)HistoryDealGetInteger(ticket,DEAL_TIME);
         //time_done=        HistoryOrderGetInteger(ticket,ORDER_TIME_DONE);
         symbol_order=HistoryDealGetString(ticket,DEAL_SYMBOL);
         order_magic=HistoryDealGetInteger(ticket,DEAL_MAGIC);
         
         deal_order_entry=HistoryDealGetInteger(ticket,DEAL_ENTRY);
        }

    
      if(symbol!=symbol_order || order_magic!=MagicNumber) continue;
     // if(deal_order_entry!=DEAL_ENTRY_IN) continue;
      if(symbol==symbol_order && order_magic==MagicNumber)
        {
      
         if(time_open-timeNowBar>=0)
           {
           return(false);//no run
           }
          else return(true);// run
         break;
        
        }
     }
     
   return(1);

  }
 
yyy999:
谢谢,我参考一下。
Reason: