In the logic of the averaging down martingale EA, the settings of the averaging down range and martingale ratio are important, but another key point is when to take profits when unrealized gains occur (how much to let profits grow).
The smaller the profit-taking range, the earlier the profit-taking timing, and the less risk of holding too many positions. However, the profit per trade will be smaller, increasing the rate at which profits are gradually accumulated.
On the other hand, if the profit-taking range is larger, the profit per trade will be larger, but it will be harder to take profits and the risk of holding too many positions will increase.
In this article, we will introduce how to set the profit-taking range for the averaging down martingale EA.
EA Base Model
In this case, we will call the EA created in the following article the “base model“, and add functionality to it.
This article covers creating the simplest averaging down martingale EA from scratch, so if you haven’t seen it yet, please check it out.
Setting Input
In the base model, the following input items were set:
input double first_lot = 0.01;//Initial lot
input double nanpin_range = 200;//Averaging down range
input double profit_target = 1.00;//Profit target
input int magic_number = 10001; //Magic number
static int ticket_number;//Ticket number
double slippage = 10;//Slippage
Now, let’s add input items for the profit-taking pattern.
input int close_type = 1;//Profit-taking pattern (1: position proportional, 2: lot proportional, 3: always fixed)
Profit-taking Pattern (close_type)
Set a variable to switch between different settings for how to determine the close.
In this case,
- Position proportional
- Lot proportional
- Always fixed
We have set up three patterns to choose from.
Loop Processing
With the necessary additional processing done beforehand, we now move on to adding functionality within the loop processing.
void OnTick()
void OnTick()
{
From this point on, the content will be added within the void OnTick().
Variable Definitions
In the base model, the following variables were defined:
int cnt;
int current_buy_position;//Latest buy position
int current_sell_position;//Latest sell position
int buy_position;//Number of buy positions
int sell_position;//Number of sell positions
double buy_profit;//Unrealized profit/loss of buy positions
double sell_profit;//Unrealized profit/loss of sell positions
To this, we add the following two definitions:
double buy_lot;//Total buy lot quantity
double sell_lot;//Total sell lot quantity
buy_lot, sell_lot
The buy_lot variable is used to record the total lot quantity of held buy positions. For example, if there are three buy positions, it means the total lot quantity of the first to third buy positions. The same goes for sell_lot.
Checking Positions
In the process of checking positions, we will also check the total lot quantity.
Before Adding Functionality
In the base model, it was as follows:
buy_position=0;//Initialization of the number of buy positions
sell_position=0;//Initialization of the number of sell positions
current_buy_position=-1;//Initialization of the latest buy position
current_sell_position=-1;//Initialization of the latest sell position
for(cnt=0;cnt<OrdersTotal();cnt++)//Checking positions
{
if(OrderSelect(cnt,SELECT_BY_POS)==false)continue;
if(OrderMagicNumber()!=magic_number)continue;
if(OrderType()==OP_BUY)
{
current_buy_position=cnt;
buy_position+=1;
buy_profit=buy_profit+OrderProfit();
};//Checking buy positions
if(OrderType()==OP_SELL)
{
current_sell_position=cnt;
sell_position+=1;
sell_profit=sell_profit+OrderProfit();
};//Checking sell positions
}
After Adding Functionality
We will modify it as follows. (Lines with “←Added this time” are the added parts.)
buy_position=0;//Initialization of the number of buy positions
sell_position=0;//Initialization of the number of sell positions
current_buy_position=-1;//Initialization of the latest buy position
current_sell_position=-1;//Initialization of the latest sell position
buy_lot=0;//Initialization of the total lot quantity of buy positions ←Added this time
sell_lot=0;//Initialization of the total lot quantity of sell positions ←Added this time
for(cnt=0;cnt<OrdersTotal();cnt++)//Checking positions
{
if(OrderSelect(cnt,SELECT_BY_POS)==false)continue;
if(OrderMagicNumber()!=magic_number)continue;
if(OrderType()==OP_BUY)
{
current_buy_position=cnt;
buy_position+=1;
buy_profit=buy_profit+OrderProfit();
buy_lot=buy_lot+OrderLots(); //←Added this time
};//Checking buy positions
if(OrderType()==OP_SELL)
{
current_sell_position=cnt;
sell_position+=1;
sell_profit=sell_profit+OrderProfit();
sell_lot=sell_lot+OrderLots(); //←Added this time
};//Checking sell positions
}
To briefly explain the added parts, it checks all positions in order and,
if it is a buy position, add the latest buy position’s lot quantity to buy_lot.if it is a sell position, add the latest
Position Close Conditions
Proportional to Positions
The close condition in the base model is represented by the following code:
if(buy_position>0&&buy_profit>profit_target*buy_position)
This condition is met when the following two conditions are satisfied simultaneously:
- Having at least one buy position
- Total unrealized profit exceeds profit target × number of buy positions
For example, if you have 3 buy positions and set the profit target (profit_target) to 1.00 USD, you will close the order when the total unrealized profit of the buy positions exceeds 3.00 USD, which is 3 times 1.00 USD.
Proportional to Lots
Let’s change the profit-taking pattern to be proportional to lots instead of the number of positions.
if(buy_position>0&&buy_profit>profit_target*buy_lot/first_lot)
This condition is met when the following two conditions are satisfied simultaneously:
- Having at least one buy position
- Total unrealized profit exceeds profit target × total lot quantity of buy positions ÷ initial lot quantity
For example, if you have 3 buy positions of 0.01 lot, 0.02 lot, and 0.03 lot (total of 0.06 lot), and set the profit target (profit_target) to 1.00 USD, you will close the order when the total unrealized profit of the buy positions exceeds 6.00 USD, which is 6 times 1.00 USD. This is a profit-taking pattern that seeks more profit than the position-proportional type.
Always Fixed
Of course, it is also possible to have a pattern where the profit target is always fixed.
if(buy_position>0&&buy_profit>profit_target)
This condition is met when the following two conditions are satisfied simultaneously:
- Having at least one buy position
- Total unrealized profit exceeds the profit target
For example, if you have 3 buy positions or 5 buy positions, and set the profit target (profit_target) to 1.00 USD, you will close the order when the total unrealized profit of the buy positions exceeds 1.00 USD. This is a conservative approach that always aims for a constant profit, regardless of how many positions are accumulated. However, the profit will not grow as much as in other methods.
After Adding the Feature
After adding the feature, include the close_type condition as shown below.
if(buy_position>0&&buy_profit>profit_target*buy_position&&close_type==1) //Proportional to positions
{
buyClose(Red);//Close all buy positions
}
if(buy_position>0&&buy_profit>profit_target*buy_lot/first_lot&&close_type==2) //Proportional to lots
{
buyClose(Red);//Close all buy positions
}
if(buy_position>0&&buy_profit>profit_target&&close_type==3) //Always fixed
{
buyClose(Red);//Close all buy positions
}
if(sell_position>0&&sell_profit>profit_target*sell_position&&close_type==1) //Proportional to positions
{
sellClose(Blue);//Close all sell positions
}
if(sell_position>0&&sell_profit>profit_target*sell_lot/first_lot&&close_type==2) //Proportional to lots
{
sellClose(Blue);//Close all sell positions
}
if(sell_position>0&&sell_profit>profit_target&&close_type==3) //Always fixed
{
sellClose(Blue);//Close all sell positions
}
Summary
With this, the addition of the profit-taking pattern change feature is complete.
Other EA Creation Tutorials
Adding Trading Time Restrictions and Forced Close Features
Introducing the Martingale System and Changing the Martingale Multiplier
Creating an FX Automated Trading Tool (bot) with Python
We also introduce a method for creating an FX automated trading tool (bot) using Python.





Comments