Login Page - Create Account

Support Board


Date/Time: Thu, 23 Jan 2025 09:35:07 +0000



How to limit the number of open trades

View Count: 1517

[2018-11-13 21:34:24]
Yoda - Posts: 106
I cannot seem to limit the number of open trades. I have tried two methods to limit open trades to only one (1):

Method 1 - sc.MaximumPositionAllowed

Although I have the following line of code in the sc.SetDefaults section:

sc.MaximumPositionAllowed = 1;


the Trade Activity Log is showing thousands of open positions. (see the attached screenshot of my Trader Activity Log)

This is occurring with a Single Chart replay in ver 1841.


Method 2 - PositionQuantity in Buy and Sell code

Following the documentation here: Automated Trading From an Advanced Custom Study: sc.GetTradePosition my code includes the following:

s_SCPositionData PositionData;
sc.GetTradePosition(PositionData);

int Quantity = PositionData.PositionQuantity; // Access the quantity

...

// Buy Order
if (Quantity == 0 && sc.CrossOver(Cross_CloseOrLow, LowOfDay) == CROSS_FROM_TOP)
{
Result = (int)sc.BuyOrder(NewBuyOrder);
...
}

The crossover works fine, but when I print out the Quantity in the Message Log, it is always showing as zero. Therefore the strategy keeps on buying.

Note: Setting sc.AllowOnlyOneTradePerBar to true significantly reduces the number of trades, but I would like to get the above two methods working properly.

Any suggestions on how I can get both Method 1 and 2 working?
imageTrade Activity Log Max Open Quantity 2018-11-13 1508.png / V - Attached On 2018-11-13 21:21:43 UTC - Size: 59.85 KB - 272 views
[2018-11-13 23:00:16]
ForgivingComputers.com - Posts: 990
Try adding parentheses around the two parts of the AND statement:
if ((Quantity == 0) && (sc.CrossOver(Cross_CloseOrLow, LowOfDay) == CROSS_FROM_TOP))

{

Result = (int)sc.BuyOrder(NewBuyOrder);

...

}

and see if that helps. Sometimes the logic isn't always what you want unless you are very explicit.
[2018-11-13 23:49:10]
Yoda - Posts: 106
Thank you for the suggestion. But unfortunately adding parentheses did not help. I think the issue is that Quantity is not being populated or is being cleared by the time Buy and Sell is being called. Although I don't think it's necessary since Quantity should not be changing until it's called again, I did assign Quantity to a Persistent variable... but that didn't help either. The following is a snip-it code I am using:


s_SCPositionData PositionData;
sc.GetTradePosition(PositionData);

int Quantity = PositionData.PositionQuantity; // Access the quantity

int& Quantity_Persist = sc.GetPersistentInt(6);
  
Quantity_Persist = Quantity;

...

// Buy Order

if ((Quantity == 0) && (sc.CrossOver(Cross_CloseOrLow, LowOfDay) == CROSS_FROM_TOP))
{
Result = (int)sc.BuyOrder(NewBuyOrder);
...

SCString DebugString;
DebugString.Format("Quantity: %d, Quantity_Persist: %d", Quantity, Quantity_Persist);
sc.AddMessageToLog(DebugString, 0);
}

[2018-11-14 06:50:45]
Sierra Chart Engineering - Posts: 104368
We tested sc.MaximumPositionAllowed = 1;

In the sc.SetDefaults code block and it did work properly:

Auto-trade: Replay 60X: YMZ18 1 Min #1 | Trading Test 2 | BuyEntry | Bar start date-time: 2018-11-13 21:13:00.000 | BuyEntry signal is ignored because maximum Long Position quantity allowed has been reached or would be exceeded with new order. Max. Position allowed: 1. Resulting Position without working orders: 2. Resulting Position with all working orders except exits: 2. Resulting Position with Market working orders: 2 | 2018-11-14 01:48:26

Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2018-11-14 06:51:08

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

Login

Login Page - Create Account