You need to define a variable. I call it latest_balance_snapshot. Every time you reach target you update this variable... Not to miss its value when reloading EA it is best to save this into a file. Not that simple.
Yashar Seyyedin #:
You need to define a variable. I call it latest_balance_snapshot. Every time you reach target you update this variable... Not to miss its value when reloading EA it is best to save this into a file. Not that simple.
Sorry, i did not understand. Can you explain or give some example code. Ones the if statement get executed, it should redefined the value to static double target_Capital based on new Account Balance.
You need to define a variable. I call it latest_balance_snapshot. Every time you reach target you update this variable... Not to miss its value when reloading EA it is best to save this into a file. Not that simple.
anuj71 #:
Sorry, i did not understand. Can you explain or give some example code. Ones the if statement get executed, it should redefined the value to static double target_Capital based on new Account Balance.
Sorry, i did not understand. Can you explain or give some example code. Ones the if statement get executed, it should redefined the value to static double target_Capital based on new Account Balance.
I am little aware of static variables. This is how I do it:
input double equityGrowthPercentage = 2; // Growth by percent double latest_balance_snapshot=0; int OnInit() { latest_balance_snapshot = AccountBalance(); return(INIT_SUCCEEDED); } void OnTick() { double target=(1+equityGrowthPercentage/100)*latest_balance_snapshot; if(AccountEquity()>=target) CloseAllPositions(); } void CloseAllPositions() { int total = OrdersTotal(); for (int i = total - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS)==false) continue; if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE) == false) Print("Order close failed for ticket ", OrderTicket()); } latest_balance_snapshot = AccountBalance(); }
Thanks for the code. Yes, this will work great. except few instance like changing Chart Period will reset the Account latest_balance_snapshot or Saturday/Sunday EA usually stop due to Market closed and reopened on Monday, if it deinit and reinit on market close and open than that time also make problem.
Yashar Seyyedin #:
extern string inc_MoneyManagement = "***Money Management***"; extern bool Money_Management = FALSE; enum MoneyManagement_enum {Fixed_Money, Balance_Percentage}; extern MoneyManagement_enum MoneyManagement_Type = Balance_Percentage; extern double Target_Revenue = 1000.0; double gTargeted_Revenue; int OnInit() { if(MoneyManagement_Type == Balance_Percentage) gTargeted_Revenue = (1 + Target_Revenue/100) * AccountBalance(); else gTargeted_Revenue = Target_Revenue + AccountBalance(); } void OnTick() { if(Money_Management && AccountEquity() >= gTargeted_Revenue) { for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS)==false) continue; if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE) == false) Print("Order close failed for ticket ", OrderTicket()); } Print("All The Trade Closed due to it exceeded defined Money Management (Target Revenue) : " + AccountEquity()); if(MoneyManagement_Type == Balance_Percentage) gTargeted_Revenue = (1 + Target_Revenue/100) * AccountBalance(); else gTargeted_Revenue = Target_Revenue + AccountBalance(); } }
Like this?
You may save the latest balance in a file so it will remain there even if you restart system.
Yashar Seyyedin #:
You may save the latest balance in a file so it will remain there even if you restart system.
You may save the latest balance in a file so it will remain there even if you restart system.
Like this. When i open the .txt file. it is empty.
I am getting Error 5011.
extern string inc_MoneyManagement = "***Money Management***"; extern bool Money_Management = FALSE; enum MoneyManagement_enum {Fixed_Money, Balance_Percentage}; extern MoneyManagement_enum MoneyManagement_Type = Balance_Percentage; extern double Target_Revenue = 1000.0; double gTargeted_Revenue; // File to store gTargeted_Revenue value string FileName = _Symbol+"targeted_revenue.txt"; int OnInit() { // Load gTargeted_Revenue value from file if it exists int fileHandle = FileOpen(FileName, FILE_READ); if (fileHandle != INVALID_HANDLE) { gTargeted_Revenue = FileReadDouble(fileHandle); FileClose(fileHandle); Print("Loaded gTargeted_Revenue from file: ", gTargeted_Revenue); } else { // Create a new file and calculate gTargeted_Revenue ResetLastError(); fileHandle = FileOpen(FileName, FILE_WRITE); if (fileHandle != INVALID_HANDLE) { if(MoneyManagement_Type == Balance_Percentage) gTargeted_Revenue = (1 + Target_Revenue/100) * AccountBalance(); else gTargeted_Revenue = Target_Revenue + AccountBalance(); FileWriteDouble(fileHandle, gTargeted_Revenue); FileClose(fileHandle); Print("Created new file and wrote gTargeted_Revenue: ", gTargeted_Revenue); Print(" File creation Error : " + GetLastError()); } else { Print("Failed to create file: ", FileName); // Handle file creation failure here } } } void OnTick() { if(Money_Management && AccountEquity() >= gTargeted_Revenue) { for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS)==false) continue; if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE) == false) Print("Order close failed for ticket ", OrderTicket()); } Print("All The Trade Closed due to it exceeded defined Money Management (Target Revenue) : " + AccountEquity()); // Update gTargeted_Revenue value if(MoneyManagement_Type == Balance_Percentage) gTargeted_Revenue = (1 + Target_Revenue/100) * AccountBalance(); else gTargeted_Revenue = Target_Revenue + AccountBalance(); // Save gTargeted_Revenue value to file int fileHandle = FileOpen(FileName, FILE_WRITE); if (fileHandle != INVALID_HANDLE) { FileWriteDouble(fileHandle, gTargeted_Revenue); FileClose(fileHandle); } else { Print("Failed to open file for writing: ", FileName); // Handle file writing failure here } } }
I don't think even a global variable is needed here
#property strict input double inpTargetPercent = 10.0; void OnTick() { double balance = AccountBalance(); double equity = AccountEquity(); double profitCurrency = equity - balance; // Can be replaced by the total profit of all orders of this EA double profitPercent = profitCurrency / balance * 100.0; string text = profitPercent < inpTargetPercent ? "Target profit not yet reached" : "Target profit reached"; Comment(text + StringFormat(": (%.2f%%/%.2f%%)", profitPercent, inpTargetPercent)); }
This is not what i want. you misunderstood my question.
Regarding the percentage calculation and if condition, it already short out. My current error is while writing the values in the file. So even when it Deinit and OnInit, it will remember it values instead of replacing to new one without fulfilling existing value/Profits.

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello,
I am adding new feature in my EA, which close trade automatically when it fulfilled defined earning set by user and than it redefine with value again with new Account balance.
Example :
Account Balance = $1000
Target Percentage = 10% (10% of 1000$ = $100)
When Account balance is above $1100, close all the running trade whether it in positive or negative. Than it start again but now the Account balance is $1100 and 10% targeted percentage is $110. and it keep repeating.
To do this, i am trying like :
The problem is, i do not know how i can set new value to the static double target_Capital . If it static it value will be remained unchanged but i want to redefine static target_capital to new value ones it fulfilled previous targeted percentage requirement.