NexusFi: Find Your Edge


Home Menu

 





Executing trade while a bar has not closed yet?


Discussion in Sierra Chart

Updated
      Top Posters
    1. looks_one Amnesia with 3 posts (0 thanks)
    2. looks_two vegasfoster with 1 posts (0 thanks)
    3. looks_3 Trembling Hand with 1 posts (0 thanks)
    4. looks_4 chris123 with 1 posts (0 thanks)
    1. trending_up 3,763 views
    2. thumb_up 0 thanks given
    3. group 5 followers
    1. forum 7 posts
    2. attach_file 0 attachments




 
Search this Thread

Executing trade while a bar has not closed yet?

  #1 (permalink)
Amnesia
Vienna Austria
 
Posts: 116 since Mar 2012
Thanks Given: 16
Thanks Received: 67

I am trying a way to continously monitor a bar for certain conditions and then executing a trade once that condition has occured. Unfortunately i cannot find examples of how to implement/facilitate this without either freezing up sierra due to what i can only assume is a massive amount of memory leaks caused by a self repeating while loop.

But if you have autoloop=1 on, then basically once you reach the end of the code it ends and essentially reboots when there is a function call (a new bar opening, current bar closing etc). So if you use an if statement and the conditions are not met then that trade is never going to be executed from what i understand, even if the conditions are met lateron while the bar is forming.

To the point:
 
Code
while(sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_NOT_CLOSED){ 

 

    SCFloatArrayRef Last = sc.Close; 

    s_SCNewOrder NewOrder; 

    NewOrder.OrderQuantity = 2; 

    NewOrder.OrderType = SCT_MARKET; 

    if (Long) { 

      if (sc.CrossOver(Last, sc.Subgraph[1]) == CROSS_FROM_TOP)  

        {  

          sc.BuyEntry(NewOrder); 

        }
This is essentially what i need to happen but this leads to memory leaks.

Now if you try this
 
Code
if (sc.CrossOver(Last, sc.Subgraph[1]) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED) 

  { 

    sc.BuyEntry(NewOrder);
It actually works but not in the way youd expect them to.

To be more specific i am dealing with Flexrenkos here, and if you use larger settings like 32-31-30 for ES or YM then obviously those retracebars (long wick bars) can last forever, to see what i mean you can look at Outlanders Flexrenko Day Trading Thread.
So what i need is some form of loop that checks whether or not the bar has not closed yet that will not lead to sierra freezing.

I'm open to suggestions or pointers towards ACSIL member functions i should be using.

Thanks!

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
REcommedations for programming help
Sierra Chart
MC PL editor upgrade
MultiCharts
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
33 thanks
Tao te Trade: way of the WLD
24 thanks
My NQ Trading Journal
14 thanks
GFIs1 1 DAX trade per day journal
11 thanks
HumbleTraders next chapter
11 thanks
  #3 (permalink)
 vegasfoster 
las vegas
 
Experience: Intermediate
Platform: Sierra Chart
Broker: Velocity/IB
Trading: 6E
Posts: 1,145 since Feb 2010
Thanks Given: 304
Thanks Received: 844


It evaluates in real time by default, you don't have to tell it to do that. Autoloop should equal 1 unless you are controlling the indexing, which it doesn't appear you are. Also, there is a tradingsystem.cpp file you can look at that has a bunch of examples in it.

Reply With Quote
  #4 (permalink)
Amnesia
Vienna Austria
 
Posts: 116 since Mar 2012
Thanks Given: 16
Thanks Received: 67

@vegasfoster

setting autoloop = 1 means that for every function call (new bar) the code will be repeated. This doesnt change the fact that if you have something along the lines of the crossover example in an if statement for executing a trade that it should not be executed if the conditions are not met.

If the If statement is not executed then the program ends at that point and has to wait being reloaded once a new bar pops up.

So this is why i thought about using a while loop, because a while loop will not terminate until the condition stops being true, this way you can do stuff like execute trades and monitor the bar while it has not closed yet. Unfortunately as you may imagine at this point the while loop needs to end and given the way its structured its going to repeat in an infinite loop right now.

So either i am dead wrong or ill have to try and rewrite the if statement and see if that works later on. In which case i would be very confused because as far as i understand the matter: If statements are skipped if the condition is not met. So in my case wouldnt it simply be skipped?

As for the tradingstudies.cpp file... almost all examples in there (from what ive seen) execute trades after a bar has closed and not while the bar is still open.

So how do i perform a condition check while a bar is still open, where the condition is not met yet, but may be met 2-3 minutes later without terminating the program and awaiting a function call

Reply With Quote
  #5 (permalink)
 Jolew 
San Jose, CA
 
Experience: Intermediate
Platform: Sierra Chart
Broker: IB
Trading: Futures
Posts: 113 since Jan 2011
Thanks Given: 54
Thanks Received: 97


Amnesia View Post
@vegasfoster

setting autoloop = 1 means that for every function call (new bar) the code will be repeated.

The study function is called every time there is new trade data or bid/ask data, so by default it is called many times while the bar is still open. See Working with Arrays and Looping - Sierra Chart. You don't have to do anything special to check for crossover intrabar. @vegasfoster is correct.

Reply With Quote
  #6 (permalink)
Amnesia
Vienna Austria
 
Posts: 116 since Mar 2012
Thanks Given: 16
Thanks Received: 67


Jolew View Post
The study function is called every time there is new trade data or bid/ask data, so by default it is called many times while the bar is still open. See Working with Arrays and Looping - Sierra Chart. You don't have to do anything special to check for crossover intrabar. @vegasfoster is correct.

You are right, got it to work. Thanks

Reply With Quote
  #7 (permalink)
 chris123 
Frankfurt + Germany
 
Experience: Intermediate
Platform: Sierra Chart,
Trading: ES/NQ
Posts: 5 since Apr 2018
Thanks Given: 5
Thanks Received: 1

Hello,

I have a similar problem. I have an exit condition that seems to be verified only when the next bar appears which of course causes exits way beyond my exit condition. The higher the time frame the worse the execution.

I use:

if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_NOT_CLOSED) return;

At the Beginning of the dll function.

If I disable it the results are getting worser while Backtesting via Auto Trade System Bar Backtest.
When I use Replay (wich takes a lot more time) the execution is precise.

If I use Target and StopOffset the execution instead of my own exit condition it is also correct, but I want to use my own created exit conditions.

Any hints ?

Reply With Quote
  #8 (permalink)
 Trembling Hand 
Melbourne, Land of Oz
 
Experience: Advanced
Platform: Sierra Chart, CQG
Broker: CQG
Trading: HSI
Posts: 246 since Jun 2011
Thanks Given: 28
Thanks Received: 360


chris123 View Post
Hello,

I have a similar problem. I have an exit condition that seems to be verified only when the next bar appears which of course causes exits way beyond my exit condition. The higher the time frame the worse the execution.

I use:

if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_NOT_CLOSED) return;

At the Beginning of the dll function.

If I disable it the results are getting worser while Backtesting via Auto Trade System Bar Backtest.
When I use Replay (wich takes a lot more time) the execution is precise.

If I use Target and StopOffset the execution instead of my own exit condition it is also correct, but I want to use my own created exit conditions.

Any hints ?

That return > if (SOME CONDITION IS TRUE) return;

Will stop any code from executing below it so is probably not the right way. But it's hard to tell without you posting more code to see what is happening.

Follow me on Twitter Reply With Quote




Last Updated on May 10, 2018


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts