Less code, more action... writing an EA - page 6

 
Maxim Kuznetsov:

I allocate memory to them, of course. To a depth no greater than that needed for calculations and debugging. In the given fragment it is 30, which is more than enough. If somewhere it will be necessary to calculate for example standard deviation of depth 50, then the cache should be increased. And it is only for the sake of speeding up the calculations.

Ok. Everyone has his own vision.
 
Vladimir Simakov:

There is nothing wrong with the outlook

Forum on trading, automated trading systems and trading strategies testing

Features of mql5 language, tips and tricks

fxsaber, 2018.02.15 11:48

I suggest you try writing a script in MQL5 with this kind of trading logic (MQL4-style just for quick sense display)

void OnStart()
{
  OrderCloseBy(OrderSend(_Symbol, OP_BUY, 1, Ask, 0, 0, 0), OrderSend(_Symbol, OP_SELL, 1, Bid, 0, 0, 0));
}
 
fxsaber:
#include <Template\Objects\COrder.mqh>
#include <Template\Objects\CPosition.mqh>

COrder  *order1,*order2;
CPosition *pos1,*pos2;

//----------------------------------------------------------------------
int OnInit()
  {
   order1=new COrder(NULL,ORDER_TYPE_BUY,0.2,0.0,0.0,0.0);
   order2=new COrder(NULL,ORDER_TYPE_SELL,0.1,0.0,0.0,0.0);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   delete order1;
   delete order2;
   delete pos1;
   delete pos2;
  }
//+------------------------------------------------------------------+
void OnTick()
  {
   CheckOrder(order1,pos1);
   CheckOrder(order2,pos2);
   if (CheckPointer(pos1)&&CheckPointer(pos2)) pos1.CloseBy(pos2);
   if (CheckPointer(pos1)&&pos1.Control()>POSITION_MUST_CLOSE) delete pos1;
   if (CheckPointer(pos2)&&pos2.Control()>POSITION_MUST_CLOSE) delete pos2;
  }

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
   if (CheckPointer(order1)) order1.TradeTransaction(trans,request,result);
   if (CheckPointer(order2)) order2.TradeTransaction(trans,request,result);
  }      
//-----------------------------------------------------------------
void CheckOrder(COrder* &order, CPosition* &position){
   if (!CheckPointer(order)) return;
   switch(order.Control()){
      case ORDER_FULL:        position=NewPosition(order);
                              Print(position.GetTicket()," Ok");
      case ORDER_REMOVE:
      case ORDER_ERROR:       delete order;}}
//-------------------------------------------------------------------
CPosition* NewPosition(COrder* &order)  {return CheckPointer(order)&&PositionSelectByTicket(order.GetTicket())?new CPosition():NULL;}

I won't show you the libraries. ex5 file is attached.

CloseBy was made on the scratch, so far there was no need in it, but thanks for the "weak", the only thing left is to make the changes in the positions after CloseBy

In the future, there will be one wrapper class for COrder and CPostion

Files:
Test.ex5  43 kb
 

I'll try to explain what's being done in words :-)

Suppose we need an Expert Advisor. First, we need the simplest one that trades by crossing of fractals and trailing stops by fractals. That is, in the terminal, it would look like this :

What is circled in BLACK is line 1 of the table. Those data that the Expert Advisor takes into account in its algorithms.

The purpose of the use-case that have been given is to describe as compactly as possible what lies in this area, how it is counted and what complements it with the Expert Advisor. What calculations are made on the basis of this data.

In my opinion, the easiest thing to do is to
1) list these fields by naming them, i.e. list them in the ENUM
2.) write a simple function, which by the name from the ENUM and the bar gives their values to the Expert Advisor.

For an ordinary programmer, there is a certain methodology (clearly marked steps) of development of simple Expert Advisors:

1. Define the Input

2. describe and complete Data by writing formulas; add columns as needed

3. Specify which data from the resulting table is used and where.

In order to store data there is a class DataFrame that stores data "by column" provides access by indexes, data caching and calculations on demand.

On this thin (there is not much code, only what is strictly necessary for the use-case), we can develop various Expert Advisors. The table is counting, the signals are received, and trades are opened.

Of course, it's not enough :-) So, the project has just been launched... And there is no ready-made solution, it's a project. It is a project that has just been born and is developing.

 
Vladimir Simakov:

Thanks for the "weak".

So a script, not an EA. But even with an EA you have perfectly demonstrated the difference between MT4 and MT5. One of the variants has only one line. And the second one, unfortunately, failed.

 
fxsaber:

So a script, not an EA. But even with an EA you have perfectly demonstrated the difference between MT4 and MT5. There is only one line in one of the variants.

Come on, CloseBy, in case you haven't noticed, I have: pos1.CloseBy(pos2), everything else is opening orders and checking operations. In mt4 you also need to open two positions first and provide a check for their opening. Maybe you will also post the working code on the studio, just for comparison's sake.

 
Maxim Kuznetsov


:

I'll try to explain what's being done in words :-)

Suppose we need an Expert Advisor. First, we need the simplest one that trades by crossing of fractals and trailing stops by fractals. That is, in the terminal, it would look like this :

What is circled in BLACK is line 1 of the table. Those data that the Expert Advisor takes into account in its algorithms.

The purpose of the use-case that have been given is to describe as compactly as possible what lies in this area, how it is counted and what complements it with the Expert Advisor. What calculations are made on the basis of this data.

In my opinion, the easiest thing to do is to
1) list these fields by naming them, i.e. list them in the ENUM
2.) write a simple function, which by the name from the ENUM and the bar gives their values to the Expert Advisor.

For an ordinary programmer, there is a certain methodology (clearly marked steps) of development of simple Expert Advisors:

1. Define the Input

2. describe and complete Data by writing formulas; add columns as needed

3. Specify which data from the resulting table is used and where.

In order to store data there is a class DataFrame that stores data "by column" provides access by indexes, data caching and calculations on demand.

On this thin (there is not much code, only what is strictly necessary for the use-case), we can develop various Expert Advisors. The table is counting, the signals are received, and trades are opened.

Of course, it's not enough :-) So, the project has just been opened... And there is no ready-made solution, it's a project. It is a project that has just been born and is developing.

For general thinking, an example of a wrapper class for Ichimoku, but in mql4.

#ifndef _ICHIMOKU_
#define _ICHIMOKU_

#include <ProjectLibrary\Functions\MyLib.mqh>

class CIchimoku
  {
private:
   string            cSymbol;
   ENUM_TIMEFRAMES   cFrame;
   int               cTenkan;
   int               cKijun;
   int               cSenkou;
   ENUM_TIMEFRAMES   cTempFrame;
   int               cFrameShift;
public:
                     CIchimoku(string mSymbol,ENUM_TIMEFRAMES mFrame,int mTenkan,int mKijun,int mSenkou,int mDeltaFrame=0);
   double            Get(int mBuffer,int mShift=0);
   double            Tenkan(int mShift=0)       {return Get(MODE_TENKANSEN,mShift);}
   double            Kijun(int mShift=0)        {return Get(MODE_KIJUNSEN,mShift);}
   double            SpanA(int mShift=0)        {return Get(MODE_SENKOUSPANA,mShift);}
   double            SpanB(int mShift=0)        {return Get(MODE_SENKOUSPANB,mShift);}
   double            Chikou(int mShift=0)       {return Get(MODE_CHIKOUSPAN,mShift);}
   double            CloudMin(int mShift=0)     {return MathMin(SpanA(mShift),SpanB(mShift));}
   double            CloudMax(int mShift=0)     {return MathMax(SpanA(mShift),SpanB(mShift));}
private:
   ENUM_TIMEFRAMES   CheckFrame();
  };
//--------------------------------------------------------------------------------------------------
CIchimoku::CIchimoku(string mSymbol,ENUM_TIMEFRAMES mFrame,int mTenkan,int mKijun,int mSenkou,int mFrameShift=0):
   cTenkan(mTenkan),cKijun(mKijun),cSenkou(mSenkou),cFrameShift(mFrameShift){
   cSymbol=mSymbol==NULL?_Symbol:mSymbol;
   if (mFrameShift){
      cTempFrame=mFrame==PERIOD_CURRENT?(ENUM_TIMEFRAMES)Period():mFrame;
      cFrame=::GetShiftFrame(cTempFrame,mFrameShift);}
   else
      cFrame=mFrame;}
//--------------------------------------------------------------------------------------------------
ENUM_TIMEFRAMES CIchimoku::CheckFrame(void){
   if (!cFrameShift) return cFrame;//>>
   ENUM_TIMEFRAMES frame=(ENUM_TIMEFRAMES)Period();
   if (cTempFrame==frame) return cFrame;//>>
   else cTempFrame=frame;
   return ::GetShiftFrame(frame,cFrameShift);}
//--------------------------------------------------------------------------------------------------------------
double CIchimoku::Get(int mBuffer,int mShift=0){
   ResetLastError();
   double res=iIchimoku(cSymbol,CheckFrame(),cTenkan,cKijun,cSenkou,mBuffer,mShift);
   return !GetLastError()?res:0.0;}

#endif 
 
Vladimir Simakov:

For general thinking, an example of a wrapper class for Ichimoku, but in mql4.

Which Ichimoku component do you want a wrapper for ? And most importantly why and what kind ?
just chatting ? you can, why not...

PS/ Have you seen Excel ? In the DataFrame view the Ishimocks will look the same... Just like all the others... Traders work with tables actually. A chart is just a partial representation (view) of a summary table. So this data should be treated like tables.
From the trader's point of view - what is a program object ? It is nothing. There are no such things in his practical life.

 
Vladimir Simakov:

Come on, CloseBy, in case you haven't noticed, I have: pos1.CloseBy(pos2), everything else opening orders and checking operations.

That's not how it works.

In mt4 you also need to open two positions first and provide a check for their opening. Maybe you will also post the working code on the studio, just for comparison's sake.

#include <MT4Orders.mqh>

#define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID)
#define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK)

void OnStart()
{
  OrderCloseBy(OrderSend(_Symbol, OP_BUY, 1, Ask, 0, 0, 0), OrderSend(_Symbol, OP_SELL, 1, Bid, 0, 0, 0));
}
 
fxsaber:

It doesn't work like that.

I wrote that with controlling the opening of these very orders.

Reason: