I Will Code Your Strategy For Free - page 2

 
Yasar Sari #: and there will be no Stop level.

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.

  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
              Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

  4. You must normalize lots properly and check against min and max.

  5. You must also check Free Margin to avoid stop out

  6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 

First of all, Happy New Year To all!

Now, i am sorry if i am slow with my responses, but you guys must understand that i do this in my spare time.

also, please try to be specific with your requirements in plain english, i usually wont be checking out links or videos.

Yasar Sari's requirements are what i am looking for, explained in plain english and understandable.

i will be working on it and share the results here in a couple of days.


in all, i WILL respond, even if i may be slow. just bear with me. thanks

 
Yasar Sari #:

I would be very happy if you could do this.


hi,

yes, i will work on your requirement. i understand it i think, and hopefully share a code in a couple of days. kindly bear with me 

 
FxL.I.O.N #:

hi,

yes, i will work on your requirement. i understand it i think, and hopefully share a code in a couple of days. kindly bear with me 

Thanks, I'm waiting

 
Yasar Sari #:

Thanks, I'm waiting

heres the code u need


//include the trade Library
#include <Trade\Trade.mqh>


input int                              starting_TP                   =  500;     //50 point tp for first position
input int                              next_TP                       =  750;     //75 point tp for subsequent positions
input int                              minimum_Drop_inPrice          =  500;     //50 point drop for second pos
input double                           starting_lot                  =  0.1;     //starting lot size
input double                           lot_multiplier                =  1.1;     //multiplier for lot size for new positions

input double                           min_Equity_to_maintain        =  1;       // if equity falls below this, ea will terminate


double starting_Equity, current_Equity;


//--- Service Variables (Only accessible from the MetaEditor)
CTrade myTradingControlPanel;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
   starting_Equity = AccountInfoDouble(ACCOUNT_EQUITY);
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
   
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---
   
   
   //Close EA if equity falls Below user input levels
   current_Equity = AccountInfoDouble(ACCOUNT_EQUITY);
   if(current_Equity < min_Equity_to_maintain)
   {
      Alert("Equity has fallen below critical, closing EA");
      //CloseByAllOpenOrders();
      ExpertRemove();
   }
//END OF IMPORTANT CHECKS EACH TICK
//-------------------------------------------------------------------------------
//MAIN EA CODE BEGINS HERE
//---------------------------------------------------------------------------   
   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   
   //if we have no open position or order      
   int total_OpenPositions = GetTotalOpenPositions();
   if(total_OpenPositions == 0)
   {
      OpenABuyPosition(starting_lot,0,Ask + (starting_TP * _Point));
   }//end of zero open positions
   else // we have open positions
   {
      //find latest position
      ulong latest_ticket = GetLatestTicketNumber();
      
      if(PositionSelectByTicket(latest_ticket) == true)
      {
         double position_open_price = PositionGetDouble(POSITION_PRICE_OPEN);
         double position_vol = PositionGetDouble(POSITION_VOLUME);
         
         if(Ask < (position_open_price - (minimum_Drop_inPrice*_Point))) //price drops by required amount
         {
            double lot = NormalizeDouble((position_vol*lot_multiplier),_Digits);
            OpenABuyPosition(lot,0,Ask + (next_TP * _Point));
         }
         
      }
   }
   
}
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//| Get Open Positions total function                                  |
//+------------------------------------------------------------------+
int GetTotalOpenPositions()
{
   int totalActiveOrders = 0;
   
   for(int i=PositionsTotal()-1; i>=0; i--) // Loop through all open positions (including those not opened by EA)
   {
      ulong ticketNumber = 0;
      
      if(PositionGetSymbol(i) == Symbol())//matches the symbol and selects the position for further processing
      {
         ticketNumber = PositionGetTicket(i); // This selects open position by the order they appear in our list of positions
         if(ticketNumber == 0)
         {
            Print("Position exists but not selected. Error: ", GetLastError());  
            ResetLastError();
            continue;
         }//end of if ticket is 0 
         else
         //if(PositionGetInteger(POSITION_MAGIC) == magicNumber)
         {
            totalActiveOrders++;
         }//end of if magic number matches         
      }//end of symbol matching      
   }//end of for loop
   
   return totalActiveOrders;
}//end of function
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Get Current Profit / Loss function                               |
//+------------------------------------------------------------------+
double GetCurrentProfitLoss()
{
   double totalprofitLoss = 0;
   
   for(int i=PositionsTotal()-1; i>=0; i--) // Loop through all open positions (including those not opened by EA)
   {
      ulong ticketNumber = 0;
      
      if(PositionGetSymbol(i) == Symbol())//matches the symbol and selects the position for further processing
      {
         ticketNumber = PositionGetTicket(i); // This selects open position by the order they appear in our list of positions
         if(ticketNumber == 0)
         {
            Print("Position exists but not selected. Error: ", GetLastError());  
            ResetLastError();
            continue;
         }//end of if ticket is 0 
         else
         //if(PositionGetInteger(POSITION_MAGIC) == magicNumber)
         {
            totalprofitLoss = totalprofitLoss + PositionGetDouble(POSITION_PROFIT);
         }//end of if magic number matches         
      }//end of symbol matching      
   }//end of for loop
   
   return totalprofitLoss;
}//end of function
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Get Latest Position ticket number function                       |
//+------------------------------------------------------------------+
ulong GetLatestTicketNumber()
{
   ulong LatestTicket = 0;
   long latestPositionTime = 0;
   
   for(int i=PositionsTotal()-1; i>=0; i--) // Loop through all open positions (including those not opened by EA)
   {
      ulong ticketNumber = 0;
      
      
      if(PositionGetSymbol(i) == Symbol())//matches the symbol and selects the position for further processing
      {
         ticketNumber = PositionGetTicket(i); // This selects open position by the order they appear in our list of positions
         if(ticketNumber == 0)
         {
            Print("Position exists but not selected. Error: ", GetLastError());  
            ResetLastError();
            continue;
         }//end of if ticket is 0 
         else
         //if(PositionGetInteger(POSITION_MAGIC) == magicNumber)
         {
            if(PositionGetInteger(POSITION_TIME) > latestPositionTime)
            {
               LatestTicket = ticketNumber;
               latestPositionTime = PositionGetInteger(POSITION_TIME);               
            }
             
         }//end of if magic number matches         
      }//end of symbol matching      
   }//end of for loop
   
   return LatestTicket;
}//end of function
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Open a buy position function                                     | 
//+------------------------------------------------------------------+

void OpenABuyPosition(double LotSize, double StopLoss, double TakeProfit)
{
   double posLot = LotSize;
   double posSL = StopLoss;
   double posTP = TakeProfit;
   
   int tries = 5;
   while(tries > 0) // Loop to reattempt opening positions if the initial attempt failed
   {
      myTradingControlPanel.PositionOpen(Symbol(),ORDER_TYPE_BUY,posLot,SymbolInfoDouble(Symbol(),SYMBOL_ASK),posSL,posTP,NULL); // Open a Buy (i.e. long) position
      
      if(myTradingControlPanel.ResultRetcode()==TRADE_RETCODE_PLACED || myTradingControlPanel.ResultRetcode()==TRADE_RETCODE_DONE) // Request is completed or order placed
      {
         tries = 0;
         //position_Taken_on_Signal = true;
         Print(" Buy order has been successfully placed with Ticket#: ",myTradingControlPanel.ResultOrder());                  
      }
      else
      {
         if(tries > 0) // If we are here, it means the position was not open.
         {
            tries--;
            Alert("Retrying to open buy position as requested");
            Sleep(250); // Wait for 250 milliseconds before reattempting to open the position
         }
         else
         {
            Alert(" Buy order request could not be completed. Error: ",GetLastError());
            ResetLastError();
         }
      }    
   }
   
}//end of open buy function
//+------------------------------------------------------------------+

