Login Page - Create Account

Support Board


Date/Time: Sat, 20 Apr 2024 02:11:22 +0000



I buy at the end of a candle but it is filled in the next candle

View Count: 389

[2021-06-04 22:17:03]
User582602 - Posts: 1
Hello Team,

I have written a simple strategy to buy a contract at the close of a candle (I showed it on the chart) but I see it is executed in the next candle (not exactly at the close of the candle). My question is what is my problem?

The code that I am using to buy is as simple as follow;
  // Buy when the last price crosses the moving average from below.
  if (TradingAllowed && sc.CrossOver(Last, Subgraph_SimpMovAvg) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
  {
      Result = (int)sc.BuyEntry(NewOrder);
      //Result = sc.BuyOrder(NewOrder);
      //If there has been a successful order entry, then draw an arrow at the low of the bar.
      if (Result > 0)
      {
        r_BuyEntryInternalOrderID = NewOrder.InternalOrderID;
        SCString InternalOrderIDNumberString;
        InternalOrderIDNumberString.Format("BuyEntry Internal Order ID: %d", r_BuyEntryInternalOrderID);
        sc.AddMessageToLog(InternalOrderIDNumberString, 0);

        Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
      }
  }


I have attached a pic that shows the one-candle difference between when I asked the system to buy and when the order was executed and filled.


Thanks
imagecandle.png / V - Attached On 2021-06-04 22:13:53 UTC - Size: 59.93 KB - 207 views
[2021-06-05 10:44:14]
User907968 - Posts: 802
Review the documentation for sc.GetBarHasClosedStatus, particularly this "The very last bar in the chart is never considered a closed bar until there is a new bar added to the chart."

sc.GetBarHasClosedStatus()

At the point you are entering your order, a new bar has been added to the chart already, so although your arrow is drawn under the closed bar, the order is actually entered on the new chart bar.

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account