Login Page - Create Account

Support Board


Date/Time: Fri, 22 Nov 2024 22:26:00 +0000



[Programming Help] - Code optimization

View Count: 1141

[2021-09-11 22:58:38]
ertrader - Posts: 672
The attached code works as expected, compiles with the latest SC and works without issue. It is designed to take a trade signal from another study and initiate a short (-1) or long (1), then disable until re-enabled with a button push. There are some conditions (1-4) that work fine. It uses custom study buttons for enable/disable, short/long enable/disable etc. It all works ok.

The only issue I have is performance. Something about the code is inefficient. I believe it has to do with the custom study button code (rows 249-355) This code should have refresh times less than 5 ms IMO as it is not very complicated and relatively short. In studies, the MS refresh time is anywhere from 40-200 MS and more if I add more data.

If anyone has recommendations on more efficient code, please let me know!
Date Time Of Last Edit: 2021-09-11 23:08:06
imageScreenshot from 2021-09-11 18-57-49.png / V - Attached On 2021-09-11 22:58:09 UTC - Size: 102.28 KB - 241 views
imageScreenshot from 2021-09-11 18-57-24.png / V - Attached On 2021-09-11 22:58:17 UTC - Size: 1.15 KB - 236 views
attachmentAutoTrade.cpp - Attached On 2021-09-11 23:06:17 UTC - Size: 18.98 KB - 342 views
[2021-09-11 22:59:58]
ertrader - Posts: 672
#include "sierrachart.h"

SCDLLName("AutoTrade")
SCSFExport scsf_AutoTrade(SCStudyGraphRef sc)
{
  SCSubgraphRef TradeSignalIn        = sc.Subgraph[0];
  SCSubgraphRef BuyArrow          = sc.Subgraph[1];
  SCSubgraphRef SellArrow          = sc.Subgraph[2];
  SCSubgraphRef LT_Trend          = sc.Subgraph[5];
  SCSubgraphRef LT_TrendSlope        = sc.Subgraph[6];  
  SCSubgraphRef LT_Acceleration      = sc.Subgraph[8];  
  SCSubgraphRef ValuesOut1        = sc.Subgraph[9];  
  SCSubgraphRef ValuesOut2        = sc.Subgraph[10];    

  SCInputRef Enabled             = sc.Input[0];
  SCInputRef SendOrdersToService       = sc.Input[1];
  SCInputRef MaxPositions           = sc.Input[2];  
  SCInputRef MultipleEntrys         = sc.Input[3];    
  SCInputRef INTradeSignal        = sc.Input[4];
  SCInputRef INLT_Trend          = sc.Input[7];      
  SCInputRef INSignalType          = sc.Input[9];
  SCInputRef Long              = sc.Input[10];  
  SCInputRef Short            = sc.Input[11];    
  
// Bar type
  SCInputRef INBarType          = sc.Input[14];  
  
  SCInputRef Input_ACSButtonNumber    = sc.Input[15];
  SCInputRef Input_AllowCustomPropertiesForControlBarButton = sc.Input[16];
  SCInputRef Input_ACSButtonNumber1    = sc.Input[17];
  SCInputRef Input_AllowCustomPropertiesForControlBarButton1 = sc.Input[18];  
  SCInputRef Input_ACSButtonNumber2    = sc.Input[19];
  SCInputRef Input_AllowCustomPropertiesForControlBarButton2 = sc.Input[20];    
  SCInputRef Input_ACSButtonNumber3    = sc.Input[21];
  SCInputRef Input_AllowCustomPropertiesForControlBarButton3 = sc.Input[22];  
  
  SCInputRef INLTLongValue        = sc.Input[23];  
  SCInputRef INLTShortValue        = sc.Input[24];  
  SCInputRef INLTLongSlopeValue      = sc.Input[25];  
  SCInputRef INLTShortSlopeValue      = sc.Input[26];  
  SCInputRef INLTAccelerationLongValue  = sc.Input[30];  
  SCInputRef INLTAccelerationShortValue  = sc.Input[31];  
  
if(sc.SetDefaults)
{
    sc.StudyDescription="Auto Trade V4";
    sc.GraphName="Auto Trade V4";
    //sc.CalculationPrecedence = LOW_PREC_LEVEL;
//sc.DrawZeros = false;
sc.AutoLoop = true;
sc.GraphRegion = 0;
    //sc.ValueFormat = 4;    
    //sc.ScaleRangeType = SCALE_SAMEASREGION;  
    
    MaxPositions.Name = "Max Positions";
    MaxPositions.SetInt(1);
    MaxPositions.SetIntLimits(1, 10);  

    Enabled.Name = "Enabled";
    Enabled.SetYesNo(true);
    
    MultipleEntrys.Name = "Support Multiple Entries";
    MultipleEntrys.SetYesNo(false);  

    SendOrdersToService.Name = "Send Orders to Trade Service";    
    SendOrdersToService.SetYesNo(false);    

    TradeSignalIn.Name = "Trade Signal";
    TradeSignalIn.DrawStyle = DRAWSTYLE_IGNORE;
TradeSignalIn.LineWidth = 1;
TradeSignalIn.PrimaryColor = COLOR_YELLOW;
    
    BuyArrow.Name = "Buy Arrow";
    BuyArrow.DrawStyle = DRAWSTYLE_ARROWUP;
    BuyArrow.PrimaryColor = COLOR_BLUE;
    BuyArrow.LineWidth = 3;     

    SellArrow.Name = "Sell Arrow";
    SellArrow.DrawStyle = DRAWSTYLE_ARROWDOWN;
    SellArrow.PrimaryColor = COLOR_RED;
    SellArrow.LineWidth = 3;     

    LT_Trend.Name="Long Term Trend";
    LT_Trend.DrawStyle = DRAWSTYLE_IGNORE;
    LT_Trend.PrimaryColor = RGB(0,255,0);
    LT_Trend.DrawZeros = true;    

    LT_TrendSlope.Name="Long Term Trend Slope";
    LT_TrendSlope.DrawStyle = DRAWSTYLE_IGNORE;
    LT_TrendSlope.PrimaryColor = RGB(0,255,0);
    LT_TrendSlope.DrawZeros = true;    

    LT_Acceleration.Name="Long Term Acceleration";
    LT_Acceleration.DrawStyle = DRAWSTYLE_IGNORE;
    LT_Acceleration.PrimaryColor = RGB(0,255,0);
    LT_Acceleration.DrawZeros = true;      
  
    Long.Name = "Trade Long";    
    Long.SetYesNo(true);

    Short.Name = "Trade Short";    
    Short.SetYesNo(true);    

    INTradeSignal.Name = "Trade Signal";
    INTradeSignal.SetChartStudySubgraphValues(1, 1, 0);    

    INSignalType.Name = "Enter signal #:";    
INSignalType.SetInt(1);
INSignalType.SetIntLimits(1,5000);      

    INBarType.Name = "Bar Type? 1-Renko,0-Other?";
    INBarType.SetInt(0);
INBarType.SetIntLimits(0,1);      

    Input_ACSButtonNumber.Name = "ACS Control Bar Button # for Study Enable/Disable";
    Input_ACSButtonNumber.SetInt(1);
    Input_ACSButtonNumber.SetIntLimits(1, 150);

    Input_AllowCustomPropertiesForControlBarButton.Name = "Allow Custom 'Properties' for Study Button";
    Input_AllowCustomPropertiesForControlBarButton.SetYesNo(false);

    Input_ACSButtonNumber1.Name = "ACS Control Bar Button # for Long Enable/Disable";
    Input_ACSButtonNumber1.SetInt(2);
    Input_ACSButtonNumber1.SetIntLimits(1, 150);

    Input_AllowCustomPropertiesForControlBarButton1.Name = "Allow Custom 'Properties' for Study Button";
    Input_AllowCustomPropertiesForControlBarButton1.SetYesNo(false);    

    Input_ACSButtonNumber2.Name = "ACS Control Bar Button # for Short Enable/Disable";
    Input_ACSButtonNumber2.SetInt(3);
    Input_ACSButtonNumber2.SetIntLimits(1, 150);

    Input_AllowCustomPropertiesForControlBarButton2.Name = "Allow Custom 'Properties' for Study Button";
    Input_AllowCustomPropertiesForControlBarButton2.SetYesNo(false);      

    Input_ACSButtonNumber3.Name = "ACS Control Bar Button # for Send Order Enable/Disable";
    Input_ACSButtonNumber3.SetInt(4);
    Input_ACSButtonNumber3.SetIntLimits(1, 150);

    Input_AllowCustomPropertiesForControlBarButton3.Name = "Allow Custom 'Properties' for Study Button";
    Input_AllowCustomPropertiesForControlBarButton3.SetYesNo(false);      

    INLT_Trend.Name = "Long Term Trend";
    INLT_Trend.SetChartStudySubgraphValues(1, 1, 0);    
    
    INLTLongValue.Name = "LT Long Value";
    INLTLongValue.SetFloat(0.0f);      

    INLTShortValue.Name = "LT Short Value";
    INLTShortValue.SetFloat(0.0f);    

    INLTLongSlopeValue.Name = "LT Long Slope Value";
    INLTLongSlopeValue.SetFloat(0.0f);      

    INLTShortSlopeValue.Name = "LT Short Slope Value";
    INLTShortSlopeValue.SetFloat(0.0f);  

    INLTShortSlopeValue.Name = "LT Short Slope Value";
    INLTShortSlopeValue.SetFloat(0.0f);      

    INLTAccelerationLongValue.Name = "LT Acceleration Long Value";
    INLTAccelerationLongValue.SetFloat(0.0f);  
    
    INLTAccelerationShortValue.Name = "LT Acceleration Short Value";
    INLTAccelerationShortValue.SetFloat(0.0f);    

    ValuesOut1.Name="Values Out 1";
ValuesOut1.DrawStyle = DRAWSTYLE_IGNORE;
ValuesOut1.LineWidth = 1;
ValuesOut1.PrimaryColor = COLOR_YELLOW;        

    ValuesOut2.Name="Values Out 2";
ValuesOut2.DrawStyle = DRAWSTYLE_IGNORE;
ValuesOut2.LineWidth = 1;
ValuesOut2.PrimaryColor = COLOR_YELLOW;      
    
    //sc.MaintainVolumeAtPriceData = 1;
    //sc.MaintainAdditionalChartDataArrays = 1;    
// sc.AllowMultipleEntriesInSameDirection = true;
// sc.MaximumPositionAllowed = MaxPositions.GetInt();
// sc.SupportReversals = false;
// sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
// sc.SupportAttachedOrdersForTrading = true;// This variable controls whether to use or not use attached orders that are configured on the Trade Window for the chart.
// sc.UseGUIAttachedOrderSetting =true;//This variable controls whether to use the "Use Attached Orders" setting on the Trade Window for the chart
// sc.CancelAllOrdersOnEntriesAndReversals= false;
// sc.AllowEntryWithWorkingOrders = true;
// sc.CancelAllWorkingOrdersOnExit = true;
// sc.AllowOnlyOneTradePerBar = true;
// sc.MaintainTradeStatisticsAndTradesData = true;  
    sc.AllowMultipleEntriesInSameDirection = false;
    //sc.MaximumPositionAllowed = 2;
    sc.SupportReversals = false;
    sc.UseGUIAttachedOrderSetting =true;
    // This is false by default. Orders will go to the simulation system always.
    sc.SendOrdersToTradeService = false;

    sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;

    // This can be false in this function because we specify Attached Orders directly with the order which causes this to be considered true when submitting an order.
    sc.SupportAttachedOrdersForTrading = true;

    sc.CancelAllOrdersOnEntriesAndReversals= false;
    sc.AllowEntryWithWorkingOrders = false;
    sc.CancelAllWorkingOrdersOnExit = true;

    // Only 1 trade for each Order Action type is allowed per bar.
    sc.AllowOnlyOneTradePerBar = true;

    //This needs to be set to true when a trading study uses trading functions.
    sc.MaintainTradeStatisticsAndTradesData = true;  

    // Do not receive mouse pointer events when ACS Tool Control Bar button is pressed in/enabled
    //sc.ReceivePointerEvents = true;    

    return;
}
  
  SCFloatArray TradeSignalInSubgraph;  
  
  SCFloatArray LT_TrendSubgraph;  
  
  SCFloatArrayRef PriceHigh      = sc.Subgraph[1].Arrays[0];    
  SCFloatArrayRef PriceLow      = sc.Subgraph[1].Arrays[1];
  SCFloatArrayRef TradeSignal      = sc.Subgraph[1].Arrays[2];  

  SCFloatArrayRef PriceOpen      = sc.Subgraph[2].Arrays[0];    
  SCFloatArrayRef PriceClose      = sc.Subgraph[2].Arrays[1];  
  
  int SignalType          = INSignalType.GetInt();  

  float LTLongValue                = INLTLongValue.GetFloat();
  float LTShortValue                = INLTShortValue.GetFloat();  
  float LTLongSlopeValue              = INLTLongSlopeValue.GetFloat();
  float LTShortSlopeValue              = INLTShortSlopeValue.GetFloat();  
  float LTAccelerationLongValue          = INLTAccelerationLongValue.GetFloat();
  float LTAccelerationShortValue          = INLTAccelerationShortValue.GetFloat();  

  
// set renko or not renko bar type to address correct OHLC  
  int BarType            = INBarType.GetInt();  

  sc.SendOrdersToTradeService = SendOrdersToService.GetYesNo();

  sc.MaximumPositionAllowed = MaxPositions.GetInt();  
  sc.SupportTradingScaleIn = 0;
sc.SupportTradingScaleOut = 0;  


    // set ACS Tool Control Bar tool tip
    sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber.GetInt(), "Trade Enable/Disable");
    sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber1.GetInt(), "Long");
    sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber2.GetInt(), "Short");
    sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber3.GetInt(), "Send");     
    // set Custom Study Control Bar button text if the input to allow custom properties is not true. Otherwise, rely upon what the user sets.

    if (!Input_AllowCustomPropertiesForControlBarButton.GetYesNo())
     sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber.GetInt(), "Trade");
    if (!Input_AllowCustomPropertiesForControlBarButton1.GetYesNo())
     sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber1.GetInt(), "Long");
    if (!Input_AllowCustomPropertiesForControlBarButton2.GetYesNo())
     sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber2.GetInt(), "Short");   
    if (!Input_AllowCustomPropertiesForControlBarButton3.GetYesNo())
     sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber3.GetInt(), "Send");   
    // set buttons to disabled i.e. not pushed in state
    sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber.GetInt(), 0);
    sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber1.GetInt(), 0);
    sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber2.GetInt(), 0);     
    sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber3.GetInt(), 0);     
    // set button color to show enabled / disabled state

    if (Enabled.GetYesNo())
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber.GetInt(), RGB(0, 128, 255));
    else
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber.GetInt(), COLOR_RED);
  
    if (Long.GetYesNo())
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber1.GetInt(), RGB(0, 128, 255));
    else
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber1.GetInt(), COLOR_RED);
  
    if (Short.GetYesNo())
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber2.GetInt(), RGB(0, 128, 255));
    else
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber2.GetInt(), COLOR_RED);   
  
    if (SendOrdersToService.GetYesNo())
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber3.GetInt(), RGB(0, 128, 255));
    else
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber3.GetInt(), COLOR_RED);   

  if (sc.MenuEventID != 0)
  {
    if (sc.MenuEventID == Input_ACSButtonNumber.GetInt())
    {
     // reset button to disabled i.e. not pushed in state
     sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber.GetInt(), 0);
     if (Enabled.GetYesNo())
     {
      // change state of input to disabled and set color accordingly
      Enabled.SetYesNo(0);
      sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber.GetInt(), COLOR_RED);
     }
     else
     {
      // change state of input to disabled and set color accordingly
      Enabled.SetYesNo(1);
      sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber.GetInt(), RGB(0, 128, 255));
     }

    }
    else if (sc.MenuEventID == Input_ACSButtonNumber1.GetInt())
    {
      sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber1.GetInt(), 0);
      if (Long.GetYesNo())
      {
        Long.SetYesNo(0);
        sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber1.GetInt(), RGB(0, 128, 255));
      }
      else
      {
        Long.SetYesNo(1);
        sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber1.GetInt(), COLOR_RED);
      }

    }
    else if (sc.MenuEventID == Input_ACSButtonNumber2.GetInt())
    {
     sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber2.GetInt(), 0);
     if (Short.GetYesNo())
     {
      Short.SetYesNo(0);
      sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber2.GetInt(), RGB(0, 128, 255));
     }
     else
     {
      Short.SetYesNo(1);
      sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber2.GetInt(), COLOR_RED);
     }
  
    }  
    else if (sc.MenuEventID == Input_ACSButtonNumber3.GetInt())
    {
     sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber3.GetInt(), 0);
     if (SendOrdersToService.GetYesNo())
     {
      SendOrdersToService.SetYesNo(0);
      sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber3.GetInt(), RGB(0, 128, 255));
     }
     else
     {
      SendOrdersToService.SetYesNo(1);
      sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber3.GetInt(), COLOR_RED);
     }
    }    
  }

    sc.GetStudyArrayFromChartUsingID(INTradeSignal.GetChartNumber(), INTradeSignal.GetStudyID(), INTradeSignal.GetSubgraphIndex(), TradeSignalInSubgraph);    
    sc.GetStudyArrayFromChartUsingID(INLT_Trend.GetChartNumber(), INLT_Trend.GetStudyID(), INLT_Trend.GetSubgraphIndex(), LT_TrendSubgraph);    
      
//  if (sc.GetBarHasClosedStatus(sc.Index)==BHCS_BAR_HAS_CLOSED)
//  {
    
    TradeSignalIn[sc.Index] = TradeSignalInSubgraph[sc.Index];      
    LT_Trend[sc.Index] = LT_TrendSubgraph[sc.Index];
    sc.Slope(LT_Trend,LT_TrendSlope, sc.Index);    
    sc.Slope(LT_TrendSlope,LT_Acceleration, sc.Index);      
    

    PriceHigh[sc.Index]    = sc.BaseData[SC_HIGH][sc.Index];  
    PriceLow[sc.Index]    = sc.BaseData[SC_LOW][sc.Index];  
    
    if (BarType == 1)
    {  PriceOpen[sc.Index] = sc.BaseData[SC_RENKO_OPEN][sc.Index];
      PriceClose[sc.Index] = sc.BaseData[SC_RENKO_CLOSE][sc.Index];
    }
    if (BarType == 0)
    {  PriceOpen[sc.Index] = sc.BaseData[SC_OPEN][sc.Index];
      PriceClose[sc.Index] = sc.BaseData[SC_LAST][sc.Index];
    }    
    

    if (SignalType==1)
    {      
      if (Long.GetYesNo() && TradeSignal[sc.Index] < int(1))
      {
        if (TradeSignalIn[sc.Index] == int(1) && sc.Close[sc.Index]>=sc.Open[sc.Index])
        {
          BuyArrow[sc.Index] = PriceLow[sc.Index];
          TradeSignal[sc.Index] = int(1);
  
          
        }
      }    
      if (Short.GetYesNo() && TradeSignal[sc.Index] > int(-1))
      {          
        if (TradeSignalIn[sc.Index] == int(-1) && sc.Close[sc.Index]<=sc.Open[sc.Index])
        {
          SellArrow[sc.Index] = PriceHigh[sc.Index];
          TradeSignal[sc.Index] = int(-1);  
        
        }  
      }
  
    }
    else if (SignalType==2)
    {      
      if (Long.GetYesNo() && TradeSignal[sc.Index] < int(1))
      {
        if (TradeSignalIn[sc.Index] == int(1) && sc.Close[sc.Index]>=sc.Open[sc.Index] && LT_Trend[sc.Index]>=0.0000f)
        {
          BuyArrow[sc.Index] = PriceLow[sc.Index];
          
        }
      }    
      if (Short.GetYesNo() && TradeSignal[sc.Index] > int(-1))
      {          
        if (TradeSignalIn[sc.Index] == int(-1) && sc.Close[sc.Index]<=sc.Open[sc.Index] && LT_Trend[sc.Index]<=0.0000f)
        {
          SellArrow[sc.Index] = PriceHigh[sc.Index];
          TradeSignal[sc.Index] = int(-1);

        
        }  
      }
  
    }    
    else if (SignalType==3)
    {      
      if (Long.GetYesNo() && TradeSignal[sc.Index] < int(1))
      {
        if (TradeSignalIn[sc.Index] == int(1) && sc.Close[sc.Index]>=sc.Open[sc.Index] && LT_Trend[sc.Index]<=LTLongValue && LT_TrendSlope[sc.Index]>=LTLongSlopeValue && LT_Acceleration[sc.Index]>=LTAccelerationLongValue)
        {
          BuyArrow[sc.Index] = PriceLow[sc.Index];
          TradeSignal[sc.Index] = int(1);
          
        }
      }    
      if (Short.GetYesNo() && TradeSignal[sc.Index] > int(-1))
      {          
        if (TradeSignalIn[sc.Index] == int(-1) && sc.Close[sc.Index]<=sc.Open[sc.Index] && LT_Trend[sc.Index]>=LTShortValue && LT_TrendSlope[sc.Index]<=LTShortSlopeValue && LT_Acceleration[sc.Index]<=LTAccelerationShortValue)
        {
          SellArrow[sc.Index] = PriceHigh[sc.Index];
          TradeSignal[sc.Index] = int(-1);
  
        }  
      }
  
    }  
    else if (SignalType==4)
    {      
      if (Long.GetYesNo() && TradeSignal[sc.Index] < int(1))
      {
        if (TradeSignalIn[sc.Index] == int(1) && sc.Close[sc.Index]>sc.Open[sc.Index])
        {
          BuyArrow[sc.Index] = PriceLow[sc.Index];
          TradeSignal[sc.Index] = int(1);
          
        }
      }    
      if (Short.GetYesNo() && TradeSignal[sc.Index] > int(-1))
      {          
        if (TradeSignalIn[sc.Index] == int(-1) && sc.Close[sc.Index]<sc.Open[sc.Index])
        {
          SellArrow[sc.Index] = PriceHigh[sc.Index];
          TradeSignal[sc.Index] = int(-1);
  
        }  
      }
  
    }    
  
    else      
    {
      TradeSignal[sc.Index] = int(0);  
    }

    ValuesOut1[sc.Index] = TradeSignal[sc.Index];    
    ValuesOut2[sc.Index] = LT_Trend[sc.Index];    

    // if enabled, and with a trade sigal, submit order
    if (Enabled.GetYesNo() && TradeSignal[sc.Index]!=int(0))
    {

      // Create an s_SCNewOrder object.
      s_SCNewOrder NewOrder;
      NewOrder.OrderQuantity = MaxPositions.GetInt();
  //    NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
      NewOrder.TimeInForce = SCT_TIF_DAY;  
      NewOrder.OCOGroup1Quantity = 1;    

      // Buy when the signal is sent
      if (TradeSignal[sc.Index]>0.00f)
      {
        NewOrder.Price1 = PriceClose[sc.Index];
      
        int Result = (int)sc.BuyEntry(NewOrder);
        if (Result > 0 )
        {
          BuyArrow[sc.Index] = PriceLow[sc.Index];
        //Disable after order. Set button to current status
          Enabled.SetYesNo(false);
          sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber.GetInt(), Enabled.GetBoolean() );  
      
        }  
      }
      
      else if (TradeSignal[sc.Index]<0.00f)
      {
        NewOrder.Price1 = PriceClose[sc.Index];
        int Result = (int)sc.SellEntry(NewOrder);
        if (Result > 0)
        //Disable after order. Set button to current status        
        {
          SellArrow[sc.Index]= PriceHigh[sc.Index];
          Enabled.SetYesNo(false);
          sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber.GetInt(), Enabled.GetBoolean() );  
        
        }
          
      }
    }
  //}  
}
            

