Calculation Pips

 

Hallo Freunde,


ich hab da mal was getestet, bitte keine Diskussion ob Sinnvoll oder nicht, ich hab nur probiert über ein einfaches Grid System Order zu eröffnen,

jetzt hab ich das mal laufen lassen und bin verwundert, warum mir der Debugger diesen Wert anzeigt.


anbei mal der Code, sollte so funktionieren

input double Lots=0.01;

input int Grid=5000;

input int Profit=5000;

input string Text="Grid_EA"; // Text des EA
input int    MN=123456;         // MN des EA

int Slip=100;                  // maximaler Slip den Eine Order bei der ausführung haben darf


int TrailingStopOffset=0;

double ShortMA[];
int   ShortMAHandle=0;
input int ShortMAPeriode=10;
input ENUM_APPLIED_PRICE Price_Short_MA=PRICE_WEIGHTED;
input ENUM_MA_METHOD Methode_Short_MA=MODE_LWMA;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ShortMAHandle=iMA(_Symbol,PERIOD_CURRENT,ShortMAPeriode,0,Methode_Short_MA,Price_Short_MA);
   ArraySetAsSeries(ShortMA,true);

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

   CopyBuffer(ShortMAHandle,0,0,20,ShortMA);

   MqlTick tick;
   if(!SymbolInfoTick(Symbol(),tick)) GetLastError();


   if(!PositionSelect(_Symbol)
      && tick.bid>ShortMA[0]
      )

     {
      OrderAsync(_Symbol,ORDER_TYPE_BUY,Lots,0,0,Text,MN);
     }

   int total=PositionsTotal();

   if(total>0)
     {

      

      if(CountPipsLong()/total<Profit*-1)
        {
        double PipsLong=CountPipsLong();
         OrderAsync(_Symbol,ORDER_TYPE_BUY,Lots,0,0,Text,MN);
         Sleep(1500);
        }

      else if(CountPipsLong()>Profit) CloseAllSymbol(_Symbol,Slip);

     }



  }
//+------------------------------------------------------------------+

double CountPipsLong()
  {
   int total=PositionsTotal();
   double Pips=0;
   if(total>0)
     {
      for(int j=total-1;j>=0;j--)
        {

         PositionSelect(_Symbol);
         double Price_open = PositionGetDouble(POSITION_PRICE_OPEN);
         double Price_curr = PositionGetDouble(POSITION_PRICE_CURRENT);

         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
           {
            Pips+=Price_curr-Price_open;
           }

        }
     }

   Print(Pips);
   return(Pips);

  }
//+------------------------------------------------------------------+

void OrderAsync(const string symbol,const ENUM_ORDER_TYPE order_type,const double volume,const double StopLoss,const double TakeProfit,const string text,const int Magic
                )
  {

   MqlTradeRequest neueOrder={0};
   MqlTradeResult result;
//--- setting request
   neueOrder.action       = TRADE_ACTION_DEAL;
   neueOrder.symbol       = symbol;
   neueOrder.type         = order_type;
   neueOrder.type_filling = ORDER_FILLING_FOK;
   neueOrder.volume       = volume;
   neueOrder.sl           = StopLoss;
   neueOrder.tp           = TakeProfit;
   neueOrder.magic        = Magic;
   neueOrder.comment      = text;

//--- action and return the result
//return(OrderSendAsync(neueOrder,result));
   if(!OrderSend(neueOrder,result))
      PrintFormat("OrderSend error %d",GetLastError());  // wenn die Anfrage konnte nicht gesendet werden, den Fehlercode anzeigen
//--- Details zur Transaktion   
   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);

  }
  

void CloseAllSymbol(const string symbol,const int slip)
  {

   MqlTradeRequest request={0};
   MqlTradeResult  result={0};

   int total=PositionsTotal(); // number of open positions   
//--- iterate over all open positions

   for(int i=total-1; i>=0; i--)
     {
      //--- parameters of the order
      ulong  position_ticket=PositionGetTicket(i);                                      // ticket of the position
      string position_symbol=PositionGetString(POSITION_SYMBOL);                        // symbol 
      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS);              // number of decimal places
      ulong  magicnumber=PositionGetInteger(POSITION_MAGIC);                                  // MagicNumber of the position
      double volume=PositionGetDouble(POSITION_VOLUME);                                 // volume of the position
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);    // type of the position
      //--- output information about the position
      PrintFormat("#%I64u %s  %s  %.2f  %s [%I64d]",
                  position_ticket,
                  position_symbol,
                  EnumToString(type),
                  volume,
                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
                  magicnumber);

      if(position_symbol==symbol)
        {

         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         request.action   =TRADE_ACTION_DEAL;        // type of trade operation
         request.position =position_ticket;          // ticket of the position
         request.symbol   =position_symbol;          // symbol 
         request.volume   =volume;                   // volume of the position
         request.deviation=slip;                        // allowed deviation from the price
         request.magic=magicnumber;             // MagicNumber of the position
         //--- set the price and order type depending on the position type

         if(type==POSITION_TYPE_BUY)
           {
            request.price=SymbolInfoDouble(position_symbol,SYMBOL_BID);
            request.type =ORDER_TYPE_SELL;
           }
         else
           {
            request.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
            request.type =ORDER_TYPE_BUY;
           }
         //--- output information about the closure
         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation   
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
         //--- 

        }
     }
  }


dazu bekomme ich im Debugger folgenden Wert raus


und über die Print Ausgabe krieg ich folgenden Wert als letztes im Journal des graphischen Interfaces



der Unterschied macht aus, das er immer mehrere Orders nachsetzt.

btw, ich teste das am Dow30


bediene ich den debugger falsch?

ich habe den Haltepunkt hier stehen




ACHTUNG: Zeilennummer ein wenig verschoben da ich den Header weggelassen habe


kann mir hier jemand sagen was ich falsch mache? ausser 2 Glas rotwein trinken beim proggen?


danke

amando

 

Ehrlich gesagt ich finde es komisch, erst nach PositionsTotal() durch alle Positionen zu iterieren, dann aber mit PositionSelect(_Symbol) nicht eine sondern alles des Symbols auszuwählen - ich werde fragen, ob das beabsichtigt ist.

Versuch doch einmal statt PositionSelect(_Symbol) einfach PositionGetTicket(index); - Beispiel:

   int p = PositionsTotal();
   while(p-->0) { // returns the number of open positions
      ulong ticket=PositionGetTicket(p);
 
Carl Schreiber:

Ehrlich gesagt ich finde es komisch, erst nach PositionsTotal() durch alle Positionen zu iterieren, dann aber mit PositionSelect(_Symbol) nicht eine sondern alles des Symbols auszuwählen - ich werde fragen, ob das beabsichtigt ist.

Versuch doch einmal statt PositionSelect(_Symbol) einfach PositionGetTicket(index); - Beispiel:

Hallo Carl,


nun ja, ich will ja von alle offenen Positionen eines Symbols die Pips im Profit oder im Negativen akkumulieren.

was ich nicht verstehe, das er über Print 50 ausgibt und im Debugger 6.xxE-313, was ja als Wert quasi nix ist


lg

 
In der Referenz (F1) steht zu PositionSelect(): "Bei einer unabhängigen Verrechnung von Positionen (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING) können gleichzeitig mehrere Positionen pro Symbol vorhanden sein. In diesem Fall wählt PositionSelect die Funktion mit dem kleinsten Ticket."
 
Carl Schreiber:
In der Referenz (F1) steht zu PositionSelect(): "Bei einer unabhängigen Verrechnung von Positionen (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING) können gleichzeitig mehrere Positionen pro Symbol vorhanden sein. In diesem Fall wählt PositionSelect die Funktion mit dem kleinsten Ticket."

Darum hab ich ja die vor schleife über positions total, das ich die im live betrieb noch mehr filtern musss ist klar, wie gesagt, das kommt beim backtest raus

 
PositionSelect() liefert Dir aber dann immer nur ein und dieselbe Position! Ist gedacht für Netting-Konten, da gibt es je Symbol immer nur eine Position!
 
Carl Schreiber:
PositionSelect() liefert Dir aber dann immer nur ein und dieselbe Position! Ist gedacht für Netting-Konten, da gibt es je Symbol immer nur eine Position!

Stimmt, aber das ist noch kein Grund für das ergebnis im debugger. Im print stimmt es ja, ich geb dir recht, ich müsste nich mit dem PositionSelectByTicket die Position bestimmen, aber das ist noch immer kein Grund für e^-313

Grund der Beschwerde: