#include "sierrachart.h"
SCSFExport scsf_TradingExample(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_BuyExit = sc.Subgraph[1];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SellExit = sc.Subgraph[3];
SCSubgraphRef Subgraph_SimpMovAvg = sc.Subgraph[4];
SCInputRef Input_Enabled = sc.Input[0];
SCInputRef Input_TargetValue = sc.Input[1];
SCInputRef Input_StopValue = sc.Input[2];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: Using Moving Average and Target and Stop";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_BuyExit.Name = "Buy Exit";
Subgraph_BuyExit.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_BuyExit.PrimaryColor = RGB(255, 128, 128);
Subgraph_BuyExit.LineWidth = 2;
Subgraph_BuyExit.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SellExit.Name = "Sell Exit";
Subgraph_SellExit.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_SellExit.PrimaryColor = RGB(128, 255, 128);
Subgraph_SellExit.LineWidth = 2;
Subgraph_SellExit.DrawZeros = false;
Subgraph_SimpMovAvg.Name = "Simple Moving Average";
Subgraph_SimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_SimpMovAvg.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
Input_TargetValue.Name = "Target Value";
Input_TargetValue.SetFloat(2.0f);
Input_StopValue.Name = "Stop Value";
Input_StopValue.SetFloat(1.0f);
sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading Functions. This function will display a simple moving average and perform a Buy Entry when the Last price crosses the moving average from below and a Sell Entry when the Last price crosses the moving average from above. A new entry cannot occur until the Target or Stop has been hit. When an order is sent, a corresponding arrow will appear on the chart to show that an order was sent. This study will do nothing until the Enabled Input is set to Yes.";
sc.AutoLoop = 1;
sc.GraphRegion = 0;
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 10;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= true;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = false;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
return;
}
if (!Input_Enabled.GetYesNo())
return;
SCFloatArrayRef Last = sc.Close;
sc.SimpleMovAvg(Last, Subgraph_SimpMovAvg, sc.Index, 10);
if (sc.IsFullRecalculation)
return;
s_SCPositionData PositionData;
sc.GetTradePosition(PositionData) ;
float LastTradePrice = sc.Close[sc.Index];
int64_t& r_BuyEntryInternalOrderID = sc.GetPersistentInt64(1);
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TextTag = "Trading example tag";
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
int Result = 0;
bool TradingAllowed = true;
if (TradingAllowed && sc.CrossOver(Last, Subgraph_SimpMovAvg) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
{
r_BuyEntryInternalOrderID = NewOrder.InternalOrderID;
SCString InternalOrderIDNumberString;
InternalOrderIDNumberString.Format("BuyEntry Internal Order ID: %d", r_BuyEntryInternalOrderID);
sc.AddMessageToLog(InternalOrderIDNumberString, 0);
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
}
}
else if (PositionData.PositionQuantity > 0
&& (LastTradePrice <= PositionData.AveragePrice - Input_StopValue.GetFloat() ||
LastTradePrice >= PositionData.AveragePrice + Input_TargetValue.GetFloat()))
{
Result = static_cast<int>(sc.BuyExit(NewOrder));
if (Result > 0)
{
Subgraph_BuyExit[sc.Index] = sc.High[sc.Index];
}
}
else if (TradingAllowed && sc.CrossOver(Last, Subgraph_SimpMovAvg) == CROSS_FROM_TOP && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result > 0)
{
Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
}
}
else if (PositionData.PositionQuantity < 0
&& (LastTradePrice >= PositionData.AveragePrice + Input_StopValue.GetFloat() ||
LastTradePrice <= PositionData.AveragePrice - Input_TargetValue.GetFloat()))
{
Result = static_cast<int>(sc.SellExit(NewOrder));
if (Result > 0)
{
Subgraph_SellExit[sc.Index] = sc.Low[sc.Index];
}
}
if (r_BuyEntryInternalOrderID != 0)
{
s_SCTradeOrder TradeOrder;
sc.GetOrderByOrderID(r_BuyEntryInternalOrderID, TradeOrder);
if (TradeOrder.InternalOrderID == r_BuyEntryInternalOrderID)
{
bool IsOrderFilled = TradeOrder.OrderStatusCode == SCT_OSC_FILLED;
double FillPrice = TradeOrder.LastFillPrice;
}
}
}
SCSFExport scsf_TradingExampleWithAttachedOrders(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SimpMovAvg = sc.Subgraph[4];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: With Trade Window Attached Orders";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SimpMovAvg.Name = "Simple Moving Average";
Subgraph_SimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_SimpMovAvg.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading Functions. This example uses the Attached Orders defined on the chart Trade Window. This function will display a simple moving average and perform a Buy Entry when the Last price crosses the moving average from below and a Sell Entry when the Last price crosses the moving average from above. A new entry cannot occur until the Target or Stop has been hit. When an order is sent, a corresponding arrow will appear on the chart to show that an order was sent. This study will do nothing until the Enabled Input is set to Yes.";
sc.AutoLoop = 1;
sc.GraphRegion = 0;
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 10;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = true;
sc.CancelAllOrdersOnEntriesAndReversals= true;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
return;
}
if (!Input_Enabled.GetYesNo())
return;
sc.SimpleMovAvg(sc.Close, Subgraph_SimpMovAvg, sc.Index, 10);
if (sc.IsFullRecalculation)
return;
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
if (sc.CrossOver(sc.Close, Subgraph_SimpMovAvg) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
}
else if (sc.CrossOver(sc.Close, Subgraph_SimpMovAvg) == CROSS_FROM_TOP && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result > 0)
Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
}
}
SCSFExport scsf_TradingExampleWithAttachedOrdersDirectlyDefined(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SimpMovAvg = sc.Subgraph[4];
SCInputRef Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: With Hardcoded Attached Orders";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SimpMovAvg.Name = "Simple Moving Average";
Subgraph_SimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_SimpMovAvg.DrawZeros = false;
Enabled.Name = "Enabled";
Enabled.SetYesNo(0);
sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading Functions. This example uses Attached Orders defined directly within this function. This function will display a simple moving average and perform a Buy Entry when the Last price crosses the moving average from below and a Sell Entry when the Last price crosses the moving average from above. A new entry cannot occur until the Target or Stop has been hit. When an order is sent, a corresponding arrow will appear on the chart to show that an order was sent. This study will do nothing until the Enabled Input is set to Yes.";
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 5;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= true;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 1;
sc.GraphRegion = 0;
return;
}
if (!Enabled.GetYesNo())
return;
int32_t& Target1OrderID = sc.GetPersistentInt(1);
int32_t& Stop1OrderID = sc.GetPersistentInt(2);
sc.SimpleMovAvg(sc.Close, Subgraph_SimpMovAvg, sc.Index, 10);
if (sc.IsFullRecalculation)
return;
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.Target1Offset = 8 * sc.TickSize;
NewOrder.Stop1Offset = 8 * sc.TickSize;
NewOrder.OCOGroup1Quantity = 1;
if (sc.CrossOver(sc.Close, Subgraph_SimpMovAvg) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
{
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
Target1OrderID = NewOrder.Target1InternalOrderID;
Stop1OrderID = NewOrder.Stop1InternalOrderID;
}
}
else if (sc.CrossOver(sc.Close, Subgraph_SimpMovAvg) == CROSS_FROM_TOP && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result > 0)
{
Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
Target1OrderID = NewOrder.Target1InternalOrderID;
Stop1OrderID = NewOrder.Stop1InternalOrderID;
}
}
bool ExecuteModifyOrder = false;
if (ExecuteModifyOrder && (sc.Index == sc.ArraySize - 1))
{
double NewLimit = 0.0;
s_SCTradeOrder ExistingOrder;
if (sc.GetOrderByOrderID(Target1OrderID, ExistingOrder) != SCTRADING_ORDER_ERROR)
{
if (ExistingOrder.BuySell == BSE_BUY)
NewLimit = sc.Close[sc.Index] - 5*sc.TickSize;
else if (ExistingOrder.BuySell == BSE_SELL)
NewLimit = sc.Close[sc.Index] + 5*sc.TickSize;
s_SCNewOrder ModifyOrder;
ModifyOrder.InternalOrderID = Target1OrderID;
ModifyOrder.Price1 = NewLimit;
sc.ModifyOrder(ModifyOrder);
}
}
}
SCSFExport scsf_ACSILTradingTest(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_BuyExit = sc.Subgraph[1];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SellExit = sc.Subgraph[3];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Test";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_BuyExit.Name = "Buy Exit";
Subgraph_BuyExit.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_BuyExit.PrimaryColor = RGB(255, 128, 128);
Subgraph_BuyExit.LineWidth = 2;
Subgraph_BuyExit.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SellExit.Name = "Sell Exit";
Subgraph_SellExit.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_SellExit.PrimaryColor = RGB(128, 255, 128);
Subgraph_SellExit.LineWidth = 2;
Subgraph_SellExit.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
Input_Enabled. SetDescription("This input enables the study and allows it to function. Otherwise, it does nothing.");
sc.SendOrdersToTradeService = false;
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 10000;
sc.SupportReversals = true;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = true;
sc.SupportAttachedOrdersForTrading = false;
sc.UseGUIAttachedOrderSetting = false;
sc.CancelAllOrdersOnEntriesAndReversals = false;
sc.AllowEntryWithWorkingOrders = true;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = false;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = true;
sc.GraphRegion = 0;
return;
}
sc.SupportTradingScaleIn = 0;
sc.SupportTradingScaleOut = 0;
if (!Input_Enabled.GetYesNo())
return;
if (sc.LastCallToFunction)
return;
if (sc.IsFullRecalculation)
return;
if (sc.Index < sc.ArraySize-1)
return;
s_SCPositionData PositionDataForSymbolAndAccount;
sc.GetTradePositionForSymbolAndAccount(PositionDataForSymbolAndAccount, sc.Symbol, sc.SelectedTradeAccount);
s_SCPositionData PositionData;
sc.GetTradePosition(PositionData);
if (PositionData.PositionQuantity == 0 && !PositionData.WorkingOrdersExist)
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_LIMIT;
NewOrder.Price1 = sc.Close[sc.Index] - 5 * sc.TickSize;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.AttachedOrderStopAllType = SCT_ORDERTYPE_TRIGGERED_STEP_TRAILING_STOP;
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
}
else
{
double PositionQuantity = PositionData.PositionQuantity;
}
}
SCSFExport scsf_TradingExampleUsingReversals(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SimpMovAvg = sc.Subgraph[4];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: Using Reversals";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SimpMovAvg.Name = "Simple Moving Average";
Subgraph_SimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_SimpMovAvg.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading Functions and demonstrates using reversals. This study will do nothing until the Enabled Input is set to Yes.";
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 2;
sc.SupportReversals = true;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = true;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= true;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 1;
sc.GraphRegion = 0;
return;
}
if (!Input_Enabled.GetYesNo())
return;
SCFloatArrayRef BarLastArray = sc.Close;
sc.SimpleMovAvg(BarLastArray, Subgraph_SimpMovAvg, sc.Index, 3);
s_SCNewOrder NewOrder;
NewOrder.OrderType = SCT_ORDERTYPE_LIMIT;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
if (sc.CrossOver(BarLastArray, Subgraph_SimpMovAvg) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
NewOrder.Price1 = sc.Ask;
NewOrder.OrderQuantity = 1;
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
{
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
}
}
else if (sc.CrossOver(BarLastArray, Subgraph_SimpMovAvg) == CROSS_FROM_TOP && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
NewOrder.Price1 = sc.Bid;
NewOrder.OrderQuantity = 1;
int Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result > 0)
{
Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
}
}
}
SCSFExport scsf_TradingExampleWithAttachedOrdersUsingActualPrices (SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SimpMovAvg = sc.Subgraph[4];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: With Attached Orders Using Actual Prices";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SimpMovAvg.Name = "Simple Moving Average";
Subgraph_SimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_SimpMovAvg.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading Functions. This example uses Attached Orders defined directly within this function. This function will display a simple moving average and perform a Buy Entry when the Last price crosses the moving average from below and a Sell Entry when the Last price crosses the moving average from above. A new entry cannot occur until the Target or Stop has been filled. When an order is sent, a corresponding arrow will appear on the chart to show that an order was sent. This study will do nothing until the Enabled Input is set to Yes.";
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 5;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= true;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 1;
sc.GraphRegion = 0;
return;
}
if (!Input_Enabled.GetYesNo())
return;
SCFloatArrayRef ClosePriceArray = sc.Close;
int& Target1OrderID = sc.GetPersistentInt(1);
int& Stop1OrderID = sc.GetPersistentInt(2);
sc.SimpleMovAvg(ClosePriceArray, Subgraph_SimpMovAvg, sc.Index, 10);
if (sc.IsFullRecalculation)
return;
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.OCOGroup1Quantity = 1;
if (sc.CrossOver(ClosePriceArray, Subgraph_SimpMovAvg) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
NewOrder.Target1Price =sc.Close[sc.Index] + 8*sc.TickSize ;
NewOrder.Stop1Price = sc.Close[sc.Index]- 8*sc.TickSize;
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
{
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
Target1OrderID = NewOrder.Target1InternalOrderID;
Stop1OrderID = NewOrder.Stop1InternalOrderID;
}
}
else if (sc.CrossOver(ClosePriceArray, Subgraph_SimpMovAvg) == CROSS_FROM_TOP && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
NewOrder.Target1Price =sc.Close[sc.Index] - 8*sc.TickSize;
NewOrder.Stop1Price =sc.Close[sc.Index] + 8*sc.TickSize;
int Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result > 0)
{
Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
Target1OrderID = NewOrder.Target1InternalOrderID;
Stop1OrderID = NewOrder.Stop1InternalOrderID;
}
}
bool ExecuteModifyOrder = false;
if (ExecuteModifyOrder && (sc.Index == sc.ArraySize - 1))
{
double NewLimit = 0.0;
s_SCTradeOrder ExistingOrder;
if (sc.GetOrderByOrderID(Target1OrderID, ExistingOrder) != SCTRADING_ORDER_ERROR)
{
if (ExistingOrder.BuySell == BSE_BUY)
NewLimit = sc.Close[sc.Index] - 5*sc.TickSize;
else if (ExistingOrder.BuySell == BSE_SELL)
NewLimit = sc.Close[sc.Index] + 5*sc.TickSize;
s_SCNewOrder ModifyOrder;
ModifyOrder.InternalOrderID = Target1OrderID;
ModifyOrder.Price1 = NewLimit;
sc.ModifyOrder(ModifyOrder);
}
}
}
SCSFExport scsf_TradingExampleWithStopAllAttachedOrdersDirectlyDefined(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SimpMovAvg = sc.Subgraph[4];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: With Hardcoded Attached Orders (uses Stop All)";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SimpMovAvg.Name = "Simple Moving Average";
Subgraph_SimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_SimpMovAvg.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading Functions. This example uses Attached Orders defined directly within this function. This function will display a simple moving average and perform a Buy Entry when the Last price crosses the moving average from below and a Sell Entry when the Last price crosses the moving average from above. A new entry cannot occur until the Target or Stop has been hit. When an order is sent, a corresponding arrow will appear on the chart to show that an order was sent. This study will do nothing until the Enabled Input is set to Yes.";
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 5;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= true;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 1;
sc.GraphRegion = 0;
return;
}
if (!Input_Enabled.GetYesNo())
return;
SCFloatArrayRef Last = sc.Close;
int& Target1OrderID = sc.GetPersistentInt(0);
int& Target2OrderID = sc.GetPersistentInt(1);
int& StopAllOrderID = sc.GetPersistentInt(2);
int & ExecuteModifyOrder = sc.GetPersistentInt(3);
sc.SimpleMovAvg(Last, Subgraph_SimpMovAvg, sc.Index, 10);
if (sc.IsFullRecalculation)
return;
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 2;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.Target1Offset = 8*sc.TickSize;
NewOrder.Target2Offset = 12*sc.TickSize;
NewOrder.StopAllOffset = 5*sc.TickSize;
if (sc.CrossOver(Last, Subgraph_SimpMovAvg) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
{
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
ExecuteModifyOrder =1;
Target1OrderID = NewOrder.Target1InternalOrderID;
Target2OrderID = NewOrder.Target2InternalOrderID;
StopAllOrderID = NewOrder.StopAllInternalOrderID;
}
}
else if (sc.CrossOver(Last, Subgraph_SimpMovAvg) == CROSS_FROM_TOP && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result > 0)
{
Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
ExecuteModifyOrder =1;
Target1OrderID = NewOrder.Target1InternalOrderID;
Target2OrderID = NewOrder.Target2InternalOrderID;
StopAllOrderID = NewOrder.StopAllInternalOrderID;
}
}
if (ExecuteModifyOrder == 1 && (sc.Index == sc.ArraySize - 1))
{
ExecuteModifyOrder = 0;
double NewPrice = 0.0;
s_SCTradeOrder ExistingOrder;
if (sc.GetOrderByOrderID(StopAllOrderID, ExistingOrder) != SCTRADING_ORDER_ERROR)
{
if (IsWorkingOrderStatus(ExistingOrder.OrderStatusCode))
{
if (ExistingOrder.BuySell == BSE_BUY)
NewPrice = ExistingOrder.Price1 + 10*sc.TickSize;
else if (ExistingOrder.BuySell == BSE_SELL)
NewPrice = ExistingOrder.Price1 - 10*sc.TickSize;
s_SCNewOrder ModifyOrder;
ModifyOrder.InternalOrderID = StopAllOrderID;
ModifyOrder.Price1 = NewPrice;
sc.ModifyOrder(ModifyOrder);
}
}
}
}
SCSFExport scsf_ACSILTradingOCOExample(SCStudyInterfaceRef sc)
{
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading OCO Order Example";
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
Input_Enabled. SetDescription("This input enables the study and allows it to function. Otherwise, it does nothing.");
sc.SendOrdersToTradeService = false;
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 10;
sc.SupportReversals = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= false;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = false;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = false;
sc.GraphRegion = 0;
return;
}
sc.SupportTradingScaleIn = 0;
sc.SupportTradingScaleOut = 0;
if (!Input_Enabled.GetYesNo())
return;
if (sc.IsFullRecalculation)
return;
float Last = sc.BaseDataIn[SC_LAST][sc.Index];
s_SCPositionData PositionData;
sc.GetTradePosition(PositionData);
if(PositionData.PositionQuantity != 0 || PositionData.WorkingOrdersExist)
return;
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 4;
NewOrder.OrderType = SCT_ORDERTYPE_OCO_BUY_STOP_SELL_STOP;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.Price1 = Last + 10 * sc.TickSize;
NewOrder.Price2 = Last - 10 * sc.TickSize;
NewOrder.AttachedOrderTarget1Type = SCT_ORDERTYPE_LIMIT;
NewOrder.Target1Offset = 4 * sc.TickSize;
NewOrder.AttachedOrderStop1Type = SCT_ORDERTYPE_STOP;
NewOrder.Stop1Offset = 4 * sc.TickSize;
NewOrder.Target1Offset_2 = 8 * sc.TickSize;
NewOrder.Stop1Price_2 = NewOrder.Price2 + 2;
if (sc.SubmitOCOOrder(NewOrder, sc.ArraySize - 1) >0)
{
int BuyStopOrder1ID = NewOrder.InternalOrderID;
int SellStopOrder2ID = NewOrder.InternalOrderID2;
}
}
SCSFExport scsf_TradingExample1WithAdvancedAttachedOrders(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SimpMovAvg = sc.Subgraph[4];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: 1 With Advanced Attached Orders";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SimpMovAvg.Name = "Simple Moving Average";
Subgraph_SimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_SimpMovAvg.PrimaryColor = RGB(255,255,0);
Subgraph_SimpMovAvg.LineWidth = 2;
Subgraph_SimpMovAvg.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading functions. This example uses Attached Orders defined directly within this function. It demonstrates more advanced use of Attached Orders. A new entry cannot occur until the Target or Stop has been filled or canceled. When an order is sent, a corresponding arrow will appear on the chart to show that an order was sent. This study will do nothing until the 'Enabled' Input is set to Yes.";
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 5;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= true;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 1;
sc.GraphRegion = 0;
return;
}
if (!Input_Enabled.GetYesNo())
return;
SCFloatArrayRef Last = sc.Close;
sc.SimpleMovAvg(Last, Subgraph_SimpMovAvg, sc.Index, 10);
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 2;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.Target1Offset = 4 * sc.TickSize;
NewOrder.AttachedOrderTarget1Type = SCT_ORDERTYPE_LIMIT;
NewOrder.Target2Offset = 8 * sc.TickSize;
NewOrder.AttachedOrderTarget2Type = SCT_ORDERTYPE_LIMIT;
NewOrder.StopAllOffset = 8*sc.TickSize;
NewOrder.AttachedOrderStopAllType = SCT_ORDERTYPE_STEP_TRAILING_STOP_LIMIT;
NewOrder.TrailStopStepPriceAmount = 4 * sc.TickSize;
if (sc.CrossOver(Last, Subgraph_SimpMovAvg) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
{
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
}
}
else if (sc.CrossOver(Last, Subgraph_SimpMovAvg) == CROSS_FROM_TOP && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result > 0)
{
Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
}
}
}
SCSFExport scsf_TradingExample2WithAdvancedAttachedOrders(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SimpMovAvg = sc.Subgraph[4];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: 2 With Advanced Attached Orders";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SimpMovAvg.Name = "Simple Moving Average";
Subgraph_SimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_SimpMovAvg.PrimaryColor = RGB(255,255,0);
Subgraph_SimpMovAvg.LineWidth = 2;
Subgraph_SimpMovAvg.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading functions. This example uses Attached Orders defined directly within this function. It demonstrates more advanced use of Attached Orders. A new entry cannot occur until the Targets and Stops have been filled or canceled. When an order is sent, a corresponding arrow will appear on the chart to show that an order was sent. This study will do nothing until the Enabled Input is set to Yes.";
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 5;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= true;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 1;
sc.GraphRegion = 0;
return;
}
if (!Input_Enabled.GetYesNo())
return;
SCFloatArrayRef Last = sc.Close;
sc.SimpleMovAvg(Last, Subgraph_SimpMovAvg, sc.Index, 10);
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 2;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.Target1Offset = 4 * sc.TickSize;
NewOrder.AttachedOrderTarget1Type = SCT_ORDERTYPE_LIMIT;
NewOrder.Target2Offset = 12 * sc.TickSize;
NewOrder.AttachedOrderTarget2Type = SCT_ORDERTYPE_LIMIT;
NewOrder.StopAllOffset = 8 * sc.TickSize;
NewOrder.AttachedOrderStopAllType = SCT_ORDERTYPE_TRIGGERED_TRAILING_STOP_LIMIT_3_OFFSETS;
NewOrder.TriggeredTrailStopTriggerPriceOffset = 8 * sc.TickSize;
NewOrder.TriggeredTrailStopTrailPriceOffset = 4 * sc.TickSize;
NewOrder.MoveToBreakEven.Type = MOVETO_BE_ACTION_TYPE_OCO_GROUP_TRIGGERED;
NewOrder.MoveToBreakEven.BreakEvenLevelOffsetInTicks = 1;
NewOrder.MoveToBreakEven.TriggerOCOGroup= OCO_GROUP_1;
if (sc.CrossOver(Last, Subgraph_SimpMovAvg) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
{
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
}
}
else if (sc.CrossOver(Last, Subgraph_SimpMovAvg) == CROSS_FROM_TOP && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result > 0)
{
Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
}
}
}
SCSFExport scsf_TradingSystemStudySubgraphCrossover(SCStudyInterfaceRef sc)
{
SCInputRef Input_Line1Ref = sc.Input[0];
SCInputRef Input_Line2Ref = sc.Input[1];
SCInputRef Input_Enabled = sc.Input[2];
SCInputRef Input_SendTradeOrdersToTradeService = sc.Input[3];
SCInputRef Input_MaximumPositionAllowed = sc.Input[4];
SCInputRef Input_ActionOnCrossoverWhenInPosition = sc.Input[5];
SCInputRef Input_EvaluateOnBarCloseOnly = sc.Input[6];
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[1];
if (sc.SetDefaults)
{
sc.GraphName = "Trading System - Study Subgraph Crossover";
sc.StudyDescription = "A trading system that enters a new position or \
exits an existing one on the crossover of two study Subgraphs. \
When Line1 crosses Line2 from below, the system will go Long. \
When Line1 crosses Line2 from above, the system will go Short.";
sc.AutoLoop = 1;
sc.GraphRegion = 0;
sc.CalculationPrecedence = LOW_PREC_LEVEL;
Input_Line1Ref.Name = "Line1";
Input_Line1Ref.SetStudySubgraphValues(1, 0);
Input_Line2Ref.Name = "Line2";
Input_Line2Ref.SetStudySubgraphValues(1, 0);
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(false);
Input_SendTradeOrdersToTradeService.Name = "Send Trade Orders to Trade Service";
Input_SendTradeOrdersToTradeService.SetYesNo(false);
Input_MaximumPositionAllowed.Name = "Maximum Position Allowed";
Input_MaximumPositionAllowed.SetInt(1);
Input_ActionOnCrossoverWhenInPosition.Name = "Action on Crossover When in Position";
Input_ActionOnCrossoverWhenInPosition.SetCustomInputStrings("No Action;Exit on Crossover;Reverse on Crossover");
Input_ActionOnCrossoverWhenInPosition.SetCustomInputIndex(0);
Input_EvaluateOnBarCloseOnly.Name = "Evaluate on Bar Close Only";
Input_EvaluateOnBarCloseOnly.SetYesNo(true);
Subgraph_BuyEntry.Name = "Buy";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_POINT_ON_HIGH;
Subgraph_BuyEntry.LineWidth = 4;
Subgraph_SellEntry.Name = "Sell";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_POINT_ON_LOW;
Subgraph_SellEntry.LineWidth = 4;
sc.AllowMultipleEntriesInSameDirection = false;
sc.SupportReversals = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.UseGUIAttachedOrderSetting = true;
sc.CancelAllOrdersOnEntriesAndReversals = true;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
return;
}
sc.MaximumPositionAllowed = Input_MaximumPositionAllowed.GetInt();
sc.SendOrdersToTradeService = Input_SendTradeOrdersToTradeService.GetYesNo();
if (!Input_Enabled.GetYesNo())
return;
if (Input_EvaluateOnBarCloseOnly.GetYesNo()
&& sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_NOT_CLOSED)
{
return;
}
SCFloatArray StudyLine1;
sc.GetStudyArrayUsingID(Input_Line1Ref.GetStudyID(), Input_Line1Ref.GetSubgraphIndex(), StudyLine1);
SCFloatArray StudyLine2;
sc.GetStudyArrayUsingID(Input_Line2Ref.GetStudyID(), Input_Line2Ref.GetSubgraphIndex(), StudyLine2);
s_SCPositionData PositionData;
if (!sc.IsFullRecalculation)
sc.GetTradePosition(PositionData);
sc.SupportReversals = false;
if (sc.CrossOver(StudyLine1, StudyLine2) == CROSS_FROM_BOTTOM)
{
Subgraph_BuyEntry[sc.Index] = 1;
if (!sc.IsFullRecalculation && !sc.DownloadingHistoricalData)
{
if (Input_ActionOnCrossoverWhenInPosition.GetIndex() == 1
&& PositionData.PositionQuantity < 0)
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 0;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
int Result = static_cast<int>(sc.SellExit(NewOrder));
if (Result < 0)
sc.AddMessageToLog(sc.GetTradingErrorTextMessage(Result), false);
}
else if (Input_ActionOnCrossoverWhenInPosition.GetIndex() == 2
&& PositionData.PositionQuantity < 0)
{
sc.SupportReversals = true;
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = sc.TradeWindowOrderQuantity;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result < 0)
sc.AddMessageToLog(sc.GetTradingErrorTextMessage(Result), false);
}
else if (PositionData.PositionQuantity == 0)
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = sc.TradeWindowOrderQuantity;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result < 0)
sc.AddMessageToLog(sc.GetTradingErrorTextMessage(Result), false);
}
}
}
if (sc.CrossOver(StudyLine1, StudyLine2) == CROSS_FROM_TOP)
{
Subgraph_SellEntry[sc.Index] = 1;
if (!sc.IsFullRecalculation && !sc.DownloadingHistoricalData)
{
if (Input_ActionOnCrossoverWhenInPosition.GetIndex() == 1
&& PositionData.PositionQuantity > 0)
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 0;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
int Result = static_cast<int>(sc.BuyExit(NewOrder));
if (Result < 0)
sc.AddMessageToLog(sc.GetTradingErrorTextMessage(Result), false);
}
else if (Input_ActionOnCrossoverWhenInPosition.GetIndex() == 2
&& PositionData.PositionQuantity > 0)
{
sc.SupportReversals = true;
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = sc.TradeWindowOrderQuantity;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
int Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result < 0)
sc.AddMessageToLog(sc.GetTradingErrorTextMessage(Result), false);
}
else if (PositionData.PositionQuantity == 0)
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = sc.TradeWindowOrderQuantity;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
int Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result < 0)
sc.AddMessageToLog(sc.GetTradingErrorTextMessage(Result), false);
}
}
}
}
SCSFExport scsf_TradingOrderEntryExamples(SCStudyInterfaceRef sc)
{
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Order Entry Examples";
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
sc.StudyDescription = "This study function is an example of submitting various types of orders. It does not actually submit any orders, it only contains code examples for submitting orders.";
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 10;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= true;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = false;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 1;
sc.GraphRegion = 0;
return;
}
if (!Input_Enabled.GetYesNo())
return;
if (0)
{
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 2;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.Target1Offset = 10*sc.TickSize;
NewOrder.AttachedOrderTarget1Type = SCT_ORDERTYPE_LIMIT;
NewOrder.StopAllOffset = 10*sc.TickSize;
NewOrder.AttachedOrderStopAllType = SCT_ORDERTYPE_STOP;
NewOrder.MoveToBreakEven.Type=MOVETO_BE_ACTION_TYPE_OFFSET_TRIGGERED;
NewOrder.MoveToBreakEven.TriggerOffsetInTicks= 5;
NewOrder.MoveToBreakEven.BreakEvenLevelOffsetInTicks= 0;
sc.BuyOrder(NewOrder);
}
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 2;
NewOrder.OrderType = SCT_ORDERTYPE_TRAILING_STOP;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.Price1 = sc.BaseData[SC_LAST][sc.Index] - 10*sc.TickSize;
sc.SellOrder(NewOrder);
}
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 2;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.AttachedOrderStop1Type = SCT_ORDERTYPE_TRAILING_STOP;
NewOrder.Stop1Offset = 10 * sc.TickSize;
sc.BuyOrder(NewOrder);
}
}
}
SCSFExport scsf_TradingExampleMACrossOverWithAttachedOrders(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[1];
SCSubgraphRef Subgraph_FastSimpMovAvg = sc.Subgraph[4];
SCSubgraphRef Subgraph_SlowSimpMovAvg = sc.Subgraph[5];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: Moving Average Crossover with Attached Orders";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_FastSimpMovAvg.Name = "Fast Moving Average";
Subgraph_FastSimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_FastSimpMovAvg.PrimaryColor = RGB(255, 255, 255);
Subgraph_FastSimpMovAvg.DrawZeros = false;
Subgraph_FastSimpMovAvg.LineWidth = 2;
Subgraph_SlowSimpMovAvg.Name = "Slow Moving Average";
Subgraph_SlowSimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_SlowSimpMovAvg.PrimaryColor = RGB(0, 255, 0);
Subgraph_SlowSimpMovAvg.DrawZeros = false;
Subgraph_SlowSimpMovAvg.LineWidth = 2;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 2;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= false;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 1;
sc.GraphRegion = 0;
return;
}
if (!Input_Enabled.GetYesNo())
return;
int& Target1OrderID = sc.GetPersistentInt(1);
int& Stop1OrderID = sc.GetPersistentInt(2);
sc.SimpleMovAvg(sc.Close, Subgraph_FastSimpMovAvg, 10);
sc.SimpleMovAvg(sc.Close, Subgraph_SlowSimpMovAvg, 20);
s_SCPositionData PositionData;
sc.GetTradePosition(PositionData);
if(PositionData.PositionQuantity != 0)
return;
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.Target1Offset = 8*sc.TickSize;
NewOrder.Stop1Offset = 8*sc.TickSize;
NewOrder.OCOGroup1Quantity = 1;
if (sc.CrossOver(Subgraph_FastSimpMovAvg, Subgraph_SlowSimpMovAvg) == CROSS_FROM_BOTTOM && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
{
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
Target1OrderID = NewOrder.Target1InternalOrderID;
Stop1OrderID = NewOrder.Stop1InternalOrderID;
}
}
else if (sc.CrossOver(Subgraph_FastSimpMovAvg, Subgraph_SlowSimpMovAvg) == CROSS_FROM_TOP && sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_CLOSED)
{
int Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result > 0)
{
Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
Target1OrderID = NewOrder.Target1InternalOrderID;
Stop1OrderID = NewOrder.Stop1InternalOrderID;
}
}
}
SCSFExport scsf_TradingExampleModifyStopAttachedOrders(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[1];
SCSubgraphRef Subgraph_FastSimpMovAvg = sc.Subgraph[4];
SCSubgraphRef Subgraph_SlowSimpMovAvg = sc.Subgraph[5];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: Modify Stop Attached Orders";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_FastSimpMovAvg.Name = "Fast Moving Average";
Subgraph_FastSimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_FastSimpMovAvg.PrimaryColor = RGB(255, 255, 255);
Subgraph_FastSimpMovAvg.DrawZeros = false;
Subgraph_FastSimpMovAvg.LineWidth = 2;
Subgraph_SlowSimpMovAvg.Name = "Slow Moving Average";
Subgraph_SlowSimpMovAvg.DrawStyle = DRAWSTYLE_LINE;
Subgraph_SlowSimpMovAvg.PrimaryColor = RGB(0, 255, 0);
Subgraph_SlowSimpMovAvg.DrawZeros = false;
Subgraph_SlowSimpMovAvg.LineWidth = 2;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 2;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= false;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 1;
sc.GraphRegion = 0;
return;
}
if (!Input_Enabled.GetYesNo())
return;
int OrderIndex = 0;
while (true)
{
s_SCTradeOrder OrderDetails;
if (sc.GetOrderByIndex(OrderIndex, OrderDetails) == SCTRADING_ORDER_ERROR)
break;
++OrderIndex;
if (!OrderDetails.IsWorking())
continue;
if (!OrderDetails.IsAttachedOrder())
continue;
if (OrderDetails.OrderTypeAsInt != SCT_ORDERTYPE_STOP)
continue;
double NewPrice = OrderDetails.Price1;
if(NewPrice == OrderDetails.LastModifyPrice1)
continue;
s_SCNewOrder ModifyOrder;
ModifyOrder.InternalOrderID = OrderDetails.InternalOrderID;
ModifyOrder.Price1 = NewPrice;
sc.ModifyOrder(ModifyOrder);
}
}
SCSFExport scsf_TradingExampleGetPositionData(SCStudyInterfaceRef sc)
{
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: Get Position Data";
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 2;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= false;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 0;
sc.GraphRegion = 0;
return;
}
if (sc.GetBarHasClosedStatus(sc.UpdateStartIndex) != BHCS_BAR_HAS_CLOSED)
return;
s_SCPositionData PositionData;
sc.GetTradePosition(PositionData);
SCString PositionDataMessage;
PositionDataMessage.Format("MaximumOpenPositionProfit: %f. MaximumOpenPositionLoss: %f."
, PositionData.MaximumOpenPositionProfit
, PositionData.MaximumOpenPositionLoss);
sc.AddMessageToLog(PositionDataMessage, 0);
}
SCSFExport scsf_TradingExampleIteratingOrderList(SCStudyInterfaceRef sc)
{
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: Iterating Order List";
sc.SendOrdersToTradeService = false;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 0;
sc.GraphRegion = 0;
return;
}
int Index = 0;
s_SCTradeOrder OrderDetails;
while( sc.GetOrderByIndex(Index, OrderDetails) != SCTRADING_ORDER_ERROR)
{
++Index;
if (!IsWorkingOrderStatus(OrderDetails.OrderStatusCode))
continue;
if (OrderDetails.ParentInternalOrderID != 0)
continue;
int InternalOrderID = OrderDetails.InternalOrderID;
}
int TargetInternalOrderID = -1;
int StopInternalOrderID = -1;
int ParentInternalOrderID = 0;
sc.GetAttachedOrderIDsForParentOrder(ParentInternalOrderID, TargetInternalOrderID, StopInternalOrderID);
}
SCSFExport scsf_TradingExampleOrdersForDifferentSymbolAndAccount(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_BuyExit = sc.Subgraph[1];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SellExit = sc.Subgraph[3];
SCInputRef Input_Enabled = sc.Input[0];
SCInputRef Input_ReferenceChartForPrice = sc.Input[1];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: Orders for Different Symbol and Account";
sc.StudyDescription = "This function demonstrates submitting, modifying and canceling an order for a different symbol than the chart the trading study is applied to";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_BuyExit.Name = "Buy Exit";
Subgraph_BuyExit.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_BuyExit.PrimaryColor = RGB(255, 128, 128);
Subgraph_BuyExit.LineWidth = 2;
Subgraph_BuyExit.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SellExit.Name = "Sell Exit";
Subgraph_SellExit.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_SellExit.PrimaryColor = RGB(128, 255, 128);
Subgraph_SellExit.LineWidth = 2;
Subgraph_SellExit.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
Input_Enabled.SetDescription("This input enables the study and allows it to function. Otherwise, it does nothing.");
Input_ReferenceChartForPrice.Name = "Reference Chart For Price";
Input_ReferenceChartForPrice.SetChartNumber(0);
sc.SendOrdersToTradeService = false;
sc.AllowMultipleEntriesInSameDirection = true;
sc.MaximumPositionAllowed = 20000;
sc.SupportReversals = true;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = true;
sc.SupportAttachedOrdersForTrading = false;
sc.UseGUIAttachedOrderSetting = false;
sc.CancelAllOrdersOnEntriesAndReversals= true;
sc.AllowEntryWithWorkingOrders = true;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = false;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.ReceiveNotificationsForChangesToOrdersPositionsForAnySymbol = true;
sc.AutoLoop = true;
sc.GraphRegion = 0;
return;
}
sc.SupportTradingScaleIn = 0;
sc.SupportTradingScaleOut = 0;
int& r_BarIndexOfOrder = sc.GetPersistentInt(1);
int& r_InternalOrderID = sc.GetPersistentInt(2);
int& r_PerformedOrderModification = sc.GetPersistentInt(3);
if (!Input_Enabled.GetYesNo())
{
r_BarIndexOfOrder = 0;
r_InternalOrderID = 0;
r_PerformedOrderModification = 0;
return;
}
if (sc.LastCallToFunction)
return;
if (sc.ChartIsDownloadingHistoricalData(Input_ReferenceChartForPrice.GetChartNumber())
|| sc.IsFullRecalculation
|| sc.ServerConnectionState != SCS_CONNECTED
)
{
return;
}
if (sc.Index < sc.ArraySize - 1)
return;
SCFloatArray LastPriceArrayFromChartToTrade;
sc.GetChartArray
( Input_ReferenceChartForPrice.GetChartNumber()
, SC_LAST
, LastPriceArrayFromChartToTrade
);
if (LastPriceArrayFromChartToTrade.GetArraySize() == 0)
return;
int LastIndexOfArrayFromChartToTrade = LastPriceArrayFromChartToTrade.GetArraySize() - 1;
const char* SymbolToTrade = "XAUUSD";
const char* TradeAccount = "Sim3";
const int OrderQuantity = 100;
s_SCPositionData PositionData;
sc.GetTradePositionForSymbolAndAccount(PositionData, SymbolToTrade, sc.SelectedTradeAccount);
if (PositionData.PositionQuantity == 0 && PositionData.WorkingOrdersExist == 0 && r_InternalOrderID == 0)
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = OrderQuantity;
NewOrder.OrderType = SCT_ORDERTYPE_LIMIT;
NewOrder.TimeInForce = SCT_TIF_DAY;
NewOrder.Price1
= LastPriceArrayFromChartToTrade[LastIndexOfArrayFromChartToTrade] - 10 * sc.TickSize;
NewOrder.Target1Offset = 8*sc.TickSize;
NewOrder.Stop1Offset = 8*sc.TickSize;
NewOrder.OCOGroup1Quantity = OrderQuantity;
NewOrder.Symbol = SymbolToTrade;
NewOrder.TradeAccount = TradeAccount;
int Result = static_cast<int>(sc.BuyOrder(NewOrder));
if (Result > 0)
{
r_BarIndexOfOrder = sc.Index;
r_InternalOrderID = NewOrder.InternalOrderID;
}
}
else if (r_InternalOrderID != 0)
{
s_SCTradeOrder ExistingOrderDetails;
if (sc.GetOrderByOrderID(r_InternalOrderID, ExistingOrderDetails))
{
if (IsWorkingOrderStatus(ExistingOrderDetails.OrderStatusCode) )
{
if (r_PerformedOrderModification == 0 && sc.Index >= r_BarIndexOfOrder + 1)
{
s_SCNewOrder ModifyOrder;
ModifyOrder.InternalOrderID = r_InternalOrderID;
ModifyOrder.Price1 = LastPriceArrayFromChartToTrade[LastIndexOfArrayFromChartToTrade] - 12 * sc.TickSize;
if (sc.ModifyOrder(ModifyOrder) > 0)
r_PerformedOrderModification = 1;
}
else if (sc.Index >= r_BarIndexOfOrder + 2)
{
sc.CancelOrder(r_InternalOrderID);
}
}
}
}
}
SCSFExport scsf_TradingExampleWithSingleAttachedOrder(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: With Single Attached Order";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading Functions.\
This example uses a single Attached Order defined directly within this function.";
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 5;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals = false;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 1;
sc.GraphRegion = 0;
return;
}
if (!Input_Enabled.GetYesNo())
return;
int& Stop1OrderID = sc.GetPersistentInt(2);
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_LIMIT;
NewOrder.TimeInForce = SCT_TIF_DAY;
NewOrder.Stop1Offset = 8*sc.TickSize;
NewOrder.OCOGroup1Quantity = 1;
if (0)
{
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
{
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
Stop1OrderID = NewOrder.Stop1InternalOrderID;
}
}
else if (0)
{
int Result = static_cast<int>(sc.SellEntry(NewOrder));
if (Result > 0)
{
Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
Stop1OrderID = NewOrder.Stop1InternalOrderID;
}
}
}
SCSFExport scsf_TradingScaleInExample(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_BuyExit = sc.Subgraph[1];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SellExit = sc.Subgraph[3];
SCInputRef Input_Enabled = sc.Input[0];
SCInputRef Input_TargetValue = sc.Input[1];
SCInputRef Input_StopValue = sc.Input[2];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: Scale In";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_BuyExit.Name = "Buy Exit";
Subgraph_BuyExit.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_BuyExit.PrimaryColor = RGB(255, 128, 128);
Subgraph_BuyExit.LineWidth = 2;
Subgraph_BuyExit.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SellExit.Name = "Sell Exit";
Subgraph_SellExit.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_SellExit.PrimaryColor = RGB(128, 255, 128);
Subgraph_SellExit.LineWidth = 2;
Subgraph_SellExit.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
Input_TargetValue.Name = "Target Offset in Ticks";
Input_TargetValue.SetFloat(10.0f);
Input_StopValue.Name = "Stop Offset in Ticks";
Input_StopValue.SetFloat(10.0f);
sc.StudyDescription = "This study function is an example of how to use the ACSIL Trading Functions and increase the Trade Position quantity by scaling in.";
sc.AutoLoop = 1;
sc.GraphRegion = 0;
sc.AllowMultipleEntriesInSameDirection = true;
sc.MaximumPositionAllowed = 2;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = true;
sc.CancelAllOrdersOnEntriesAndReversals= false;
sc.AllowEntryWithWorkingOrders = true;
sc.CancelAllWorkingOrdersOnExit = false;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;
return;
}
sc.SupportTradingScaleIn = true;
sc.SupportTradingScaleOut = false;
if (!Input_Enabled.GetYesNo())
return;
s_SCPositionData PositionData;
sc.GetTradePosition(PositionData) ;
int& r_BuyEntryInternalOrderID = sc.GetPersistentInt(1);
bool ConditionForInitialBuyEntry = true;
bool ConditionForSecondBuyEntry = true;
if (ConditionForInitialBuyEntry && PositionData.PositionQuantity == 0
&& !PositionData.WorkingOrdersExist)
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.Target1Offset = Input_TargetValue.GetFloat() * sc.TickSize;
NewOrder.Stop1Offset = Input_StopValue.GetFloat() * sc.TickSize;
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
{
r_BuyEntryInternalOrderID = NewOrder.InternalOrderID;
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
}
}
else if (ConditionForSecondBuyEntry
&& PositionData.PositionQuantity == 1
&& !PositionData.NonAttachedWorkingOrdersExist
)
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
int Result = static_cast<int>(sc.BuyEntry(NewOrder));
if (Result > 0)
{
r_BuyEntryInternalOrderID = NewOrder.InternalOrderID;
Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
}
}
}
SCSFExport scsf_ACSILTradingTest2(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_BuyExit = sc.Subgraph[1];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SellExit = sc.Subgraph[3];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Test 2";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_BuyExit.Name = "Buy Exit";
Subgraph_BuyExit.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_BuyExit.PrimaryColor = RGB(255, 128, 128);
Subgraph_BuyExit.LineWidth = 2;
Subgraph_BuyExit.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SellExit.Name = "Sell Exit";
Subgraph_SellExit.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_SellExit.PrimaryColor = RGB(128, 255, 128);
Subgraph_SellExit.LineWidth = 2;
Subgraph_SellExit.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
Input_Enabled.SetDescription("This Input enables the study and allows it to function. Otherwise, it does nothing.");
sc.SendOrdersToTradeService = false;
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 4;
sc.SupportReversals = true;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = true;
sc.SupportAttachedOrdersForTrading = false;
sc.UseGUIAttachedOrderSetting = false;
sc.CancelAllOrdersOnEntriesAndReversals = false;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = false;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = false;
sc.GraphRegion = 0;
return;
}
sc.SupportTradingScaleIn = 0;
sc.SupportTradingScaleOut = 0;
if (!Input_Enabled.GetYesNo())
return;
if (sc.LastCallToFunction)
return;
if (sc.IsFullRecalculation)
return;
s_SCPositionData PositionData;
sc.GetTradePosition(PositionData);
if (PositionData.PositionQuantity == 0)
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 4;
NewOrder.Price1 = sc.BaseDataIn[SC_LAST][sc.ArraySize - 1] - (20 * sc.TickSize);
NewOrder.OrderType = SCT_ORDERTYPE_LIMIT;
NewOrder.Target1Offset = 8 * sc.TickSize;
NewOrder.Stop1Offset = 8 * sc.TickSize;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.TextTag = "Tag";
sc.BuyEntry(NewOrder, sc.ArraySize - 1);
}
if (PositionData.WorkingOrdersExist)
return;
}
SCSFExport scsf_GetOrderFillEntryExample(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_BuyExit = sc.Subgraph[1];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[2];
SCSubgraphRef Subgraph_SellExit = sc.Subgraph[3];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "GetOrderFillEntry Example";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_BuyExit.Name = "Buy Exit";
Subgraph_BuyExit.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_BuyExit.PrimaryColor = RGB(255, 128, 128);
Subgraph_BuyExit.LineWidth = 2;
Subgraph_BuyExit.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Subgraph_SellExit.Name = "Sell Exit";
Subgraph_SellExit.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_SellExit.PrimaryColor = RGB(128, 255, 128);
Subgraph_SellExit.LineWidth = 2;
Subgraph_SellExit.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
Input_Enabled.SetDescription("This input enables the study and allows it to function. Otherwise, it does nothing.");
sc.SendOrdersToTradeService = false;
sc.AllowMultipleEntriesInSameDirection = false;
sc.MaximumPositionAllowed = 10000;
sc.SupportReversals = true;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = true;
sc.SupportAttachedOrdersForTrading = false;
sc.UseGUIAttachedOrderSetting = false;
sc.CancelAllOrdersOnEntriesAndReversals = false;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = true;
sc.AllowOnlyOneTradePerBar = false;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = true;
sc.GraphRegion = 0;
return;
}
sc.SupportTradingScaleIn = 0;
sc.SupportTradingScaleOut = 0;
if (!Input_Enabled.GetYesNo())
return;
if (sc.LastCallToFunction)
return;
int& PriorOrderFillEntrySize = sc.GetPersistentInt(1);
int CurrentOrderFillEntrySize = sc.GetOrderFillArraySize();
if (CurrentOrderFillEntrySize != PriorOrderFillEntrySize)
{
PriorOrderFillEntrySize = CurrentOrderFillEntrySize;
if (CurrentOrderFillEntrySize > 0)
{
s_SCOrderFillData OrderFillData;
sc.GetOrderFillEntry(CurrentOrderFillEntrySize - 1, OrderFillData);
SCString OrderFillMessage;
uint32_t OrderQuantity = static_cast<uint32_t>(OrderFillData.Quantity);
OrderFillMessage.Format("New order fill: InternalOrderID = %u, FillPrice = %f, Quantity = %u", OrderFillData.InternalOrderID, OrderFillData.FillPrice, OrderQuantity);
sc.AddMessageToLog(OrderFillMessage, 0);
s_SCPositionData SCPositionData;
if (sc.GetTradePosition(SCPositionData))
{
SCString PositionDataMessage;
PositionDataMessage.Format("Position Data: AveragePrice = %f, AllWorkingBuyOrdersQuantity = %d, AllWorkingSellOrdersQuantity = %d", SCPositionData.AveragePrice, static_cast<int>(SCPositionData.AllWorkingBuyOrdersQuantity), static_cast<int> (SCPositionData.AllWorkingSellOrdersQuantity));
sc.AddMessageToLog(PositionDataMessage, 0);
}
}
}
}
SCSFExport scsf_TradingExampleUnmanagedAutomatedTrading(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[1];
SCInputRef Input_Enabled = sc.Input[0];
if (sc.SetDefaults)
{
sc.GraphName = "Trading Example: UnmanagedAutomatedTrading";
Subgraph_BuyEntry.Name = "Buy Entry";
Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0);
Subgraph_BuyEntry.LineWidth = 2;
Subgraph_BuyEntry.DrawZeros = false;
Subgraph_SellEntry.Name = "Sell Entry";
Subgraph_SellEntry.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0);
Subgraph_SellEntry.LineWidth = 2;
Subgraph_SellEntry.DrawZeros = false;
Input_Enabled.Name = "Enabled";
Input_Enabled.SetYesNo(0);
sc.AllowMultipleEntriesInSameDirection = true;
sc.MaximumPositionAllowed = 3;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = true;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals = false;
sc.AllowEntryWithWorkingOrders = true;
sc.CancelAllWorkingOrdersOnExit = false;
sc.AllowOnlyOneTradePerBar = false;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.AutoLoop = 0;
sc.GraphRegion = 0;
return;
}
if (!Input_Enabled.GetYesNo())
return;
int& r_MenuID = sc.GetPersistentIntFast(1);
if (sc.LastCallToFunction)
{
sc.RemoveACSChartShortcutMenuItem(sc.ChartNumber, r_MenuID);
return;
}
if (sc.IsFullRecalculation != 0)
{
if (r_MenuID <= 0)
{
r_MenuID = sc.AddACSChartShortcutMenuItem(sc.ChartNumber, "Execute auto trade");
}
}
if (sc.MenuEventID != 0)
{
if (sc.MenuEventID == r_MenuID)
{
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 3;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.Target1Offset = 50 * sc.TickSize;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
NewOrder.Stop1Offset = 50 * sc.TickSize;
sc.BuyEntry(NewOrder);
s_SCNewOrder ExitOrder;
ExitOrder.OrderQuantity = 1;
ExitOrder.OrderType = SCT_ORDERTYPE_MARKET;
sc.SellEntry(ExitOrder);
}
}
}