Date Time Of Last Edit: 2021-09-11 23:05:53
[2021-09-11 23:10:13]
ertrader - Posts: 672
For YM today, refresh time is 195 MS. Quite long!
Date Time Of Last Edit: 2021-09-11 23:11:16
imageScreenshot from 2021-09-11 19-08-40.png / V - Attached On 2021-09-11 23:09:26 UTC - Size: 3.72 KB - 244 views
[2021-09-12 16:44:59]
ertrader - Posts: 672
I would be willing to pay someone to help me get this optimized.
[2021-09-12 18:11:02]
User907968 - Posts: 823
Consider enclosing the button configuration details in a conditional block as described here - ACSIL Programming Concepts: One Time Calculations That Do Not Run During Study Updating

Maybe consider reworking the code to use manual looping - Working with ACSIL Arrays and Understanding Looping: Manual Looping/Iterating

In either case the aim would be to revise your code so as to only call configuration details once, as it is now they are called for each and every chart bar.
You should find that this change reduces the initial calculation time to single digit milliseconds.
Date Time Of Last Edit: 2021-09-12 21:17:45
[2021-09-12 20:41:47]
ForgivingComputers.com - Posts: 960
Buttons have extra overhead, because they interact with Windows itself. You don't need to set them up on every function call.

Do all the one time button setup stuff like this:

