Login Page - Create Account

Support Board


Date/Time: Thu, 28 Nov 2024 00:42:25 +0000



Post From: Delayed

[2023-03-23 18:50:00]
User352781 - Posts: 24
Hi,
Here is the program in C++.
My problem is that it shows no graph on my chart. I will try to send a screen shot of what I see when I add this to my custom study for your reference.



#include <sierrachart.h>

SCDLLName("Moving Average Strategy")

// Set the values of the moving average period and the buy/sell threshold
SCSFExport scsf_MovingAverageStrategy(SCStudyInterfaceRef sc)
{
SCInputRef movingAverageLength = sc.Input[0];
SCInputRef buyThreshold = sc.Input[1];
SCInputRef sellThreshold = sc.Input[2];
SCInputRef shares = sc.Input[3];

// Set the data series for the close price
SCSubgraphRef close = sc.Subgraph[0];
close.Name = "Close";
close.DrawStyle = DRAWSTYLE_LINE;
sc.Input[0].SetInputDataIndex(SC_LAST);

// Create a subgraph for the moving average
SCSubgraphRef movingAverage = sc.Subgraph[1];
movingAverage.Name = "Moving Average";
movingAverage.DrawStyle = DRAWSTYLE_LINE;

// Set the moving average length
sc.MovingAverage(sc.Close, movingAverage, MOVAVGTYPE_SIMPLE, movingAverageLength.GetInt());

// Initialize the position to flat
int currentPosition = 0;

// Loop through the chart data
for (int i = sc.UpdateStartIndex; i < sc.ArraySize; i++)
{
// Check if the price has crossed above the moving average by the buy threshold
if (close > movingAverage && close[i-1] <= movingAverage[i-1] - buyThreshold.GetFloat())
{
// Enter a long position
sc.BuyEntry(i, shares.GetInt());

// Set the current position to long
currentPosition = 1;
}
// Check if the price has crossed below the moving average by the sell threshold
else if (close < movingAverage && close[i-1] >= movingAverage[i-1] + sellThreshold.GetFloat())
{
// Enter a short position
sc.SellEntry(i, shares.GetInt());

// Set the current position to short
currentPosition = -1;
}
// Check if the current position is long and the price has crossed below the moving average
else if (currentPosition == 1 && close < movingAverage)
{
// Exit the long position
sc.SellEntry(i, shares.GetInt());

// Set the current position to flat
currentPosition = 0;
}
// Check if the current position is short and the price has crossed above the moving average
else if (currentPosition == -1 && close > movingAverage)
{
// Exit the short position
sc.BuyEntry(i, shares.GetInt());

// Set the current position to flat
currentPosition = 0;
}
}
}




https://www.sierrachart.com/image.php?Image=1679597300708.png