NavigationUser loginWho's new
Who's onlineThere are currently 1 user and 32 guests online.
Online users:
|
Examples: ProfitProtector Expert Advisor!Hi folks, Today we are going to take an idea to create Expert Advisor (That the Expert Advisors are for). The idea:
How to protect my profits of all opening orders (if there's any) by the mean I want to take them when they reach a specific amount and close all the trades when the drop down to a specific amount. For example: There are 4 opened positions and they make profit. They've made 200 Pip right now. suddenly the profit went to 150 then 100 then I'm losing money. Our code today will prevent this situation, let's see the idea programicaly. The problem:
First we want a way to calculate the profit of all opened positions. When the profit goes to specific amount we are going to close all the opened position. But, we want the program too to prevent the profit to drop again. So, we want to set another profit point that the program will prevent any drops below it. For example: the program will take the profit when it reaches 200 Pips and prevent the profit to drop below to 100 Pips . The programming problem now is when to start preventing the profit drop. How to tell the program that the profit went over 100 Pip and we don't want it drop back? We need a third point when the profit reach it the program will start to monitor the profit drop. The example right now is: the program will take the profit when it reaches 200 Pips and start to monitor the drop down of the profit when the profit reaches 150 Pips and prevent the profit to drop again to 100 Pips level. Let's convert that to code! The code:
Please download the code. I've added the profit protector code to a normal expert advisor of mine. We will not study the expert advisor strategy in buying/selling/closing/trailing. But we are going to study the code aimed to protect the profit. These are the pieces of code concerning our idea: .....//your normal code extern double ProfitToProtect = 200; bool ProtectLoss= false; .....//your normal code int start() if(ProtectProfit) void ProfitProtect(double profit) void LossProtect(double profit) void CloseAll() Code study:
We have defined our profit point as external variables at the top of the program: extern double ProfitToProtect = 200; Where the the ProfitToProtect is the amount we want the program to take the profit when it is reached. And the ProtectStarter is the amount the program start at monitoring the profit to prevent drop down again. And finally the LossToProtect is the amount of the profit the program prevent the profit to drop below it. We have added the option to the user to use our profit protector or not with the variable ProtectProfit; bool ProtectLoss= false; We have declared this variable outside the start() function to make it in a global scope and we will use this variable to start the profit monitoring. if(ProtectProfit) If the user has chosen to use our protect (ProtectProfit=True) then the program will call the function ProfitProtect(). We are going to study this function soon. if(ProtectLoss) If the ProtectLoss variable is true (It will be true when the profit reaches the ProtectStarter level) then the program will call the function LossProtect() which we are going to study it soon. void ProfitProtect(double profit) The ProfitProtect() function takes a double parameter (profit you want to protect). Now we enter in loop from 0 to the count of the opened positions. In every loop we select the order using OrderSelect() function and examine is the MagicNumber of the order is the same as our MagicNumber variable to be sure that we are working only with the positions have been opened with our Expert Advisor. Then we get the profit of the order using OrderProfit() and add it to the MyCurrentProfit variable. Now we have the total profit stored in the MyCurrentProfit variable, We can check it now to find did it reach the ProtectStarter value; if true we will enable ProtectLoss. And we have to check the MyCurrentProfit variable against ProfitToProtect variable to find did it reach it or not; if true we will call the CloseAll() function which will close all the opened positions. void LossProtect(double profit) The LossProtect() function is very like the ProfitProtect() function, the only difference is it will monitor the MyCurrentProfit value and close all the opened position if it goes below the LossToProtect vlaue. void CloseAll() The CloseAll() function goes through all the opened position and check them against our expert MagicNubmer then close every buy or sell position using OrderClose() function. I hope everything is clear, And hope you find it a useful idea! Coders Guru
|