if (sc.IsFullRecalculation)
{
// set ACS Tool Control Bar tool tip
sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber.GetInt(), "Trade Enable/Disable");
sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber1.GetInt(), "Long");
sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber2.GetInt(), "Short");
sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber3.GetInt(), "Send");

// set Custom Study Control Bar button text if the input to allow custom properties is not true. Otherwise, rely upon what the user sets.
if (!Input_AllowCustomPropertiesForControlBarButton.GetYesNo())
sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber.GetInt(), "Trade");
if (!Input_AllowCustomPropertiesForControlBarButton1.GetYesNo())
sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber1.GetInt(), "Long");
if (!Input_AllowCustomPropertiesForControlBarButton2.GetYesNo())
sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber2.GetInt(), "Short");

if (!Input_AllowCustomPropertiesForControlBarButton3.GetYesNo())
sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber3.GetInt(), "Send");

// set buttons to disabled i.e. not pushed in state
sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber.GetInt(), 0);
sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber1.GetInt(), 0);
sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber2.GetInt(), 0);
sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber3.GetInt(), 0);

// set button color to show enabled / disabled state

if (Enabled.GetYesNo())
sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber.GetInt(), RGB(0, 128, 255));
else
sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber.GetInt(), COLOR_RED);

if (Long.GetYesNo())
sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber1.GetInt(), RGB(0, 128, 255));
else
sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber1.GetInt(), COLOR_RED);

if (Short.GetYesNo())
sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber2.GetInt(), RGB(0, 128, 255));
else
sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber2.GetInt(), COLOR_RED);

if (SendOrdersToService.GetYesNo())
sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber3.GetInt(), RGB(0, 128, 255));
else
sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber3.GetInt(), COLOR_RED);
}

[2021-09-12 23:33:14]
ertrader - Posts: 672
Thank you all.. I will see how this impacts usage. I used sc.IsFullRecalculation and tried conditional setting techniques initially but found that the button status would be out of sequence with the study setting the button pushed were updating. Button changes (status-color changing and study updates) are often initiated during bar updating. Removing these conditions work but apparently at a performance cost.
Date Time Of Last Edit: 2021-09-12 23:35:17
[2021-09-12 23:52:04]
ertrader - Posts: 672
Adding sc.IsFullRecalculation does help...now in the sub 5ms. Adding the bracket just before the if statements (as apposed to after) keeps the button in sync.

Thank you for the suggestions!
Date Time Of Last Edit: 2021-09-13 00:55:51
[2021-09-13 00:09:01]
ertrader - Posts: 672
  if (sc.IsFullRecalculation)

  {

    // set ACS Tool Control Bar tool tip
    sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber.GetInt(), "Trade Enable/Disable");
    sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber1.GetInt(), "Long");
    sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber2.GetInt(), "Short");
    sc.SetCustomStudyControlBarButtonHoverText(Input_ACSButtonNumber3.GetInt(), "Send");     
    // set Custom Study Control Bar button text if the input to allow custom properties is not true. Otherwise, rely upon what the user sets.

    if (!Input_AllowCustomPropertiesForControlBarButton.GetYesNo())
     sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber.GetInt(), "Trade");
    if (!Input_AllowCustomPropertiesForControlBarButton1.GetYesNo())
     sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber1.GetInt(), "Long");
    if (!Input_AllowCustomPropertiesForControlBarButton2.GetYesNo())
     sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber2.GetInt(), "Short");   
    if (!Input_AllowCustomPropertiesForControlBarButton3.GetYesNo())
     sc.SetCustomStudyControlBarButtonText(Input_ACSButtonNumber3.GetInt(), "Send");   
    // set buttons to disabled i.e. not pushed in state
    sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber.GetInt(), 0);
    sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber1.GetInt(), 0);
    sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber2.GetInt(), 0);     
    sc.SetCustomStudyControlBarButtonEnable(Input_ACSButtonNumber3.GetInt(), 0);     
    // set button color to show enabled / disabled state
  }
    if (Enabled.GetYesNo())
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber.GetInt(), RGB(0, 128, 255));
    else
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber.GetInt(), COLOR_RED);
  
    if (Long.GetYesNo())
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber1.GetInt(), RGB(0, 128, 255));
    else
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber1.GetInt(), COLOR_RED);
  
    if (Short.GetYesNo())
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber2.GetInt(), RGB(0, 128, 255));
    else
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber2.GetInt(), COLOR_RED);   
  
    if (SendOrdersToService.GetYesNo())
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber3.GetInt(), RGB(0, 128, 255));
    else
     sc.SetCustomStudyControlBarButtonColor(Input_ACSButtonNumber3.GetInt(), COLOR_RED);   

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

Login

Login Page - Create Account