Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

algorithmic trading - Mql5 function for selecting open orders by Ticket, then Symbol, and trade Direction

Total newbie to Mql5, and need some help with this int OpenOrders() piece of code originally from Mql4 EA's, that finds open positions - (if any), and selects originally by SYMBOL, and MAGIC Number, and the expert doesn't open any other positions for as long as a trade it has opened on that symbol has not yet been closed. I wish the EA to identify a TICKET it has opened, related SYMBOL, and POSITION_TYPE to be used in the ONTICK, and not open any other trades on the same chart until that one position is closed, but can continue trading on any other charts.

input double LotSize    =0.3;
input double Incriment  =0.01;
input int    StopLoss   =50;
input int    TakeProfit =100;
input int    Trend     =21;
input int    Momentum  =21;
input int    Strength  =13;
int adxlevel       =34;
int buylevel       =62;
int selllevel      =36;
//---------------------
double pips;
#include <TradeTrade.mqh>
CTrade Execute;
ulong passport, StopLevel;
double ask, bid;
double takeout=0,stopout=0;
int moving,rsi,adx,Spread;
//+------------------------------------------------------------------+
int OnInit()
  {
//---
      double ticksize = SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   if(_Digits==3||_Digits==4)
      pips=ticksize*1000;
   else
      pips =ticksize;
//---
        moving=iMA(_Symbol,PERIOD_CURRENT,Trend,0,MODE_SMA,PRICE_CLOSE);
        rsi=iRSI(_Symbol,PERIOD_CURRENT,Momentum,PRICE_MEDIAN);
        adx=iADX(_Symbol,PERIOD_CURRENT,Strength);
//---
   return(INIT_SUCCEEDED);
  }
***int OpenOrders()
  {
      int   BUYS=0,SELLS=0;
         for(int i=0; i<PositionsTotal(); i++)
            {
               if(PositionSelectByTicket(passport)==false) break;
                  int dealtype=(int)PositionGetInteger(POSITION_TYPE); // buy or sell
//                  string position_symbol=PositionGetString(POSITION_SYMBOL); // chart symbol
               if(Symbol()==PositionGetSymbol(i) && passport==PositionGetTicket(POSITION_TICKET))
                  {
                     if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)  BUYS++;
                     if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL) SELLS++;
                  }
            }      
//---       
      if(BUYS>0) return(BUYS);
      else       return(SELLS);
  }***
//+------------------------------------------------------------------+
void OnTick()
  {
//---       
            MqlRates rates[3];
            double movingarray[],rsiarray[],adxarray[];
            CopyRates(_Symbol,PERIOD_CURRENT,1,3,rates);
            CopyBuffer(rsi,0,1,3,rsiarray);
            CopyBuffer(adx,0,1,3,adxarray);
            ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
            bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
            Spread=int(SymbolInfoInteger(_Symbol,SYMBOL_SPREAD));
            StopLevel=SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
//---
      if(OpenOrders()==0)
         {
               if(rates[0].open > moving )// CONDITION
               if(rsiarray[0] > buylevel && rsiarray[1] < buylevel )//SIGNAL
                  {
                     if(TakeProfit>0)  takeout=ask+TakeProfit*pips;
                        if(StopLoss>0)  stopout=ask-StopLoss*pips;
                           Execute.Buy(LotSize,NULL,ask,stopout,takeout,NULL);
                              passport=Execute.ResultOrder();
                              Print("BUY Opened");
                  }
               if(rates[0].open < moving )//CONTITION
               if(rsiarray[0] < selllevel &&rsiarray[1] > selllevel  )//SIGNAL
                  {
                     if(TakeProfit>0)  takeout=bid+TakeProfit*pips;
                        if(StopLoss>0)  stopout=bid-StopLoss*pips;
                           Execute.Sell(LotSize,NULL,bid,stopout,takeout,NULL);
                              passport=Execute.ResultOrder();
                              Print("SELL Opened");
                  }
          } 
//---
         if(OpenOrders()>0)
           {
            int dealtype=(int)PositionGetInteger(POSITION_TYPE);
                  if(dealtype==POSITION_TYPE_BUY)
                  if(rsiarray[0] < buylevel )
                    {
                     Execute.PositionClose(passport);
                        passport=0;
                    }
                    else if(dealtype==POSITION_TYPE_SELL)
                           if(rsiarray[0] > selllevel )
                             {
                              Execute.PositionClose(passport);
                                 passport=0;
                             }               
           }

  }

question from:https://stackoverflow.com/questions/65949362/mql5-function-for-selecting-open-orders-by-ticket-then-symbol-and-trade-direct

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think what you really after is this:

input double LotSize    =0.3;
input double Incriment  =0.01;
input int    StopLoss   =50;
input int    TakeProfit =100;
input int    Trend     =21;
input int    Momentum  =21;
input int    Strength  =13;
int adxlevel       =34;
int buylevel       =62;
int selllevel      =36;
//---------------------
double pips;
#include <TradeTrade.mqh>
CTrade Execute;
ulong StopLevel;
double ask, bid;
double takeout=0,stopout=0;
int moving,rsi,adx,Spread;
//+------------------------------------------------------------------+
int OnInit()
  {
//---
      double ticksize = SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   if(_Digits==3||_Digits==4)
      pips=ticksize*1000;
   else
      pips =ticksize;
//---
        moving=iMA(_Symbol,PERIOD_CURRENT,Trend,0,MODE_SMA,PRICE_CLOSE);
        rsi=iRSI(_Symbol,PERIOD_CURRENT,Momentum,PRICE_MEDIAN);
        adx=iADX(_Symbol,PERIOD_CURRENT,Strength);
//---
   Execute.SetExpertMagicNumber(0xCAFE); // INCLUDED
   Execute.SetAsyncMode(false); // CHANGED
   return(INIT_SUCCEEDED);
  }
int OpenPositions(ulong &passport)
  {
      int   BUYS=0,SELLS=0;
         for(int i=0; i<PositionsTotal(); i++)
            {
              ulong ticket=PositionGetTicket(i); // changed
               if(PositionSelectByTicket(ticket)==false) break;
                  int dealtype=(int)PositionGetInteger(POSITION_TYPE); // buy or sell
//                  string position_symbol=PositionGetString(POSITION_SYMBOL); // chart symbol
               if(Symbol()==PositionGetSymbol(i) && 0xCAFE==PositionGetInteger(POSITION_MAGIC)) // CHANGED
                  {
                     passport = ticket; // CHANGED
                     if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)  BUYS++;
                     if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) SELLS++; // CHANGED
                  }
            }      
//---       
      if(BUYS>0) return(BUYS);
      else       return(SELLS);
  }
//+------------------------------------------------------------------+
void OnTick()
  {
//---       
            MqlRates rates[3];
            double movingarray[],rsiarray[],adxarray[];
            CopyRates(_Symbol,PERIOD_CURRENT,1,3,rates);
            CopyBuffer(rsi,0,1,3,rsiarray);
            CopyBuffer(adx,0,1,3,adxarray);
            ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
            bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
            Spread=int(SymbolInfoInteger(_Symbol,SYMBOL_SPREAD));
            StopLevel=SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
//---
      ulong passport = 0; // CHANGED
      int positions = OpenPositions(passport); // CHANGED
      if(positions==0) // CHANGED
         {
               if(rates[0].open > moving )// CONDITION
               if(rsiarray[0] > buylevel && rsiarray[1] < buylevel )//SIGNAL
                  {
                     if(TakeProfit>0)  takeout=ask+TakeProfit*pips;
                     if(StopLoss>0)  stopout=ask-StopLoss*pips;
                     Execute.Buy(LotSize,NULL,ask,stopout,takeout,NULL);
                     Print("BUY Opened");
                  }
               if(rates[0].open < moving )//CONTITION
               if(rsiarray[0] < selllevel &&rsiarray[1] > selllevel  )//SIGNAL
                  {
                     if(TakeProfit>0)  takeout=bid+TakeProfit*pips;
                       if(StopLoss>0)  stopout=bid-StopLoss*pips;
                     Execute.Sell(LotSize,NULL,bid,stopout,takeout,NULL);
                     passport=Execute.ResultDeal(); // CHANGED
                     Print("SELL Opened");
                  }
          } 
         else if(positions>0) // CHANGED to prevent on the same tick do both
           {
            int dealtype=(int)PositionGetInteger(POSITION_TYPE);
                  if(dealtype==POSITION_TYPE_BUY)
                  if(rsiarray[0] < buylevel )
                    {
                     Execute.PositionClose(passport);
                    }
                    else if(dealtype==POSITION_TYPE_SELL)
                           if(rsiarray[0] > selllevel )
                             {
                              Execute.PositionClose(passport);
                             }               
           }
  }

I dont tested the changes. And it should be fine most part of time. It will only have problems when having delay to execute the Buy/Sell stuff.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...