Support Board
Date/Time: Sat, 23 Nov 2024 02:33:29 +0000
Post From: Code optimization
[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
|