Login Page - Create Account

Support Board


Date/Time: Fri, 10 Jan 2025 16:44:23 +0000



Post From: ACSIL - Unexpected reversal when using sc.SellOrder() and sc.BuyOrder() to flatten

[2016-09-15 08:04:39]
William O - Posts: 16
When sc.SupportReversals is true and I'm flattening a position using sc.SellOrder() or sc.BuyOrder(), I unexpectedly get a reversal. When I run the study below and enter a long market position while connected to live streaming data, I get a reversal when I should be flat. Can you please let me know if this is a problem on my end or yours. Thanks in advance!


SCSFExport scsf_UnexpectedReversal(SCStudyInterfaceRef sc)
{
  if (sc.SetDefaults)
  {
    sc.GraphName = "X: Unexpected Reversal";
    sc.StudyDescription = "";

    sc.SupportReversals = true;

    sc.AutoLoop = true;
    sc.GraphRegion = 0;

    sc.FreeDLL = 0;

    return;
  }

  if (sc.ServerConnectionState == SCS_CONNECTED &&
    sc.Index == (sc.ArraySize - 1) &&
    !sc.DownloadingHistoricalData &&
    !sc.IsFullRecalculation)
  {
    const char* TradeAccount = "Sim1";

    s_SCPositionData OrigPositionData;
    int returnVal4 = sc.GetTradePosition(OrigPositionData);

    if (!OrigPositionData.WorkingOrdersExist && OrigPositionData.PositionQuantity > 0 )
    {
      s_SCNewOrder o;
      o.OrderType = SCT_MARKET;
      o.Symbol = sc.Symbol;
      o.TimeInForce = SCT_TIF_DAY;
      o.TradeAccount = TradeAccount;
      o.OrderQuantity = OrigPositionData.PositionQuantity;
      int return0 = sc.SellOrder(o);
      if (return0 <= 0)
        sc.AddMessageToLog(sc.GetTradingErrorTextMessage(return0), 1);
    }
  }
}