![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
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
It is OK to work without SL and TP.
But we still need condition to close order in case of profit.
Please take a look at the renewed function "iSignalClose", https://www.mql5.com/en/forum/124521/page2.
Now it is, of course, condition of virtual SL.
But we still need condition of virtual TP.
Waiting for your reply.
:)
Hello AisWow that is great work. Thank you.
Sorry I had to leave. It was Sunday, and family time at the lake and then dinner.
I just opened the program a few minutes ago to see all the work you have preformed.
How can I thank you enough? It may take a little time for me to absorb.
The idea that the TP had not been discussed was that I was putting this together one
statement or function at a time. Just slowly understanding how to build a program.
Your take on this shortens the program and is professional. Thank you for sharing .
The TP and SL will be a bit more intricate. As I had stated, I am slowly learning this.
Not sure what function should be done next. If I may, I will inform you of the bigger picture.
1. With this program, I was looking to add on more positions. Such as the initial position.
The one that is already in the program, and then position 2, 3, 4 and 5.
Note: Have not backtested with more than 5 positions.
2. With new positions, the SL will need adjusting with each. When the SL is hit, all positions
are liquidated.
3. Each additional position will be 1/2 ATR from the one prior. A Buy will be 1/2 ATR greater,
where as a Sell will be 1/2 ATR lower.
4. The SL will be adjusted by 1/2 ATR, in step with each additional position.
Note: There comes a time when you are on a decent trend, and the price does go past the 1/2 ATR.
And wish no more positions. There is a time within this trend, that the price will also reach a point when the lowest low of the last ten bars, will be higher than the last SL. In this case, the SL is adjusted by the lowest low of the last 10 bars. And is adjusted accordingly after a low has been shifted off the chart. As you can see, the SL is not a trailing stop. But is adjusted by ATR*2, then by adjusted by the lowest low of the last ten days. (The situation mentioned was a Buy condition. A Sell condition is just the opposite).
This is the system I had backtested by hand.
I have not had the luxery of backtesting with codes, or with a TP. Maybe in the near future, I could look into
price patterns. But as for now, I have let the profits run, and take many small losses.
I have been told that I should not try to be sophisticated. That I should learn in small programs
and build from them. So I am trying to do just that. So I suppose the next step would be to learn
how to pivot on additional positions. Then learn how to adjust the SL.
Money management could be held off until the last. But so important. If you think I'm heading in the wrong direction, I appriciate
any input.
I do hope that what work that has been done, will not be hampered by additional steps.
I just did not know which step should be done first, or what should follow.
Thanks for everything.
I'm admiring over your improvements. I'll be back, I'm sure with more questions as well. There is much to learn with what you have given.
Cheers
Hello Huckleberry,
The best thanks:
"
Hello Ais
Wow that is great work. Thank you.
"
Really and truly.
Now I am going to improve computations.
Exactly, I want to compute ATR only on new bar.
It lets to test and optimize faster.
And, the main, my intention was that You will able easily to change the program.
Even if you will extend the program and add new features.
The structure made especially to reduce complexity.
I will use the same structure in the nearest future for large programs.
As for trading strategy, I studied it only fleetingly.
I am sure that a great lot of strategies can and must work.
I would like to help you to study the structure for best understanding.
Best regards and bye for now
P.S. In future I am going to work only with virtual takes and stops.
////////////////////////////////////////////////////////////////////< 1> // < Program : Property > //< > // #define 1 " " //< > // #define 2 " " //< > // </Program : Property > //< > // //< > // < Program : Content > //< > // //< > // < Structure 16 elements in 4 domains > //< > // < 1. Data 7 elements in 2 domains /> //< > // < 2. Code 9 elements in 2 domains /> //< > // </Structure 16 elements in 4 domains > //< > // //< > // < 1. Data 7 = 4 i 3 d - s > //< > // < 1.1. Input 7 = 4 i 3 d - s /> //< > // < 1.2. Buffer - = - i - d - s /> //< > // </1. Data 7 = 4 i 3 d - s > //< > // //< > // < 2. Code 9 / - i 82 l 4 o > //< > // < 2.1. Interface 6 / - i 71 l 4 o /> //< > // < 2.2. Special 3 / - i 11 l - o /> //< > // </2. Code 9 / - i 82 l 4 o > //< > // //< > // </Program : Content > //< >
////////////////////////////////////////////////////////////////////< 2> // < 1.1. Data : Input > //< > // //< > // < 1.1. Input 7 = 4 i 3 d - s > //< > // < 1. Strategy 4 = 2 i 2 d - s /> //< > // < 2. Trading 3 = 2 i 1 d - s /> //< > // </1.1. Input 7 = 4 i 3 d - s > //< > // //< > // < 1. Strategy 4 >=========================================//< > int iBaseLag = 20 ; //< > int iBaseBar = 1 ; //< > double dFactorTP = 2.0 ; //< > double dFactorSL = 2.0 ; //< > // </ 1. Strategy 4 >=========================================//< > // //< > // < 2. Trading 3 >==========================================//< > int iSlippage = 1 ; //< > int iMagic = 1 ; //< > double dLots = 0.1 ; //< > // </ 2. Trading 3 >==========================================//< > // //< > // //< > // //< > // </1.1. Data : Input > //< >
////////////////////////////////////////////////////////////////////< 3> // < 1.2. Data : Buffer > //< > // //< > // < 1.2. Buffer - = - i - d - s > //< > // </1.2. Buffer - = - i - d - s > //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // </1.2. Data : Buffer > //< >
////////////////////////////////////////////////////////////////////< 4> // < 2.1. Code : Interface > //< > // //< > // < 2.1. Interface 6 / - i 71 l 4 o > //< > // < 1. iNewBar - i 4 l 1 o /> //< > // < 2. iSignalOpen - i 15 l 1 o /> //< > // < 3. iSignalClose - i 15 l 1 o /> //< > // < 4. iGetTicket - i 7 l 1 o /> //< > // < 5. iTryOpen - i 15 l - o /> //< > // < 6. iTryClose - i 15 l - o /> //< > // </2.1. Interface 6 / - i 71 l 4 o > //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // </2.1. Code : Interface > //< >
////////////////////////////////////////////////////////////////////< 5> // < 2.1.1. Code : Interface : iNewBar > //< > int iNewBar () // - i 4 l 1 o //< > { //< > static int iTime_0 = 0 ; //< > // //< > if ( iTime_0 < iTime ( 0 , 0 , 0 ) ) //< > { iTime_0 = iTime ( 0 , 0 , 0 ) ; return ( TRUE ) ; } //< > else { return ( EMPTY ) ; } //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > } //< > // </2.1.1. Code : Interface : iNewBar > //< >
////////////////////////////////////////////////////////////////////< 6> // < 2.1.2. Code : Interface : iSignalOpen > //< > int iSignalOpen () // - i 15 l 1 o //< > { //< > static int iFirstRun = 1 ; //< > if ( ( iNewBar () == TRUE ) || ( iFirstRun == 1 ) ) //< > { iFirstRun = 0 ; //< > int iIndexH = iHighest ( 0 , 0 , MODE_HIGH , //< > iBaseLag , iBaseBar ) ; //< > int iIndexL = iLowest ( 0 , 0 , MODE_LOW , //< > iBaseLag , iBaseBar ) ; //< > static double dHigh ; dHigh = High [ iIndexH ] ; //< > static double dLow ; dLow = Low [ iIndexL ] ; //< > } // if //< > // //< > double dAsk = MarketInfo ( Symbol () , MODE_ASK ) ; //< > if ( dAsk > dHigh ) return ( OP_BUY ) ; //< > // //< > double dBid = MarketInfo ( Symbol () , MODE_BID ) ; //< > if ( dBid < dLow ) return ( OP_SELL ) ; //< > // //< > return ( EMPTY ) ; //< > } //< > // </2.1.2. Code : Interface : iSignalOpen > //< >
////////////////////////////////////////////////////////////////////< 7> // < 2.1.3. Code : Interface : iSignalClose > //< > int iSignalClose () // - i 15 l 1 o //< > { //< > static int iFirstRun = 1 ; //< > if ( ( iNewBar () == TRUE ) || ( iFirstRun == 1 ) ) //< > { iFirstRun = 0 ; //< > static double dATR , dProfit , dLoss ; //< > dATR = iATR ( 0 , 0 , iBaseLag , iBaseBar ) ; //< > } // if //< > // //< > double dDelta = OrderOpenPrice () - OrderClosePrice () ; //< > // //< > if ( OrderType () == OP_BUY ) //< > { dProfit = -dDelta ; dLoss = dDelta ; } //< > else if ( OrderType () == OP_SELL ) //< > { dProfit = dDelta ; dLoss = -dDelta ; } //< > else return ( EMPTY ) ; //< > // //< > if ( dProfit > dATR * dFactorTP ) return ( TRUE ) ; //< > if ( dLoss > dATR * dFactorSL ) return ( TRUE ) ; //< > return ( EMPTY ) ; //< > } //< > // </2.1.3. Code : Interface : iSignalClose > //< >
////////////////////////////////////////////////////////////////////< 8> // < 2.1.4. Code : Interface : iGetTicket > //< > int iGetTicket () // - i 7 l 1 o //< > { //< > for ( int i = OrdersTotal () - 1 ; i >= 0 ; i -- ) //< > { //< > if ( OrderSelect ( i , SELECT_BY_POS ) == TRUE ) //< > if ( OrderMagicNumber () == iMagic ) //< > return ( OrderTicket () ) ; //< > } // for //< > return ( EMPTY ) ; //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > } //< > // </2.1.4. Code : Interface : iGetTicket > //< >
////////////////////////////////////////////////////////////////////< 9> // < 2.1.5. Code : Interface : iTryOpen > //< > int iTryOpen () // - i 15 l - o //< > { //< > int iCommand = iSignalOpen () ; //< > if ( iCommand == EMPTY ) return ; //< > if ( iCommand == OP_BUY ) //< > { string sType = "Buy" ; int iColor = Blue ; } //< > else { sType = "Sell" ; iColor = Red ; } //< > // //< > if ( iCommand == OP_BUY ) int iMode = MODE_ASK ; //< > else iMode = MODE_BID ; //< > double dPrice = MarketInfo ( Symbol () , iMode ) ; //< > // //< > OrderSend ( Symbol () , iCommand , dLots , //< > NormalizeDouble ( dPrice , Digits ) , //< > iSlippage , 0 , 0 , "" , iMagic , 0 , iColor ) ; //< > // //< > int iTrap = GetLastError () ; //< > if ( iTrap == 0 ) //< > Alert ( sType , " Was a Big Success" ) ; //< > else Alert ( sType , " open exception " , iTrap ) ; //< > } //< > // </2.1.5. Code : Interface : iTryOpen > //< >
////////////////////////////////////////////////////////////////////< 10> // < 2.1.6. Code : Interface : iTryClose > //< > int iTryClose () // - i 15 l - o //< > { //< > int iCommand = iSignalClose () ; //< > if ( iCommand == EMPTY ) return ; //< > if ( OrderType () == OP_BUY ) //< > { string sType = "Buy" ; int iColor = Red ; } //< > else { sType = "Sell" ; iColor = Blue ; } //< > // //< > if ( OrderProfit () > 0 ) string sAct = "Take" ; //< > else sAct = "Stop" ; //< > double dPrice = OrderClosePrice () ; //< > // //< > OrderClose ( OrderTicket () , OrderLots () , //< > NormalizeDouble ( dPrice , Digits ) , //< > iSlippage , iColor ) ; //< > // //< > int iTrap = GetLastError () ; //< > if ( iTrap == 0 ) //< > Alert ( sType , " closed with Hard " , sAct ) ; //< > else Alert ( sType , " close exception " , iTrap ) ; //< > } //< > // </2.1.6. Code : Interface : iTryClose > //< >
////////////////////////////////////////////////////////////////////< 11> // < 2.2. Code : Special > //< > // //< > // < 2.2. Special 3 / - i 11 l - o > //< > // < 1. init - i 1 l - o /> //< > // < 2. deinit - i 1 l - o /> //< > // < 3. start - i 9 l - o /> //< > // </2.2. Special 3 / - i 11 l - o > //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // </2.2. Code : Special > //< >
////////////////////////////////////////////////////////////////////< 12> // < 2.2.1. Code : Special : Init > //< > int init () // - i 1 l - o //< > { //< > Alert ( "" , "Start " , UninitializeReason () ) ; //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > } //< > // </2.2.1. Code : Special : Init > //< >
////////////////////////////////////////////////////////////////////< 13> // < 2.2.2. Code : Special : Deinit > //< > int deinit () // - i 1 l - o //< > { //< > Alert ( "" , "Stop " , UninitializeReason () ) ; //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > // //< > } //< > // </2.2.2. Code : Special : Deinit > //< >
////////////////////////////////////////////////////////////////////< 14> // < 2.2.3. Code : Special : Start > //< > int start () // - i 9 l - o //< > { //< > // < 2.2.3.1. History data inspection 4 >`````````````````````````//< > static int iTrigger = 0 ; if ( iTrigger == 0 ) { //< > if ( ( iTime ( 0 , 0 , 0 ) == 0 ) //< > || ( iBars ( 0 , 0 ) < iBaseLag + iBaseBar ) ) //< > return ; else iTrigger = 1 ; } //< > // </2.2.3.1. History data inspection 4 >`````````````````````````//< > // //< > // < 2.2.3.2. Main routine 3 >````````````````````````````````````//< > int iTicket = iGetTicket () ; //< > // //< > if ( iTicket < 0 ) iTryOpen () ; //< > else iTryClose () ; //< > // </2.2.3.2. Main routine 3 >````````````````````````````````````//< > // //< > // < 2.2.3.3. Exception handler 2 >```````````````````````````````//< > int iTrap = GetLastError () ; //< > if ( iTrap > 0 ) Alert ( "Exception " , iTrap ) ; //< > // </2.2.3.3. Exception handler 2 >```````````````````````````````//< > } //< > // </2.2.3. Code : Special : Start > //< > ////////////////////////////////////////////////////////////////////< 0>
Adjust MetaEditor window to see 25 lines x 80 columns. Use <Page Up> and <Page Down> to scroll the content. Last edit: 2010.03.16 18:44 State: Ready