Example of trading tool for my own trading RTT#7

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
maxmax68
Posts: 149
Joined: 20 Nov 2012
Has thanked: 52 times
Been thanked: 41 times

Example of trading tool for my own trading RTT#7

Postby maxmax68 » 01 Apr 2016

Code: Select all

//
// By Massimo Rizzi
//
// A trading tool I created for my own semi-discretionary trading on italian future Ftse Mib.
//
// Do not use it for real money trading !!!
//

[ProcessMouseEvents = True];
[IntrabarOrderGeneration = False];
[RecoverDrawings = False];

Inputs:
vQuantita(1),      // Input Quantity of Shares to Trade
vTL(0),       // Input Trigger Line Price
vEMA_Price(close),   // Ema Price
vEMA_Len(5),      // Ema Length
vTicks_of_StopProfit(2000),   // Number of ticks for the stop profit
Strength(1),      // Per il calcolo dello Swing
atr_len(10), // ATR Len per calcolo Supertrend
atr_mult1(2.5), // ATR Mult per calcolo Supertrend 1
atr_mult2(3.8), // ATR Mult per calcolo Supertrend 2
atr_mult3(5), // ATR Mult per calcolo Supertrend 3
atr_mult4(6.8), // ATR Mult per calcolo Supertrend 4
vTick(5),       // Ticks of TriggerLine movement
vEndSessionTime_s(180000), // EOD Time for closing open position
vButtonsDistance(5); // Distance between buttons

Vars:
vEMA(0), // EMA for trailing cross
vEMA_Offset(0),   // Ema Offset for tracking mode
recalcpersist vAtrMult(0), // vATR Mult for TriggerLine
recalcpersist vAtrMultCounter(1), // Counter to choose vATR Mult
recalcpersist vTextButtonId_1(0), // id button n1
recalcpersist vTextButtonId_2(0), // id button n2
recalcpersist vTextButtonId_3(0), // id button n3
recalcpersist vTextButtonId_4A(0), // id button n4A
recalcpersist vTextButtonId_4B(0), // id button n4B
recalcpersist vTextButtonId_4C(0), // id button n4C
recalcpersist vTextButtonId_5(0), // id button n5
recalcpersist vTextButtonId_6(0), // id button n6
recalcpersist vTextButtonId_7(0), // id button n7
recalcpersist vValueTL(0), // id TriggerLine
recalcpersist vMostLeftDateTime(0),
recalcpersist vMostRightDateTime(0),
recalcpersist vHighestDispValue(0),
recalcpersist vLowestDispValue(0),
recalcpersist vRangeDateTime(0),
recalcpersist vMinMove(1), // Movimento minimo scala prezzi
recalcpersist vX(0), // coordinate x buttons
recalcpersist vY(0), // coordinate y punto inferiore (click mouse)
recalcpersist vXOffset(0.1), // Offset % x from low-left sx angle
recalcpersist vYOffset(0.2), // Offset % y from low-left sx angle
recalcpersist vY1(0), // coordinate y button 1
recalcpersist vY2(0), // coordinate y button 2
recalcpersist vY3(0), // coordinate y button 3
recalcpersist vY4(0), // coordinate y button 4
recalcpersist vY4B(0), // coordinate y button 4B
recalcpersist vY4C(0), // coordinate y button 4C
recalcpersist vY5(0), // coordinate y button 5
recalcpersist vY6(0), // coordinate y button 6
recalcpersist vY7(0), // coordinate y button 7
recalcpersist vTextButtonCounter_1(0), // counter associato button 1
recalcpersist vTextButtonCounter_2(0), // counter associato button 2
recalcpersist vTextButtonCounter_3(0), // counter associato button 3
recalcpersist vTextButtonCounter_4A(0), // counter associato button 4A
recalcpersist vTextButtonCounter_4B(0), // counter associato button 4B
recalcpersist vTextButtonCounter_4C(0), // counter associato button 4C
recalcpersist vTextButtonCounter_5(0), // counter associato button 5
recalcpersist vTextButtonCounter_6(0), // counter associato button 6
recalcpersist vTextButtonCounter_7(0), // counter associato button 7
recalcpersist vQTy(0),  // Quantity of Shares to Trade
recalcpersist vTriggerLine(0), // Trigger Line Price
recalcpersist vMP(0), // MarketPosition at Broker 0=Flat 1=Long -1=Short
recalcpersist vStrategy_mp (0), // Inner Strategy Position
recalcpersist vBroker_mp (0), // Actual MarketPosition_at_Broker;
recalcpersist vLastPivotHigh(0), //Records value of last pivot high
recalcpersist vLastPivotLow(0), //Records value of last pivot low
recalcpersist vSuperTrend(0), // Supertrend Value
recalcpersist vSuperTrend1(0), // Supertrend Value
recalcpersist vSuperTrend2(0), // Supertrend Value
recalcpersist vSuperTrend3(0), // Supertrend Value
recalcpersist vSuperTrend4(0), // Supertrend Value
recalcpersist vProfitTicks(0); //Ticks of profit before Trailing Mode Active

once vProfitTicks=vTicks_of_StopProfit;

//
// Calculates  Quantity of Shares to Trade
//
if getappinfo(aiStrategyAuto)=1 then
   begin
      if MarketPosition_at_Broker = 0 then begin
         vQTy = vQuantita;
         vMP = 0;
      end else if MarketPosition_at_Broker > 0 then begin
         vQTy = MarketPosition_at_Broker;
         vMP = 1;
      end else if MarketPosition_at_Broker < 0 then begin
         vQTy = -1*MarketPosition_at_Broker;
         vMP = -1;
      end;
   end
else
   begin
      if MarketPosition = 0 then begin
         vQTy = 1;
         vMP = 0;
      end else if MarketPosition > 0 then begin
         vQTy = 1;
         vMP = 1;
      end else if MarketPosition < 0 then begin
         vQTy = 1;
         vMP = -1;
      end;
   end;

//
// Calculates Initial Trigger Line Price
//
once vTriggerLine=0;
if vTL <> 0 and vTriggerLine=0 then vTriggerLine = vTL
else if vTL=0 then begin
   if Date <> Date[1] then vTriggerLine = Open;
end;


// Draws TriggerLine
if TL_Exist(vValueTL)=true then tl_delete(vValueTL);
if TL_Exist(vValueTL)=false then vValueTL = tl_new(Date, Time[1], vTriggerLine, Date, Time, vTriggerLine);
TL_SetExtLeft(vValueTL,True);
TL_SetExtRight(vValueTL,True);
TL_Lock(vValueTL,True);

//
// Get DateTime MostLeft and MostRight
// and Highest and Lowest price displayed on the chart.
//
vMostLeftDateTime = GetAppInfo(aiLeftDispDateTime);
vMostRightDateTime = GetAppInfo(aiRightDispDateTime);
vHighestDispValue = GetAppInfo(aiHighestDispValue);
vLowestDispValue = GetAppInfo(aiLowestDispValue);
vMinMove = (vHighestDispValue-vLowestDispValue)/100;

//
// Initializes starting values of vX e vY
// and calculates ordinate values (vYn) of buttons
//
if vX=0 then vX = vMostLeftDateTime + vXOffset;
if vY=0 then vY = vLowestDispValue + (vHighestDispValue-vLowestDispValue) * vYOffset;
vY1= vY+(4*vButtonsDistance) * vMinMove;
vY2= vY+(3*vButtonsDistance) * vMinMove;
vY3= vY+(2*vButtonsDistance) * vMinMove;
vY4= vY+(1*vButtonsDistance) * vMinMove;
vY5= vY+(0*vButtonsDistance) * vMinMove;
vY4B= vY+(6*vButtonsDistance) * vMinMove;
vY4C= vY+(5*vButtonsDistance) * vMinMove;
vY6= vY+(7*vButtonsDistance) * vMinMove;
vY7= vY+(8*vButtonsDistance) * vMinMove;

//
// Creates buttons on the chart
//
once begin
   vTextButtonId_1=Text_New_Dt(vX, vY1,"");
   vTextButtonId_2=Text_New_Dt(vX, vY2,"");
   vTextButtonId_3=Text_New_Dt(vX, vY3,"");
   vTextButtonId_4A=Text_New_Dt(vX, vY4,"");
   vTextButtonId_4B=Text_New_Dt(vX, vY4B,"");
   vTextButtonId_4C=Text_New_Dt(vX, vY4C,"");
   vTextButtonId_5=Text_New_Dt(vX, vY5,"RTT#7: Line BreakOut ");
   vTextButtonId_6=Text_New_Dt(vX, vY6,"Stop Profit: "+NumToStr(vProfitTicks,0)+" Ticks ");
   vTextButtonId_7=Text_New_Dt(vX, vY7,"ATR Mult: "+NumToStr(vAtrMult,1));
end; // once

//
// If MouseClickShiftPressed ...
//
if MouseClickShiftPressed then begin
   //
   // if click on the chart and no button actived
   //
   if mc_text_getactive=-1 then begin
      // calculates new offsets for x & y buttons coordinates
      if (vMostRightDateTime - vMostLeftDateTime)<>0 then
      vXOffset = (MouseClickDateTime - vMostLeftDateTime) / (vMostRightDateTime - vMostLeftDateTime);
      if (vHighestDispValue-vLowestDispValue)<>0 then
      vYOffset = (MouseClickPrice - vLowestDispValue) / (vHighestDispValue-vLowestDispValue);
   end
   // Checks for MouseClick on TextBoxes and updates vCounters
   else if MC_Text_GetActive=vTextButtonId_1 then begin
      vTextButtonCounter_1=vTextButtonCounter_1+1;
      if vTextButtonCounter_1>1 then vTextButtonCounter_1=0;
   end
   else if MC_Text_GetActive=vTextButtonId_2 then begin
         vTextButtonCounter_2=vTextButtonCounter_2+1;
         if vTextButtonCounter_2>5 then vTextButtonCounter_2=0;
         // if long (vMP=1) and Entry Fade (vTextButtonCounter_2=3) then set OnlyLong Mode (vTextButtonCounter_2=4)
      if (vMP=1 AND vTextButtonCounter_2=3) then vTextButtonCounter_2=4;
      // if short (vMP=-1) and Entry Fade (vTextButtonCounter_2=3) then set OnlyShort Mode (vTextButtonCounter_2=5)
      if (vMP=-1 AND vTextButtonCounter_2=3) then vTextButtonCounter_2=5;
   end
   else if MC_Text_GetActive=vTextButtonId_3 then begin
         vTextButtonCounter_3=vTextButtonCounter_3+1;
         if vTextButtonCounter_3>1 then vTextButtonCounter_3=0;
   end
   else if MC_Text_GetActive=vTextButtonId_4B then begin
      // if Active StopProfit Button (vTextButtonCounter_6=1)then increase vProfitTicks
      if vTextButtonCounter_6=1 then vProfitTicks=vProfitTicks+20
      // if Active ATR Mult Button (vTextButtonCounter_7=1)then increase vATRMultCounter
      else if vTextButtonCounter_7=1 then begin
         if vAtrMultCounter<3 then vAtrMultCounter=vAtrMultCounter+1;
         // Recalculate vSupertrend and vTriggerLine
         if vAtrMultCounter=0 then vAtrMult=atr_mult1
         else if vAtrMultCounter=1 then vAtrMult=atr_mult2
         else if vAtrMultCounter=2 then vAtrMult=atr_mult3
         else if vAtrMultCounter=3 then vAtrMult=atr_mult4;
         vSuperTrend =  SUPERTREND(atr_len,vAtrMult)[0];
         vTriggerLine=vSuperTrend;
         // ReDraws TriggerLine
         if TL_Exist(vValueTL)=true then tl_delete(vValueTL);
         if TL_Exist(vValueTL)=false then vValueTL = tl_new(Date, Time[1], vTriggerLine, Date, Time, vTriggerLine);
         TL_SetExtLeft(vValueTL,True);
         TL_SetExtRight(vValueTL,True);
         TL_Lock(vValueTL,True);
      end
      // else increase TriggerLine
      else begin
         // increase TriggerLine
         if PriceScale<>0 then
            vTriggerLine = vTriggerLine + (vTick/PriceScale);
            tl_setbegin(vValueTL,Date,Time[1], vTriggerLine);
         tl_setend(vValueTL,Date,Time, vTriggerLine);
         text_setstring(vTextButtonId_4A," TL: "+NumToStr(vTriggerLine,0)+" ");
      end;
   end
   else if MC_Text_GetActive=vTextButtonId_4C then begin
      // if Active StopProfit Button (vTextButtonCounter_6=1)then decrease vProfitTicks
      if vTextButtonCounter_6=1 then vProfitTicks=vProfitTicks-20
      // if Active ATR Mult Button (vTextButtonCounter_7=1)then decrease vATRMultCounter
      else if vTextButtonCounter_7=1 then begin
         if vAtrMultCounter>0 then vAtrMultCounter=vAtrMultCounter-1;
         // Recalculate vSupertrend and vTriggerLine
         if vAtrMultCounter=0 then vAtrMult=atr_mult1
         else if vAtrMultCounter=1 then vAtrMult=atr_mult2
         else if vAtrMultCounter=2 then vAtrMult=atr_mult3
         else if vAtrMultCounter=3 then vAtrMult=atr_mult4;
         vSuperTrend =  SUPERTREND(atr_len,vAtrMult)[0];
         vTriggerLine=vSuperTrend;
         // ReDraws TriggerLine
         if TL_Exist(vValueTL)=true then tl_delete(vValueTL);
         if TL_Exist(vValueTL)=false then vValueTL = tl_new(Date, Time[1], vTriggerLine, Date, Time, vTriggerLine);
         TL_SetExtLeft(vValueTL,True);
         TL_SetExtRight(vValueTL,True);
         TL_Lock(vValueTL,True);
      end
      // else decrease TriggerLine
      else begin
         // decrease TriggerLine
         if PriceScale<>0 then
            vTriggerLine = vTriggerLine - (vTick/PriceScale);
            tl_setbegin(vValueTL,Date,Time[1], vTriggerLine);
         tl_setend(vValueTL,Date,Time, vTriggerLine);
         text_setstring(vTextButtonId_4A," TL: "+NumToStr(vTriggerLine,0)+" ");
      end;
   end
   else if MC_Text_GetActive=vTextButtonId_5 then begin
         vTextButtonCounter_5=vTextButtonCounter_5+1;
         if vTextButtonCounter_5>4 then vTextButtonCounter_5=0;
   end
   else if MC_Text_GetActive=vTextButtonId_6 then begin
      // Toggles StopProfit Button paused(vTextButtonCounter_6=0)/active(vTextButtonCounter_6=1)
      vTextButtonCounter_6=vTextButtonCounter_6+1;
         if vTextButtonCounter_6>1 then vTextButtonCounter_6=0;
         // Sets ATR Mult Button in not active mode (vTextButtonCounter_7=0)
      vTextButtonCounter_7=0;
   end
   else if MC_Text_GetActive=vTextButtonId_7 then begin
      // Toggles ATR Mult Button paused(vTextButtonCounter_7=0)/active(vTextButtonCounter_7=1)
      vTextButtonCounter_7=vTextButtonCounter_7+1;
         if vTextButtonCounter_7>1 then vTextButtonCounter_7=0;
      // Sets StopProfit Button in not active mode (vTextButtonCounter_6=0)
      vTextButtonCounter_6=0;
   end;
end; // if MouseClickShiftPressed

//
// Calculates new X e Y buttons coordinates
//
vX = vMostLeftDateTime + (vMostRightDateTime - vMostLeftDateTime) * vXOffset;
vY = vLowestDispValue + (vHighestDispValue-vLowestDispValue) * vYOffset;
vY1= vY+(4*vButtonsDistance) * vMinMove;
vY2= vY+(3*vButtonsDistance) * vMinMove;
vY3= vY+(2*vButtonsDistance) * vMinMove;
vY4= vY+(1*vButtonsDistance) * vMinMove;
vY5= vY+(0*vButtonsDistance) * vMinMove;
vY4B= vY+(6*vButtonsDistance) * vMinMove;
vY4C= vY+(5*vButtonsDistance) * vMinMove;
vY6= vY+(7*vButtonsDistance) * vMinMove;
vY7= vY+(8*vButtonsDistance) * vMinMove;

//
// Calculates SuperTrend Value
//
vSuperTrend1 = SUPERTREND(atr_len,atr_mult1)[0];
vSuperTrend2 = SUPERTREND(atr_len,atr_mult2)[0];
vSuperTrend3 = SUPERTREND(atr_len,atr_mult3)[0];
vSuperTrend4 = SUPERTREND(atr_len,atr_mult4)[0];

if vAtrMultCounter=0 then vAtrMult=atr_mult1
else if vAtrMultCounter=1 then vAtrMult=atr_mult2
else if vAtrMultCounter=2 then vAtrMult=atr_mult3
else if vAtrMultCounter=3 then vAtrMult=atr_mult4;
vSuperTrend =  SUPERTREND(atr_len,vAtrMult)[0];

//
// Calculates new EMA value
//
vEMA=HullMA(vEMA_Price,vEMA_Len)[0];
// if Sell Mode (vTextButtonCounter_3=1)
if vTextButtonCounter_3=1 then vEMA=maxlist(vEMA,Close)
// if Buy Mode (vTextButtonCounter_3=0)
else if vTextButtonCounter_3=0 then vEMA=minlist(vEMA,Close);

//
// Calculates new vTriggerline value
//

// if Track mode Dn Only(vTextButtonCounter_5=4)updates vTriggerLine
if vTextButtonCounter_5=4 then begin
   if vSuperTrend>vEMA then vTriggerLine=minlist(vTriggerLine,vSuperTrend);
end
// if Track mode (vTextButtonCounter_5=1 or vTextButtonCounter_5=2) and Buy Mode (vTextButtonCounter_3=0) updates vTriggerLine
else if (vTextButtonCounter_5=1 or vTextButtonCounter_5=2) and vTextButtonCounter_3=0 then begin
   if vSuperTrend>vEMA then vTriggerLine=vSuperTrend;
end;

// if Track mode Up Only(vTextButtonCounter_5=3)updates vTriggerLine
if vTextButtonCounter_5=3 then begin
   if vSuperTrend<vEMA then vTriggerLine=maxlist(vTriggerLine,vSuperTrend);
end
// if Track mode (vTextButtonCounter_5=1 or vTextButtonCounter_5=2) and Sell Mode (vTextButtonCounter_3=1) updates vTriggerLine
else if (vTextButtonCounter_5=1 or vTextButtonCounter_5=2) and vTextButtonCounter_3=1 then begin
   if vSuperTrend<vEMA then vTriggerLine=vSuperTrend;
end;

//
// Formats e Redraws buttons on every recalc
//

// if time_s > vEndSessionTime_s and Flat position (vMP=0) set Paused Mode ( vTextButtonCounter_1=0)
if time_s > vEndSessionTime_s AND vMP=0 then vTextButtonCounter_1=0;
// if flat (vMP=0) and OnlyLong Mode (vTextButtonCounter_2=4) then set Buy Mode (vTextButtonCounter_3=0)
if vMP=0 AND vTextButtonCounter_2=4 then vTextButtonCounter_3=0 ;
// if flat (vMP=0) and OnlyShort Mode (vTextButtonCounter_2=5) then set Sell Mode (vTextButtonCounter_3=1)
if vMP=0 AND vTextButtonCounter_2=5 then vTextButtonCounter_3=1 ;
// if long (vMP=1) and Entry Mode (vTextButtonCounter_2=0) or Entry Fade (vTextButtonCounter_2=3) then set Reverse Mode (vTextButtonCounter_2=1)
if (vMP=1 AND (vTextButtonCounter_2=0 or vTextButtonCounter_2=3)) then vTextButtonCounter_2=1;
// if long (vMP=1) set buy/sell to Sell (vTextButtonCounter_3=1)
if vMP=1 then vTextButtonCounter_3=1;
// if short (vMP=-1) and Entry Mode (vTextButtonCounter_2=0) or Entry Fade (vTextButtonCounter_2=3) then  set Reverse Mode (vTextButtonCounter_2=1)
if (vMP=-1 AND (vTextButtonCounter_2=0 or vTextButtonCounter_2=3)) then vTextButtonCounter_2=1;
// if short (vMP=-1) set buy/sell to Buy (vTextButtonCounter_3=0)
if vMP=-1 then vTextButtonCounter_3=0;

// if Active Mode (vTextButtonCounter_1=1) then Paused StopProfit(vTextButtonCounter_6=0)and ATR Mult Buttons(vTextButtonCounter_6=0)
if vTextButtonCounter_1=1 then begin
   vTextButtonCounter_6=0;
   vTextButtonCounter_7=0;
end;

