forex software

Create and Test Forex Strategies

forex software

Skip to forum content

Forex Forum

Testing and Trading Forex Strategies

You are not logged in. Please login or register.


Forex Forum → Expert Advisor Modifications → Add TakeProfit in Percent in Portfolio?

Pages 1

You must login or register to post a reply

RSS topic feed

Posts: 5

Topic: Add TakeProfit in Percent in Portfolio?

Hi,

i like to mod my Portfolio Ea with a special function. I want to Close all trades if % profit reached.

I tried this out with a input amount and it is working. But how i have to change this when i want to say if 5% profit reached close all trades?

THE INPUT:

input bool                 useTakeProfitBasket        = true;                 // USE TAKEPROFIT BASKET
input double               TakeProfitBasket           = 100;  

ON ON TICK I ADD THIS:

void OnTick()
  {
    if(useTakeProfitBasket && (Open_Orders_Profit()>=TakeProfitBasket))
   {
      CloseAllPositions();
      Print("Take Profit Basket Reached. All Trades CLOSED!");
      printf("Take Profit Basket Reached. All Trades CLOSED!");
      Alert("Take Profit Basket Reached. All Trades CLOSED!");
      Comment("Take Profit Basket Reached. All Trades CLOSED!");
   }

and i have this function added to get Profit of open orders.

double Open_Orders_Profit()
{
   double openordersprofit = 0;
   RefreshRates();
   int Total=OrdersTotal();
   if(Total>0)
   {
      for(int i=Total-1; i>=0; i--)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==TRUE)
         {
            const int magicNumber = OrderMagicNumber();
            if(OrderSymbol()==Symbol() && (1000 * Base_Magic_Number <= magicNumber && magicNumber < (1000 * Base_Magic_Number + 100)))
            {
               openordersprofit +=OrderProfit()+OrderSwap()+OrderCommission();
            }
         }
      }
   }
   return(openordersprofit);
}

Can someone help me out to change this to have it in percent?

2 (edited by geektrader 2019-01-24 20:54:54)

Re: Add TakeProfit in Percent in Portfolio?

The code looks good so far, also great that you are doing "+OrderSwap()+OrderCommission()", most programmers do "-OrderSwap()-OrderCommission()", as they do not realize that the values are expressed negative by MT4 already. Good job :-)

As for your % based take profit, you somehow (before opening the next batch of trades) need to have a look at the AccountBalance() (https://docs.mql4.com/account/accountbalance), as you need to relate your percentage to the account balance in order to figure out when to close the trades. So your TakeProfitBasket would become something like this to express it in percent:

TakeProfitBasket = AccountBalance() * 0.06;

^ which then would make it take profit if the profit equals 6% of your current account balance. Of course you can make the % an input variable like this:

input double TakeProfitPercent = 6.0;

and then do:

TakeProfitBasket = AccountBalance() * TakeProfitPercent / 100;

Good luck!

Re: Add TakeProfit in Percent in Portfolio?

thanks i will try

Re: Add TakeProfit in Percent in Portfolio?

now its based on accountbalance. but what is when i use 5 different eas on one account? than this formula not working ?

Re: Add TakeProfit in Percent in Portfolio?

Sure it will work, as long as you only reference the EAs that belong to it when closing positions.

Posts: 5

Pages 1

You must login or register to post a reply

Forex Forum → Expert Advisor Modifications → Add TakeProfit in Percent in Portfolio?

Similar topics in this forum