Variables keep resetting to initial declared values

Variables keep resetting to initial declared values

Manage alerts

Loading saved threads...

Intelis · External communityPost link
External question — Stack Overflow Stack Exchange Author: Intelis Original post: https://stackoverflow.com/questions/66482083 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. wondering whether someone may be able to point out what I'm doing wrong here. Just for background I am very new to MQL4 (or coding at all) I wrote the following code to act as a virtual trailing stop loss. I am trying to lock in HH (Hihest High) and LL (Lowest Low) based on the below loop. However when I print, it looks like both HH and LL are resetting to both Bid and Ask on every tick, and not "locking". I had three questions: What am I doing wrong?! Does HH and LL just reset to zero continuously because of where the variables have been declared? Will HH and LL be unique for every order? If not, is there a way of setting that? (The ~are just placeholders for now - can be ignored, I will correct that) Thanks a lot! double HH = 0; double LL = 0; int orderstotal = OrdersTotal(); int orders = 0; int ordticket[90][2]; for (int i = 0; i < orderstotal; i++) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol() || OrderMagicNumber() != ~OrderId~) { continue; } ordticket[orders][0] = OrderOpenTime(); ordticket[orders][1] = OrderTicket(); orders++; } if (orders > 1) { ArrayResize(ordticket,orders); ArraySort(ordticket); } for (i = 0; i < orders; i++) { if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == ~OrderId~) { if (Ask > HH) { HH = Ask; Print("Print(HH)=",HH); } if (Bid < LL) { LL = Bid; Print("Print(LL)=",LL); } if ((OrderType() == OP_BUY && HH - OrderOpenPrice() > ~TrailingStartGap~*PipValue*Point) || (OrderType() == OP_SELL && OrderOpenPrice() - LL > ~TrailingStartGap~*PipValue*Point)) { if ((OrderType() == OP_BUY && HH - Ask > ~TrailingStop~*PipValue*Point) || (OrderType() == OP_SELL && Bid - LL > ~TrailingStop~*PipValue*Point)) { bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), ~Slippage~, ~Color~); if (ret == true) { int error = GetLastError(); if (ret == false && error > 0) Print("OrderClose() error - ", ErrorDescription(error)); } } } } } }
Quote
Report
Intelis · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Intelis Original post: https://stackoverflow.com/a/66664247 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. tomgyn - that was indeed the issue, it was placed inside OnTick() and therefore the variables were being initialised on every new tick. Was a rookie mistake, I didn't understand at that point the code will run top to bottom on every single tick when placed there. Issue has been resolved. Thanks for the pointer and edits - much appreciated!
Quote
Report
tomgny · External communityPost link
External answer — Stack Overflow Stack Exchange Author: tomgny Original post: https://stackoverflow.com/a/66691791 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Declare the HH and LL as a global variables, outside OnTick() function to avoid reinitialization them on every tick.
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: tomgny Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/66691791 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Declare the HH and LL as a global variables, outside OnTick() function to avoid reinitialization them on every tick.

Cancel quote

Checking account access…