// Formats and Redraws Button1
text_setborder(vTextButtonId_1,TRUE);
text_setColor(vTextButtonId_1,blue);
if vTextButtonCounter_1=0 then text_setBGColor(vTextButtonId_1,lightgray)
else if vTextButtonCounter_1=1 then text_setBGColor(vTextButtonId_1,cyan);
if getappinfo(aiStrategyAuto)=1 then
   begin
      if vTextButtonCounter_1=0 then begin
         if vMP=0 then text_setstring(vTextButtonId_1,"PAUSED: FLAT MPB "+NumToStr(MarketPosition_at_Broker,0)+" MPS: "+NumToStr(MarketPosition_at_Broker_for_The_Strategy,0))
         else if vMP=1 then text_setstring(vTextButtonId_1,"PAUSED: LONG MPB "+NumToStr(MarketPosition_at_Broker,0)+" MPS: "+NumToStr(MarketPosition_at_Broker_for_The_Strategy,0))
         else if vMP=-1 then text_setstring(vTextButtonId_1,"PAUSED: SHORT MPB "+NumToStr(MarketPosition_at_Broker,0)+" MPS: "+NumToStr(MarketPosition_at_Broker_for_The_Strategy,0));
      end else if vTextButtonCounter_1=1 then begin
         if vMP=0 then text_setstring(vTextButtonId_1,"ACTIVE: FLAT MPB "+NumToStr(MarketPosition_at_Broker,0)+" MPS: "+NumToStr(MarketPosition_at_Broker_for_The_Strategy,0))
         else if vMP=1 then text_setstring(vTextButtonId_1,"ACTIVE: LONG MPB "+NumToStr(MarketPosition_at_Broker,0)+" MPS: "+NumToStr(MarketPosition_at_Broker_for_The_Strategy,0))
         else if vMP=-1 then text_setstring(vTextButtonId_1,"ACTIVE: SHORT MPB "+NumToStr(MarketPosition_at_Broker,0)+" MPS: "+NumToStr(MarketPosition_at_Broker_for_The_Strategy,0));
      end;
   end
else
   begin
      if vTextButtonCounter_1=0 then begin
         if vMP=0 then text_setstring(vTextButtonId_1,"PAUSED: FLAT MPB 0"+" MPS: "+NumToStr(MarketPosition,0))
         else if vMP=1 then text_setstring(vTextButtonId_1,"PAUSED: LONG MPB  1 "+" MPS: "+NumToStr(MarketPosition,0))
         else if vMP=-1 then text_setstring(vTextButtonId_1,"PAUSED: SHORT MPB -1"+" MPS: "+NumToStr(MarketPosition,0));
      end else if vTextButtonCounter_1=1 then begin
         if vMP=0 then text_setstring(vTextButtonId_1,"ACTIVE: FLAT MPB  0"+" MPS: "+NumToStr(MarketPosition,0))
         else if vMP=1 then text_setstring(vTextButtonId_1,"ACTIVE: LONG MPB  1"+" MPS: "+NumToStr(MarketPosition,0))
         else if vMP=-1 then text_setstring(vTextButtonId_1,"ACTIVE: SHORT MPB -1"+" MPS: "+NumToStr(MarketPosition,0));
      end;
   end;
text_SetStyle(vTextButtonId_1,0,1);
text_lock(vTextButtonId_1,TRUE);
Text_SetLocation_DT(vTextButtonId_1,vX,vY1);
// Formats and Redraws Button2
text_setborder(vTextButtonId_2,TRUE);
text_setColor(vTextButtonId_2,blue);
if vTextButtonCounter_2=0 then text_setstring(vTextButtonId_2," Entry Mode ")
else if vTextButtonCounter_2=1 then text_setstring(vTextButtonId_2," Reverse Mode ")
else if vTextButtonCounter_2=2 then text_setstring(vTextButtonId_2," Stop Mode ")
else if vTextButtonCounter_2=3 then text_setstring(vTextButtonId_2," Entry Fade ")
else if vTextButtonCounter_2=4 then text_setstring(vTextButtonId_2," Only Long ")
else if vTextButtonCounter_2=5 then text_setstring(vTextButtonId_2," Only Short ");
//
if vTextButtonCounter_2=0 then text_setBGColor(vTextButtonId_2,green)
else if vTextButtonCounter_2=1 then text_setBGColor(vTextButtonId_2, RGB(218,112,212))
else if vTextButtonCounter_2=2 then text_setBGColor(vTextButtonId_2,RGB(244,0,161))
else if vTextButtonCounter_2=3 then text_setBGColor(vTextButtonId_2, RGB(255,153,51))
else if vTextButtonCounter_2=4 then text_setBGColor(vTextButtonId_2,DarkGreen)
else if vTextButtonCounter_2=5 then text_setBGColor(vTextButtonId_2,Magenta);
text_SetStyle(vTextButtonId_2,0,1);
text_lock(vTextButtonId_2,TRUE);
Text_SetLocation_DT(vTextButtonId_2,vX,vY2);
// Formats and Redraws Button3
text_setborder(vTextButtonId_3,TRUE);
text_setColor(vTextButtonId_3,blue);
if vTextButtonCounter_3=0 then text_setBGColor(vTextButtonId_3,green)
else if vTextButtonCounter_3=1 then text_setBGColor(vTextButtonId_3,red);
if vTextButtonCounter_3=0 then text_setstring(vTextButtonId_3,"  Buy  : "+NumToStr(vQty,0)+" ")
else if vTextButtonCounter_3=1 then text_setstring(vTextButtonId_3,"  Sell  : "+NumToStr(vQty,0)+" ");
text_SetStyle(vTextButtonId_3,0,1);
text_lock(vTextButtonId_3,TRUE);
Text_SetLocation_DT(vTextButtonId_3,vX,vY3);
// Formats and Redraws Button4A
text_setborder(vTextButtonId_4A,TRUE);
text_setColor(vTextButtonId_4A,blue);
text_setBGColor(vTextButtonId_4A,yellow);
if vTriggerLine>100 then text_setstring(vTextButtonId_4A," TL: "+NumToStr(vTriggerLine,0)+" ")
else text_setstring(vTextButtonId_4A," TL: "+NumToStr(vTriggerLine,3)+" ");
text_SetStyle(vTextButtonId_4A,0,1);
text_lock(vTextButtonId_4A,TRUE);
Text_SetLocation_DT(vTextButtonId_4A,vX,vY4);
// Formats and Redraws Button4B
text_setborder(vTextButtonId_4B,TRUE);
text_setColor(vTextButtonId_4B,blue);
text_setBGColor(vTextButtonId_4B,green);
text_setstring(vTextButtonId_4B," Up ");
text_SetStyle(vTextButtonId_4B,0,1);
text_lock(vTextButtonId_4B,TRUE);
Text_SetLocation_DT(vTextButtonId_4B,vX,vY4B);
// Formats and Redraws Button4C
text_setborder(vTextButtonId_4C,TRUE);
text_setColor(vTextButtonId_4C,blue);
text_setBGColor(vTextButtonId_4C,red);
text_setstring(vTextButtonId_4C," Dn ");
text_SetStyle(vTextButtonId_4C,0,1);
text_lock(vTextButtonId_4C,TRUE);
Text_SetLocation_DT(vTextButtonId_4C,vX,vY4C);
// Formats and Redraws Button5
if vTextButtonCounter_5=0 then begin
   text_setstring(vTextButtonId_5, "RTT#7: Line BreakOut ");
   text_setBGColor(vTextButtonId_5,yellow);
