Login Page - Create Account

Support Board


Date/Time: Tue, 26 Nov 2024 04:26:39 +0000



[Programming Help] - Liquidation ACSIL

View Count: 564

[2023-08-07 21:12:19]
User373245 - Posts: 42
I need to create a liquidation function....as all my trades are a day trade with a stop and limit that I need to liquidate at 1pm EST....can I use a combination of sc.FlattenPosition(); with Input_EndTimeForAllowedTimeRange function. Can I load sc.FlattenPosition(Input_EndTimeForAllowedTimeRange); Or maybe build a variable to Input_EndTimeForAllowedTimeRange and then use it in the sc.FlattenPosition function..???
[2023-08-07 21:12:44]
User373245 - Posts: 42
Liquidating if they do not hit the stop or limit
[2023-08-10 09:27:00]
ForgivingComputers.com - Posts: 960
Something like this should work. Using SCDateTime persistent variables, save the time when the study begins, and the next time through compare the current time and the previous time with the cutoff time. If the Current Time is equal to or past the cutoff, and the previous time is before the cutoff, then
sc.FlattenAndCancelAllOrders();
Converting the cutoff time to a DateTime (i.e. Today's date + Cutoff Time) will make the comparisons simpler.
[2024-01-03 19:39:04]
User373245 - Posts: 42
is something off with how I built the time variables ...they show up in the log the same....like my trade will execute at say 02:38:00 and my liquidation time reads in the log as 19:00:00 but the trade liquidates immediately as it opens. Hence I tried building it a couple ways. I built it without the milliseconds becuase I thought it was throwing it off. But I have played around with a bunch of variations...if I remove the if for flattening at the time...the script performs perfectly as it should but when I try to ad any variation for flattening trades it flattens as soon as it opens no matter what variation I use.


int Hour = 19;
  int Minute = 0;
  int Second = 0;
  int MilliSecond = 0;
  SCDateTime liquidationTime = SCDateTime(Hour, Minute, Second, MilliSecond);
  
// Create another SCDateTime object for comparison without milliseconds
  int hour2 = liquidationTime.GetHour();
  int minute2 = liquidationTime.GetMinute();
  int second2 = liquidationTime.GetSecond();

SCDateTime liquidatoinTime2;
liquidatoinTime2.SetTimeHMS(hour2, minute2, second2);

  
// Current Time

SCDateTime currentDateTime = sc.GetCurrentDateTime();
int Hour1 = 0;
int Minute1 = 0;
int Second1 = 0;
currentDateTime.GetTimeHMS(Hour1, Minute1, Second1);



if(currentDateTime >= liquidationTime){
sc.FlattenAndCancelAllOrders()
}
[2024-01-04 15:58:56]
ForgivingComputers.com - Posts: 960

int& TradeFlattened = sc.GetPersistentInt(0); // reset to zero when in a trade

// Set Liquidation Time
int Hour = 19;
int Minute = 0;
int Second = 0;
SCDateTime liquidationTime = SetTime(HMS_TIME(Hour, Minute, Second);

// Get Current Date Time
SCDateTime CurrentDateTime;
CurrentDateTime = sc.CurrentSystemDateTime;

// Get Current Time
SCDateTime CurrentTime;
CurrentTime = CurrentDateTime.GetTime();

// flatten if liquidation time
if(TradeFlattened == 0 &&
CurrentTime >= liquidationTime &&
CurrentTime < liquidationTime + SCDateTime::SECONDS(1))
{
sc.FlattenAndCancelAllOrders()
TradeFlattened = 1;
}

[2024-01-09 21:44:07]
User373245 - Posts: 42
/ Set Liquidation Time

int Hour = 19;
int Minute = 0;
int Second = 0;
SCDateTime liquidationTime = SetTime(HMS_TIME(Hour, Minute, Second); I cant get this to compile.

this will compile
int hour = 18;
  int minute = 00;
  int second = 0;

SCDateTime liquidatoinTime;
liquidatoinTime.SetTimeHMS(hour, minute, second);

but neither liquidate the trade...it does however not close it out immediately as it opens???

I have played around with a couple different options and none of them flatten the trade...This does compile. Possible the if statement needs to be inside the new order loop or I need to get the order number also...which I also tired from the SC instructions page but the order came up as zero in the log and that did not work either! ???

#include "sierrachart.h"
SCDLLName("Phil Code - Automated Trading AUDJPY")
#include<iostream>
using namespace std;



SCSFExport scsf_AutomatedTradingAUDJPY(SCStudyInterfaceRef sc)
{
  SCString msg;
  
  SCInputRef Input_Enabled = sc.Input[0];
  SCInputRef Input_AllowTradingOnlyDuringTimeRange = sc.Input[1];
  SCInputRef Input_StartTimeForAllowedTimeRange = sc.Input[2];
  SCInputRef Input_EndTimeForAllowedTimeRange = sc.Input[3];
  SCInputRef Input_AllowOnlyOneTradePerBar = sc.Input[4];
  SCInputRef Input_SupportReversals = sc.Input[5];
  SCInputRef Input_LongSignalSelection = sc.Input[6];
  SCInputRef Input_ShortSignalSelection = sc.Input[7];
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[8];
  SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[9];
  SCInputRef Input_CustomTime = sc.Input[10];
  SCInputRef Input_OrderQuantity = sc.Input[11];


  


  
  
  if(sc.SetDefaults){
  
  sc.GraphName = "AUDJPY AUTO";
  sc.GraphRegion = 0;
  
   sc.AutoLoop = 1;
    sc.GraphRegion = 0;
    sc.CalculationPrecedence = LOW_PREC_LEVEL;
    
    Input_AllowTradingOnlyDuringTimeRange.Name = "Allow Trading Only During Time Range";
    Input_AllowTradingOnlyDuringTimeRange.SetYesNo(1);
   Input_StartTimeForAllowedTimeRange.Name = "Start Time For Allowed Time Range";
    Input_StartTimeForAllowedTimeRange.SetTime(HMS_TIME(21, 00, 00));
   Input_EndTimeForAllowedTimeRange.Name = "End Time For Allowed Time Range";
    Input_EndTimeForAllowedTimeRange.SetTime(HMS_TIME(15, 00, 00));

Input_AllowOnlyOneTradePerBar.Name = "Allow Only One Trade per Bar";
    Input_AllowOnlyOneTradePerBar.SetYesNo(1);
Input_SupportReversals.Name = "Support Reversals";
    Input_SupportReversals.SetYesNo(1);


    

Input_LongSignalSelection.Name = "Selct Long Signal";
     Input_LongSignalSelection.SetChartStudySubgraphValues(1,4, 0);
    
    
    
     Input_ShortSignalSelection.Name = "Select Short Signal";
     Input_ShortSignalSelection.SetChartStudySubgraphValues(1,9, 0);
    
     Subgraph_BuyEntry.Name = "Buy Title";
     Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_POINT_ON_HIGH;
     Subgraph_BuyEntry.LineWidth = 3;
    
     Subgraph_SellEntry.Name = "Sell Title";
     Subgraph_SellEntry.DrawStyle = DRAWSTYLE_POINT_ON_LOW;
     Subgraph_SellEntry.LineWidth = 3;
    
    Input_CustomTime.Name = "Liquidate Time";
    Input_CustomTime.SetTime(HMS_TIME(17,00,00));
    
    Input_OrderQuantity.Name = "Set lot size";
    Input_OrderQuantity.SetInt(1);

  return;
  
  }
  
  //current time

SCDateTime CurrentDateTime;
CurrentDateTime = sc.CurrentSystemDateTime;

SCDateTime CurrentTime;
CurrentTime = CurrentDateTime.GetTime();

//liquidationTime

int Hour = 18;
int Minute = 00;
int Second = 0;
SCDateTime liquidationTime;
liquidationTime.SetTime(HMS_TIME(18,00,00));


int &RememberedOrderID = sc.GetPersistentInt(1);
bool CancelOrder = false;

int& TradeFlattened = sc.GetPersistentInt(0);
  

  //Alert signal array
  
  SCFloatArray LongArray;
sc.GetStudyArrayUsingID(Input_LongSignalSelection.GetStudyID(), Input_LongSignalSelection.GetSubgraphIndex(), LongArray);

    if (LongArray.GetArraySize() == 1)
return;

SCFloatArray ShortArray;
sc.GetStudyArrayUsingID(Input_ShortSignalSelection.GetStudyID(), Input_ShortSignalSelection.GetSubgraphIndex(), ShortArray);

if (ShortArray.GetArraySize() == -1)

return;
  
  if(LongArray[sc.Index] == 1)
  
{


int OrderQuantity = 1;

Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
  s_SCNewOrder NewOrder;
  NewOrder.OrderQuantity = Input_OrderQuantity.GetInt();
  NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
  NewOrder.Target1Offset = 4.5 * sc.TickSize;
NewOrder.Stop1Offset = 3.5 * sc.TickSize;
  sc.MaximumPositionAllowed = OrderQuantity;
  RememberedOrderID = NewOrder.InternalOrderID;

sc.BuyEntry(NewOrder);
  


}
  
  if(ShortArray[sc.Index] == 1)
  {

  
int OrderQuantity = 1;
  Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
  s_SCNewOrder NewOrder;
  NewOrder.OrderQuantity = Input_OrderQuantity.GetInt();
  NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
  NewOrder.Target1Offset = 4.5 * sc.TickSize;
NewOrder.Stop1Offset = 3.5 * sc.TickSize;
  sc.MaximumPositionAllowed = OrderQuantity;
  RememberedOrderID = NewOrder.InternalOrderID;
  sc.SellEntry(NewOrder);
  

  }

  
// cancel working orders
if(TradeFlattened == 0 &&

CurrentTime >= liquidationTime &&

CurrentTime < liquidationTime + SCDateTime::SECONDS(1))

{

sc.FlattenAndCancelAllOrders();

TradeFlattened = 1;

}




}

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

Login

Login Page - Create Account