Download MetaTrader 5

Detect Last Candle Close Price ---> pending buy + X pips above/below

To add comments, please log in or register
johnny72
25
johnny72  

Hello,

(I have searched everywhere already, found many people who have the same question but received no answer. I tried hours to code it myself, i have very bad programming skills.)


What I need is a script which opens a pending buy order, X pips above the last candle's close price.

(additionally, put StopLoss X pips below the open price)

Example: Pending Distance is set to 12 pips. SL is set to 20 pips. Last candle close price is 1.2236. When i run the script (press the hotkey for it) it then puts a buy stop at 1.2248, and SL at 12216.


The rest i will try to edit and change for myself, I would thank you very very much if you could tell me how to write this basic code.


Thanks to any programmer who can help me, you are the best!


-johnny

qjol
3243
qjol  

well, let's get started

https://www.mql5.com/en/forum/135741

johnny72
25
johnny72  

Ok, this is what I have, opened a new Script:


//+------------------------------------------------------------------+
//|                                      PO for last close price.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "https://forum.mql4.com/50686"

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+


Now I need to know the command to open a pending buy order right? I think it is OP_BUYSTOP, but I don't know where to place it. Man, that's complicated ;)

johnny72
25
johnny72  

More important:


How do we determine last candle's close price?

smoknfx
85
smoknfx  
double iClose( string symbol, int timeframe, int shift) 


Returns Close value for the bar of indicated symbol with timeframe and shift. 



example:

double price;

price=iClose("EURUSD",0,1);

johnny72
25
johnny72  
smoknfx:

iClose[1]?


Thanks for the code, i tried implementing it in another code, giving the error: "left parenthesis expected".

My head is hurting, i need a break here.... ): Thanks for the reply though

smoknfx
85
smoknfx  
johnny72:
smoknfx:

Thanks for the code, i tried implementing it in another code, giving the error: "left parenthesis expected".

My head is hurting, i need a break here.... ): Thanks for the reply though


CORRECT.

i did a typo.

notice my edited post.

syntax:

double price;

price=iClose(Symbol(),0,1);

johnny72
25
johnny72  

Thanks smoknfx! I got your code working a bit, i will continue working on the whole thing tomorrow.

iClose(Symbol(),0,1);

that is. 1 means 1 previous candle right? so if it were 3 it would look 3 candles back. awesome. (And the 0 means? not that important, just curious :D)

smoknfx
85
smoknfx  
johnny72:

Thanks smoknfx! I got your code working a bit, i will continue working on the whole thing tomorrow.

that is. 1 means 1 previous candle right? so if it were 3 it would look 3 candles back. awesome. (And the 0 means? not that important, just curious :D)


johnny,

0=current bar (first bar)

1=one bar back in time(second bar, actually)

2=two bars back in time(third bar)

good luck.

Simon Gniadkowski
Moderator
18025
Simon Gniadkowski  
johnny72:

Thanks smoknfx! I got your code working a bit, i will continue working on the whole thing tomorrow.

that is. 1 means 1 previous candle right? so if it were 3 it would look 3 candles back. awesome. (And the 0 means? not that important, just curious :D)

Read the documentation, it tells all there: iClose() timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe.
johnny72
25
johnny72  

After some hours now i got it working :) Now it gets more complicated, because my "buy stop" i want to put above the candle gets "swapped prices" on a bearish candle (because the open price is above the close price)

Is there a way we can check if the last candle was bearish or bullish?

And then if the last candle was bearish, swap

iClose(Symbol(),0,1);

with

iOpen(Symbol(),0,1);


You probably write is like that: if the candle was bearish, use this code .......

and then below that: else use this normal code ........


Somebody can help how to check that, check if the candle was bearish and then use a different code?

12
To add comments, please log in or register