Guidance for a beginner - Timing and 'resetting'

To add comments, please log in or register
charlie.cooper
42
charlie.cooper  

So when my algo satisfies certain criteria which it processes in OnTick(), it then needs to wait a few bars for a specified price to be hit.

I have a few questions about how this is possible.

1. How do I tell it to look for that price but give up if it doesn't happen in the next 3 bars? I feel like I need to use OnTimer but I'm not sure.

2. How do I make it 'reset' if the timer activates but the specific price doesn't get hit?


Huge thanks in advance! I'm new but really enjoying things so far.

Lorentzos Roussos
19011
Lorentzos Roussos  
charlie.cooper:

So when my algo satisfies certain criteria which it processes in OnTick(), it then needs to wait a few bars for a specified price to be hit.

I have a few questions about how this is possible.

1. How do I tell it to look for that price but give up if it doesn't happen in the next 3 bars? I feel like I need to use OnTimer but I'm not sure.

2. How do I make it 'reset' if the timer activates but the specific price doesn't get hit?


Huge thanks in advance! I'm new but really enjoying things so far.

Hello mr Cooper.

I will assume you need one trigger (one symbol , one thread)
First thing you need is to identify bar change . When a new bar forms that means you can advance your counter , and for this you can use Time (for starters).its also convenient because all operations will happen at the open of a new bar.so

//Declare this above the oninit function , this is your global "bar time keeper"
datetime BarStamp=0;
//Also above the oninit function , this is your global "bar counter"
//Notice we use variables since you need one thread.In a multisymbol senario where
//the user enters their own pending orders and you manage them all by an Interface you
//would-will use arrays
int BarsElapsed=0;
//Now that can get messy so you can also define a structure of what you need
struct trigger
{
string symbol;
ENUM_TIMEFRAMES tf;
datetime barstamp;
int barselapsed;
};
trigger Watcher;
//if you do use strings in your structures remember you cannot copy struct to struct and you cannot also save 
//in a file - ignore that for now , use the simple form .

//Inside on init >>

//Reset your trigger
BarsElapsed=-1;//because the first(only the first) tick will identify a new bar so it will go to 0
BarStamp=0;

//Inside on Tick >>

//check if the bar is new (Time[i] points to the open time of each bar) 
if(Time[0]!=BarStamp)
{
BarsElapsed++;
//set the new BarStamp
BarStamp=Time[0];
//if Bars reach limit perform checks 
  if(BarsElapsed>=3)
  {
  //reset bars first 
  BarsElapsed=0;
  //check your prices ,etc-----
    
  //check your prices ,etc-----
  }
//if Bars reach limit perform checks ends here 
}
charlie.cooper
42
charlie.cooper  
Lorentzos Roussos:

Hello mr Cooper.

I will assume you need one trigger (one symbol , one thread)
First thing you need is to identify bar change . When a new bar forms that means you can advance your counter , and for this you can use Time (for starters).its also convenient because all operations will happen at the open of a new bar.

Lorentzos,


This is hugely helpful!! Thank you so much for taking the time to clarify this...

I may have some more questions soon so that I can fully understand exactly what is happening but after a read through it seems to make sense.


Thanks again.

charlie.cooper
42
charlie.cooper  
Lorentzos Roussos:

Hello mr Cooper.

I will assume you need one trigger (one symbol , one thread)
First thing you need is to identify bar change . When a new bar forms that means you can advance your counter , and for this you can use Time (for starters).its also convenient because all operations will happen at the open of a new bar. 

Back again - a few things I've noticed I wanted to check I am understanding:

1. It looks like this code is asking me to check my prices if BarsElapsed is >=3, which is wrong - am I missing something or do I just need to add

if (BarsElapsed<=3)
   //check prices

2. When my criteria is fulfilled, how do I trigger this with BarsElapsed reset?

3. Can you explain to me what BarStamp is doing? Is it counting all the bars that have closed since market open?

Sorry if these questions seem stupid but solving this and understanding it is huge for helping me get to grips with this stuff!


Thanks again

Lorentzos Roussos
19011
Lorentzos Roussos  
charlie.cooper:

Back again - a few things I've noticed I wanted to check I am understanding:

1. It looks like this code is asking me to check my prices if BarsElapsed is >=3, which is wrong - am I missing something or do I just need to add

2. When my criteria is fulfilled, how do I trigger this with BarsElapsed reset?

3. Can you explain to me what BarStamp is doing? Is it counting all the bars that have closed since market open?

Sorry if these questions seem stupid but solving this and understanding it is huge for helping me get to grips with this stuff!


Thanks again

1.Correct i thought you wanted to "deflect" checks for 3 bars .
In that case ,since you have other checks as well that determine the operation of this code block you need to incorporate another switch.
BarStamp is the latest processed open time that the system found in on tick ,its used to identify bar changes.
So 

//Declare this above the oninit function , this is your global "bar time keeper"
datetime BarStamp=0;
bool AreWeInSeek=false;//master switch 
//Also above the oninit function , this is your global "bar counter"
//Notice we use variables since you need one thread.In a multisymbol senario where
//the user enters their own pending orders and you manage them all by an Interface you
//would-will use arrays
int BarsElapsed=0;
//Now that can get messy so you can also define a structure of what you need
struct trigger
{
string symbol;
ENUM_TIMEFRAMES tf;
datetime barstamp;
int barselapsed;
};
trigger Watcher;
//if you do use strings in your structures remember you cannot copy struct to struct and you cannot also save 
//in a file - ignore that for now , use the simple form .

//Inside on init >>

//Reset your trigger
BarsElapsed=-1;//because the first(only the first) tick will identify a new bar so it will go to 0
BarStamp=0;
AreWeInSeek=false;//reset

//Inside on Tick >>

//your code performs checks to see if you must wait at least  3 bars for a price 
  //inside your code , there is a part where you discover this is valid (that you need to check the following 3 bars)
  //its nested in ifs and fors probably
  //add this there as the activation of the "seek"
    AreWeInSeek=true;
    BarStamp=0;
    BarsElapsed=0;
//your code performs checks to see if you must wait at least  3 bars for a price ends here 

//check if the bar is new (Time[i] points to the open time of each bar) if we in seek mode 
if(Time[0]!=BarStamp&&AreWeInSeek==true)
{
BarsElapsed++;
//set the new BarStamp
BarStamp=Time[0];
//perform checks within bars limit
  if(BarsElapsed<=3)
  {
  //check your prices ,etc-----
    //inside the case your criteria is met add this line too
      AreWeInSeek=false;//kill seek
  //check your prices ,etc-----
  }
//perform checks within bars limit ends here 
//if bars go out of limit
  if(BarsElapsed>3)
  {
  AreWeInSeek=false;//kill seek
  }
}
To add comments, please log in or register