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
{
Print("checkfortrade");
static int prev_level = GetPriceLevel(Close[0]);
int curr_level = GetPriceLevel(Close[0]);
...
}
Also you are using Close[0] before OnInit() so you will get sooner or later an Array Out of Range error too.
Ahhh I see! I didn't know that. I thought it would be initialized the first time the function is called. I've got to remember that, thanks :)
Ah i was too late ;)
Anyway try this:
{
Print("checkfortrade");
int prev_level = GetPriceLevel(Close[0]);
int curr_level = GetPriceLevel(Close[0]);
if (curr_level != prev_level)
{
// there must be hedged positions for each level
if (GetLongTicket(curr_level) < 0) OpenLongPosition();
if (GetShortTicket(curr_level) < 0) OpenShortPosition();
// buy low sell high
if (curr_level < prev_level) // buy low when price dropped
{
int pos = curr_level - ShortTicketsStartLevel;
// realize profits from positions that are in profit
for (int i = pos+InpTakeProfitLevels; i < ArraySize(ShortTickets); i++) // look above
CloseShortPosition(i);
// realize loss from positions that are too far away below
for (int i = pos-InpLevels; i >= 0; i--) // look below
CloseShortPosition(i);
}
else if (curr_level > prev_level) // sell high when price rises
{
int pos = curr_level - LongTicketsStartLevel;
// realize profits from positions that are in profit
for (int i = pos-InpTakeProfitLevels; i >= 0; i--) // look below
CloseLongPosition(i);
// realize loss from positions that are too far away above
for (int i = pos+InpLevels; i < ArraySize(ShortTickets); i++) // look above
CloseLongPosition(i);
}
}
prev_level = curr_level;
}
That won't work, you just made the static variable non static, now it's always the same as prev_level. I made it static so the function remembers it the next time. The fix should be easy now that I know what went wrong.
Thank you all for your help :)
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.