end
else if vTextButtonCounter_5=1 then begin
   text_setstring(vTextButtonId_5, "RTT#7: Line BreakOut w/Track ");
   text_setBGColor(vTextButtonId_5,RGB(255,200,0));
end
else if vTextButtonCounter_5=2 then begin
   text_setstring(vTextButtonId_5, "RTT#7: Line BrkOut w/Track Single ");
   text_setBGColor(vTextButtonId_5,RGB(255,204,255));
end
else if vTextButtonCounter_5=3 then begin
   text_setstring(vTextButtonId_5, "RTT#7: Line BrkOut w/Track Up Only ");
   text_setBGColor(vTextButtonId_5,RGB(204,255,0));
end
else if vTextButtonCounter_5=4 then begin
   text_setstring(vTextButtonId_5, "RTT#7: Line BrkOut w/Track Dn Only ");
   text_setBGColor(vTextButtonId_5,RGB(255,102,102));
end;
text_setborder(vTextButtonId_5,TRUE);
text_setColor(vTextButtonId_5,blue);
text_SetStyle(vTextButtonId_5,0,1);
text_lock(vTextButtonId_5,TRUE);
Text_SetLocation_DT(vTextButtonId_5,vX,vY5);
// Formats and Redraws Button6
if vTextButtonCounter_6=0 then begin
   text_setstring(vTextButtonId_6,"Stop Profit: "+NumToStr(vProfitTicks,0)+" Ticks ");
   text_setBGColor(vTextButtonId_6,yellow);
end
else if vTextButtonCounter_6=1 then begin
   text_setstring(vTextButtonId_6,"Stop Profit: "+NumToStr(vProfitTicks,0)+" Ticks ");
   text_setBGColor(vTextButtonId_6,RGB(255,153,255));
end;
text_setborder(vTextButtonId_6,TRUE);
text_setColor(vTextButtonId_6,blue);
text_SetStyle(vTextButtonId_6,0,1);
text_lock(vTextButtonId_6,TRUE);
Text_SetLocation_DT(vTextButtonId_6,vX,vY6);
// Formats and Redraws Button7
if vTextButtonCounter_7=0 then begin
   if vAtrMultCounter=0 then text_setstring(vTextButtonId_7,"ATR mult: "+NumToStr(atr_mult1,1))
   else if vAtrMultCounter=1 then text_setstring(vTextButtonId_7,"ATR mult: "+NumToStr(atr_mult2,1))
   else if vAtrMultCounter=2 then text_setstring(vTextButtonId_7,"ATR mult: "+NumToStr(atr_mult3,1))
   else if vAtrMultCounter=3 then text_setstring(vTextButtonId_7,"ATR mult: "+NumToStr(atr_mult4,1));
   text_setBGColor(vTextButtonId_7,yellow);
end
else if vTextButtonCounter_7=1 then begin
   if vAtrMultCounter=0 then text_setstring(vTextButtonId_7,"ATR mult: "+NumToStr(atr_mult1,1))
   else if vAtrMultCounter=1 then text_setstring(vTextButtonId_7,"ATR mult: "+NumToStr(atr_mult2,1))
   else if vAtrMultCounter=2 then text_setstring(vTextButtonId_7,"ATR mult: "+NumToStr(atr_mult3,1))
   else if vAtrMultCounter=3 then text_setstring(vTextButtonId_7,"ATR mult: "+NumToStr(atr_mult4,1));
   text_setBGColor(vTextButtonId_7,RGB(255,153,255));
end;
text_setborder(vTextButtonId_7,TRUE);
text_setColor(vTextButtonId_7,blue);
text_SetStyle(vTextButtonId_7,0,1);
text_lock(vTextButtonId_6,TRUE);
Text_SetLocation_DT(vTextButtonId_7,vX,vY7);


