Using both OnBar and OnTick methods in cBot
Using both OnBar and OnTick methods in cBot
12 May 2018, 11:14
I need some help to work out how to use both OnTick and OnBar in my trading logic. I would like to check the CheckMarketTrend() method in the OnBar() method and based on the result go to a specific method within the OnTick() method that holds the logic for executing the trade. In other words, I want to check the trend in OnBar() but execute trade operations in OnTick() based on results from OnBar. Something like below but ofcourse you can't do OnTick.ManageOpenSellPositions(). Any help would be much appreciated. protected override void OnBar() { if (CheckMarketTrend()) { if (Positions.Count < 1) { OnTick.ManageOpenSellPositions(); } } else if (!CheckMarketTrend()) { if (Positions.Count < 1) { OnTick.ManageOpenBuyPositions; } } }
Replies
pogostick
14 May 2018, 11:56
RE:
Hi Panagiotis
Thanks for the above suggested code. I solved my issue using something very similar. To better understand the flow of the code, I also tried the below variation where the OnBar() method was also called inside the OnTick() method to ensure the correct _marketTrend variable was used.
If my understanding is correct, if the cbot is attached eg. on the 1 minute chart, theOnBar() method should execute every 1 minute regardless of what methods are being called in the OnTick() method settiing the _marketTrend variable to the correct state every 1 minute to be used by the methods in the OnTick(). However, when I call the OnBar() inside the OnTick(), the backtesting using tick data from server delivered slightly different trading results. Would you have an explanation for this?
Thanks in advance for your assistance.
protected override void OnTick() { OnBar(); if (_marketTrend) { if (Positions.Count < 1) { ManageOpenSellPositions(); } } else { if (Positions.Count < 1) { ManageOpenBuyPositions(); } } } protected override void OnBar() { _marketTrend = CheckMarketTrend(); }
@pogostick
PanagiotisCharalampous
15 May 2018, 09:09
Hi pogostick,
Most probably the reason is that CheckMarketTrend() changes value when called every time from the OnTick method.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
14 May 2018, 09:35
Hi pogostick,
Thanks for posting in our forum. You can consider something like the following
Let me know if the above helps,
Best Regards,
Panagiotis
@PanagiotisCharalampous