Orders Execution
0
Views
0
Downloads
0
Favorites
Scale
#property copyright "Igor Zakharov"
#property version "1.0"
#property description "Draws Scale to simplify trading"
#property strict
#property indicator_chart_window
enum MODE
{
PTS, //Points
PRC, //% of ballance (Risk/Gain)
};
input string Cap0 ="" ; // ........Main Parameters:
extern MODE Mode =PRC ; // Units
input double Scale_Size =3 ; // Scale size
extern double Scale_Step =1 ; // Scale step
int DIGITS =0 ; // Decimal digits
extern double Lot =0.05 ; // Lot (for % mode)
input string Cap1 ="" ; // ........Decoration:
input int Scale_Shift =3 ; // Shift forward (bars)
input color COLOR =clrWhite ; // Color
input int Text_Size =8 ; // Text size
input bool Once =false ; // Refresh at new bar only
input bool Header =true ; // Show header
input bool Fix_if_Order =true ; // Fix if order opened
input bool Mark_TP_SL =true ; // Mark %s for TP/SL
input string Pref ="Scale" ; // Prefix
string Pref_plus="";
double ZeroLevelAll=0;
double lots=0;
double Zero;
double point_is=1;
datetime LastTime=0;
int Commission;
void init()
{
Pref_plus=Pref+"_"+EnumToString(Mode)+"("+DoubleToStr(Scale_Size,DIGITS)+"/"+DoubleToStr(Scale_Step,DIGITS)+"/"+IntegerToString(Scale_Shift)+") ";
Scale_Step=MathMin(Scale_Step,Scale_Size);
Lot=MathMax(Lot,MarketInfo(Symbol(),MODE_MINLOT));
if(AccountBalance()<=0) Mode=PTS;
double tail=Scale_Step-MathFloor(Scale_Step);
if(tail==0) DIGITS=0;
else
{
if(tail<1 && DIGITS<1) DIGITS=1;
if(tail<0.1 && DIGITS<2) DIGITS=2;
if(tail<0.01 && DIGITS<3) DIGITS=3;
if(tail<0.001 && DIGITS<4) DIGITS=4;
if(tail<0.0001 && DIGITS<5) DIGITS=5;
if(tail<0.00001 && DIGITS<6) DIGITS=6;
if(tail<0.000001 && DIGITS<7) DIGITS=7;
if(tail<0.0000001 && DIGITS<8) DIGITS=8;
}
LastTime=0;
}
void FindLoseless()
{
ZeroLevelAll=0;
lots=0;
double sum=0;
double sumsvop=0;
double tickvalue = MarketInfo(Symbol(), MODE_TICKVALUE);
for(int i=0; i<OrdersTotal(); i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderSymbol()!=Symbol()) continue;
if(OrderType()==OP_BUY)
{
lots+=OrderLots();
sum+=OrderLots()*OrderOpenPrice();
sumsvop+=OrderSwap()+OrderCommission();
}
if(OrderType()==OP_SELL)
{
lots-=OrderLots();
sum-=OrderLots()*OrderOpenPrice();
sumsvop+=OrderSwap()+OrderCommission();
}
Commission=int(MathCeil(-OrderCommission()/OrderLots()/MarketInfo(Symbol(),MODE_TICKVALUE)));
}
if(lots==0) ZeroLevelAll=0;
else ZeroLevelAll=sum/lots-(sumsvop/(tickvalue*lots)*Point);
}
void start()
{
Zero=Bid;
double Clots=Lot;
int F_UP=1;
int F_DN=1;
if(Fix_if_Order)
{
FindLoseless();
if(lots!=0) {Zero=ZeroLevelAll;Clots=lots; if(lots>0) F_DN=-1; else F_UP=-1;}
}
if(Once && lots==0)
{
if(LastTime==Time[0]) return;
LastTime = Time[0];
}
ObjectsDeleteAll(ChartID(),Pref_plus);
datetime Timer=Time[0]+Scale_Shift*_Period*60;
if(Mode==PTS)
{
string name=Pref_plus+"_VL";
if(ObjectCreate(ChartID(),name,OBJ_TREND,0,Timer,Zero-Scale_Size*Point,Timer,Zero+Scale_Size*Point) )
{
ObjectSetInteger(ChartID(),name,OBJPROP_COLOR,COLOR);
ObjectSetInteger(ChartID(),name,OBJPROP_STYLE,STYLE_SOLID);
ObjectSetInteger(ChartID(),name,OBJPROP_WIDTH,1);
ObjectSetInteger(ChartID(),name,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),name,OBJPROP_RAY_RIGHT,false);
}
if(Header)
{
string obj_pref=Pref_plus+"_H";
int res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,Zero+Scale_Size*Point);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," Points");
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCHOR_LOWER);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
}
for(int i=1; i<=Scale_Size/Scale_Step && !IsStopped(); i++)
{
string obj_pref=Pref_plus+"_UP_"+DoubleToStr(i*Scale_Step,DIGITS);
int res=ObjectCreate(ChartID(),obj_pref,OBJ_ARROW,0,Timer,Zero+Scale_Step*i*Point);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ARROWCODE,4);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
obj_pref=Pref_plus+"_DN_"+DoubleToStr(i*Scale_Step,DIGITS);
res=ObjectCreate(ChartID(),obj_pref,OBJ_ARROW,0,Timer,Zero-Scale_Step*i*Point);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ARROWCODE,4);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
obj_pref=Pref_plus+"_T_UP_"+DoubleToStr(i*Scale_Step,DIGITS);;
res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,Zero+Scale_Step*i*Point);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToStr(i*Scale_Step*F_UP,DIGITS));
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCHOR_LEFT);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
obj_pref=Pref_plus+"_T_DN_"+DoubleToStr(i*Scale_Step,DIGITS);;
res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,Zero-Scale_Step*i*Point);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToStr(i*Scale_Step*F_DN,DIGITS));
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCHOR_LEFT);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
}
}//if scale for points
else // if %%
{
if(Clots==0) Clots=Lot;
point_is=MarketInfo(Symbol(),MODE_TICKVALUE)/AccountBalance()*fabs(Clots)*100;//%
// double point_is=MarketInfo(Symbol(),MODE_TICKVALUE)/AccountBalance()*Lot;//%
string name=Pref_plus+"_VL";
if(ObjectCreate(ChartID(),name,OBJ_TREND,0,Timer,Zero-Scale_Size/point_is*Point,Timer,Zero+Scale_Size/point_is*Point) )
{
ObjectSetInteger(ChartID(),name,OBJPROP_COLOR,COLOR);
ObjectSetInteger(ChartID(),name,OBJPROP_STYLE,STYLE_SOLID);
ObjectSetInteger(ChartID(),name,OBJPROP_WIDTH,1);
ObjectSetInteger(ChartID(),name,OBJPROP_SELECTABLE,false);
ObjectSetInteger(ChartID(),name,OBJPROP_SELECTED,false);
ObjectSetInteger(ChartID(),name,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),name,OBJPROP_RAY_RIGHT,false);
}
if(Header)
{
string obj_pref=Pref_plus+"_H";
int res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,Zero+Scale_Size/point_is*Point);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," %("+DoubleToString(Clots,2)+")");
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCHOR_LOWER);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
}
for(int i=1; i<=Scale_Size/Scale_Step && !IsStopped(); i++)
{
string obj_pref=Pref_plus+"_UP_"+DoubleToStr(i*Scale_Step,DIGITS);
int res=ObjectCreate(ChartID(),obj_pref,OBJ_ARROW,0,Timer,Zero+Scale_Step*i*Point/point_is);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ARROWCODE,4);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
obj_pref=Pref_plus+"_DN_"+DoubleToStr(i*Scale_Step,DIGITS);
res=ObjectCreate(ChartID(),obj_pref,OBJ_ARROW,0,Timer,Zero-Scale_Step*i*Point/point_is);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ARROWCODE,4);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
obj_pref=Pref_plus+"_T_UP_"+DoubleToStr(i*Scale_Step,DIGITS);
res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,Zero+Scale_Step*i*Point/point_is);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToStr(i*Scale_Step*F_UP,DIGITS));
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCHOR_LEFT);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
obj_pref=Pref_plus+"_T_DN_"+DoubleToStr(i*Scale_Step,DIGITS);
res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,Zero-Scale_Step*i*Point/point_is);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToStr(i*Scale_Step*F_DN,DIGITS));
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCHOR_LEFT);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
}
}
// if(Once)
{
string obj_pref=Pref_plus+"_0";
int res=ObjectCreate(ChartID(),obj_pref,OBJ_ARROW,0,Timer,Zero);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ARROWCODE,4);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
obj_pref=Pref_plus+"_T0";
res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,Zero);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,COLOR);
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," 0");
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCHOR_LEFT);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
}
if(Fix_if_Order && lots!=0)
{
double CurrProf=0;
double CurrPoss=0;
ENUM_ANCHOR_POINT ANCH;
if(lots<0) {CurrProf=ZeroLevelAll-Ask; CurrPoss=Ask; ANCH=ANCHOR_LEFT_LOWER;}
else {CurrProf=Bid-ZeroLevelAll; CurrPoss=Bid; ANCH=ANCHOR_LEFT_UPPER;}
string C_Text=" "+DoubleToStr(CurrProf/Point,0);
if(Mode==PRC) C_Text=" "+DoubleToStr(CurrProf/Point*point_is,MathMax(DIGITS,2))+" %";
string obj_pref=Pref_plus+"_CP";
int res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,CurrPoss);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,ChartGetInteger(ChartID(),CHART_COLOR_VOLUME));
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT,C_Text);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCH);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
if(Mark_TP_SL)
for(int i=0; i<OrdersTotal(); i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderSymbol()!=Symbol()) continue;
if(OrderType()==OP_BUY)
{
if(OrderTakeProfit()!=0)
{
obj_pref=Pref_plus+"_TP_"+IntegerToString(OrderTicket());
res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,OrderTakeProfit());
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,ChartGetInteger(ChartID(),CHART_COLOR_STOP_LEVEL));
if(Mode==PRC)
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToString(
((OrderTakeProfit()-OrderOpenPrice())/Point-Commission)*point_is
,MathMax(DIGITS,2))+" %");
else
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToString(
(OrderTakeProfit()-OrderOpenPrice())/Point-Commission,0)+" pts");
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
}
if(OrderStopLoss()!=0)
{
obj_pref=Pref_plus+"_SL_"+IntegerToString(OrderTicket());
res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,OrderStopLoss());
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,ChartGetInteger(ChartID(),CHART_COLOR_STOP_LEVEL));
if(Mode==PRC)
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToString(
((OrderStopLoss()-OrderOpenPrice())/Point-Commission)*point_is
,MathMax(DIGITS,2))+" %");
else
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToString(
(OrderStopLoss()-OrderOpenPrice())/Point-Commission,0)+" pts");
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
}
}
if(OrderType()==OP_SELL)
{
if(OrderTakeProfit()!=0)
{
obj_pref=Pref_plus+"_TP_"+IntegerToString(OrderTicket());
res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,OrderTakeProfit());
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,ChartGetInteger(ChartID(),CHART_COLOR_STOP_LEVEL));
if(Mode==PRC)
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToString(
((OrderOpenPrice()-OrderTakeProfit())/Point-Commission)*point_is
,MathMax(DIGITS,2))+" %");
else
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToString(
(OrderOpenPrice()-OrderTakeProfit())/Point-Commission,0)+" pts");
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
}
if(OrderStopLoss()!=0)
{
obj_pref=Pref_plus+"_SL_"+IntegerToString(OrderTicket());
res=ObjectCreate(ChartID(),obj_pref,OBJ_TEXT,0,Timer,OrderStopLoss());
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_COLOR,ChartGetInteger(ChartID(),CHART_COLOR_STOP_LEVEL));
if(Mode==PRC)
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToString(
((OrderOpenPrice()-OrderStopLoss())/Point-Commission)*point_is
,MathMax(DIGITS,2))+" %");
else
ObjectSetString (ChartID(),obj_pref,OBJPROP_TEXT," "+DoubleToString(
(OrderOpenPrice()-OrderStopLoss())/Point-Commission,0)+" pts");
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_HIDDEN,true);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER);
ObjectSetInteger(ChartID(),obj_pref,OBJPROP_FONTSIZE, Text_Size);
}
}
}
}
}
void OnDeinit(const int reason)
{
ObjectsDeleteAll(ChartID(),Pref_plus);
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---