Call OnBar code from OnTick
Call OnBar code from OnTick
10 Jul 2023, 04:14
Hi, im quite new to this and by no means a programmer, so hoping for some assistance on how/if you can execute OnTick based on checking OnBar code first?
I see a few examples around that seem to require a bool answer, and/or referring to other methods etc, is that the only way?
Wondering if something like below can be done if I want the bot to execute OnTick after checking multiple criteria within the OnBar.......
protected override void OnTick()
{
if (IncludeTrailingStop)
{
SetTrailingStop();
}
if (Positions.Count < 1)
if (OnBar()) - example, I know this doesnt work.
if (_Ma1.Result.LastValue > _Ma2.Result.LastValue && _Ma1.Result.Last(2) < _Ma2.Result.Last(2))
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, Quantity, Label, StopLossInPips, TakeProfitInPips);
}
protected override void OnBar()
{
if (_Ma3.Result.LastValue > _Ma4.Result.LastValue && _macdCross.MACD.LastValue > _macdCross.Signal.LastValue && etc)
{
??
}
Thanks for any help.
Replies
firemyst
11 Jul 2023, 14:58
You'll need to call the OnBar method yourself, so you can then execute code both before and/or after it.
//Example code
//class variable. Set to zero in the OnStart method so it's 0 every time you start your bot
int _previousIndex;
int _currentIndex;
//in the "OnTick" Method
protected override void OnTick()
{
//do whatever you need to do in OnTick first here
_currentIndex = _marketSeries.OpenTimes.GetIndexByTime(_marketSeries.OpenTimes.LastValue);
//Call the OnBar method yourself. Just make your own.
if (_currentIndex != _previousIndex)
{
//when currentIndex isn't equal to previous index, we know a new bar has opened
//so call your own OnBar method:
OnBarForBot();
_previousIndex = _currentIndex;
}
//Now do whatever you have to after you called your OnBar method
}
private void OnBarForBot()
{
//do whatever you want to on each new bar
}
//Don't have anything in the built in method.
protected override void OnBar()
{
}
@firemyst
... Deleted by UFO ...