//
// Starts trading routine  --------------------------------------------------------------------
//
//if (getappinfo(aiStrategyAuto)=1 AND MarketPosition_at_Broker_for_The_Strategy=MarketPosition_at_Broker) or getappinfo(aiStrategyAuto)=0 then begin
//if LastBarOnChart AND getappinfo(aiStrategyAuto)=1 then begin
   //
   // If Active Mode (vTextButtonCounter_1=1) And TriggerLine drawed(exists) and TriggerLine value > 0
   //
   if (vTextButtonCounter_1=1 AND TL_Exist(vValueTL) AND  vTriggerLine>0) then begin
      //
      // Checks if reached number of ticks of trailing stop
      //
      // if long (vMP=1) and close>entryprice+vProfitTicks
      if vMP=1 AND entryprice<>0 AND close>entryprice+vProfitTicks then begin
         // sets stop mode (vTextButtonCounter_2=2)
         vTextButtonCounter_2=2;
         // sets Track mode (vTextButtonCounter_5=1)
         vTextButtonCounter_5=1;
      end;
      // if short (vMP=-1) and close<entryprice-vProfitTicks
      if vMP=-1 AND entryprice<>0 AND close<entryprice-vProfitTicks then begin
         // sets stop mode (vTextButtonCounter_2=2)
         vTextButtonCounter_2=2;
         // sets Track mode (vTextButtonCounter_5=1)
         vTextButtonCounter_5=1;
      end;
      //
      // if Flat Position (vMP=0) and Entry Fade (vTextButtonCounter_2=3)
      if vMP=0 AND vTextButtonCounter_2=3 then begin
          // if Buy Mode (vTextButtonCounter_3=0) and TriggerLine reached (vEMA<vTriggerLine)
         if vTextButtonCounter_3=0 AND vEMA<vTriggerLine then begin
            // Sets new TriggerLine
            if vSuperTrend>vEMA then vTriggerLine=vSuperTrend;
            // Sets Tracking Mode Single (vTextButtonCounter_5=2)
            vTextButtonCounter_5=2;
            // Sets Entry Mode (vTextButtonCounter_2=0)
            vTextButtonCounter_2=0;
         end
         // if Sell Mode (vTextButtonCounter_3=1) and Sell Condition true (vEMA>vTriggerLine)
         else if vTextButtonCounter_3=1 AND vEMA>vTriggerLine then begin
            // Sets new TriggerLine
            if vSuperTrend<vEMA then vTriggerLine=vSuperTrend;
            // Sets Tracking Mode Single (vTextButtonCounter_5=2)
            vTextButtonCounter_5=2;
            // Sets Entry Mode (vTextButtonCounter_2=0)
            vTextButtonCounter_2=0;
         end;
      end
      //
      // if Flat Position (vMP=0) and Entry Mode (vTextButtonCounter_2=0)
      //
      else if vMP=0 AND vTextButtonCounter_2=0 then begin
         // if Buy Mode (vTextButtonCounter_3=0) and Buy Condition true (vEMA>triggerline)
         if vTextButtonCounter_3=0 AND vEMA>vTriggerLine then begin
            BUY vQty shares next bar at close+5 limit;
            // If not track only Up (vTextButtonCounter_5<>3)
            if vTextButtonCounter_5<>3 then begin
               if vSuperTrend<vEMA then vTriggerLine=vSuperTrend;
            end;
            //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
            if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
            //Alert(Text("Started position")) ;
         end
         // if Sell Mode (vTextButtonCounter_3=1) and Sell Condition true (vEMA<triggerline)
         else if vTextButtonCounter_3=1 AND vEMA<vTriggerLine then begin
            sellshort vQty shares next bar at close-5 limit;
            // If not track only Down (vTextButtonCounter_5<>4)
            if vTextButtonCounter_5<>4 then begin
               if vSuperTrend>vEMA then vTriggerLine=vSuperTrend;
            end;
            //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
            if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
            //Alert(Text("Started position")) ;
         end;
      end
      //
      // if Flat Position (vMP=0) and OnlyLong Mode (vTextButtonCounter_2=4)
      //
      else if vMP=0 AND vTextButtonCounter_2=4 then begin
         // if Buy Mode (vTextButtonCounter_3=0) and Buy Condition true (vEMA>triggerline)
         if vTextButtonCounter_3=0 AND vEMA>vTriggerLine then begin
            BUY vQty shares next bar at close+5 limit;
            // If not track only Up (vTextButtonCounter_5<>3)
            if vTextButtonCounter_5<>3 then begin
               if vSuperTrend<vEMA then vTriggerLine=vSuperTrend;
            end;
            //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
            if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
            //Alert(Text("Started position")) ;
         end;
      end
      //
      // if Flat Position (vMP=0) and OnlyShort Mode (vTextButtonCounter_2=5)
      //
      else if vMP=0 AND vTextButtonCounter_2=5 then begin
         // if Sell Mode (vTextButtonCounter_3=1) and Sell Condition true (vEMA<triggerline)
         if vTextButtonCounter_3=1 AND vEMA<vTriggerLine then begin
            sellshort vQty shares next bar at close-5 limit;
            // If not track only Down (vTextButtonCounter_5<>4)
            if vTextButtonCounter_5<>4 then begin
               if vSuperTrend>vEMA then vTriggerLine=vSuperTrend;
            end;
            //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
            if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
            //Alert(Text("Started position")) ;
         end;
      end
      //
      // il Long Position (vMP=1)
      else if vMP=1 then begin
         // if Stop Mode (vTextButtonCounter_2=2) and Stop Condition true (vEMA<triggerline ) or
         // if time_s > vEndSessionTime_s close position
         if (vTextButtonCounter_2=2 AND vEMA<vTriggerLine) or (time_s >= vEndSessionTime_s) then begin
            SELL vQty shares next bar at close-5 limit;
            //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
            if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
         end
         // if Reverse Mode (vTextButtonCounter_2=1) and Reverse Condition true (vEMA<triggerline )
         else if vTextButtonCounter_2=1 AND vEMA<vTriggerLine then begin
            vQTy = vQuantita;
            sellShort vQty shares next bar at close-5 limit;
            // If not track only Down (vTextButtonCounter_5<>4)
            if vTextButtonCounter_5<>4 then begin
               if vSuperTrend>vEMA then vTriggerLine=vSuperTrend;
            end;
            //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
            if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
         end
         // if OnlyLong Mode (vTextButtonCounter_2=4) and Stop Condition true (vEMA<triggerline )
         else if vTextButtonCounter_2=4 AND vEMA<vTriggerLine then begin
            SELL vQty shares next bar at close-5 limit;
            // If not track only Down (vTextButtonCounter_5<>4)
            if vTextButtonCounter_5<>4 then begin
               if vSuperTrend>vEMA then vTriggerLine=vSuperTrend;
            end;
            //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
            if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
         
         end
         //
         // if OnlyShort Mode (vTextButtonCounter_2=5)
         //
         else if vTextButtonCounter_2=5 then begin
            // if Sell Mode (vTextButtonCounter_3=1) and Sell Condition true (vEMA<triggerline)
            if vTextButtonCounter_3=1 AND vEMA<vTriggerLine then begin
               sellshort vQty shares next bar at close-5 limit;
               // If not track only Down (vTextButtonCounter_5<>4)
               if vTextButtonCounter_5<>4 then begin
                  if vSuperTrend>vEMA then vTriggerLine=vSuperTrend;
               end;
               //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
               if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
               //Alert(Text("Started position")) ;
            end;
         end;
      end
      //
      // il Short Position (vMP=-1)
      else if vMP=-1 then begin
         // if Stop Mode (vTextButtonCounter_2=2) and Stop Condition true (vEMA>triggerline )or
         // if time_s > vEndSessionTime_s close position
         if (vTextButtonCounter_2=2 AND vEMA>vTriggerLine) or (time_s >= vEndSessionTime_s) then begin
            BuytoCover vQty shares next bar at close+5 limit;
            //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
            if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
         end
         // if Reverse Mode (vTextButtonCounter_2=1) and Reverse Condition true (vEMA>triggerline )
         else if vTextButtonCounter_2=1 AND vEMA>vTriggerLine then begin
            vQTy = vQuantita;
            BUY vQty shares next bar at close+5 limit;
            // If not track only Up (vTextButtonCounter_5<>3)
            if vTextButtonCounter_5<>3 then begin
               if vSuperTrend<vEMA then vTriggerLine=vSuperTrend;
            end;
            //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
            if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
         end
         // if OnlyShort Mode (vTextButtonCounter_2=5) and Stop Condition true (vEMA>triggerline )
         else if vTextButtonCounter_2=5 AND vEMA>vTriggerLine then begin
            BuytoCover vQty shares next bar at close+5 limit;
            // If not track only Up (vTextButtonCounter_5<>3)
            if vTextButtonCounter_5<>3 then begin
               if vSuperTrend<vEMA then vTriggerLine=vSuperTrend;
            end;
            //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
            if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
         end
         //
         // if OnlyLong Mode (vTextButtonCounter_2=4)
         //
         else if vTextButtonCounter_2=4 then begin
            // if Buy Mode (vTextButtonCounter_3=0) and Buy Condition true (vEMA>triggerline)
            if vTextButtonCounter_3=0 AND vEMA>vTriggerLine then begin
               BUY vQty shares next bar at close+5 limit;
               // If not track only Up (vTextButtonCounter_5<>3)
               if vTextButtonCounter_5<>3 then begin
                  if vSuperTrend<vEMA then vTriggerLine=vSuperTrend;
               end;
               //if Tracking mode Single(vTextButtonCounter_5=2) then sets No Tracking Mode(vTextButtonCounter_5=0);
               if vTextButtonCounter_5=2 then vTextButtonCounter_5=0;
               //Alert(Text("Started position")) ;
            end;
         end;
      end;   
end; // if vTextButtonCounter_1=1
//end; // if LastBarOnChart
//end; // if MarketPosition_at_Broker_for_The_Strategy=MarketPosition_at_Broker

// ReDraws TriggerLine
if TL_Exist(vValueTL)=true then tl_delete(vValueTL);
if TL_Exist(vValueTL)=false then vValueTL = tl_new(Date, Time[1], vTriggerLine, Date, Time, vTriggerLine);
TL_SetExtLeft(vValueTL,True);
TL_SetExtRight(vValueTL,True);
TL_Lock(vValueTL,True);



i_setplotvalue(1, vTriggerLine);
i_setplotvalue(3, vEMA);
i_setplotvalue(2, vSuperTrend);
i_setplotvalue(4, vSuperTrend1);
i_setplotvalue(5, vSuperTrend2);
i_setplotvalue(6, vSuperTrend3);
i_setplotvalue(7, vSuperTrend4);

//if vMP>0 then i_setplotvalue(5, entryprice+vProfitTicks);
//if vMP<0 then i_setplotvalue(6, entryprice-vProfitTicks);

//if getappinfo(aiStrategyAuto)=1 AND MarketPosition_at_Broker<>MarketPosition_at_Broker_for_The_Strategy AND Mod(currentbar,10)=0 then Alert(Text("Disallineamento Strategia")) ;

print(entryprice:0:0," - ",entryprice(1):0:0," = ",(entryprice-entryprice(1)):0:0);


Attachments
_RTT#7_4_SuperTrend_OnlyLong_OnlyShort_04Marzo2016.pla
(99.43 KiB) Downloaded 82 times

Return to “User Contributed Studies and Indicator Library”