Repository navigation
Expand file tree
/
Copy pathDISCStudies.cpp
More file actions
109 lines (79 loc) · 3.45 KB
/
Copy pathDISCStudies.cpp
File metadata and controls
109 lines (79 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "sierrachart.h"
#include "common.h"
SCDLLName("Danijel Ilisin's SierraChart Studies")
/*==========================================================================*/
SCSFExport scsf_ATRToRisk(SCStudyInterfaceRef sc)
{
SCInputRef Input_Enabled = sc.Input[0];
SCInputRef Input_AtrStudyPointer = sc.Input[1];
SCInputRef Input_Atr_Multiplier = sc.Input[2];
SCInputRef Input_MaxRiskPerTrade = sc.Input[3];
SCString MessageText;
if (sc.SetDefaults)
{
sc.GraphName = "ATR2Risk";
sc.StudyDescription = "This study is automatically adjusting the current contract size based on ATR and a configured money risk value.";
sc.AutoLoop = 1;//Auto looping
sc.GraphRegion = 0;
sc.ScaleRangeType = SCALE_SAMEASREGION;
Input_Enabled.Name = "ATR2Risk Enabled";
Input_Enabled.SetYesNo(true);
Input_Enabled.SetDescription("Enable/Disable the study");
Input_Atr_Multiplier.Name = "ATR Multiplier";
Input_Atr_Multiplier.SetDouble(1.5);
Input_Atr_Multiplier.SetDoubleLimits(0.1, 100);
Input_Atr_Multiplier.SetDescription("ATR multiplier (e.g. 1.5x)");
Input_MaxRiskPerTrade.Name = "Max. Money Risk per Trade";
Input_MaxRiskPerTrade.SetInt(300);
Input_MaxRiskPerTrade.SetIntLimits(1, 10000000);
Input_MaxRiskPerTrade.SetDescription("How much max. money risk to be taken per trade");
Input_AtrStudyPointer.Name = "Pointer to ATR Study Subgraph";
Input_AtrStudyPointer.SetStudySubgraphValues(1,1);
Input_AtrStudyPointer.SetDescription("Where the study is supposed to take the current ATR data from");
sc.ReceivePointerEvents = false;
// During development set this flag to 1, so the DLL can be rebuilt without restarting Sierra Chart. When development is completed, set it to 0 to improve performance.
// sc.FreeDLL = 1;
sc.AllowMultipleEntriesInSameDirection = 1;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = 1;
sc.CancelAllOrdersOnEntriesAndReversals = 0;
sc.AllowOnlyOneTradePerBar = 0;
sc.SupportReversals = 0;
sc.AllowEntryWithWorkingOrders = 1;
sc.SupportAttachedOrdersForTrading = 0;
sc.UseGUIAttachedOrderSetting = 1;
//use a high-value to effectively not put any Position Quantity restrictions
sc.MaximumPositionAllowed = 100000000;
return;
}
if (sc.LastCallToFunction)
{
// abriss
return;
}
if (sc.HideStudy || (!Input_Enabled.GetBoolean()))
return;
//For safety must never do any order management while historical data is being downloaded.
if (sc.ChartIsDownloadingHistoricalData(sc.ChartNumber))
return;
int index = sc.Index;
if (sc.UpdateStartIndex == 0)//Perform initialization
{
return;
}
SCFloatArray SubgraphArray;
sc.GetStudyArrayUsingID(Input_AtrStudyPointer.GetStudyID(), Input_AtrStudyPointer.GetSubgraphIndex(), SubgraphArray);
if (SubgraphArray.GetArraySize() == 0)
return; // No subgraph data
double currentATR = SubgraphArray[sc.Index];
if (currentATR < 0.0)
return;
// int ticks = sc.PriceValueToTicks(currentATR);
// target ATR
double adjustedATR = currentATR * Input_Atr_Multiplier.GetDouble();
float currencyValuePerTick = sc.CurrencyValuePerTick;
float tickDiff = adjustedATR / sc.TickSize;
float riskInCurrencyPerContract = tickDiff * currencyValuePerTick;
int contractsToTrade = static_cast<int>(floor(Input_MaxRiskPerTrade.GetInt() / riskInCurrencyPerContract));
if (contractsToTrade <= 0) contractsToTrade = 1;
sc.TradeWindowOrderQuantity = contractsToTrade;
}