Skip to content

Instantly share code, notes, and snippets.

@mirismaili
Created January 24, 2021 12:48
Show Gist options
  • Save mirismaili/66d3e7b3f40f3948a0e625eb362b8b27 to your computer and use it in GitHub Desktop.
Save mirismaili/66d3e7b3f40f3948a0e625eb362b8b27 to your computer and use it in GitHub Desktop.
Simple algorithmic trading with SierraChart
#include "sierrachart.h"
SCDLLName("SCTradingExample1")
/*
Overview
--------
An example of a trading system that enters a new position or
reverses 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.
Comments
--------
* Let the user of this trading system study select the two study
Subgraph lines to monitor for a crossover. This is accomplished using
sc.Input[].SetStudySubgraphValues. In the Study Settings the user is
provided with list boxes to select the study and subgraph.
* The example uses the Auto Trade Management reversal functionality by
setting sc.SupportReversals to 1. This means that we simply call
sc.BuyEntry for a long and sc.SellEntry for a short with the number of
contracts we want to long/short. In the example the number of contracts
is 1.
So:
** If we are flat, we will enter 1 contract long/short.
** If we are currently short, Sierra Chart will reverse the position
for us and we will be 1 long.
** If we are currently long, Sierra Chart will reverse the Position
for us and we will be 1 short.
* For the simplicity of the example, the study process events on the
close of the bar.
* To keep the example simple, the study uses market order types to enter the Position.
* Note that if the system enters a Position, and the user manually
closes the Position, the system will remain flat until the next
crossover at which point a new Position will be established.
*/
SCSFExport scsf_MovingAverageCrossover(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_Avg1 = sc.Subgraph[0];
SCSubgraphRef Subgraph_Avg2 = sc.Subgraph[1];
SCSubgraphRef Subgraph_CrossFromBottom = sc.Subgraph[2];
SCSubgraphRef Subgraph_CrossFromTop = sc.Subgraph[3];
SCInputRef Input_MAType1 = sc.Input[0];
SCInputRef Input_Data1 = sc.Input[1];
SCInputRef Input_Length1 = sc.Input[2];
SCInputRef Input_MAType2 = sc.Input[3];
SCInputRef Input_Data2 = sc.Input[4];
SCInputRef Input_Length2 = sc.Input[5];
if (sc.SetDefaults)
{
sc.GraphName = "Moving Average Crossover 7";
sc.GraphRegion = 0;
sc.ValueFormat = VALUEFORMAT_INHERITED;
sc.AutoLoop = 1;
Subgraph_Avg1.Name = "Avg1";
Subgraph_Avg1.DrawStyle = DRAWSTYLE_LINE;
Subgraph_Avg1.PrimaryColor = RGB(0,255,0);
Subgraph_Avg2.Name = "Avg2";
Subgraph_Avg2.DrawStyle = DRAWSTYLE_LINE;
Subgraph_Avg2.PrimaryColor = RGB(255,0,255);
Subgraph_CrossFromBottom.Name = "Cross From Bottom";
Subgraph_CrossFromBottom.DrawStyle = DRAWSTYLE_ARROW_UP;
Subgraph_CrossFromBottom.PrimaryColor = RGB(127,127,255);
Subgraph_CrossFromBottom.LineWidth = 2;
Subgraph_CrossFromTop.Name = "Cross From Top";
Subgraph_CrossFromTop.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Subgraph_CrossFromTop.PrimaryColor = RGB(255,63,0);
Subgraph_CrossFromTop.LineWidth = 2;
Input_MAType1.Name = "Moving Average Type 1";
Input_MAType1.SetMovAvgType(MOVAVGTYPE_SIMPLE);
Input_Data1.Name = "Input Data 1";
Input_Data1.SetInputDataIndex(SC_LAST);
Input_Length1.Name = "Length 1";
Input_Length1.SetInt(10);
Input_Length1.SetIntLimits(1, MAX_STUDY_LENGTH);
Input_MAType2.Name = "Moving Average Type 2";
Input_MAType2.SetMovAvgType(MOVAVGTYPE_SIMPLE);
Input_Data2.Name = "Input Data 2";
Input_Data2.SetInputDataIndex(SC_LAST);
Input_Length2.Name = "Length 2";
Input_Length2.SetInt(20);
Input_Length2.SetIntLimits(1, MAX_STUDY_LENGTH);
return;
}
sc.DataStartIndex = max(Input_Length1.GetInt(), Input_Length2.GetInt());
sc.MovingAverage(sc.BaseDataIn[Input_Data1.GetInputDataIndex()], Subgraph_Avg1, Input_MAType1.GetMovAvgType(), Input_Length1.GetInt());
sc.MovingAverage(sc.BaseDataIn[Input_Data2.GetInputDataIndex()], Subgraph_Avg2, Input_MAType2.GetMovAvgType(), Input_Length2.GetInt());
if(Input_Length1.GetInt() <= Input_Length2.GetInt() && sc.CrossOver(Subgraph_Avg1, Subgraph_Avg2) == CROSS_FROM_TOP)//Sell
{
sc.AddMessageToLog("Sell1 is TRUE", 0);
Subgraph_CrossFromBottom[sc.Index] = 0;
Subgraph_CrossFromTop[sc.Index] = sc.High[sc.Index];
// create a market order and enter short
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
sc.SellEntry(NewOrder);
//sc.PlaySound(50);
}
else if(Input_Length1.GetInt() <= Input_Length2.GetInt() && sc.CrossOver(Subgraph_Avg1, Subgraph_Avg2) == CROSS_FROM_BOTTOM)//Buy
{
sc.AddMessageToLog("Buy1 is TRUE", 0);
Subgraph_CrossFromBottom[sc.Index] = sc.Low[sc.Index];
Subgraph_CrossFromTop[sc.Index] = 0;
// Create a market order and enter long.
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
sc.BuyEntry(NewOrder);
//sc.PlaySound(1);
}
else if(Input_Length1.GetInt() > Input_Length2.GetInt() && sc.CrossOver(Subgraph_Avg1, Subgraph_Avg2) == CROSS_FROM_TOP)//Buy
{
sc.AddAlertLine("Buy2 is TRUE");
Subgraph_CrossFromBottom[sc.Index] = sc.Low[sc.Index];
Subgraph_CrossFromTop[sc.Index] = 0;
// Create a market order and enter long.
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
sc.BuyEntry(NewOrder);
sc.PlaySound(2, "Buy 2", 1);
}
else if(Input_Length1.GetInt() > Input_Length2.GetInt() && sc.CrossOver(Subgraph_Avg1, Subgraph_Avg2) == CROSS_FROM_BOTTOM)//Sell
{
sc.AddAlertLine("Sell2 is TRUE");
Subgraph_CrossFromBottom[sc.Index] = 0;
Subgraph_CrossFromTop[sc.Index] = sc.High[sc.Index];
// create a market order and enter short
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = 1;
NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;
sc.SellEntry(NewOrder);
sc.PlaySound(12, "Sell 2", 1);
}
else
{
Subgraph_CrossFromBottom[sc.Index] = 0;
Subgraph_CrossFromTop[sc.Index] = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment