Login Page - Create Account

Support Board


Date/Time: Wed, 27 Nov 2024 15:53:47 +0000



Post From: Unmanaged Automated Trading not working

[2023-08-08 09:40:57]
User538264 - Posts: 26
Here is a simple example code I am using to demonstrate this. It takes a long entry with 2 lots when there are two consecutive green bars printed on chart and places a limit sell order with 3 qty as soon as the entry is taken.
I am also attaching a picture of the chart including the service log.

You can see that as soon as I place the sell order, the position is flattened first and then the 3 lot sell order is placed.
https://www.sierrachart.com/image.php?Image=1691487560369.png




int32_t PlaceMarketEntry(SCStudyInterfaceRef sc, SCString longShort, int qty, SCString OrderTag)
{
  s_SCNewOrder NewOrder;
  NewOrder.OrderQuantity = qty;
  NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
  NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
  NewOrder.TextTag = OrderTag;


  if (longShort == "BUY")
    sc.BuyOrder(NewOrder);
  else
    sc.SellOrder(NewOrder);

  SCString msg;
  msg.Format("Market order with qty %d", qty);
  sc.AddMessageToLog(msg, 1);


  return NewOrder.InternalOrderID;

}

int32_t PlaceLimitEntry(SCStudyInterfaceRef sc, SCString longShort, int qty, double EntryPrice, SCString OrderTag)
{
  s_SCNewOrder NewOrder;
  NewOrder.OrderQuantity = qty;
  NewOrder.OrderType = SCT_ORDERTYPE_LIMIT;
  NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
  NewOrder.TextTag = OrderTag;
  NewOrder.Price1 = EntryPrice;

  if (longShort == "BUY")
    sc.BuyOrder(NewOrder);
  else
    sc.SellOrder(NewOrder);

  SCString msg;
  msg.Format("Limit order - price %.02f with qty %d", EntryPrice, qty);
  sc.AddMessageToLog(msg, 1);

  return NewOrder.InternalOrderID;

}

SCSFExport scsf_aza_testBothSidePosition(SCStudyInterfaceRef sc)
{

  SCString scriptName = "testBothSidePosition";


  if (sc.SetDefaults)
  {

    sc.GraphName = scriptName;
    sc.GraphRegion = 0; //Main chart region
    sc.AutoLoop = 1;

    sc.MaximumPositionAllowed = 500;

    sc.AllowMultipleEntriesInSameDirection = true;
    sc.AllowOppositeEntryWithOpposingPositionOrOrders = true;
    sc.CancelAllOrdersOnEntriesAndReversals = false;
    sc.CancelAllWorkingOrdersOnExit = false;
    sc.AllowEntryWithWorkingOrders = true;
    sc.AllowOnlyOneTradePerBar = false;
    sc.SupportReversals = true;

    sc.MaintainTradeStatisticsAndTradesData = true;



    return;
  }

  if (sc.IsFullRecalculation && sc.Index == 0)
    sc.ClearAllPersistentData();

  s_SCPositionData PositionData;
  sc.GetTradePosition(PositionData);


  if (sc.Close[sc.Index] > sc.Close[sc.Index - 1] && sc.Close[sc.Index - 1] > sc.Close[sc.Index - 2] && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED && !sc.IsFullRecalculation)
    PlaceMarketEntry(sc, "BUY", 2, scriptName);
  



  if (PositionData.PositionQuantity != 0 && !PositionData.WorkingOrdersExist)
  {
    PlaceLimitEntry(sc, "SELL", 3, sc.Close[sc.Index] + 10 * sc.TickSize, scriptName);
  }


}



Date Time Of Last Edit: 2023-08-08 09:44:33
imagetoSC.png / V - Attached On 2023-08-08 09:36:53 UTC - Size: 77.34 KB - 74 views