since u r not using any stop loss, i have included an overall equity fall value to terminate the ea. u can set it as u like, i made it user input.

when terminating, the ea will leave positions as it is, u will have to handle them manually.

also, if u open a manual position, that position will become the latest position and any subsequent positions will be opened from there.

also, i have made tp for first position and subsequent positions a user input so u can adjust them as u like.

same goes for price drop for new positions, u can modify that too

i have included an executable file for your testing.


settings can be changed while running the ea, but new settings will take effect on new positions not already opened positions.


let me know if u like it or if u want some changes.

i wont be able to share the code for the whole executable as it contains a lot of commercial code as well, but i have shared the code pertaining to your needs above.

u would just need to address the  GetTotalOpenPositions(), OpenABuyPosition(double lotSize, double SL, double TP) and GetLatestTicketNumber() functions.


there can be improvements to this, but i just coded exactly what u wanted for now.

tests shows it to work just like you wrote, but i will take ur word on it


<ex5 file deleted>


 
FxL.I.O.N #: heres the code u need...

Please remember the requirements for the existence of this thread as outlined by a moderator. You should supply only open source code files and not executable files.

Forum on trading, automated trading systems and testing trading strategies

I Will Code Your Strategy For Free

Keith Watford, 2023.01.03 12:37

Please note that this is an open forum so requirements should be posted here and if you code anything you should also attach the source code here.

Also, you should post the code either as an attached file or by using code functionality that posting offers. So, please edit your post (don't create a new post) and replace your code properly (with "</>" or Alt-S), or attach the original file directly with the "+ Attach file" button below the text box.

 
i need to code an ea can you do it for me
Files:
candle_ea_1.zip  146 kb
 
FxL.I.O.N #:

Please take note of Fernando's comments above.

Post code properly and only post code that will compile.

If you are not willing to post/attach the complete source code, then this thread is not in the spirit of the forum.

I will wait for the moment and see what your response is, but I will likely delete this thread.

 
Keith Watford #:

Please take note of Fernando's comments above.

Post code properly and only post code that will compile.

If you are not willing to post/attach the complete source code, then this thread is not in the spirit of the forum.

I will wait for the moment and see what your response is, but I will likely delete this thread.

ok, i cannot possibly share the original code, but i will rewrite the missing functions and edit my response when i am done. could take a bit of time so please be patient.

in the mean time, can you direct me to a forum where i can share my work as executables instead of source code. I mean, i dont mind sharing the code which i write myself, like i did above, but since i use a main template file to create my ea's, i cannot possibly share the entire code. That would be like giving away my lifes work for free.

so i WILL be sharing the new code that i add to make the request possible, but not the entire code from my template.

but yes, if i create an entire code from scratch, i will definitely share it in full.

the above code is pretty simple, so i wont mind coding the missing functions separately and making it compilable, but u must understand, it wont always be possible.

if there is another section here which would cater to my needs, i would be obliged to be directed there. i need the exercise to improve my skills afterall.

thanks in advance.

 
FxL.I.O.N #:

ok, i cannot possibly share the original code, but i will rewrite the missing functions and edit my response when i am done. could take a bit of time so please be patient.

in the mean time, can you direct me to a forum where i can share my work as executables instead of source code. I mean, i dont mind sharing the code which i write myself, like i did above, but since i use a main template file to create my ea's, i cannot possibly share the entire code. That would be like giving away my lifes work for free.

so i WILL be sharing the new code that i add to make the request possible, but not the entire code from my template.

I cannot direct you to another forum.

This thread is already being treated with suspicion by some moderators. This type of thread is often used by developers to get a list of people who they then send a PM offering the coding in exchange for money. This may or may not be the case here but if anyone reports such spam by PM the developer can expect to be banned.

Reason: