Aide LotSize automatique

 

Bonjour, je suis débutant en programmation et je code un EA fonctionnant autour du point pivot. Ma problématique est que je n'arrive pas à faire un code qui permette de calculer automatiquement la taille du lot en fonction de la distance du SL et du risque par trade. Je veux un risque réglable de 2 pourcent et surtout arriver à calculer le lot avec le sl qui est la distance entre le point d'ouverture du trade et le point pivot.

Voici mon code actuel:


//+------------------------------------------------------------------+
//|                                           PointPivotBreakOut.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int MagicNumber = 1234;
int bougie = 0;
double Pips;
extern double LotSize = 1;

int OnInit()
  {
//---
double TickSize = MarketInfo (Symbol(),MODE_TICKSIZE);
if (TickSize == 0.00001 || TickSize == 0.001 )
Pips = TickSize * 10 ;
else Pips = TickSize ;
 
//---
   return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
      double pivot;
      double S1;
      double R1;
      double high = iHigh(_Symbol,PERIOD_M30,1);
      double low = iLow(_Symbol,PERIOD_M30,1);
      double close = iClose(_Symbol,PERIOD_M30,1);
      
      pivot = ((high+low+close)/3);
      R1 = ((2*pivot)-low);
      S1 = ((2*pivot)-high);
      
      double StopLossB = ((Ask)-(Ask-((high+low+close)/3)));
      double TakeProfitB = ((Ask)+(Ask-((high+low+close)/3)));
      double StopLossS = ((Bid)+(((high+low+close)/3)-Bid));
      double TakeProfitS = ((Bid)-(((high+low+close)/3)-Bid));
      
/*      double tickSize = MarketInfo(_Symbol,MODE_TICKSIZE);
      double tickValue = MarketInfo(_Symbol,MODE_TICKVALUE);
      double point = MarketInfo(_Symbol,MODE_POINT);
      double ticksPerPoint = tickSize/point;
      double pointValue = tickValue/ticksPerPoint;*/
      
      double riskAmount = (AccountBalance()/100)*1;
      double riskPointsBuy = Ask-((high+low+close)/3);
      double riskPointsSell = ((high+low+close)/3)-Bid;
      double riskLotsBuy = riskAmount / riskPointsBuy;
      double riskLotsSell = riskAmount / riskPointsSell;
    PrintFormat("slbuy %f et slsell %f", riskPointsBuy, riskPointsSell);
      
ObjectCreate(_Symbol,"PivotPoint", OBJ_HLINE,0,0,pivot);
ObjectCreate(_Symbol,"Resistance1", OBJ_HLINE,0,0,R1);
ObjectCreate(_Symbol,"Support1", OBJ_HLINE,0,0,S1);

ObjectSetDouble(0,"PivotPoint",OBJPROP_PRICE,pivot);
ObjectSetDouble(0,"Resistance1",OBJPROP_PRICE,R1);
ObjectSetDouble(0,"Support1",OBJPROP_PRICE,S1);

ObjectSetInteger(0,"PivotPoint", OBJPROP_COLOR,clrYellow);
ObjectSetInteger(0,"Resistance1", OBJPROP_COLOR,clrGreen);
ObjectSetInteger(0,"Support1", OBJPROP_COLOR,clrRed);

ObjectSetInteger(0,"PivotPoint",OBJPROP_WIDTH,3);
ObjectSetInteger(0,"Resistance1",OBJPROP_WIDTH,2);
ObjectSetInteger(0,"Support1",OBJPROP_WIDTH,2);
   
   //Vérification pour achat
   if (OrdersTotal()==0 && Bars>bougie)
   {
         if (Close[0]>R1&&Open[0]<R1)
         {
               int buyticket = OrderSend(_Symbol,OP_BUY,riskLotsBuy,Ask,3,StopLossB,TakeProfitB,NULL,MagicNumber,0,Green);
               bougie=Bars;    
         }   
   }
  //Vérification pour vente
   if (OrdersTotal()==0 && Bars>bougie)
   {
         if (Close[0]<S1&&Open[0]>S1)
         {
               int sellticket = OrderSend(_Symbol,OP_SELL,riskLotsSell,Bid,3,StopLossS,TakeProfitS,NULL,MagicNumber,0,Red); 
               bougie=Bars; 
         } 
   } 
}



Merci d'avance

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2022.07.25
  • 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
 

Bonjour,

Ces exemples pourraient être utiles :

https://www.mql5.com/fr/code/17199

https://www.mql5.com/fr/code/19870

Money Fixed Risk
  • www.mql5.com
An example for calculating the lot value in accordance with the risk per trade.
Raison: