Login Page - Create Account

Support Board


Date/Time: Mon, 25 Nov 2024 22:34:30 +0000



[Programming Help] - Request for ASCIL Code

View Count: 280

[2024-02-01 17:19:24]
User719760 - Posts: 6
I have tried implementing this way but it is placing orders even when I am not in position.
Can You please share me the full code.


#include "sierrachart.h"

SCDLLName("Target with SLs")

SCSFExport scsf_TargetStopLossInTicks(SCStudyInterfaceRef sc)
{
static bool ordersPlaced = false; // Static variable to track if orders have been placed

if (sc.SetDefaults)
{
// Set the study name and other properties
sc.GraphName = "Target and Stop Loss in Ticks";
sc.StudyDescription = "Place target and stop loss in ticks for working orders";
sc.AutoLoop = 1;
sc.FreeDLL = 1;

// Define inputs for target and stop loss in ticks
sc.Input[0].Name = "Target in Ticks";
sc.Input[0].SetInt(70); // Set a default value

sc.Input[1].Name = "Stop Loss in Ticks";
sc.Input[1].SetInt(40); // Set a default value

return;
}

s_SCPositionData PositionData;

if (sc.PositionData.PositionQuantity >= 1)
{
// Check if orders are already placed
if (!ordersPlaced)
{
// Create an s_SCNewOrder object.
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_LIMIT;
NewOrder.TextTag = "Position Taken of 1 Qty";
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.Stop1Offset = sc.Input[1].GetInt() * sc.TickSize;
NewOrder.Target1Offset = sc.Input[0].GetInt() * sc.TickSize;

// Place the orders
sc.SellEntry(NewOrder);
sc.BuyEntry(NewOrder);

// Set the flag to true to indicate that orders have been placed
ordersPlaced = true;
}
}
else
{
// Reset the flag when there is no position
ordersPlaced = false;
}
}
[2024-02-01 20:11:26]
ForgivingComputers.com - Posts: 960
I see an issue here. You need to use sc.GetTradePosition():

s_SCPositionData PositionData;

// Need to get the trade position before you can use Position Data
sc.GetTradePosition(PositionData);

if (sc.PositionData.PositionQuantity >= 1)

[2024-02-02 13:21:05]
emmanuel - Posts: 58
The position quantity is > 0 when there's a long position and < 0 when there's a short position. See Automated Trading From an Advanced Custom Study: s_SCPositionData Structure Members

Use: if (PositionData.PositionQuantity != 0)

`if (!ordersPlaced)` always returns false because the state of the `ordersPlaced` variable is not retained between invocations of the study function. Use a persistent variable instead: See Persistent Variable Functions


if (sc.SetDefaults)
{
...

// There are no bool persistent variables. Use an int.
int& ordersPlaced = sc.GerPersistentInt(1);
return;
}

[2024-02-02 13:21:05]
emmanuel - Posts: 58
The position quantity is > 0 when there's a long position and < 0 when there's a short position. See Automated Trading From an Advanced Custom Study: s_SCPositionData Structure Members

Use: if (PositionData.PositionQuantity != 0)

`if (!ordersPlaced)` always returns false because the state of the `ordersPlaced` variable is not retained between invocations of the study function. Use a persistent variable instead: See Persistent Variable Functions


if (sc.SetDefaults)
{
...

// There are no bool persistent variables. Use an int.
int& ordersPlaced = sc.GerPersistentInt(1);
return;
}

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

Login

Login Page - Create Account